string-kit
Advanced tools
Comparing version 0.16.3 to 0.16.4
@@ -63,3 +63,4 @@ /* | ||
%p number to relative percent (e.g.: 1.25 -> +25% ; 0.75 -> -25%) | ||
%t time duration, convert ms into H:min:s | ||
%t time duration, convert ms into h min s, e.g.: 2h14min52s | ||
%T time duration, convert ms into H:min:s, e.g.: 2:14:52 | ||
%m convert degree into degree, minutes and seconds | ||
@@ -803,22 +804,36 @@ %h hexadecimal (input is a number) | ||
// /!\ Should use StringNumber??? | ||
// time duration, transform ms into H:min:s | ||
// Later it should format Date as well: number=duration, date object=date | ||
// Note that it would not replace moment.js, but it could uses it. | ||
modes.t = arg => { | ||
modes.t = ( arg , modeArg , useLetters = true ) => { | ||
if ( typeof arg === 'string' ) { arg = parseFloat( arg ) ; } | ||
if ( typeof arg !== 'number' ) { return '(NaN)' ; } | ||
var s = Math.floor( arg / 1000 ) ; | ||
if ( s < 60 ) { return s + 's' ; } | ||
var h , min , s , sn , sStr , | ||
subModes = timeModeArg( modeArg ) , | ||
hSeparator = useLetters ? 'h' : ':' , | ||
minSeparator = useLetters ? 'min' : ':' , | ||
sSeparator = useLetters ? 's' : '.' , | ||
forceDecimalSeparator = useLetters ; | ||
var min = Math.floor( s / 60 ) ; | ||
s = arg / 1000 ; | ||
if ( s < 60 && ! subModes.forceMinutes ) { | ||
sn = new StringNumber( s , sSeparator , undefined , forceDecimalSeparator ) ; | ||
sn.round( subModes.rounding , subModes.roundingType ) ; | ||
sStr = sn.toString( 1 , subModes.rightPadding , subModes.rightPaddingOnlyIfDecimal ) ; | ||
return sStr ; | ||
} | ||
min = Math.floor( s / 60 ) ; | ||
s = s % 60 ; | ||
if ( min < 60 ) { return min + 'min' + ( '' + s ).padStart( 2 , '0' ) + 's' ; } | ||
var h = Math.floor( min / 60 ) ; | ||
sn = new StringNumber( s , sSeparator , undefined , forceDecimalSeparator ) ; | ||
sn.round( subModes.rounding , subModes.roundingType ) ; | ||
sStr = sn.toString( 2 , subModes.rightPadding , subModes.rightPaddingOnlyIfDecimal ) ; | ||
if ( min < 60 && ! subModes.forceHours ) { return min + minSeparator + sStr ; } | ||
h = Math.floor( min / 60 ) ; | ||
min = min % 60 ; | ||
//if ( h < 24 ) { return h + 'h' + zeroPadding( min ) +'min' + zeroPadding( s ) + 's' ; } | ||
return h + 'h' + ( '' + min ).padStart( 2 , '0' ) + 'min' + ( '' + s ).padStart( 2 , '0' ) + 's' ; | ||
return h + hSeparator + ( '' + min ).padStart( 2 , '0' ) + minSeparator + sStr ; | ||
} ; | ||
@@ -828,4 +843,7 @@ | ||
modes.T = ( arg , modeArg ) => modes.t( arg , modeArg , false ) ; | ||
modes.T.noSanitize = true ; | ||
// unsigned hexadecimal | ||
@@ -1076,2 +1094,70 @@ modes.h = arg => { | ||
const TIME_MODES = { | ||
rightPadding: 0 , | ||
rightPaddingOnlyIfDecimal: false , | ||
rounding: 0 , | ||
roundingType: -1 , // -1: floor, 0: round, 1: ceil | ||
forceHours: false , | ||
forceMinutes: false | ||
} ; | ||
const TIME_MODE_ARG_FORMAT_REGEX = /([a-zA-Z]|^)([^a-zA-Z]*)/g ; | ||
// Generic number modes | ||
function timeModeArg( modeArg ) { | ||
var match , k , v , lv ; | ||
TIME_MODES.rightPadding = 0 ; | ||
TIME_MODES.rightPaddingOnlyIfDecimal = false ; | ||
TIME_MODES.rounding = 0 ; | ||
TIME_MODES.roundingType = -1 ; | ||
TIME_MODES.forceHours = TIME_MODES.forceMinutes = false ; | ||
if ( modeArg ) { | ||
TIME_MODE_ARG_FORMAT_REGEX.lastIndex = 0 ; | ||
while ( ( match = TIME_MODE_ARG_FORMAT_REGEX.exec( modeArg ) ) ) { | ||
[ , k , v ] = match ; | ||
if ( k === 'h' ) { | ||
TIME_MODES.forceHours = TIME_MODES.forceMinutes = true ; | ||
} | ||
else if ( k === 'm' ) { | ||
TIME_MODES.forceMinutes = true ; | ||
} | ||
else if ( k === 'r' ) { | ||
TIME_MODES.roundingType = 0 ; | ||
} | ||
else if ( k === 'f' ) { | ||
TIME_MODES.roundingType = -1 ; | ||
} | ||
else if ( k === 'c' ) { | ||
TIME_MODES.roundingType = 1 ; | ||
} | ||
else if ( ! k ) { | ||
if ( v[ 0 ] === '.' ) { | ||
// Rounding after the decimal | ||
lv = v[ v.length - 1 ] ; | ||
// Zero-right padding? | ||
if ( lv === '!' ) { | ||
TIME_MODES.rounding = TIME_MODES.rightPadding = parseInt( v.slice( 1 , -1 ) , 10 ) || 0 ; | ||
} | ||
else if ( lv === '?' ) { | ||
TIME_MODES.rounding = TIME_MODES.rightPadding = parseInt( v.slice( 1 , -1 ) , 10 ) || 0 ; | ||
TIME_MODES.rightPaddingOnlyIfDecimal = true ; | ||
} | ||
else { | ||
TIME_MODES.rounding = parseInt( v.slice( 1 ) , 10 ) || 0 ; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return TIME_MODES ; | ||
} | ||
// Generic inspect | ||
@@ -1078,0 +1164,0 @@ function genericInspectMode( arg , modeArg , options , modeOptions , isInspectError = false ) { |
@@ -40,3 +40,3 @@ /* | ||
function StringNumber( number , decimalSeparator = '.' , groupSeparator = '' ) { | ||
function StringNumber( number , decimalSeparator = '.' , groupSeparator = '' , forceDecimalSeparator = false ) { | ||
this.sign = 1 ; | ||
@@ -48,2 +48,3 @@ this.digits = [] ; | ||
this.decimalSeparator = decimalSeparator ; | ||
this.forceDecimalSeparator = !! forceDecimalSeparator ; | ||
this.groupSeparator = groupSeparator ; | ||
@@ -188,4 +189,4 @@ | ||
// leadingZero = minimal number of number before the dot, they will be left-padded with zero if needed. | ||
// trailingZero = minimal number of number after the dot, they will be right-padded with zero if needed. | ||
// leadingZero = minimal number of numbers before the dot, they will be left-padded with zero if needed. | ||
// trailingZero = minimal number of numbers after the dot, they will be right-padded with zero if needed. | ||
// onlyIfDecimal: set it to true if you don't want right padding zero when there is no decimal | ||
@@ -253,2 +254,5 @@ StringNumber.prototype.toNoExp = | ||
} | ||
else if ( this.forceDecimalSeparator ) { | ||
str += this.decimalSeparator ; | ||
} | ||
@@ -291,3 +295,10 @@ return str ; | ||
StringNumber.prototype.precision = function( n ) { | ||
/* | ||
type: 0=round, -1=floor, 1=ceil | ||
Floor if < .99999 | ||
Ceil if >= .00001 | ||
*/ | ||
StringNumber.prototype.precision = function( n , type = 0 ) { | ||
var roundUp ; | ||
if ( this.special !== null || n >= this.digits.length ) { return this ; } | ||
@@ -297,3 +308,18 @@ | ||
if ( this.digits[ n ] >= 5 ) { | ||
if ( type < 0 ) { | ||
roundUp = | ||
this.digits.length > n + 4 | ||
&& this.digits[ n ] === 9 && this.digits[ n + 1 ] === 9 | ||
&& this.digits[ n + 2 ] === 9 && this.digits[ n + 3 ] === 9 && this.digits[ n + 4 ] === 9 ; | ||
} | ||
else if ( type > 0 ) { | ||
roundUp = | ||
this.digits[ n ] > 0 || this.digits[ n + 1 ] > 0 | ||
|| this.digits[ n + 2 ] > 0 || this.digits[ n + 3 ] > 0 || this.digits[ n + 4 ] > 0 ; | ||
} | ||
else { | ||
roundUp = this.digits[ n ] >= 5 ; | ||
} | ||
if ( roundUp ) { | ||
let i = n - 1 , | ||
@@ -327,5 +353,5 @@ done = false ; | ||
StringNumber.prototype.round = function( decimalPlace = 0 ) { | ||
StringNumber.prototype.round = function( decimalPlace = 0 , type = 0 ) { | ||
var n = this.exposant + decimalPlace ; | ||
return this.precision( n ) ; | ||
return this.precision( n , type ) ; | ||
} ; | ||
@@ -335,2 +361,16 @@ | ||
StringNumber.prototype.floor = function( decimalPlace = 0 ) { | ||
var n = this.exposant + decimalPlace ; | ||
return this.precision( n , -1 ) ; | ||
} ; | ||
StringNumber.prototype.ceil = function( decimalPlace = 0 ) { | ||
var n = this.exposant + decimalPlace ; | ||
return this.precision( n , 1 ) ; | ||
} ; | ||
StringNumber.prototype.removeTrailingZero = function() { | ||
@@ -337,0 +377,0 @@ var i = this.digits.length - 1 ; |
{ | ||
"name": "string-kit", | ||
"version": "0.16.3", | ||
"version": "0.16.4", | ||
"engines": { | ||
@@ -5,0 +5,0 @@ "node": ">=14.15.0" |
@@ -44,3 +44,3 @@ /* | ||
//console.log( sn ) ; | ||
expect( sn ).to.be.like( { sign: 1, digits: [], exposant: 0 , special: null , decimalSeparator: '.' , groupSeparator: '' } ) ; | ||
expect( sn ).to.be.like( { sign: 1, digits: [], exposant: 0 , special: null , decimalSeparator: '.' , groupSeparator: '' , forceDecimalSeparator: false } ) ; | ||
expect( sn.toNumber() ).to.be( 0 ) ; | ||
@@ -50,3 +50,3 @@ | ||
//console.log( sn ) ; | ||
expect( sn ).to.be.like( { sign: 1, digits: [ 1, 2, 3, 4 ], exposant: 0 , special: null , decimalSeparator: '.' , groupSeparator: '' } ) ; | ||
expect( sn ).to.be.like( { sign: 1, digits: [ 1, 2, 3, 4 ], exposant: 0 , special: null , decimalSeparator: '.' , groupSeparator: '' , forceDecimalSeparator: false } ) ; | ||
expect( sn.toNumber() ).to.be( 0.1234 ) ; | ||
@@ -56,3 +56,3 @@ | ||
//console.log( sn ) ; | ||
expect( sn ).to.be.like( { sign: 1, digits: [ 1, 2, 3, 4 ], exposant: -3 , special: null , decimalSeparator: '.' , groupSeparator: '' } ) ; | ||
expect( sn ).to.be.like( { sign: 1, digits: [ 1, 2, 3, 4 ], exposant: -3 , special: null , decimalSeparator: '.' , groupSeparator: '' , forceDecimalSeparator: false } ) ; | ||
expect( sn.toNumber() ).to.be( 0.0001234 ) ; | ||
@@ -62,3 +62,3 @@ | ||
//console.log( sn ) ; | ||
expect( sn ).to.be.like( { sign: 1, digits: [ 1, 2, 3, 4 ], exposant: 1 , special: null , decimalSeparator: '.' , groupSeparator: '' } ) ; | ||
expect( sn ).to.be.like( { sign: 1, digits: [ 1, 2, 3, 4 ], exposant: 1 , special: null , decimalSeparator: '.' , groupSeparator: '' , forceDecimalSeparator: false } ) ; | ||
expect( sn.toNumber() ).to.be( 1.234 ) ; | ||
@@ -68,3 +68,3 @@ | ||
//console.log( sn ) ; | ||
expect( sn ).to.be.like( { sign: 1, digits: [ 1, 2, 3, 4 ], exposant: 3 , special: null , decimalSeparator: '.' , groupSeparator: '' } ) ; | ||
expect( sn ).to.be.like( { sign: 1, digits: [ 1, 2, 3, 4 ], exposant: 3 , special: null , decimalSeparator: '.' , groupSeparator: '' , forceDecimalSeparator: false } ) ; | ||
expect( sn.toNumber() ).to.be( 123.4 ) ; | ||
@@ -74,3 +74,3 @@ | ||
//console.log( sn ) ; | ||
expect( sn ).to.be.like( { sign: 1, digits: [ 1, 2, 3, 4 ], exposant: 4 , special: null , decimalSeparator: '.' , groupSeparator: '' } ) ; | ||
expect( sn ).to.be.like( { sign: 1, digits: [ 1, 2, 3, 4 ], exposant: 4 , special: null , decimalSeparator: '.' , groupSeparator: '' , forceDecimalSeparator: false } ) ; | ||
expect( sn.toNumber() ).to.be( 1234 ) ; | ||
@@ -80,3 +80,3 @@ | ||
//console.log( sn ) ; | ||
expect( sn ).to.be.like( { sign: 1, digits: [ 1, 2, 3, 4 ], exposant: 5 , special: null , decimalSeparator: '.' , groupSeparator: '' } ) ; | ||
expect( sn ).to.be.like( { sign: 1, digits: [ 1, 2, 3, 4 ], exposant: 5 , special: null , decimalSeparator: '.' , groupSeparator: '' , forceDecimalSeparator: false } ) ; | ||
expect( sn.toNumber() ).to.be( 12340 ) ; | ||
@@ -86,3 +86,3 @@ | ||
//console.log( sn ) ; | ||
expect( sn ).to.be.like( { sign: 1, digits: [ 1, 2, 3, 4 ], exposant: 9 , special: null , decimalSeparator: '.' , groupSeparator: '' } ) ; | ||
expect( sn ).to.be.like( { sign: 1, digits: [ 1, 2, 3, 4 ], exposant: 9 , special: null , decimalSeparator: '.' , groupSeparator: '' , forceDecimalSeparator: false } ) ; | ||
expect( sn.toNumber() ).to.be( 123400000 ) ; | ||
@@ -92,3 +92,3 @@ | ||
//console.log( sn , sn.toNumber() ) ; | ||
expect( sn ).to.be.like( { sign: 1, digits: [ 1, 2, 3, 4 ], exposant: 15 , special: null , decimalSeparator: '.' , groupSeparator: '' } ) ; | ||
expect( sn ).to.be.like( { sign: 1, digits: [ 1, 2, 3, 4 ], exposant: 15 , special: null , decimalSeparator: '.' , groupSeparator: '' , forceDecimalSeparator: false } ) ; | ||
expect( sn.toNumber() ).to.be( 123400000000000 ) ; | ||
@@ -98,3 +98,3 @@ | ||
//console.log( sn , sn.toNumber() ) ; | ||
expect( sn ).to.be.like( { sign: 1, digits: [ 1, 2, 3, 4 ], exposant: 25 , special: null , decimalSeparator: '.' , groupSeparator: '' } ) ; | ||
expect( sn ).to.be.like( { sign: 1, digits: [ 1, 2, 3, 4 ], exposant: 25 , special: null , decimalSeparator: '.' , groupSeparator: '' , forceDecimalSeparator: false } ) ; | ||
expect( sn.toNumber() ).to.be( 1234000000000000000000000 ) ; | ||
@@ -104,3 +104,3 @@ | ||
//console.log( sn , sn.toNumber() ) ; | ||
expect( sn ).to.be.like( { sign: 1, digits: [ 1, 2, 3, 4 ], exposant: -20 , special: null , decimalSeparator: '.' , groupSeparator: '' } ) ; | ||
expect( sn ).to.be.like( { sign: 1, digits: [ 1, 2, 3, 4 ], exposant: -20 , special: null , decimalSeparator: '.' , groupSeparator: '' , forceDecimalSeparator: false } ) ; | ||
expect( sn.toNumber() ).to.be( 0.000000000000000000001234 ) ; | ||
@@ -110,3 +110,3 @@ | ||
//console.log( sn , sn.toNumber() ) ; | ||
expect( sn ).to.be.like( { sign: 1, digits: [ 9, 8, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 4 ], exposant: 3 , special: null , decimalSeparator: '.' , groupSeparator: '' } ) ; | ||
expect( sn ).to.be.like( { sign: 1, digits: [ 9, 8, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 4 ], exposant: 3 , special: null , decimalSeparator: '.' , groupSeparator: '' , forceDecimalSeparator: false } ) ; | ||
expect( sn.toNumber() ).to.be( 987.00000000001234 ) ; | ||
@@ -116,3 +116,3 @@ | ||
//console.log( sn ) ; | ||
expect( sn ).to.be.like( { sign: -1, digits: [ 1, 2, 3, 4 ], exposant: 0 , special: null , decimalSeparator: '.' , groupSeparator: '' } ) ; | ||
expect( sn ).to.be.like( { sign: -1, digits: [ 1, 2, 3, 4 ], exposant: 0 , special: null , decimalSeparator: '.' , groupSeparator: '' , forceDecimalSeparator: false } ) ; | ||
expect( sn.toNumber() ).to.be( -0.1234 ) ; | ||
@@ -122,3 +122,3 @@ | ||
//console.log( sn ) ; | ||
expect( sn ).to.be.like( { sign: -1, digits: [ 1, 2, 3, 4 ], exposant: 3 , special: null , decimalSeparator: '.' , groupSeparator: '' } ) ; | ||
expect( sn ).to.be.like( { sign: -1, digits: [ 1, 2, 3, 4 ], exposant: 3 , special: null , decimalSeparator: '.' , groupSeparator: '' , forceDecimalSeparator: false } ) ; | ||
expect( sn.toNumber() ).to.be( -123.4 ) ; | ||
@@ -132,3 +132,3 @@ } ) ; | ||
//console.log( sn ) ; | ||
expect( sn ).to.be.like( { sign: 1, digits: [ 1 ], exposant: 4 , special: null , decimalSeparator: '.' , groupSeparator: '' } ) ; | ||
expect( sn ).to.be.like( { sign: 1, digits: [ 1 ], exposant: 4 , special: null , decimalSeparator: '.' , groupSeparator: '' , forceDecimalSeparator: false } ) ; | ||
expect( sn.toNumber() ).to.be( 1000 ) ; | ||
@@ -138,3 +138,3 @@ | ||
//console.log( sn ) ; | ||
expect( sn ).to.be.like( { sign: 1, digits: [ 1 ], exposant: 4 , special: null , decimalSeparator: '.' , groupSeparator: '' } ) ; | ||
expect( sn ).to.be.like( { sign: 1, digits: [ 1 ], exposant: 4 , special: null , decimalSeparator: '.' , groupSeparator: '' , forceDecimalSeparator: false } ) ; | ||
expect( sn.toNumber() ).to.be( 1000 ) ; | ||
@@ -144,3 +144,3 @@ | ||
//console.log( sn ) ; | ||
expect( sn ).to.be.like( { sign: 1, digits: [ 1, 0, 1 ], exposant: 4 , special: null , decimalSeparator: '.' , groupSeparator: '' } ) ; | ||
expect( sn ).to.be.like( { sign: 1, digits: [ 1, 0, 1 ], exposant: 4 , special: null , decimalSeparator: '.' , groupSeparator: '' , forceDecimalSeparator: false } ) ; | ||
expect( sn.toNumber() ).to.be( 1010 ) ; | ||
@@ -150,3 +150,3 @@ | ||
//console.log( sn ) ; | ||
expect( sn ).to.be.like( { sign: 1, digits: [ 1, 0, 1 ], exposant: 4 , special: null , decimalSeparator: '.' , groupSeparator: '' } ) ; | ||
expect( sn ).to.be.like( { sign: 1, digits: [ 1, 0, 1 ], exposant: 4 , special: null , decimalSeparator: '.' , groupSeparator: '' , forceDecimalSeparator: false } ) ; | ||
expect( sn.toNumber() ).to.be( 1010 ) ; | ||
@@ -156,3 +156,3 @@ | ||
//console.log( sn ) ; | ||
expect( sn ).to.be.like( { sign: 1, digits: [ 1, 0, 4 ], exposant: 4 , special: null , decimalSeparator: '.' , groupSeparator: '' } ) ; | ||
expect( sn ).to.be.like( { sign: 1, digits: [ 1, 0, 4 ], exposant: 4 , special: null , decimalSeparator: '.' , groupSeparator: '' , forceDecimalSeparator: false } ) ; | ||
expect( sn.toNumber() ).to.be( 1040 ) ; | ||
@@ -162,3 +162,3 @@ | ||
//console.log( sn ) ; | ||
expect( sn ).to.be.like( { sign: 1, digits: [ 2 ], exposant: 4 , special: null , decimalSeparator: '.' , groupSeparator: '' } ) ; | ||
expect( sn ).to.be.like( { sign: 1, digits: [ 2 ], exposant: 4 , special: null , decimalSeparator: '.' , groupSeparator: '' , forceDecimalSeparator: false } ) ; | ||
expect( sn.toNumber() ).to.be( 2000 ) ; | ||
@@ -165,0 +165,0 @@ } ) ; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
320501
5009