Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(util): Human readable duration helper #20231

Merged
merged 4 commits into from Feb 6, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 33 additions & 0 deletions lib/util/pretty-time.spec.ts
@@ -0,0 +1,33 @@
import { toMs } from './pretty-time';

describe('util/pretty-time', () => {
test.each`
input | expected
${'1h'} | ${1 * 60 * 60 * 1000}
${' 1 h '} | ${1 * 60 * 60 * 1000}
${'1 h'} | ${1 * 60 * 60 * 1000}
${'1 hour'} | ${1 * 60 * 60 * 1000}
${'1hour'} | ${1 * 60 * 60 * 1000}
${'1h 1m'} | ${1 * 60 * 60 * 1000 + 1 * 60 * 1000}
${'1hour 1minute'} | ${1 * 60 * 60 * 1000 + 1 * 60 * 1000}
${'1 hour 1 minute'} | ${1 * 60 * 60 * 1000 + 1 * 60 * 1000}
${'1h 1m 1s'} | ${1 * 60 * 60 * 1000 + 1 * 60 * 1000 + 1000}
${'1h 1 m 1s'} | ${1 * 60 * 60 * 1000 + 1 * 60 * 1000 + 1000}
${'1hour 1 min 1s'} | ${1 * 60 * 60 * 1000 + 1 * 60 * 1000 + 1000}
${'1h 1m 1s 1ms'} | ${1 * 60 * 60 * 1000 + 1 * 60 * 1000 + 1000 + 1}
${'3 days'} | ${3 * 24 * 60 * 60 * 1000}
${'0'.repeat(100)} | ${0}
${'0'.repeat(101)} | ${null}
${'1 whatever'} | ${null}
${'whatever'} | ${null}
${''} | ${null}
${' '} | ${null}
${' \t\n '} | ${null}
${'minute'} | ${null}
${'m'} | ${null}
${'hour'} | ${null}
${'h'} | ${null}
`(`toMs('$input') === $expected`, ({ input, expected }) => {
expect(toMs(input)).toBe(expected);
});
});
38 changes: 38 additions & 0 deletions lib/util/pretty-time.ts
@@ -0,0 +1,38 @@
import is from '@sindresorhus/is';
import ms from 'ms';
import { logger } from '../logger';
import { regEx } from './regex';

const splitRegex = regEx(/(.*?[a-z]+)/);

function split(time: string): string[] {
return time
.toLocaleLowerCase()
.split(splitRegex)
.map((x) => x.trim())
.filter(is.nonEmptyString);
}

export function toMs(time: string): number | null {
try {
const specs = split(time);
if (!specs.length) {
logger.debug({ time }, `Invalid time specifier: '${time}'`);
return null;
}

let totalMillis = 0;
for (const spec of specs) {
const millis = ms(spec);
if (!is.number(millis)) {
logger.debug({ time }, `Invalid time specifier: '${spec}'`);
return null;
}
totalMillis += millis;
}
return totalMillis;
} catch (err) /* istanbul ignore next: shouldn't happen */ {
zharinov marked this conversation as resolved.
Show resolved Hide resolved
logger.debug({ time, err }, `Invalid time specifier: '${time}'`);
return null;
}
}
2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -159,6 +159,7 @@
"@renovatebot/pep440": "2.1.13",
"@renovatebot/ruby-semver": "2.1.8",
"@sindresorhus/is": "4.6.0",
"@types/ms": "0.7.31",
"@types/tmp": "0.2.3",
"@yarnpkg/core": "3.3.1",
"@yarnpkg/parsers": "2.5.1",
Expand Down Expand Up @@ -209,6 +210,7 @@
"markdown-table": "2.0.0",
"minimatch": "5.1.6",
"moo": "0.5.2",
"ms": "2.1.3",
"nanoid": "3.3.4",
"node-html-parser": "6.1.4",
"openpgp": "5.5.0",
Expand Down
7 changes: 6 additions & 1 deletion yarn.lock
Expand Up @@ -3076,6 +3076,11 @@
resolved "https://registry.yarnpkg.com/@types/moo/-/moo-0.5.5.tgz#83220b7349c59fd4bb1bc14b1d4ea0041899dc15"
integrity sha512-eXQpwnkI4Ntw5uJg6i2PINdRFWLr55dqjuYQaLHNjvqTzF14QdNWbCbml9sza0byyXNA0hZlHtcdN+VNDcgVHA==

"@types/ms@0.7.31":
version "0.7.31"
resolved "https://registry.yarnpkg.com/@types/ms/-/ms-0.7.31.tgz#31b7ca6407128a3d2bbc27fe2d21b345397f6197"
integrity sha512-iiUgKzV9AuaEkZqkOLDIvlQiL6ltuZd9tGcW3gwpnX8JbuiuhFlEGmmFXEXkN50Cvq7Os88IY2v0dkDqXYWVgA==

"@types/nock@10.0.3":
version "10.0.3"
resolved "https://registry.yarnpkg.com/@types/nock/-/nock-10.0.3.tgz#dab1d18ffbccfbf2db811dab9584304eeb6e1c4c"
Expand Down Expand Up @@ -7729,7 +7734,7 @@ ms@2.1.2:
resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.2.tgz#d09d1f357b443f493382a8eb3ccd183872ae6009"
integrity sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==

ms@^2.0.0, ms@^2.1.1, ms@^2.1.2:
ms@2.1.3, ms@^2.0.0, ms@^2.1.1, ms@^2.1.2:
version "2.1.3"
resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.3.tgz#574c8138ce1d2b5861f0b44579dbadd60c6615b2"
integrity sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==
Expand Down