@intlayer/chokidar
Version:
Uses chokidar to scan and build Intlayer declaration files into dictionaries based on Intlayer configuration.
70 lines (68 loc) • 2.58 kB
JavaScript
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
//#region src/utils/reduceObjectFormat.ts
/**
* Reduce an object to only the shape provided by a format object.
* Values are always taken from the source object; the format is used only for structure.
*
* Examples:
* reduceObjectFormat({ a: 1, b: 2 }, { a: 0 }) => { a: 1 }
* reduceObjectFormat({ a: { x: 1, y: 2 } }, { a: { x: 0 } }) => { a: { x: 1 } }
*/
const reduceObjectFormat = (source, format) => {
if (Array.isArray(format)) {
const sourceArray = Array.isArray(source) ? source : [];
return format.map((formatItem, index) => reduceObjectFormat(sourceArray[index], formatItem));
}
if (typeof format === "object" && format !== null) {
const result = {};
const sourceObject = typeof source === "object" && source !== null && !Array.isArray(source) ? source : {};
for (const key of Object.keys(format)) {
const nextSource = sourceObject[key];
const nextFormat = format[key];
result[key] = reduceObjectFormat(nextSource, nextFormat);
}
return result;
}
return source;
};
/**
* Returns the subset of source whose keys are NOT present in the format object.
* Inverse of reduceObjectFormat.
*
* Null values in source are always included as explicit fallback markers
* (they signal "use default locale" at render time).
*
* Examples:
* excludeObjectFormat({ a: 1, b: 2 }, { b: 0 }) => { a: 1 }
* excludeObjectFormat({ a: { x: 1, y: 2 } }, { a: { x: 0 } }) => { a: { y: 2 } }
* excludeObjectFormat({ a: null, b: 2 }, { b: 0 }) => { a: null }
*/
const excludeObjectFormat = (source, format) => {
if (source === null) return null;
if (format === void 0 || format === null) return source;
if (typeof source === "object" && !Array.isArray(source) && typeof format === "object" && !Array.isArray(format)) {
const result = {};
let hasContent = false;
for (const key of Object.keys(source)) {
const excluded = excludeObjectFormat(source[key], format[key]);
if (excluded !== void 0) {
result[key] = excluded;
hasContent = true;
}
}
return hasContent ? result : void 0;
}
if (Array.isArray(source)) {
const formatArr = Array.isArray(format) ? format : [];
const result = [];
for (let i = 0; i < source.length; i++) {
const excluded = excludeObjectFormat(source[i], formatArr[i]);
if (excluded !== void 0) result.push(excluded);
}
return result.length > 0 ? result : void 0;
}
};
//#endregion
exports.excludeObjectFormat = excludeObjectFormat;
exports.reduceObjectFormat = reduceObjectFormat;
//# sourceMappingURL=reduceObjectFormat.cjs.map