UNPKG

@intlayer/chokidar

Version:

Uses chokidar to scan and build Intlayer declaration files into dictionaries based on Intlayer configuration.

64 lines (63 loc) 2.94 kB
//#region src/utils/verifyIdenticObjectFormat.ts /** * Verifies that two objects have identical structure (same keys, array lengths, and types) * but not necessarily the same values. * Useful for validating translations maintain the same format as the original. * * @param object - The object to verify * @param expectedFormat - The expected format to compare against * @param path - Current path in the object tree (for error messages) * @returns true if structures match, throws error with details if they don't */ const verifyIdenticObjectFormat = (object, expectedFormat, path = "root") => { if (expectedFormat === null || expectedFormat === void 0) { if (expectedFormat !== object) return { isIdentic: false, error: `Type mismatch at ${path}: expected ${expectedFormat === null ? "null" : "undefined"}, got ${object === null ? "null" : typeof object}` }; return { isIdentic: true }; } if ((object === null || object === void 0) && typeof expectedFormat !== "object" && !Array.isArray(expectedFormat)) return { isIdentic: true }; const expectedType = Array.isArray(expectedFormat) ? "array" : typeof expectedFormat; const objectType = Array.isArray(object) ? "array" : typeof object; if (expectedType !== objectType) return { isIdentic: false, error: `Type mismatch at ${path}: expected ${expectedType}, got ${objectType}` }; if (Array.isArray(expectedFormat) && Array.isArray(object)) { if (expectedFormat.length !== object.length) return { isIdentic: false, error: `Array length mismatch at ${path}: expected ${expectedFormat.length} elements, got ${object.length}` }; for (let i = 0; i < expectedFormat.length; i++) { const result = verifyIdenticObjectFormat(object[i], expectedFormat[i], `${path}[${i}]`); if (!result.isIdentic) return result; } return { isIdentic: true }; } if (typeof expectedFormat === "object" && typeof object === "object" && expectedFormat !== null && object !== null) { const expectedKeys = Object.keys(expectedFormat); const objectKeys = Object.keys(object); if (expectedKeys.length !== objectKeys.length) return { isIdentic: false, error: `Object keys count mismatch at ${path}: expected ${expectedKeys.length} keys, got ${objectKeys.length}` }; for (const key of expectedKeys) if (!objectKeys.includes(key)) return { isIdentic: false, error: `Missing key at ${path}: expected key "${key}" not found` }; for (const key of objectKeys) if (!expectedKeys.includes(key)) return { isIdentic: false, error: `Unexpected key at ${path}: key "${key}" was found but not expected` }; for (const key of expectedKeys) { const result = verifyIdenticObjectFormat(object[key], expectedFormat[key], `${path}.${key}`); if (!result.isIdentic) return result; } return { isIdentic: true }; } return { isIdentic: true }; }; //#endregion export { verifyIdenticObjectFormat }; //# sourceMappingURL=verifyIdenticObjectFormat.mjs.map