bystr-sort

1.3.1Β β€’Β PublicΒ β€’Β Published

Build Status npm version

ByStr~Sort

Does sort your arrays of T object following a verbose sortString πŸ™‚ .


Getting started πŸš€

  1. install it
    $ npm install -D bystr-sort
    and require it
    const { sort } = require( "bystr-sort" );
  2. define a sort string: Assuming you have an array of T object like this:
    const 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" }
    ];
    A valid sort string could be
    const sortString = `
         by greet of a greater than b's then
         by level of a < than b's
    `;
  3. sort your T[] :
    const sorted : T[] = sort( greetings , sortString );

Sortstring

  • it is a string used to sort your array of T objects.
  • It gives you the ability to set multiple sorts by using the then keyword at the end of a sort sentence. a sort sentence is matches this regex pattern
    by {colA|colB|colC|...|colN} of a[|b] lower[|greater,<,>] than b[|a]'s [then]
  • The by keyword starts the sentence and a's or b's ends it. Then is used to chain the next sort sentence
  • To invert the sort direction, you can play with keywords lower, greater, >, < but also with a, b order in the sentence. So: by ... b ... > ... a's is 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.

Package Sidebar

Install

npm i bystr-sort

Weekly Downloads

7

Version

1.3.1

License

MIT

Unpacked Size

9.46 kB

Total Files

6

Last publish

Collaborators

  • maze014