string-kit
Version:
A string manipulation toolbox, featuring a string formatter (inspired by sprintf), a variable inspector (output featuring ANSI colors and HTML) and various escape functions (shell argument, regexp, html, etc).
146 lines (107 loc) • 4.51 kB
JavaScript
/*
String Kit
Copyright (c) 2014 - 2021 Cédric Ronvel
The MIT License (MIT)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
"use strict" ;
const CONTROL_CLASS = 1 ;
const WORD_SEPARATOR_CLASS = 2 ;
const LETTER_CLASS = 3 ;
const NUMBER_CLASS = 4 ;
const SYMBOL_CLASS = 5 ;
function getCharacterClass( char , code ) {
if ( isWordSeparator( code ) ) { return WORD_SEPARATOR_CLASS ; }
if ( code <= 0x1f || code === 0x7f ) { return CONTROL_CLASS ; }
if ( isNumber( code ) ) { return NUMBER_CLASS ; }
// Here we assume that a letter is a char with a “case”
if ( char.toUpperCase() !== char.toLowerCase() ) { return LETTER_CLASS ; }
return SYMBOL_CLASS ;
}
function isWordSeparator( code ) {
if (
// space, tab, no-break space
code === 0x20 || code === 0x09 || code === 0xa0 ||
// hyphen, underscore
code === 0x2d || code === 0x5f
) {
return true ;
}
return false ;
}
function isNumber( code ) {
if ( code >= 0x30 && code <= 0x39 ) { return true ; }
return false ;
}
function naturalSort( a , b ) {
a = '' + a ;
b = '' + b ;
var aIndex , aEndIndex , aChar , aCode , aClass , aCharLc , aNumber ,
aTrim = a.trim() ,
aLength = aTrim.length ,
bIndex , bEndIndex , bChar , bCode , bClass , bCharLc , bNumber ,
bTrim = b.trim() ,
bLength = bTrim.length ,
advantage = 0 ;
for ( aIndex = bIndex = 0 ; aIndex < aLength && bIndex < bLength ; aIndex ++ , bIndex ++ ) {
aChar = aTrim[ aIndex ] ;
bChar = bTrim[ bIndex ] ;
aCode = aTrim.charCodeAt( aIndex ) ;
bCode = bTrim.charCodeAt( bIndex ) ;
aClass = getCharacterClass( aChar , aCode ) ;
bClass = getCharacterClass( bChar , bCode ) ;
if ( aClass !== bClass ) { return aClass - bClass ; }
switch ( aClass ) {
case WORD_SEPARATOR_CLASS :
// Eat all white chars and continue
while ( isWordSeparator( aTrim.charCodeAt( aIndex + 1 ) ) ) { aIndex ++ ; }
while ( isWordSeparator( bTrim.charCodeAt( bIndex + 1 ) ) ) { bIndex ++ ; }
break ;
case CONTROL_CLASS :
case SYMBOL_CLASS :
if ( aCode !== bCode ) { return aCode - bCode ; }
break ;
case LETTER_CLASS :
aCharLc = aChar.toLowerCase() ;
bCharLc = bChar.toLowerCase() ;
if ( aCharLc !== bCharLc ) { return aCharLc > bCharLc ? 1 : -1 ; }
// As a last resort, we would sort uppercase first
if ( ! advantage && aChar !== bChar ) { advantage = aChar !== aCharLc ? -1 : 1 ; }
break ;
case NUMBER_CLASS :
// Lookup for a whole number and parse it
aEndIndex = aIndex + 1 ;
while ( isNumber( aTrim.charCodeAt( aEndIndex ) ) ) { aEndIndex ++ ; }
aNumber = parseFloat( aTrim.slice( aIndex , aEndIndex ) ) ;
bEndIndex = bIndex + 1 ;
while ( isNumber( bTrim.charCodeAt( bEndIndex ) ) ) { bEndIndex ++ ; }
bNumber = parseFloat( bTrim.slice( bIndex , bEndIndex ) ) ;
if ( aNumber !== bNumber ) { return aNumber - bNumber ; }
// As a last resort, we would sort the number with the less char first
if ( ! advantage && aEndIndex - aIndex !== bEndIndex - bIndex ) { advantage = ( aEndIndex - aIndex ) - ( bEndIndex - bIndex ) ; }
// Advance the index at the end of the number area
aIndex = aEndIndex - 1 ;
bIndex = bEndIndex - 1 ;
break ;
}
}
// If there was an “advantage”, use it now
if ( advantage ) { return advantage ; }
// Finally, sort by remaining char, or by trimmed length or by full length
return ( aLength - aIndex ) - ( bLength - bIndex ) || aLength - bLength || a.length - b.length ;
}
module.exports = naturalSort ;