All files / public/admin tag-domain.js

95.55% Statements 43/45
90.19% Branches 46/51
100% Functions 23/23
95.23% Lines 40/42

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 12052x   297x       194x 238x   238x 238x         11x 11x 5x           7x   7x 10x         11x       12x 19x 27x   19x         15x   15x               7x 10x     10x         8x 8x 13x   13x     13x     8x 2x 1x   1x         10x 14x   14x         4x 4x 4x     4x         12x           52x                          
(function () {
  function normalizeTag(value) {
    return typeof value === "string" ? value.trim() : "";
  }
 
  function uniqueTags(values) {
    return (Array.isArray(values) ? values : []).reduce(function (result, value) {
      var tag = normalizeTag(value);
 
      if (tag && result.indexOf(tag) === -1) result.push(tag);
      return result;
    }, []);
  }
 
  function sortTags(values) {
    return uniqueTags(values).sort(function (left, right) {
      if (left < right) return -1;
      Eif (left > right) return 1;
      return 0;
    });
  }
 
  function missingTags(selected, library) {
    var known = uniqueTags(library);
 
    return uniqueTags(selected).filter(function (tag) {
      return known.indexOf(tag) === -1;
    });
  }
 
  function mergeTags(library, additions) {
    return sortTags(uniqueTags(library).concat(uniqueTags(additions)));
  }
 
  function countUsage(entries) {
    return (Array.isArray(entries) ? entries : []).reduce(function (usage, entry) {
      uniqueTags(entry && entry.data && entry.data.tags).forEach(function (tag) {
        usage[tag] = (usage[tag] || 0) + 1;
      });
      return usage;
    }, Object.create(null));
  }
 
  function canDelete(tag, usage) {
    var normalizedTag = normalizeTag(tag);
 
    return (
      !usage ||
      !Object.hasOwn(usage, normalizedTag) ||
      usage[normalizedTag] === 0
    );
  }
 
  function tagStats(tags, usage) {
    return uniqueTags(tags).map(function (tag) {
      var count = usage && Object.hasOwn(usage, tag)
        ? usage[tag]
        : 0;
      return { name: tag, count: count, used: count > 0 };
    });
  }
 
  function filterTagStats(items, query, filter, sort) {
    var term = normalizeTag(query).toLocaleLowerCase();
    var result = (Array.isArray(items) ? items : []).filter(function (item) {
      var matchesQuery = !term || item.name.toLocaleLowerCase().includes(term);
      var matchesFilter =
        filter === "used" ? item.used :
        filter === "unused" ? !item.used :
        true;
      return matchesQuery && matchesFilter;
    });
 
    return result.sort(function (left, right) {
      if (sort === "usage") {
        return right.count - left.count || left.name.localeCompare(right.name, "zh-CN");
      }
      return left.name.localeCompare(right.name, "zh-CN");
    });
  }
 
  function replaceTag(values, source, target) {
    return uniqueTags(values).map(function (tag) {
      return tag === source ? target : tag;
    }).filter(function (tag, index, all) {
      return all.indexOf(tag) === index;
    });
  }
 
  function mergePlan(library, source, target, usage) {
    var oldTag = normalizeTag(source);
    var newTag = normalizeTag(target);
    Iif (!oldTag || !newTag || oldTag === newTag) {
      throw new Error("源标签和目标标签必须不同且不能为空");
    }
    return {
      source: oldTag,
      target: newTag,
      affectedCount: usage && usage[oldTag] ? usage[oldTag] : 0,
      library: mergeTags(
        uniqueTags(library).filter(function (tag) { return tag !== oldTag; }),
        [newTag],
      ),
    };
  }
 
  window.DecapTagDomain = {
    normalizeTag: normalizeTag,
    uniqueTags: uniqueTags,
    missingTags: missingTags,
    mergeTags: mergeTags,
    countUsage: countUsage,
    canDelete: canDelete,
    tagStats: tagStats,
    filterTagStats: filterTagStats,
    replaceTag: replaceTag,
    mergePlan: mergePlan,
  };
})();