filter.js 1.1 KB
Newer Older
vipcxj's avatar
vipcxj committed
1 2 3 4 5 6
const findSep = (value, offset = 0) => {
  const find = value.indexOf('|', offset);
  if (find !== -1) {
    let count = 0;
    let from = find;
    while (value[--from] === '#') ++count;
7
    if ((count & 1) === 1) {
vipcxj's avatar
vipcxj committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
      return findSep(value, find + 1);
    } else {
      return find;
    }
  } else {
    return -1;
  }
};

const transform = (value) => {
  const find = value.indexOf('#');
  if (find !== -1) {
    const left = value.slice(0, find);
    const me = value[find + 1] ? value[find + 1] : '';
    const right = value.slice(find + 2);
    return `${left}${me}${transform(right)}`;
  } else {
    return value;
  }
};

29
export const split = (value, unescape = true) => {
vipcxj's avatar
vipcxj committed
30 31 32 33 34 35
  const ret = [];
  let offset = 0;
  let posSep = -1;
  do {
    posSep = findSep(value, offset);
    if (posSep !== -1) {
36 37 38 39 40
      if (unescape) {
        ret.push(transform(value.slice(offset, posSep)));
      } else {
        ret.push(value.slice(offset, posSep));
      }
vipcxj's avatar
vipcxj committed
41 42 43
      offset = posSep + 1;
    }
  } while (posSep !== -1);
44 45 46 47 48
  if (unescape) {
    ret.push(transform(value.slice(offset)));
  } else {
    ret.push(value.slice(offset));
  }
vipcxj's avatar
vipcxj committed
49 50 51
  return ret;
};