webpack-assets-manifest
Version:
This Webpack plugin will generate a JSON file that matches the original filename with the hashed version.
25 lines (24 loc) • 1 kB
JavaScript
import { createHash } from 'node:crypto';
import { isPropertyKey } from './type-predicate.js';
export function asArray(data) {
return Array.isArray(data) ? data : [data];
}
export function getSRIHash(algorithm, content) {
return `${algorithm}-${createHash(algorithm).update(content).digest('base64')}`;
}
export function getSortedObject(object, compareFunction) {
return Object.fromEntries(Object.entries(object).sort(compareFunction ? (left, right) => compareFunction(left[0], right[0]) : undefined));
}
export function findMapKeysByValue(map) {
const entries = [...map.entries()];
return (searchValue) => entries.filter(([, value]) => value === searchValue).map(([name]) => name);
}
export function group(data, getGroup, mapper) {
return data.reduce((obj, item) => {
const group = getGroup(item);
if (isPropertyKey(group)) {
(obj[group] ??= []).push(mapper ? mapper(item, group) : item);
}
return obj;
}, Object.create(null));
}