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

2.0 #58

Merged
merged 17 commits into from
Jan 26, 2025
Merged

2.0 #58

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 36 additions & 72 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -1,101 +1,65 @@

# parse-duration [![Build Status](https://travis-ci.org/jkroso/parse-duration.svg?branch=master)](https://travis-ci.org/jkroso/parse-duration)
# parse-duration [![Test](https://github.com/jkroso/parse-duration/actions/workflows/test.yml/badge.svg)](https://github.com/jkroso/parse-duration/actions/workflows/test.yml)

convert a human readable duration to ms

## Installation
Convert a human readable duration to ms.

[![NPM](https://nodei.co/npm/parse-duration.png?mini=true)](https://npmjs.org/package/parse-duration)

then in your app:
## Usage

```js
import parse from 'parse-duration'
```

or CommonJS:

```js
var parse = require('parse-duration')
```

## API

### parse(str, format='ms')

Convert `str` to ms

```js
var ns = parse('1ns') // => 1 / 1e6
var μs = parse('1μs') // => 1 / 1000
var ms = parse('1ms') // => 1
var s = parse('1s') // => ms * 1000
var m = parse('1m') // => s * 60
var h = parse('1h') // => m * 60
var d = parse('1d') // => h * 24
var w = parse('1w') // => d * 7
var y = parse('1y') // => d * 365.25
```

It can also handle basic compound expressions

```js
// parse different time units
let ns = parse('1ns') // => 1 / 1e6
let μs = parse('1μs') // => 1 / 1000
let ms = parse('1ms') // => 1
let s = parse('1s') // => ms * 1000
let m = parse('1m') // => s * 60
let h = parse('1h') // => m * 60
let d = parse('1d') // => h * 24
let w = parse('1w') // => d * 7
let y = parse('1y') // => d * 365.25

// compound expressions
parse('1hr 20mins') // => 1 * h + 20 * m
parse('1 hr 20 mins') // => 1 * h + 20 * m
```

youtube format

```js
// youtube format
parse('1h20m0s') // => 1 * h + 20 * m
```

comma seperated numbers

```js
// comma seperated numbers
parse('27,681 ns') // => 27681 * ns
```

And most other types of noise

```js
// noisy input
parse('running length: 1hour:20mins') // => 1 * h + 20 * m
```

You can even use negatives

```js
// negatives
parse('-1hr 40mins') // => 1 * h + 40 * m
```

And exponents

```js
// exponents
parse('2e3s') // => 2000 * s
```

#### Available unit types are:
// custom output format
parse('1hr 20mins', 'm') // => 80

- nanoseconds (ns)
- microseconds (μs)
- milliseconds (ms)
- seconds (s, sec)
- minutes (m, min)
- hours (h, hr)
- days (d)
- weeks (w, wk)
- months
- years (y, yr)
// add units
parse.unit['μs'] = parse.unit.microsecond
parse('5μs') // => 0.005
```

And its easy to add more, including unicode:
## Locales

Switch the default en locale to another language ([see /locale](/locale)).

```js
parse['сек'] = parse['sec']
parse('5сек') // => 5000
```
import es from 'parse-duration/locale/es.js'
import parse from 'parse-duration'

The output format can also be defined
parse.unit = es

```js
parse('1hr 20mins', 'm') // => 80
parse('1 hora 20 minutos', 'm') // 80
parse('1 hour 20 minutes', 'm') // 80 - extends english locale
```

<p align="center"><a href="https://github.com/krishnized/license">ॐ</a></p>
4 changes: 0 additions & 4 deletions index.d.mts

This file was deleted.

31 changes: 3 additions & 28 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,4 @@
// ./index.d.ts
// ./index.d.mts

declare namespace parse {
/**
* convert `str` to ms
*/
type Units =
'nanosecond' | 'ns' |
'µs' | 'μs' | 'us' | 'microsecond' |
'millisecond' | 'ms' |
'second' | 'sec' | 's' |
'minute' | 'min' | 'm' |
'hour' | 'hr' | 'h' |
'day' | 'd' |
'week' | 'wk' | 'w' |
'month' | 'b' |
'year' | 'yr' | 'y'
}

type Parse = {
(input: string, format?: parse.Units): number | null;
[key: string]: number;
} & {
default: Parse
}

declare const parse: Parse;

export = parse;
export * from './index.js'
export { default } from './index.js'
81 changes: 16 additions & 65 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,8 @@
'use strict'
import en from './locale/en.js'

var durationRE = /(-?(?:\d+\.?\d*|\d*\.?\d+)(?:e[-+]?\d+)?)\s*([\p{L}]*)/uig

module.exports = parse
// enable default import syntax in typescript
module.exports.default = parse

/**
* conversion ratios
*/

parse.year =
parse.yr =
parse.y = 60000 * 60 * 24 * 365.25

parse.month =
parse.b = 60000 * 60 * 24 * (365.25 / 12)

parse.week =
parse.wk =
parse.w = 60000 * 60 * 24 * 7

parse.day =
parse.d = 60000 * 60 * 24

parse.hour =
parse.hr =
parse.h = 60000 * 60

parse.minute =
parse.min =
parse.m = 60000

parse.second =
parse.sec =
parse.s = 1000

parse.millisecond =
parse.millisec =
parse.ms = 1

parse['µs'] =
parse['μs'] =
parse.us =
parse.microsecond = 1 / 1e3

parse.nanosecond =
parse.ns = 1 / 1e6
let durationRE = /(-?(?:\d+\.?\d*|\d*\.?\d+)(?:e[-+]?\d+)?)\s*([\p{L}]*)/uig

parse.unit = en

/**
* convert `str` to ms
Expand All @@ -57,32 +12,28 @@ parse.nanosecond =
* @return {Number}
*/

function parse(str = '', format = 'ms') {
if (!Object.prototype.hasOwnProperty.call(parse, format)) {
throw new TypeError('Invalid format "' + format + '"')
}

var result = null, prevUnits
export default function parse(str = '', format = 'ms') {
let result = null, prevUnits
// ignore commas/placeholders
str = (str + '').replace(/(\d)[,_](\d)/g, '$1$2')
str.replace(durationRE, function (_, n, units) {
str = (str + '')
.replace(/(\d)[_ ](\d)/g, '$1$2') // ignore placeholders
.replaceAll(parse.unit.group, '') // remove group separator
.replaceAll(parse.unit.decimal, '.') // normalize decimal separator

str.replace(durationRE, (_, n, units) => {
// if no units, find next smallest units or fall back to format value (ms)
if (!units) {
if (prevUnits) {
for (var u in parse) if (parse[u] < prevUnits) { units = u; break }
for (var u in parse.unit) if (parse.unit[u] < prevUnits) { units = u; break }
}
else units = format
}
else units = units.toLowerCase()
if (Object.prototype.hasOwnProperty.call(parse, units)) {
units = parse[units]
} else if (Object.prototype.hasOwnProperty.call(parse, units.replace(/s$/, ''))) {
units = parse[units.replace(/s$/, '')]
} else {
units = null
}

units = parse.unit[units] || parse.unit[units.replace(/s$/, '')]

if (units) result = (result || 0) + Math.abs(parseFloat(n, 10)) * units, prevUnits = units
})

return result && ((result / (parse[format] || 1)) * (str[0] === '-' ? -1 : 1))
return result && ((result / (parse.unit[format] || 1)) * (str[0] === '-' ? -1 : 1))
}
81 changes: 0 additions & 81 deletions index.mjs

This file was deleted.

19 changes: 19 additions & 0 deletions locale/de.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import en from './en.js'

const unit = Object.create(en)

unit.jahr = unit.j = en.y
unit.monat = en.month
unit.woche = en.w
unit.tag = unit.t = en.d
unit.stunde = en.h
unit.minute = en.m
unit.sekunde = en.s
unit.millisekunde = en.ms
unit.mikrosekunde = en.us
unit.nanosekunde = en.ns

unit.group = '.'
unit.decimal = ','

export default unit
18 changes: 18 additions & 0 deletions locale/en.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const unit = Object.create(null)
const m = 60000, h = m * 60, d = h * 24, y = d * 365.25

unit.year = unit.yr = unit.y = y
unit.month = unit.b = y / 12
unit.week = unit.wk = unit.w = d * 7
unit.day = unit.d = d
unit.hour = unit.hr = unit.h = h
unit.minute = unit.min = unit.m = m
unit.second = unit.sec = unit.s = 1000
unit.millisecond = unit.millisec = unit.ms = 1
unit.µs = unit.μs = unit.us = unit.microsecond = 1e-3
unit.nanosecond = unit.ns = 1e-6

unit.group = ','
unit.decimal = '.'

export default unit
Loading
Loading