UNPKG

@thumbmarkjs/thumbmarkjs

Version:

![GitHub package.json dynamic](https://img.shields.io/github/package-json/version/ilkkapeltola/thumbmarkjs) ![NPM Version](https://img.shields.io/npm/v/@thumbmarkjs/thumbmarkjs) ![NPM Downloads](https://img.shields.io/npm/dm/%40thumbmarkjs%2Fthumbmarkjs

23 lines (21 loc) 799 B
import { componentInterface } from '../factory'; /** * Recursively sorts the keys of a component object alphabetically. * This ensures a consistent order for hashing. * @param obj The component object to sort. * @returns A new object with sorted keys. */ export function sortComponentKeys(obj: componentInterface): componentInterface { // Arrays, primitives, and null values are returned as is. if (typeof obj !== 'object' || obj === null || Array.isArray(obj)) { return obj; } return Object.keys(obj) .sort() .reduce((acc: componentInterface, key: string) => { const value = obj[key]; // Recurse for nested objects acc[key] = sortComponentKeys(value as componentInterface); return acc; }, {}); }