/** * Sorts the properties of an object alphabetically, ascending or descending. * It does not mutate the original object. * * REMEMBER: In theory, JS engines do not guarantee the order of object properties. * In practice most of the popular engines do. * @param object - source object * @param asc - sort ascending? * @example sortProps({ b: 2, a: 1, z: 26 }) // { a: 1, b: 2, z: 26 } */ const sortProps = >(object: T, asc = true): T => { const sorted: Record = {}; const keys = Object.keys(object); if (asc) { keys.sort(); } else { keys.sort().reverse(); } for (const key of keys) { sorted[key] = object[key]; } return sorted as T; }; export { sortProps, };