1.3.1 • Published 4 years ago
bystr-sort v1.3.1
ByStr~Sort
Does sort your arrays of T object following a verbose sortString 🙂 .
Getting started 🚀
- install it
and require it$ npm install -D bystr-sortconst { sort } = require( "bystr-sort" ); - define a sort string:
Assuming you have an array of
Tobject like this:
A valid sort string could beconst greetings : T[] = [ { order:14 , greet:"Hello" , level:"medium" , lang:"en" }, { order:3 , greet:"yo" , level:"familiar" , lang:"fr" }, { order:5 , greet:"selamat pagi" , level:"medium" , lang:"id" } ];const sortString = ` by greet of a greater than b's then by level of a < than b's `; - sort your
T[]:const sorted : T[] = sort( greetings , sortString );
Sortstring
- it is a string used to sort your array of
Tobjects. - It gives you the ability to set multiple sorts by using the
thenkeyword at the end of a sort sentence. a sort sentence is matches this regex patternby {colA|colB|colC|...|colN} of a[|b] lower[|greater,<,>] than b[|a]'s [then] - The
bykeyword starts the sentence anda'sorb'sends it. Then is used to chain the next sort sentence - To invert the sort direction, you can play with keywords
lower, greater, >, <but also witha, border in the sentence. So:by ... b ... > ... a'sis also valid. Because why not ?
Note
- further updates will allow the case insensitiveness of the whole sort sentence ...
Bonus 🎈
there is a littel more ...
parameters
Assuming the signature for sort is like the following:
const sort = (
objectArray: any[] ,
sortString = "" ,
crashOnError = false ,
logfn = console.error
): any[]crashOnError
When set to true : It gives you the abbility to throw an exception when your sort string is invalid. You will be prompted with hints to fix your problem.
When set to false : It will prompt the error in the console.
logfn Use this to check errors within your tests.