CoolFace
Datasetpublic

basant307/AI_Governance_Project

sourceHugging Faceapache-2.0updated 2mo agoView on Hugging Face
0likes48downloads
minify.js92 linesDownload Raw Back to icon-set
1import { defaultIconDimensions } from "../icon/defaults.js";2/**3* Minify icon set4*5* Function finds common values for few numeric properties, such as 'width' and 'height' (see defaultIconDimensions keys for list of properties),6* removes entries from icons and sets default entry in root of icon set object.7*8* For example, this:9* {10*  icons: {11*      foo: {12*          body: '<g />',13*          width: 2414*      },15*      bar: {16*          body: '<g />',17*          width: 2418*      },19*      baz: {20*          body: '<g />',21*          width: 1622*      }23*  }24* }25* is changed to this:26* {27*  icons: {28*      foo: {29*          body: '<g />'30*      },31*      bar: {32*          body: '<g />'33*      },34*      baz: {35*          body: '<g />',36*          width: 1637*      }38*  },39*  width: 2440* }41*/42function minifyIconSet(data) {43	const icons = Object.keys(data.icons);44	Object.keys(defaultIconDimensions).forEach((prop) => {45		if (data[prop] === defaultIconDimensions[prop]) delete data[prop];46		const defaultValue = defaultIconDimensions[prop];47		const propType = typeof defaultValue;48		const hasMinifiedDefault = typeof data[prop] === propType && data[prop] !== defaultValue;49		let maxCount = 0;50		let maxValue = null;51		const counters = /* @__PURE__ */ new Map();52		for (let i = 0; i < icons.length; i++) {53			const item = data.icons[icons[i]];54			let value;55			if (typeof item[prop] === propType) value = item[prop];56			else if (hasMinifiedDefault) value = data[prop];57			else value = defaultIconDimensions[prop];58			if (i === 0) {59				maxCount = 1;60				maxValue = value;61				counters.set(value, 1);62				continue;63			}64			if (!counters.has(value)) {65				counters.set(value, 1);66				continue;67			}68			const count = counters.get(value) + 1;69			counters.set(value, count);70			if (count > maxCount) {71				maxCount = count;72				maxValue = value;73			}74		}75		const canMinify = maxValue !== null && maxCount > 1;76		const oldDefault = hasMinifiedDefault ? data[prop] : null;77		const newDefault = canMinify ? maxValue : oldDefault;78		if (newDefault === defaultValue) delete data[prop];79		else if (canMinify) data[prop] = newDefault;80		icons.forEach((key) => {81			const item = data.icons[key];82			const value = prop in item ? item[prop] : hasMinifiedDefault ? oldDefault : defaultValue;83			if (value === newDefault || newDefault === null && value === defaultValue) {84				delete item[prop];85				return;86			}87			if (canMinify && !(prop in item)) item[prop] = value;88		});89	});90}91export { minifyIconSet };92 
basant307/AI_Governance_Project · CoolFace