import naturalCompare from 'natural-compare-lite' ; import { indexSignature } from './common' ; function charCompare ( a , b ) { if ( a < b ) { return - 1 ; } if ( b < a ) { return 1 ; } return 0 ; } function getWeight ( value ) { switch ( true ) { case indexSignature . regex . test ( value ) : return 100 ; default : return 0 ; } } function weightedCompare ( a , b , compareFn ) { return compareFn ( a , b ) - getWeight ( a ) + getWeight ( b ) ; } const ascending = ( a , b ) => { return weightedCompare ( a , b , charCompare ) ; } ; const ascendingInsensitive = ( a , b ) => { return weightedCompare ( a . toLowerCase ( ) , b . toLowerCase ( ) , charCompare ) ; } ; const ascendingNatural = ( a , b ) => { return weightedCompare ( a , b , naturalCompare ) ; } ; const ascendingInsensitiveNatural = ( a , b ) => { return weightedCompare ( a . toLowerCase ( ) , b . toLowerCase ( ) , naturalCompare ) ; } ; /**
 * Functions which check that the given 2 names are in specific order.
 */ export const compareFn = ( isAscending , isInsensitive , isNatural ) => ( ... args ) => { if ( args . filter ( Boolean ) . length !== 2 ) { return 0 ; } const input = ( isAscending ? args : args . reverse ( ) ) as [ string , string ] ; if ( isInsensitive && isNatural ) { return ascendingInsensitiveNatural ( ... input ) ; } if ( ! isInsensitive && isNatural ) { return ascendingNatural ( ... input ) ; } if ( isInsensitive && ! isNatural ) { return ascendingInsensitive ( ... input ) ; } return ascending ( ... input ) ; } ;