UNPKG

@soleil-se/app-util

Version:

Utility functions for WebApps, RESTApps and Widgets in Sitevision.

30 lines (29 loc) 1.68 kB
/** * Compares two strings in a localized manner, taking into account special characters. * The order is defined as: a-z å ä æ ö ø (case-insensitive, with lowercase before uppercase). * Characters not in this set are compared based on their Unicode code points after removing accents. * @param {string} a - The first string to compare. * @param {string} b - The second string to compare. * @returns {number} Negative if a < b, positive if a > b, zero if equal. */ export function localizedCompare(a: string, b: string): number; /** * Creates a comparator function for sorting objects by a specific property or properties. * The property values are compared using localized string comparison. * When given an array of properties, sorts by the first property, then by the second if equal, etc. * Each property can be a string or an object with a `key` and optional `order` ('asc' | 'desc', defaults to 'asc'). * @param {string|{key: string, order?: 'asc'|'desc'}|Array<string|{key: string, order?: 'asc'|'desc'}>} prop - The property name(s) to compare by. * @returns {(a: Object, b: Object) => number} A comparator function that accepts two objects and returns their sort order. * @example * arr.sort(localizedCompareBy('title')) * arr.sort(localizedCompareBy(['lastName', 'firstName'])) * arr.sort(localizedCompareBy({ key: 'firstName', order: 'desc' })) * arr.sort(localizedCompareBy([{ key: 'lastName', order: 'asc' }, { key: 'firstName', order: 'desc' }])) */ export function localizedCompareBy(prop: string | { key: string; order?: 'asc' | 'desc'; } | Array<string | { key: string; order?: 'asc' | 'desc'; }>): (a: any, b: any) => number;