-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
68 lines (52 loc) · 1.26 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
'use strict';
const durationRE = /(-?(?:\d+\.?\d*|\d*\.?\d+)(?:e[-+]?\d+)?)\s*([\p{L}]*)/uig;
module.exports = parse;
parse.nanosaniye =
parse.ns = 1 / 1e6;
parse.mikrosaniye =
parse["µs"] =
parse["μs"] =
parse.us =
1 / 1e3;
parse.milisaniye =
parse.ms = 1;
parse.saniye =
parse.sn =
parse.s = parse.ms * 1000;
parse.dakika =
parse.dak =
parse.dk =
parse.d = parse.sn * 60;
parse.saat =
parse.sa = parse.dakika * 60;
parse["gün"] =
parse.gun =
parse.g = parse.sa * 24;
parse.hafta =
parse.hft =
parse.h = parse.gun * 7;
parse.ay =
parse.a = parse.gun * (365.25 / 12);
parse["yıl"] =
parse.yil =
parse.y = parse.gun * 365.25;
const unitRatio = str => parse[str.toLowerCase()] || 1;
/**
* `string`i ms'ye çevir
*
* @param {String} input
* @param {String} format
* @return {Number}
*/
function parse(input = "", format = "ms") {
let result = 0;
String(input)
.replace(/(\d)[_ ](\d)/g, '$1$2') // ignore placeholders
.replace(/(\d)[,](\d)/g, '$1.$2') // normalize separators
.replace(/(\d)\.(?=\d+\.)/g, '$1') // decimal fraction
.replace(durationRE, (_, n, units) =>
(units = unitRatio(units)) &&
(result += Math.abs(parseFloat(n)) * units)
);
return result && result / unitRatio(format);
}