mirror of
https://github.com/CorentinTh/it-tools.git
synced 2025-05-07 14:57:12 -04:00
fix: fix handling of milliseconds
in parsing and in formatting iso duration in parsing of timestamp
This commit is contained in:
parent
4e8d244cdc
commit
cfa2502201
5 changed files with 73 additions and 34 deletions
|
@ -128,7 +128,7 @@ function activateOption(option: PaletteOption) {
|
||||||
<c-input-text ref="inputRef" v-model:value="searchPrompt" raw-text placeholder="Type to search a tool or a command..." autofocus clearable />
|
<c-input-text ref="inputRef" v-model:value="searchPrompt" raw-text placeholder="Type to search a tool or a command..." autofocus clearable />
|
||||||
|
|
||||||
<div v-for="(options, category) in filteredSearchResult" :key="category">
|
<div v-for="(options, category) in filteredSearchResult" :key="category">
|
||||||
<div ml-3 mt-3 text-sm font-bold text-primary op-60>
|
<div ml-3 mt-3 text-sm text-primary font-bold op-60>
|
||||||
{{ category }}
|
{{ category }}
|
||||||
</div>
|
</div>
|
||||||
<command-palette-option v-for="option in options" :key="option.name" :option="option" :selected="selectedOptionIndex === getOptionIndex(option)" @activated="activateOption" />
|
<command-palette-option v-for="option in options" :key="option.name" :option="option" :selected="selectedOptionIndex === getOptionIndex(option)" @activated="activateOption" />
|
||||||
|
|
|
@ -24,6 +24,8 @@ describe('duration-calculator', () => {
|
||||||
describe('computeDuration', () => {
|
describe('computeDuration', () => {
|
||||||
it('should compute correct sum/values', () => {
|
it('should compute correct sum/values', () => {
|
||||||
expect(computeDuration('')).to.deep.eq(zeroResult);
|
expect(computeDuration('')).to.deep.eq(zeroResult);
|
||||||
|
expect(computeDuration('00:00:00')).to.deep.eq(zeroResult);
|
||||||
|
expect(computeDuration('0h')).to.deep.eq(zeroResult);
|
||||||
expect(computeDuration('0s')).to.deep.eq(zeroResult);
|
expect(computeDuration('0s')).to.deep.eq(zeroResult);
|
||||||
expect(computeDuration('3600s')).to.deep.eq({
|
expect(computeDuration('3600s')).to.deep.eq({
|
||||||
errors: [],
|
errors: [],
|
||||||
|
@ -120,7 +122,7 @@ describe('duration-calculator', () => {
|
||||||
total: {
|
total: {
|
||||||
days: 0.11128616898148148,
|
days: 0.11128616898148148,
|
||||||
hours: 2.6708680555555557,
|
hours: 2.6708680555555557,
|
||||||
iso8601Duration: 'P0Y0M0DT2H40M15S',
|
iso8601Duration: 'P0Y0M0DT2H40M15.125S',
|
||||||
milliseconds: 9615125,
|
milliseconds: 9615125,
|
||||||
minutes: 160.25208333333333,
|
minutes: 160.25208333333333,
|
||||||
prettified: '2h 40m 15s 125ms',
|
prettified: '2h 40m 15s 125ms',
|
||||||
|
@ -173,19 +175,19 @@ describe('duration-calculator', () => {
|
||||||
expect(computeDuration('P4DT12H20M20.3S')).to.deep.eq({
|
expect(computeDuration('P4DT12H20M20.3S')).to.deep.eq({
|
||||||
errors: [],
|
errors: [],
|
||||||
total: {
|
total: {
|
||||||
days: 0.5138891238425926,
|
days: 4.514123842592593,
|
||||||
hours: 12.333338972222222,
|
hours: 108.33897222222222,
|
||||||
iso8601Duration: 'P0Y0M0DT12H20M0S',
|
iso8601Duration: 'P0Y0M4DT12H20M20.3S',
|
||||||
milliseconds: 44400020.3,
|
milliseconds: 390020300,
|
||||||
minutes: 740.0003383333333,
|
minutes: 6500.338333333333,
|
||||||
prettified: '12h 20m 20ms 300µs',
|
prettified: '4d 12h 20m 20s 300ms',
|
||||||
prettifiedColonNotation: '12:20:00',
|
prettifiedColonNotation: '4:12:20:20.3',
|
||||||
prettifiedDaysColon: '12:20:00.20.299999997019768',
|
prettifiedDaysColon: '4d 12:20:20.300',
|
||||||
prettifiedHoursColon: '12:20:00.20.299999997019768',
|
prettifiedHoursColon: '108:20:20.300',
|
||||||
prettifiedVerbose: '12 hours 20 minutes 20 milliseconds 300 microseconds',
|
prettifiedVerbose: '4 days 12 hours 20 minutes 20 seconds 300 milliseconds',
|
||||||
seconds: 44400.0203,
|
seconds: 390020.3,
|
||||||
weeks: 0.07341273197751322,
|
weeks: 0.6448748346560846,
|
||||||
years: 0.0014079154077879248,
|
years: 0.012367462582445459,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
expect(computeDuration('25s\n+PT20H\n-10s')).to.deep.eq({
|
expect(computeDuration('25s\n+PT20H\n-10s')).to.deep.eq({
|
||||||
|
@ -315,7 +317,7 @@ describe('duration-calculator', () => {
|
||||||
total: {
|
total: {
|
||||||
days: 2.000001446759259,
|
days: 2.000001446759259,
|
||||||
hours: 48.000034722222225,
|
hours: 48.000034722222225,
|
||||||
iso8601Duration: 'P0Y0M2DT0H0M0S',
|
iso8601Duration: 'P0Y0M2DT0H0M0.125S',
|
||||||
milliseconds: 172800125,
|
milliseconds: 172800125,
|
||||||
minutes: 2880.0020833333333,
|
minutes: 2880.0020833333333,
|
||||||
prettified: '2d 125ms',
|
prettified: '2d 125ms',
|
||||||
|
@ -328,6 +330,42 @@ describe('duration-calculator', () => {
|
||||||
years: 0.005479456018518519,
|
years: 0.005479456018518519,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
expect(computeDuration('12:12:12.1')).to.deep.eq({
|
||||||
|
errors: [],
|
||||||
|
total: {
|
||||||
|
days: 0.5084733796296297,
|
||||||
|
hours: 12.20336111111111,
|
||||||
|
iso8601Duration: 'P0Y0M0DT12H12M12.1S',
|
||||||
|
milliseconds: 43932100,
|
||||||
|
minutes: 732.2016666666667,
|
||||||
|
prettified: '12h 12m 12s 100ms',
|
||||||
|
prettifiedColonNotation: '12:12:12.1',
|
||||||
|
prettifiedDaysColon: '12:12:12.100',
|
||||||
|
prettifiedHoursColon: '12:12:12.100',
|
||||||
|
prettifiedVerbose: '12 hours 12 minutes 12 seconds 100 milliseconds',
|
||||||
|
seconds: 43932.1,
|
||||||
|
weeks: 0.07263905423280423,
|
||||||
|
years: 0.0013930777524099442,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
expect(computeDuration('12:12:12.12')).to.deep.eq({
|
||||||
|
errors: [],
|
||||||
|
total: {
|
||||||
|
days: 0.5084736111111111,
|
||||||
|
hours: 12.203366666666666,
|
||||||
|
iso8601Duration: 'P0Y0M0DT12H12M12.12S',
|
||||||
|
milliseconds: 43932120,
|
||||||
|
minutes: 732.202,
|
||||||
|
prettified: '12h 12m 12s 120ms',
|
||||||
|
prettifiedColonNotation: '12:12:12.1',
|
||||||
|
prettifiedDaysColon: '12:12:12.120',
|
||||||
|
prettifiedHoursColon: '12:12:12.120',
|
||||||
|
prettifiedVerbose: '12 hours 12 minutes 12 seconds 120 milliseconds',
|
||||||
|
seconds: 43932.12,
|
||||||
|
weeks: 0.0726390873015873,
|
||||||
|
years: 0.001393078386605784,
|
||||||
|
},
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -23,7 +23,7 @@ interface DurationLine {
|
||||||
rawLine: string
|
rawLine: string
|
||||||
cleanedDuration: string
|
cleanedDuration: string
|
||||||
sign: number
|
sign: number
|
||||||
durationMS: number | undefined
|
durationMS: number | null
|
||||||
isValid: boolean
|
isValid: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -33,14 +33,14 @@ export function computeDuration(s: string): {
|
||||||
} {
|
} {
|
||||||
const lines: DurationLine[] = s.split('\n').filter(l => l && !/^\s*#/.test(l)).map((l) => {
|
const lines: DurationLine[] = s.split('\n').filter(l => l && !/^\s*#/.test(l)).map((l) => {
|
||||||
const isNeg = /^\s*-/.test(l);
|
const isNeg = /^\s*-/.test(l);
|
||||||
const cleanedDuration = l.replace(/^\s*[+-]\s*/, '');
|
const cleanedDuration = l.replace(/^\s*[+-]\s*/, '').replace(/\s*#.*$/, ''); // NOSONAR
|
||||||
const durationMS = convertDurationMS(cleanedDuration);
|
const durationMS = convertDurationMS(cleanedDuration);
|
||||||
return {
|
return {
|
||||||
rawLine: l,
|
rawLine: l,
|
||||||
cleanedDuration,
|
cleanedDuration,
|
||||||
sign: isNeg ? -1 : 1,
|
sign: isNeg ? -1 : 1,
|
||||||
durationMS,
|
durationMS,
|
||||||
isValid: typeof durationMS !== 'undefined',
|
isValid: durationMS !== null,
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -60,8 +60,8 @@ export function computeDuration(s: string): {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function convertDurationMS(s: string): number | undefined {
|
function convertDurationMS(s: string): number | null {
|
||||||
const hoursHandled = s.replace(/\b(?:(\d+)\.)?(\d+):(\d+)(?::(\d+)(?:\.(\d+))?)?\b/g,
|
const hoursHandled = s.trim().replace(/^(?:(\d+)\.)?(\d+):(\d+)(?::(\d+)(?:\.(\d+))?)?$/g,
|
||||||
(_, d, h, m, s, ms) => {
|
(_, d, h, m, s, ms) => {
|
||||||
const timeArr: string[] = [];
|
const timeArr: string[] = [];
|
||||||
const addPart = (part: string, unit: string) => {
|
const addPart = (part: string, unit: string) => {
|
||||||
|
@ -76,26 +76,27 @@ function convertDurationMS(s: string): number | undefined {
|
||||||
addPart(h, 'h');
|
addPart(h, 'h');
|
||||||
addPart(m, 'm');
|
addPart(m, 'm');
|
||||||
addPart(s, 's');
|
addPart(s, 's');
|
||||||
addPart(ms, 'ms');
|
addPart(ms?.padEnd(3, '0'), 'ms');
|
||||||
return timeArr.join(' ');
|
return timeArr.join(' ');
|
||||||
});
|
});
|
||||||
if (!hoursHandled) {
|
if (!hoursHandled) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
let parsedDuration = parse(hoursHandled);
|
try {
|
||||||
if (parsedDuration !== 0 && !parsedDuration) {
|
return iso8601Duration.toMilliseconds(iso8601Duration.parse(hoursHandled));
|
||||||
try {
|
}
|
||||||
parsedDuration = iso8601Duration.toMilliseconds(iso8601Duration.parse(hoursHandled));
|
catch (_) {
|
||||||
}
|
const result = parse(hoursHandled);
|
||||||
catch (_) {
|
if (typeof result === 'undefined') {
|
||||||
return undefined;
|
return null;
|
||||||
}
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
return parsedDuration;
|
|
||||||
}
|
}
|
||||||
function prepareDurationResult(durationMS: any): ConvertedDuration {
|
function prepareDurationResult(durationMS: number): ConvertedDuration {
|
||||||
const dateFnsDuration = intervalToDuration({ start: 0, end: durationMS });
|
const dateFnsDuration = intervalToDuration({ start: 0, end: durationMS });
|
||||||
|
dateFnsDuration.seconds = (dateFnsDuration.seconds || 0) + (durationMS % 1000) / 1000;
|
||||||
return {
|
return {
|
||||||
prettified: prettyMilliseconds(durationMS, { formatSubMilliseconds: true }),
|
prettified: prettyMilliseconds(durationMS, { formatSubMilliseconds: true }),
|
||||||
prettifiedVerbose: prettyMilliseconds(durationMS, { verbose: true, formatSubMilliseconds: true }),
|
prettifiedVerbose: prettyMilliseconds(durationMS, { verbose: true, formatSubMilliseconds: true }),
|
||||||
|
|
|
@ -151,7 +151,7 @@ function onSearchInput() {
|
||||||
>
|
>
|
||||||
<div flex-1 truncate>
|
<div flex-1 truncate>
|
||||||
<slot name="displayed-value">
|
<slot name="displayed-value">
|
||||||
<input v-if="searchable && isOpen" ref="searchInputRef" v-model="searchQuery" type="text" placeholder="Search..." class="search-input" w-full lh-normal color-current @input="onSearchInput">
|
<input v-if="searchable && isOpen" ref="searchInputRef" v-model="searchQuery" type="text" placeholder="Search..." class="search-input" w-full color-current lh-normal @input="onSearchInput">
|
||||||
<span v-else-if="selectedOption" lh-normal>
|
<span v-else-if="selectedOption" lh-normal>
|
||||||
{{ selectedOption.label }}
|
{{ selectedOption.label }}
|
||||||
</span>
|
</span>
|
||||||
|
|
|
@ -39,7 +39,7 @@ const headers = computed(() => {
|
||||||
<template>
|
<template>
|
||||||
<div class="relative overflow-x-auto rounded">
|
<div class="relative overflow-x-auto rounded">
|
||||||
<table class="w-full border-collapse text-left text-sm text-gray-500 dark:text-gray-400" role="table" :aria-label="description">
|
<table class="w-full border-collapse text-left text-sm text-gray-500 dark:text-gray-400" role="table" :aria-label="description">
|
||||||
<thead v-if="!hideHeaders" class="bg-#ffffff uppercase text-gray-700 dark:bg-#333333 dark:text-gray-400" border-b="1px solid dark:transparent #efeff5">
|
<thead v-if="!hideHeaders" class="bg-#ffffff text-gray-700 uppercase dark:bg-#333333 dark:text-gray-400" border-b="1px solid dark:transparent #efeff5">
|
||||||
<tr>
|
<tr>
|
||||||
<th v-for="header in headers" :key="header.key" scope="col" class="px-6 py-3 text-xs">
|
<th v-for="header in headers" :key="header.key" scope="col" class="px-6 py-3 text-xs">
|
||||||
{{ header.label }}
|
{{ header.label }}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue