Installations
npm install @musement/iso-duration
Score
77.2
Supply Chain
99
Quality
75.7
Maintenance
100
Vulnerability
99.6
License
Releases
Unable to fetch releases
Developer
musement
Developer Guide
Module System
CommonJS
Min. Node Version
Typescript Support
Yes
Node Version
12.16.2
NPM Version
6.14.9
Statistics
6 Stars
100 Commits
1 Forks
4 Watching
4 Branches
6 Contributors
Updated on 29 Aug 2024
Languages
TypeScript (96.55%)
JavaScript (3.45%)
Total Downloads
Cumulative downloads
Total Downloads
132,459
Last day
68.1%
269
Compared to previous day
Last week
24.6%
972
Compared to previous week
Last month
3%
3,749
Compared to previous month
Last year
17.6%
47,919
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
@musement/iso-duration
JS's library created to provide an easy API for working with time duration.
Based on ISO 8601.
Inspired by HumanizeDuration and ISO8601-duration.
Installation
npm i @musement/iso-duration --save
Getting started
1// Import isoDuration and locales 2import { isoDuration, en, pl, it } from '@musement/iso-duration'; 3 4// Setup locales 5isoDuration.setLocales( 6 { 7 en, 8 pl, 9 it, 10 }, 11 { 12 fallbackLocale: 'en', 13 } 14); 15 16//Create duration object 17const duration = isoDuration("P8DT30M"); 18// OR 19const duration = isoDuration({ 20 days: 8, 21 minutes: 30 22}); 23 24//Get JS duration object 25console.log(duration.parse()); 26// { 27// weeks: 0, 28// years: 0, 29// months: 0, 30// days: 8, 31// hours: 0, 32// minutes: 30, 33// seconds: 0 34// } 35 36//Get duration ISO string 37console.log(duration.toString()); 38// P8DT30M 39 40//Humanize duration 41console.log(duration.humanize('en')); 42// 8 days 30 minutes
API
isoDuration.setLocales(obj: Locales, options?: LocalesOptions)
You need to initialize the languages you want to use in .humanize(lang)
, using the isoDuration.setLocales
function. Currently, the library provides support for languages listed under /src/locales
.
Languages not imported by your application will be removed by the module bundler (ex. webpack) during a build process using a tree-shaking mechanism.
1import { isoDuration, en, pl, it } from '@musement/iso-duration';
2
3// isoDuration.setLocales(locales, localesOptions);
4isoDuration.setLocales({
5 en,
6 pl,
7 it,
8 'en-GB': en,
9 'en-US': en,
10}, {
11 fallbackLocale: 'en'
12});
LocalesOptions
parameter (optional)
1interface LocalesOptions { 2 fallbackLocale: string 3};
Key | Type | Description | Default |
---|---|---|---|
fallbackLocale | String | locale which needs to be when IsoDuration.prototype.humanize(lang: string) received incorrect parameter | undefined |
isoDuration(duration: string | Partial<DurationObj>): IsoDuration
Supported duration types :
- ISO_8601 duration string (
PnYnMnDTnHnMnS
,PnW
) - JS objects (matching
Partial<DurationObj>
interface)
1interface DurationObj { 2 "weeks": number, 3 "years": number, 4 "months": number, 5 "days": number, 6 "hours": number, 7 "minutes": number, 8 "seconds": number, 9};
Example:
1import { isoDuration } from '@musement/iso-duration'; 2 3const durationFromString = isoDuration("P8DT30M"); 4const durationFromObj = isoDuration({ 5 days: 8, 6 minutes: 30, 7});
IsoDuration.prototype.parse()
Returns JS DurationObj
1console.log(isoDuration("P8T30M").parse()); 2// { 3// weeks: 0, 4// years: 0, 5// months: 0, 6// days: 8, 7// hours: 0, 8// minutes: 30, 9// seconds: 0 10// }
IsoDuration.prototype.toString()
Returns ISO_8601 string:
1console.log(isoDuration("P8T30M").toString());
2// P8T30M
IsoDuration.prototype.humanize(lang: string, config?: HumanizeConfig)
Returns duration in a human-readable way.
âš Warning âš - used lang
needs to be previously added to the library using isoDuration.setLocales()
1console.log(isoDuration("P8T30M").humanize('en')); 2// 8 hours 30 minutes
HumanizeConfig
parameter (optional):
1interface HumanizeConfig { 2 largest: number 3};
Key | Type | Description | Default |
---|---|---|---|
largest | Number | Humanize only n largest duration values. | undefined |
1console.log( 2 isoDuration({ 3 years: 2, 4 months: 0, 5 days: 8, 6 hours: 20, 7 minutes: 30, 8 seconds: 15 9 }).humanize('en', { largest: 2 }) 10); 11// 2 years 8 days
IsoDuration.prototype.normalize(date?: Date)
returns normalized IsoDuration object:
1console.log(isoDuration("PT90M").normalize().toString()); 2// PT1H30M
This method takes an optional date
argument which defines the start of the duration. It's used to correctly normalize the number of days basing on the corresponding month's length.
If it's not present normalize function will use the current date (new Date()
) instead.
Month with 31 days:
1console.log("Duration:", isoDuration("P31D").normalize(new Date(2020, 0, 1)).humanize('en')); 2//Duration: 31 days
Month with 29 days:
1console.log("Duration:", isoDuration("P31D").normalize(new Date(2020, 1, 1)).humanize('en')); 2//Duration: 1 month 2 days
Credits
EvanHahn - author of HumanizeDuration
tolu author of ISO8601-duration
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: ISC License: LICENSE:0
Reason
Found 1/2 approved changesets -- score normalized to 5
Reason
dependency not pinned by hash detected -- score normalized to 3
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/build-and-test.yml:22: update your workflow using https://app.stepsecurity.io/secureworkflow/musement/iso-duration/build-and-test.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/build-and-test.yml:24: update your workflow using https://app.stepsecurity.io/secureworkflow/musement/iso-duration/build-and-test.yml/master?enable=pin
- Info: 0 out of 2 GitHub-owned GitHubAction dependencies pinned
- Info: 1 out of 1 npmCommand dependencies pinned
Reason
9 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-5fw9-fq32-wv5p
- Warn: Project is vulnerable to: GHSA-p8p7-x288-28g6
- Warn: Project is vulnerable to: GHSA-gcx4-mw62-g8wm
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-72xf-g2v4-qvf3
- Warn: Project is vulnerable to: GHSA-3h5v-q93c-6h6q
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/build-and-test.yml:1
- Info: no jobLevel write permissions found
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 30 are checked with a SAST tool
Score
3.6
/10
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More