UNPKG

@enonic/js-utils

Version:
17 lines (14 loc) 395 B
const {isArray} = Array; const {keys} = Object; export function sortKeys<T extends object>(obj: T) :T { if (typeof obj !== 'object' || isArray(obj)) { throw new Error('sortKeys'); } const newObject = {} as T; const sortedKeys = keys(obj).sort(); for (let i = 0, l = sortedKeys.length; i < l; i++) { const k = sortedKeys[i] as keyof T; newObject[k] = obj[k]; } return newObject; }