Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinkassimo committed Dec 20, 2018
1 parent f05ce70 commit 12e3974
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 124 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.vscode/
.DS_Store
*.swp
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ It is ported from [https://github.com/zeit/ms](https://github.com/zeit/ms) to wo
## Examples

```js
import ms from "https://raw.githubusercontent.com/denolib/ms/master/ms.ts";

ms('2 days') // 172800000
ms('1d') // 86400000
ms('10h') // 36000000
Expand Down
103 changes: 53 additions & 50 deletions ms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,27 @@ const y = d * 365.25;
* - `long` verbose formatting [false]
*/

export default function(val: string | number, options?: {[key: string]: any}): string | number {
export default function(
val: string | number,
options?: { [key: string]: any }
): string | number {
switch (typeof val) {
case "string":
if ((val as string).length > 0) {
return parse(val as string);
}
case "number":
if (!isNaN(val as number)) {
return options && options!.long ?
fmtLong(val as number) :
fmtShort(val as number);
return options && options!.long
? fmtLong(val as number)
: fmtShort(val as number);
}
}
throw new Error(
'val is not a non-empty string or a valid number. val=' +
"val is not a non-empty string or a valid number. val=" +
JSON.stringify(val)
);
};
}

/** Parse the given `str` and return milliseconds.
*/
Expand All @@ -49,45 +52,45 @@ function parse(str: string): number | undefined {
return;
}
const n = parseFloat(match[1]);
const type = (match[2] || 'ms').toLowerCase();
const type = (match[2] || "ms").toLowerCase();
switch (type) {
case 'years':
case 'year':
case 'yrs':
case 'yr':
case 'y':
case "years":
case "year":
case "yrs":
case "yr":
case "y":
return n * y;
case 'weeks':
case 'week':
case 'w':
case "weeks":
case "week":
case "w":
return n * w;
case 'days':
case 'day':
case 'd':
case "days":
case "day":
case "d":
return n * d;
case 'hours':
case 'hour':
case 'hrs':
case 'hr':
case 'h':
case "hours":
case "hour":
case "hrs":
case "hr":
case "h":
return n * h;
case 'minutes':
case 'minute':
case 'mins':
case 'min':
case 'm':
case "minutes":
case "minute":
case "mins":
case "min":
case "m":
return n * m;
case 'seconds':
case 'second':
case 'secs':
case 'sec':
case 's':
case "seconds":
case "second":
case "secs":
case "sec":
case "s":
return n * s;
case 'milliseconds':
case 'millisecond':
case 'msecs':
case 'msec':
case 'ms':
case "milliseconds":
case "millisecond":
case "msecs":
case "msec":
case "ms":
return n;
default:
return undefined;
Expand All @@ -100,18 +103,18 @@ function parse(str: string): number | undefined {
function fmtShort(ms: number): string {
const msAbs = Math.abs(ms);
if (msAbs >= d) {
return Math.round(ms / d) + 'd';
return Math.round(ms / d) + "d";
}
if (msAbs >= h) {
return Math.round(ms / h) + 'h';
return Math.round(ms / h) + "h";
}
if (msAbs >= m) {
return Math.round(ms / m) + 'm';
return Math.round(ms / m) + "m";
}
if (msAbs >= s) {
return Math.round(ms / s) + 's';
return Math.round(ms / s) + "s";
}
return ms + 'ms';
return ms + "ms";
}

/** Long format for `ms`.
Expand All @@ -120,24 +123,24 @@ function fmtShort(ms: number): string {
function fmtLong(ms: number): string {
const msAbs = Math.abs(ms);
if (msAbs >= d) {
return plural(ms, msAbs, d, 'day');
return plural(ms, msAbs, d, "day");
}
if (msAbs >= h) {
return plural(ms, msAbs, h, 'hour');
return plural(ms, msAbs, h, "hour");
}
if (msAbs >= m) {
return plural(ms, msAbs, m, 'minute');
return plural(ms, msAbs, m, "minute");
}
if (msAbs >= s) {
return plural(ms, msAbs, s, 'second');
return plural(ms, msAbs, s, "second");
}
return ms + ' ms';
return ms + " ms";
}

/** Pluralization helper.
*/

function plural(ms: number, msAbs: number, n: number, name: string) {
const isPlural = msAbs >= n * 1.5;
return Math.round(ms / n) + ' ' + name + (isPlural ? 's' : '');
}
return Math.round(ms / n) + " " + name + (isPlural ? "s" : "");
}
Loading

0 comments on commit 12e3974

Please sign in to comment.