UNPKG

@rxap/utilities

Version:

A collection of utility functions, types and interfaces.

42 lines 1.56 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DefaultSortPropertiesCompareFn = DefaultSortPropertiesCompareFn; exports.SortProperties = SortProperties; /** * Compares two strings using the locale-specific comparison to determine their relative order. * * This function is typically used as a comparator for sorting arrays of strings in the default * lexicographical order according to the current locale settings. * * @param {string} a - The first string to compare. * @param {string} b - The second string to compare. * @returns {number} A negative number if `a` comes before `b`, zero if they are equivalent, * or a positive number if `a` comes after `b`. * * @example * const strings = ['banana', 'apple', 'cherry']; * strings.sort(DefaultSortPropertiesCompareFn); // ['apple', 'banana', 'cherry'] */ function DefaultSortPropertiesCompareFn(a, b) { return a.localeCompare(b); } /** * Sort the properties of an object in place * @param obj * @param compareFn */ function SortProperties(obj, compareFn = DefaultSortPropertiesCompareFn) { if (!obj) { throw new Error('Input is null or undefined'); } if (typeof obj !== 'object') { throw new Error('Input is not an object'); } const shallowClone = Object.assign({}, obj); Object.keys(shallowClone).forEach((key) => delete obj[key]); Object.keys(shallowClone) .sort(compareFn) .forEach((key) => obj[key] = shallowClone[key]); return obj; } //# sourceMappingURL=sort-properties.js.map