WXL
4 天以前 2cc85c64f1c64a2dbaeae276a3e2ca8420de76b7
node_modules/webpack/lib/util/deterministicGrouping.js
@@ -25,6 +25,7 @@
// 3.2% that 5 or more groups are invalidated
/**
 * Returns the similarity as number.
 * @param {string} a key
 * @param {string} b key
 * @returns {number} the similarity as number
@@ -41,6 +42,7 @@
};
/**
 * Returns the common part and a single char for the difference.
 * @param {string} a key
 * @param {string} b key
 * @param {Set<string>} usedNames set of already used names
@@ -73,6 +75,7 @@
/** @typedef {Record<string, number>} Sizes */
/**
 * Adds the provided total to this object.
 * @param {Sizes} total total size
 * @param {Sizes} size single size
 * @returns {void}
@@ -84,6 +87,7 @@
};
/**
 * Subtract size from.
 * @param {Sizes} total total size
 * @param {Sizes} size single size
 * @returns {void}
@@ -95,11 +99,13 @@
};
/**
 * Returns total size.
 * @template T
 * @param {Iterable<Node<T>>} nodes some nodes
 * @returns {Sizes} total size
 */
const sumSize = (nodes) => {
   /** @type {Sizes} */
   const sum = Object.create(null);
   for (const node of nodes) {
      addSizeTo(sum, node.size);
@@ -108,6 +114,7 @@
};
/**
 * Checks whether this object is too big.
 * @param {Sizes} size size
 * @param {Sizes} maxSize minimum size
 * @returns {boolean} true, when size is too big
@@ -123,6 +130,7 @@
};
/**
 * Checks whether this object is too small.
 * @param {Sizes} size size
 * @param {Sizes} minSize minimum size
 * @returns {boolean} true, when size is too small
@@ -137,12 +145,16 @@
   return false;
};
/** @typedef {Set<string>} Types */
/**
 * Gets too small types.
 * @param {Sizes} size size
 * @param {Sizes} minSize minimum size
 * @returns {Set<string>} set of types that are too small
 * @returns {Types} set of types that are too small
 */
const getTooSmallTypes = (size, minSize) => {
   /** @type {Types} */
   const types = new Set();
   for (const key of Object.keys(size)) {
      const s = size[key];
@@ -154,9 +166,10 @@
};
/**
 * Gets number of matching size types.
 * @template {object} T
 * @param {T} size size
 * @param {Set<string>} types types
 * @param {Types} types types
 * @returns {number} number of matching size types
 */
const getNumberOfMatchingSizeTypes = (size, types) => {
@@ -168,8 +181,9 @@
};
/**
 * Selective size sum.
 * @param {Sizes} size size
 * @param {Set<string>} types types
 * @param {Types} types types
 * @returns {number} selective size sum
 */
const selectiveSizeSum = (size, types) => {
@@ -181,10 +195,12 @@
};
/**
 * Represents the node runtime component.
 * @template T
 */
class Node {
   /**
    * Creates an instance of Node.
    * @param {T} item item
    * @param {string} key key
    * @param {Sizes} size size
@@ -199,10 +215,12 @@
/** @typedef {number[]} Similarities */
/**
 * Represents the group runtime component.
 * @template T
 */
class Group {
   /**
    * Creates an instance of Group.
    * @param {Node<T>[]} nodes nodes
    * @param {Similarities | null} similarities similarities between the nodes (length = nodes.length - 1)
    * @param {Sizes=} size size of the group
@@ -216,13 +234,18 @@
   }
   /**
    * Returns removed nodes.
    * @param {(node: Node<T>) => boolean} filter filter function
    * @returns {Node<T>[] | undefined} removed nodes
    */
   popNodes(filter) {
      /** @type {Node<T>[]} */
      const newNodes = [];
      /** @type {Similarities} */
      const newSimilarities = [];
      /** @type {Node<T>[]} */
      const resultNodes = [];
      /** @type {undefined | Node<T>} */
      let lastNode;
      for (let i = 0; i < this.nodes.length; i++) {
         const node = this.nodes[i];
@@ -249,6 +272,7 @@
}
/**
 * Returns similarities.
 * @template T
 * @param {Iterable<Node<T>>} nodes nodes
 * @returns {Similarities} similarities
@@ -257,6 +281,7 @@
   // calculate similarities between lexically adjacent nodes
   /** @type {Similarities} */
   const similarities = [];
   /** @type {undefined | Node<T>} */
   let last;
   for (const node of nodes) {
      if (last !== undefined) {
@@ -268,6 +293,7 @@
};
/**
 * Defines the shared type used by this module.
 * @template T
 * @typedef {object} GroupedItems<T>
 * @property {string} key
@@ -276,6 +302,7 @@
 */
/**
 * Defines the options type used by this module.
 * @template T
 * @typedef {object} Options
 * @property {Sizes} maxSize maximum size of a group
@@ -286,6 +313,7 @@
 */
/**
 * Returns grouped items.
 * @template T
 * @param {Options<T>} options options object
 * @returns {GroupedItems<T>[]} grouped items
@@ -323,6 +351,7 @@
      const initialGroup = new Group(initialNodes, getSimilarities(initialNodes));
      /**
       * Removes problematic nodes.
       * @param {Group<T>} group group
       * @param {Sizes} consideredSize size of the group to consider
       * @returns {boolean} true, if the group was modified
@@ -396,6 +425,7 @@
            // going minSize from left and right
            // at least one node need to be included otherwise we get stuck
            let left = 1;
            /** @type {Sizes} */
            const leftSize = Object.create(null);
            addSizeTo(leftSize, group.nodes[0].size);
            while (left < group.nodes.length && isTooSmall(leftSize, minSize)) {
@@ -403,6 +433,7 @@
               left++;
            }
            let right = group.nodes.length - 2;
            /** @type {Sizes} */
            const rightSize = Object.create(null);
            addSizeTo(rightSize, group.nodes[group.nodes.length - 1].size);
            while (right >= 0 && isTooSmall(rightSize, minSize)) {
@@ -422,6 +453,7 @@
            if (left - 1 > right) {
               // We try to remove some problematic nodes to "fix" that
               /** @type {Sizes} */
               let prevSize;
               if (right < group.nodes.length - left) {
                  subtractSizeFrom(rightSize, group.nodes[right + 1].size);
@@ -487,6 +519,7 @@
            // create two new groups for left and right area
            // and queue them up
            /** @type {Node<T>[]} */
            const rightNodes = [group.nodes[right + 1]];
            /** @type {Similarities} */
            const rightSimilarities = [];
@@ -498,6 +531,7 @@
            }
            queue.push(new Group(rightNodes, rightSimilarities));
            /** @type {Node<T>[]} */
            const leftNodes = [group.nodes[0]];
            /** @type {Similarities} */
            const leftSimilarities = [];
@@ -520,6 +554,7 @@
   });
   // give every group a name
   /** @type {Set<string>} */
   const usedNames = new Set();
   for (let i = 0; i < result.length; i++) {
      const group = result[i];