@gmjs/comparers
v0.0.1
Published
comparers
Readme
Comparers
This library contains some comparison functions.
Installation
npm install --save @gmjs/comparersUsage
import { compareNumberAsc } from '@gmjs/comparers';
const array = [3, 1, 2];
array.sort(compareNumberAsc());
console.log(array);
// [1, 2, 3]API Listing
Number
compareNumberAsc- Creates a comparison function which can be used to sort a number array in ascending order.compareNumberDesc- Creates a comparison function which can be used to sort a number array in descending order.compareByNumberAsc- Creates a comparison function which can be used to sort an array of items of arbitrary type, by first applying amapperfunction returning a number, then sorting by the number in ascending order.compareByNumberDesc- Creates a comparison function which can be used to sort an array of items of arbitrary type, by first applying amapperfunction returning a number, then sorting by the number in descending order.
String
compareStringAsc- Creates a comparison function which can be used to sort a string array in ascending order.compareStringDesc- Creates a comparison function which can be used to sort a string array in descending order.compareByStringAsc- Creates a comparison function which can be used to sort an array of items of arbitrary type, by first applying amapperfunction returning a string, then sorting by the string in ascending order.compareByStringDesc- Creates a comparison function which can be used to sort an array of items of arbitrary type, by first applying amapperfunction returning a string, then sorting by the string in descending order.
API
Number
compareNumberAsc
Creates a comparison function which can be used to sort a number array in ascending order.
import { compareNumberAsc } from '@gmjs/comparers';
const array = [3, 1, 2];
array.sort(compareNumberAsc());
console.log(array);
// [1, 2, 3]compareNumberDesc
Creates a comparison function which can be used to sort a number array in descending order.
import { compareNumberDesc } from '@gmjs/comparers';
const array = [3, 1, 2];
array.sort(compareNumberDesc());
console.log(array);
// [3, 2, 1]compareByNumberAsc
Creates a comparison function which can be used to sort an array of items of arbitrary type.
The sort is done by applying a mapper function (first parameter) which returns a number. The comparison is done based on the number returned by the mapper function, in ascending order.
import { compareByNumberAsc } from '@gmjs/comparers';
const array = [{ value: 3 }, { value: 1 }, { value: 2 }];
array.sort(compareByNumberAsc((item) => item.value));
console.log(array);
// [{ value: 1 }, { value: 2 }, { value: 3 }]compareByNumberDesc
Creates a comparison function which can be used to sort an array of items of arbitrary type.
The sort is done by applying a mapper function (first parameter) which returns a number. The comparison is done based on the number returned by the mapper function, in descending order.
import { compareByNumberDesc } from '@gmjs/comparers';
const array = [{ value: 3 }, { value: 1 }, { value: 2 }];
array.sort(compareByNumberDesc((item) => item.value));
console.log(array);
// [{ value: 3 }, { value: 2 }, { value: 1 }]String
compareStringAsc
Creates a comparison function which can be used to sort a string array in ascending order.
Accepts an optional options parameter of type CompareStringOptions.
import { compareStringAsc } from '@gmjs/comparers';
const array = ['c', 'a', 'b'];
array.sort(compareStringAsc());
console.log(array);
// ['a', 'b', 'c']compareStringDesc
Creates a comparison function which can be used to sort a string array in descending order.
Accepts an optional options parameter of type CompareStringOptions.
import { compareStringDesc } from '@gmjs/comparers';
const array = ['c', 'a', 'b'];
array.sort(compareStringDesc());
console.log(array);
// ['c', 'b', 'a']compareByStringAsc
Creates a comparison function which can be used to sort an array of items of arbitrary type.
The sort is done by applying a mapper function (first parameter) which returns a string. The comparison is done based on the string returned by the mapper function, in ascending order.
Accepts an optional options parameter of type CompareStringOptions.
import { compareByStringAsc } from '@gmjs/comparers';
const array = [{ value: 'c' }, { value: 'a' }, { value: 'b' }];
array.sort(compareByStringAsc((item) => item.value));
console.log(array);
// [{ value: 'a' }, { value: 'b' }, { value: 'c' }]compareByStringDesc
Creates a comparison function which can be used to sort an array of items of arbitrary type.
The sort is done by applying a mapper function (first parameter) which returns a string. The comparison is done based on the string returned by the mapper function, in descending order.
Accepts an optional options parameter of type CompareStringOptions.
import { compareByStringDesc } from '@gmjs/comparers';
const array = [{ value: 'c' }, { value: 'a' }, { value: 'b' }];
array.sort(compareByStringDesc((item) => item.value));
console.log(array);
// [{ value: 'c' }, { value: 'b' }, { value: 'a' }]Types
CompareStringOptions
If unspecified, the default are as follows:
locale:'en'sensitivity:'case'caseFirst:'upper'
For description of options, check out:
type CompareStringSensivity = 'base' | 'accent' | 'case' | 'variant';
type CompareStringCaseFirst = 'upper' | 'lower' | 'none';
interface CompareStringOptions {
readonly locale?: string;
readonly sensitivity?: CompareStringSensivity;
readonly caseFirst?: CompareStringCaseFirst;
}