MCPcopy
hub / github.com/webpack/webpack / assignNames

Function assignNames

lib/ids/IdHelpers.js:315–376  ·  view source on GitHub ↗
(
	items,
	getShortName,
	getLongName,
	comparator,
	usedIds,
	assignName
)

Source from the content-addressed store, hash-verified

313 * @returns {T[]} list of items without a name
314 */
315const assignNames = (
316 items,
317 getShortName,
318 getLongName,
319 comparator,
320 usedIds,
321 assignName
322) => {
323 /**
324 * Defines the map to item type used by this module.
325 * @template T
326 * @typedef {Map<string, T[]>} MapToItem
327 */
328
329 /** @type {MapToItem<T>} */
330 const nameToItems = new Map();
331
332 for (const item of items) {
333 const name = getShortName(item);
334 addToMapOfItems(nameToItems, name, item);
335 }
336
337 /** @type {MapToItem<T>} */
338 const nameToItems2 = new Map();
339
340 for (const [name, items] of nameToItems) {
341 if (items.length > 1 || !name) {
342 for (const item of items) {
343 const longName = getLongName(item, name);
344 addToMapOfItems(nameToItems2, longName, item);
345 }
346 } else {
347 addToMapOfItems(nameToItems2, name, items[0]);
348 }
349 }
350
351 /** @type {T[]} */
352 const unnamedItems = [];
353
354 for (const [name, items] of nameToItems2) {
355 if (!name) {
356 for (const item of items) {
357 unnamedItems.push(item);
358 }
359 } else if (items.length === 1 && !usedIds.has(name)) {
360 assignName(items[0], name);
361 usedIds.add(name);
362 } else {
363 items.sort(comparator);
364 let i = 0;
365 for (const item of items) {
366 while (nameToItems2.has(name + i) && usedIds.has(name + i)) i++;
367 assignName(item, name + i);
368 usedIds.add(name + i);
369 i++;
370 }
371 }
372 }

Callers 2

applyMethod · 0.85
applyMethod · 0.85

Calls 5

addToMapOfItemsFunction · 0.85
sortMethod · 0.80
pushMethod · 0.45
hasMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected