jkroso / parse-duration

convert a human readable duration to ms
MIT License
243 stars 36 forks source link

[feature request] support more units which not in English #26

Closed alonesuperman closed 3 years ago

alonesuperman commented 4 years ago

Hi, jkroso

Although we can use parse.newUnit to extend more unit, your regexp may limit the unit as [a-zµμ]

I suggest it could change to some else such as [^\d\s] to make more possibility

The change is just example, It fail to pass test case because of the noisy word

So a safer way could be either of follow:

If u agree, I can do some job(by the way, improve the .d.ts file to allow us extend units under typescript) and take a pr in several days

dy commented 4 years ago

No need for dynamic rules/units, locales can be added separately.

On Tue, Jul 21, 2020 at 3:55 AM alonesuperman notifications@github.com wrote:

Hi, jkroso

Although we can use parse.newUnit to extend more unit, your regexp may limit the unit as [a-zµμ]

I suggest it could change to [^\d\s] to make more possibility

var duration = /(-?(?:\d+.?\d|\d.?\d+)(?:e[-+]?\d+)?)\s([^\d\s])/ig

If u agree, I will do this job(by the way, improve the .d.ts file to allow us extend units under typescript) and take a pr in 1~2 day

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/jkroso/parse-duration/issues/26, or unsubscribe https://github.com/notifications/unsubscribe-auth/AACJII42FWR3QJJ3DMIEADLR4VCVXANCNFSM4PDI3KRQ .

alonesuperman commented 4 years ago

wu..., but how to add locale without refactor?

dy commented 4 years ago

@alonesuperman something like

import parse from 'parse-duration'
parse['сек'] = parse['s'] // ru seconds
parse('5сек') // 5000

should be enough. If you wish to come up with a PR, adding locales as

import parse from 'parse-duration'
import 'parse-duration/fr'

or alike - you're welcome

alonesuperman commented 4 years ago

No, I had run test case for some times

If u use 0.4.4 without any change(except test case), let us see the test case below

image

The problem here as I said: The last capture group not include your unit, because сек is out of [a-zµμ]*

alonesuperman commented 4 years ago

And, If u apply my suggestion about modify regexp (change the last capture group [a-zµμ] to [^\d\s]), it still can not pass the test case for this threerunning length: 1hour:20mins, 1y.2b.5days.12hours.34sec.20ms, -1y.2b.5days 12hours,34sec,20ms

dy commented 4 years ago

Right, it needs adjusting regex to match unicode property escapes like that var duration = /(-?(?:\d+\.?\d*|\d*\.?\d+)(?:e[-+]?\d+)?)\s*([\p{L}]*)/uig I can't contribute for some time, so if you'd like PR welcome!

alonesuperman commented 4 years ago

OK, Let me have a try

dy commented 3 years ago

Added unicode support in v1.0