UNPKG

pixi.js

Version:

<p align="center"> <a href="https://pixijs.com" target="_blank" rel="noopener noreferrer"> <img height="150" src="https://files.pixijs.download/branding/pixijs-logo-transparent-dark.svg?v=1" alt="PixiJS logo"> </a> </p> <br/> <p align="center">

1 lines 3.05 kB
{"version":3,"file":"clean.mjs","sources":["../../../src/utils/data/clean.ts"],"sourcesContent":["/**\n * Takes a hash and removes all the `undefined`/`null` values from it.\n * In PixiJS, we tend to null properties instead of using 'delete' for performance reasons.\n * However, in some cases, this could be a problem if the hash grows too large over time,\n * this function can be used to clean a hash.\n * @param hash - The hash to clean.\n * @returns A new hash with all the `undefined`/`null` values removed.\n * @category utils\n * @internal\n */\nexport function cleanHash<T>(hash: Record<string, T>): Record<string, T>\n{\n let clean = false;\n\n for (const i in hash)\n {\n // eslint-disable-next-line eqeqeq\n if (hash[i] == undefined)\n {\n clean = true;\n break;\n }\n }\n\n if (!clean) return hash;\n\n const cleanHash = Object.create(null);\n\n for (const i in hash)\n {\n const value = hash[i];\n\n if (value)\n {\n cleanHash[i] = value;\n }\n }\n\n return cleanHash;\n}\n\n/**\n * Removes all `undefined`/`null` elements from the given array and compacts the array.\n *\n * This function iterates through the array, shifting non-undefined elements to the left\n * to fill gaps created by `undefined` elements. The length of the array is then adjusted\n * to remove the trailing `undefined` elements.\n * @param arr - The array to be cleaned.\n * @returns The cleaned array with all `undefined` elements removed.\n * @example\n * // Example usage:\n * const arr = [1, undefined, 2, undefined, 3];\n * const cleanedArr = cleanArray(arr);\n * console.log(cleanedArr); // Output: [1, 2, 3]\n * @category utils\n * @internal\n */\nexport function cleanArray<T>(arr: T[]): T[]\n{\n let offset = 0;\n\n for (let i = 0; i < arr.length; i++)\n {\n // eslint-disable-next-line eqeqeq\n if (arr[i] == undefined)\n {\n offset++;\n }\n else\n {\n arr[i - offset] = arr[i];\n }\n }\n\n arr.length -= offset;\n\n return arr;\n}\n"],"names":["cleanHash"],"mappings":";AAUO,SAAS,UAAa,IAAA,EAC7B;AACI,EAAA,IAAI,KAAA,GAAQ,KAAA;AAEZ,EAAA,KAAA,MAAW,KAAK,IAAA,EAChB;AAEI,IAAA,IAAI,IAAA,CAAK,CAAC,CAAA,IAAK,KAAA,CAAA,EACf;AACI,MAAA,KAAA,GAAQ,IAAA;AACR,MAAA;AAAA,IACJ;AAAA,EACJ;AAEA,EAAA,IAAI,CAAC,OAAO,OAAO,IAAA;AAEnB,EAAA,MAAMA,UAAAA,mBAAY,MAAA,CAAO,MAAA,CAAO,IAAI,CAAA;AAEpC,EAAA,KAAA,MAAW,KAAK,IAAA,EAChB;AACI,IAAA,MAAM,KAAA,GAAQ,KAAK,CAAC,CAAA;AAEpB,IAAA,IAAI,KAAA,EACJ;AACI,MAAAA,UAAAA,CAAU,CAAC,CAAA,GAAI,KAAA;AAAA,IACnB;AAAA,EACJ;AAEA,EAAA,OAAOA,UAAAA;AACX;AAkBO,SAAS,WAAc,GAAA,EAC9B;AACI,EAAA,IAAI,MAAA,GAAS,CAAA;AAEb,EAAA,KAAA,IAAS,CAAA,GAAI,CAAA,EAAG,CAAA,GAAI,GAAA,CAAI,QAAQ,CAAA,EAAA,EAChC;AAEI,IAAA,IAAI,GAAA,CAAI,CAAC,CAAA,IAAK,KAAA,CAAA,EACd;AACI,MAAA,MAAA,EAAA;AAAA,IACJ,CAAA,MAEA;AACI,MAAA,GAAA,CAAI,CAAA,GAAI,MAAM,CAAA,GAAI,GAAA,CAAI,CAAC,CAAA;AAAA,IAC3B;AAAA,EACJ;AAEA,EAAA,GAAA,CAAI,MAAA,IAAU,MAAA;AAEd,EAAA,OAAO,GAAA;AACX;;;;"}