def func_unique(parser, multi, case_sensitive="", separator=MULTI_VALUED_JOINER): multi_value = MultiValue(parser, multi, separator) if not case_sensitive: multi_value._multi = list({v.lower(): v for v in multi_value}.values()) return multi_value.separator.join(sorted(set(multi_value)))
def func_inmulti(parser, haystack, needle, separator=MULTI_VALUED_JOINER): """Searches for ``needle`` in ``haystack``, supporting a list variable for ``haystack``. If a string is used instead, then a ``separator`` can be used to split it. In both cases, it returns true if the resulting list contains exactly ``needle`` as a member.""" needle = needle.eval(parser) return func_in(parser, MultiValue(parser, haystack, separator), needle)
def func_map(parser, multi, loop_code, separator=MULTI_VALUED_JOINER): multi_value = MultiValue(parser, multi, separator) for loop_count, value in enumerate(multi_value, 1): func_set(parser, '_loop_count', str(loop_count)) func_set(parser, '_loop_value', str(value)) multi_value[loop_count - 1] = str(loop_code.eval(parser)) func_unset(parser, '_loop_count') func_unset(parser, '_loop_value') return str(multi_value)
def func_getmulti(parser, multi, item_index, separator=MULTI_VALUED_JOINER): if not item_index: return '' try: index = int(item_index.eval(parser)) multi_value = MultiValue(parser, multi, separator) return str(multi_value[index]) except (ValueError, IndexError): return ''
def func_slice(parser, multi, start_index, end_index, separator=MULTI_VALUED_JOINER): try: start = int(start_index.eval(parser)) if start_index else None except ValueError: start = None try: end = int(end_index.eval(parser)) if end_index else None except ValueError: end = None multi_value = MultiValue(parser, multi, separator) return multi_value.separator.join(multi_value[start:end])
def func_replacemulti(parser, multi, search, replace, separator=MULTI_VALUED_JOINER): if not multi or not search or replace is None or not separator: return multi.eval(parser) search = search.eval(parser) replace = replace.eval(parser) multi_value = MultiValue(parser, multi, separator) for n, value in enumerate(multi_value): if value == search: multi_value[n] = replace return str(multi_value)
def func_lenmulti(parser, multi, separator=MULTI_VALUED_JOINER): return str(len(MultiValue(parser, multi, separator)))
def func_reversemulti(parser, multi, separator=MULTI_VALUED_JOINER): multi_value = MultiValue(parser, multi, separator) return multi_value.separator.join(reversed(multi_value))
def func_join(parser, multi, join_phrase, separator=MULTI_VALUED_JOINER): join_phrase = str(join_phrase.eval(parser)) multi_value = MultiValue(parser, multi, separator) return join_phrase.join(multi_value)
def func_is_multi(parser, multi): multi_value = MultiValue(parser, multi, MULTI_VALUED_JOINER) return '' if len(multi_value) < 2 else '1'