string-kit
Advanced tools
Comparing version 0.18.1 to 0.18.2
@@ -63,2 +63,3 @@ /* | ||
%p number to relative percent (e.g.: 1.25 -> +25% ; 0.75 -> -25%) | ||
%T date/time display, using ISO date, or Intl module | ||
%t time duration, convert ms into h min s, e.g.: 2h14min52s or 2:14:52 | ||
@@ -764,5 +765,78 @@ %m convert degree into degree, minutes and seconds | ||
// 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. | ||
// Date/time | ||
// Minimal Date formating, only support sort of ISO ATM. | ||
// It will be improved later. | ||
modes.T = ( arg , modeArg ) => { | ||
// Always get a copy of the arg | ||
try { | ||
arg = new Date( arg ) ; | ||
} | ||
catch ( error ) { | ||
return '(invalid)' ; | ||
} | ||
if ( Number.isNaN( arg.getTime() ) ) { | ||
return '(invalid)' ; | ||
} | ||
var datePart = '' , | ||
timePart = '' , | ||
str = '' , | ||
subModes = dateTimeModeArg( modeArg ) , | ||
roundingType = subModes.roundingType , | ||
forceDecimalSeparator = subModes.useAbbreviation ; | ||
// For instance, we only support the ISO-like type | ||
if ( subModes.years ) { | ||
if ( datePart ) { datePart += '-' ; } | ||
datePart += arg.getFullYear() ; | ||
} | ||
if ( subModes.months ) { | ||
if ( datePart ) { datePart += '-' ; } | ||
datePart += ( '' + ( arg.getMonth() + 1 ) ).padStart( 2 , '0' ) ; | ||
} | ||
if ( subModes.days ) { | ||
if ( datePart ) { datePart += '-' ; } | ||
datePart += ( '' + arg.getDate() ).padStart( 2 , '0' ) ; | ||
} | ||
if ( subModes.hours ) { | ||
if ( timePart && ! subModes.useAbbreviation ) { timePart += ':' ; } | ||
timePart += ( '' + arg.getHours() ).padStart( 2 , '0' ) ; | ||
if ( subModes.useAbbreviation ) { timePart += 'h' ; } | ||
} | ||
if ( subModes.minutes ) { | ||
if ( timePart && ! subModes.useAbbreviation ) { timePart += ':' ; } | ||
timePart += ( '' + arg.getMinutes() ).padStart( 2 , '0' ) ; | ||
if ( subModes.useAbbreviation ) { timePart += 'min' ; } | ||
} | ||
if ( subModes.seconds ) { | ||
if ( timePart && ! subModes.useAbbreviation ) { timePart += ':' ; } | ||
timePart += ( '' + arg.getSeconds() ).padStart( 2 , '0' ) ; | ||
if ( subModes.useAbbreviation ) { timePart += 's' ; } | ||
} | ||
if ( datePart ) { | ||
if ( str ) { str += ' ' ; } | ||
str += datePart ; | ||
} | ||
if ( timePart ) { | ||
if ( str ) { str += ' ' ; } | ||
str += timePart ; | ||
} | ||
return str ; | ||
} ; | ||
modes.T.noSanitize = true ; | ||
// Time duration, transform ms into H:min:s | ||
modes.t = ( arg , modeArg ) => { | ||
@@ -774,3 +848,3 @@ if ( typeof arg === 'string' ) { arg = parseFloat( arg ) ; } | ||
sign = '' , | ||
subModes = timeModeArg( modeArg ) , | ||
subModes = timeDurationModeArg( modeArg ) , | ||
roundingType = subModes.roundingType , | ||
@@ -1064,6 +1138,109 @@ hSeparator = subModes.useAbbreviation ? 'h' : ':' , | ||
const TIME_MODES = { | ||
const DATE_TIME_MODES = { | ||
useAbbreviation: false , | ||
rightPadding: 0 , | ||
rightPaddingOnlyIfDecimal: false , | ||
years: true , | ||
months: true , | ||
days: true , | ||
hours: true , | ||
minutes: true , | ||
seconds: true | ||
} ; | ||
// Generic number modes | ||
function dateTimeModeArg( modeArg ) { | ||
DATE_TIME_MODES.rightPadding = 0 ; | ||
DATE_TIME_MODES.rightPaddingOnlyIfDecimal = false ; | ||
DATE_TIME_MODES.rounding = 0 ; | ||
DATE_TIME_MODES.roundingType = -1 ; | ||
DATE_TIME_MODES.years = DATE_TIME_MODES.months = DATE_TIME_MODES.days = false ; | ||
DATE_TIME_MODES.hours = DATE_TIME_MODES.minutes = DATE_TIME_MODES.seconds = false ; | ||
DATE_TIME_MODES.useAbbreviation = false ; | ||
var hasSelector = false ; | ||
if ( modeArg ) { | ||
for ( let [ , k , v ] of modeArg.matchAll( MODE_ARG_FORMAT_REGEX ) ) { | ||
if ( k === 'T' ) { | ||
DATE_TIME_MODES.years = DATE_TIME_MODES.months = DATE_TIME_MODES.days = false ; | ||
DATE_TIME_MODES.hours = DATE_TIME_MODES.minutes = DATE_TIME_MODES.seconds = true ; | ||
hasSelector = true ; | ||
} | ||
else if ( k === 'D' ) { | ||
DATE_TIME_MODES.years = DATE_TIME_MODES.months = DATE_TIME_MODES.days = true ; | ||
DATE_TIME_MODES.hours = DATE_TIME_MODES.minutes = DATE_TIME_MODES.seconds = false ; | ||
hasSelector = true ; | ||
} | ||
else if ( k === 'Y' ) { | ||
DATE_TIME_MODES.years = true ; | ||
hasSelector = true ; | ||
} | ||
else if ( k === 'M' ) { | ||
DATE_TIME_MODES.months = true ; | ||
hasSelector = true ; | ||
} | ||
else if ( k === 'd' ) { | ||
DATE_TIME_MODES.days = true ; | ||
hasSelector = true ; | ||
} | ||
else if ( k === 'h' ) { | ||
DATE_TIME_MODES.hours = true ; | ||
hasSelector = true ; | ||
} | ||
else if ( k === 'm' ) { | ||
DATE_TIME_MODES.minutes = true ; | ||
hasSelector = true ; | ||
} | ||
else if ( k === 's' ) { | ||
DATE_TIME_MODES.seconds = true ; | ||
hasSelector = true ; | ||
} | ||
else if ( k === 'r' ) { | ||
DATE_TIME_MODES.roundingType = 0 ; | ||
} | ||
else if ( k === 'f' ) { | ||
DATE_TIME_MODES.roundingType = -1 ; | ||
} | ||
else if ( k === 'c' ) { | ||
DATE_TIME_MODES.roundingType = 1 ; | ||
} | ||
else if ( k === 'a' ) { | ||
DATE_TIME_MODES.useAbbreviation = true ; | ||
} | ||
else if ( ! k ) { | ||
if ( v[ 0 ] === '.' ) { | ||
// Rounding after the decimal | ||
let lv = v[ v.length - 1 ] ; | ||
// Zero-right padding? | ||
if ( lv === '!' ) { | ||
DATE_TIME_MODES.rounding = DATE_TIME_MODES.rightPadding = parseInt( v.slice( 1 , -1 ) , 10 ) || 0 ; | ||
} | ||
else if ( lv === '?' ) { | ||
DATE_TIME_MODES.rounding = DATE_TIME_MODES.rightPadding = parseInt( v.slice( 1 , -1 ) , 10 ) || 0 ; | ||
DATE_TIME_MODES.rightPaddingOnlyIfDecimal = true ; | ||
} | ||
else { | ||
DATE_TIME_MODES.rounding = parseInt( v.slice( 1 ) , 10 ) || 0 ; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
if ( ! hasSelector ) { | ||
DATE_TIME_MODES.years = DATE_TIME_MODES.months = DATE_TIME_MODES.days = true ; | ||
DATE_TIME_MODES.hours = DATE_TIME_MODES.minutes = DATE_TIME_MODES.seconds = true ; | ||
} | ||
return DATE_TIME_MODES ; | ||
} | ||
const TIME_DURATION_MODES = { | ||
useAbbreviation: false , | ||
rightPadding: 0 , | ||
rightPaddingOnlyIfDecimal: false , | ||
rounding: 0 , | ||
@@ -1076,8 +1253,8 @@ roundingType: -1 , // -1: floor, 0: round, 1: ceil | ||
// Generic number modes | ||
function timeModeArg( modeArg ) { | ||
TIME_MODES.rightPadding = 0 ; | ||
TIME_MODES.rightPaddingOnlyIfDecimal = false ; | ||
TIME_MODES.rounding = 0 ; | ||
TIME_MODES.roundingType = -1 ; | ||
TIME_MODES.useAbbreviation = TIME_MODES.forceHours = TIME_MODES.forceMinutes = false ; | ||
function timeDurationModeArg( modeArg ) { | ||
TIME_DURATION_MODES.rightPadding = 0 ; | ||
TIME_DURATION_MODES.rightPaddingOnlyIfDecimal = false ; | ||
TIME_DURATION_MODES.rounding = 0 ; | ||
TIME_DURATION_MODES.roundingType = -1 ; | ||
TIME_DURATION_MODES.useAbbreviation = TIME_DURATION_MODES.forceHours = TIME_DURATION_MODES.forceMinutes = false ; | ||
@@ -1087,18 +1264,18 @@ if ( modeArg ) { | ||
if ( k === 'h' ) { | ||
TIME_MODES.forceHours = TIME_MODES.forceMinutes = true ; | ||
TIME_DURATION_MODES.forceHours = TIME_DURATION_MODES.forceMinutes = true ; | ||
} | ||
else if ( k === 'm' ) { | ||
TIME_MODES.forceMinutes = true ; | ||
TIME_DURATION_MODES.forceMinutes = true ; | ||
} | ||
else if ( k === 'r' ) { | ||
TIME_MODES.roundingType = 0 ; | ||
TIME_DURATION_MODES.roundingType = 0 ; | ||
} | ||
else if ( k === 'f' ) { | ||
TIME_MODES.roundingType = -1 ; | ||
TIME_DURATION_MODES.roundingType = -1 ; | ||
} | ||
else if ( k === 'c' ) { | ||
TIME_MODES.roundingType = 1 ; | ||
TIME_DURATION_MODES.roundingType = 1 ; | ||
} | ||
else if ( k === 'a' ) { | ||
TIME_MODES.useAbbreviation = true ; | ||
TIME_DURATION_MODES.useAbbreviation = true ; | ||
} | ||
@@ -1112,10 +1289,10 @@ else if ( ! k ) { | ||
if ( lv === '!' ) { | ||
TIME_MODES.rounding = TIME_MODES.rightPadding = parseInt( v.slice( 1 , -1 ) , 10 ) || 0 ; | ||
TIME_DURATION_MODES.rounding = TIME_DURATION_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 ; | ||
TIME_DURATION_MODES.rounding = TIME_DURATION_MODES.rightPadding = parseInt( v.slice( 1 , -1 ) , 10 ) || 0 ; | ||
TIME_DURATION_MODES.rightPaddingOnlyIfDecimal = true ; | ||
} | ||
else { | ||
TIME_MODES.rounding = parseInt( v.slice( 1 ) , 10 ) || 0 ; | ||
TIME_DURATION_MODES.rounding = parseInt( v.slice( 1 ) , 10 ) || 0 ; | ||
} | ||
@@ -1127,3 +1304,3 @@ } | ||
return TIME_MODES ; | ||
return TIME_DURATION_MODES ; | ||
} | ||
@@ -1130,0 +1307,0 @@ |
{ | ||
"name": "string-kit", | ||
"version": "0.18.1", | ||
"version": "0.18.2", | ||
"engines": { | ||
@@ -5,0 +5,0 @@ "node": ">=14.15.0" |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
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
807378
7965