polish-sort
Description
Comparator functions for dealing with Polish accented characters when sorting in JavaScript. Features 100% test coverage.
Why
JavaScript fails to sort polish characters properly. Example:
[ 'la', 'łb', 'z' ].sort(); // ["la", "z", "łb"]
Now, there is String.prototype.localeCompare
to deal with localized comparisons:
[ 'la', 'łb', 'z' ].sort((a, b) => a.localeCompare(b, 'pl')); // ["la", "łb", "z"]
Unfortunately, it does not work in older browsers, such as IE <= 10 or Safari <= 9.1.
Installation
npm install --save polish-sort
API
caseInsensitiveComparator(a, b)
Useful when you want to sort an Array
of String
. Just pass the reference as an argument to Array.prototype.sort
.
; 'łc' 'Ła' 'łb' ; // ["Ła", "łb", "łc"]
caseSensitiveComparator(a, b)
Useful when you want to sort an Array
of String
. Just pass the reference as an argument to Array.prototype.sort
.
; 'łc' 'Ła' 'łb' ; // ["łb", "łc", "Ła"]
createComparator(options)
Useful when you want to sort an Array
of Object
by some attribute. Returns a comparator. Accepts 1 optional argument of type Object
with the following (both optional) attributes:
getter
-Function
- defaults to:(a) => a
ignoreCase
-Boolean
- defaults to:false
; const comparator = ; name: 'la' age: 1 name: 'łb' age: 2 name: 'lc' age: 3 ;// [// { name: "la", age: 1 },// { name: "lc", age: 3 },// { name: "łb", age: 2 }// ]