Skip to content

Commit

Permalink
Add Duration.fromString() method
Browse files Browse the repository at this point in the history
  • Loading branch information
Caligo committed Mar 4, 2022
1 parent eac21c5 commit 2135b19
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 5 deletions.
21 changes: 17 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,26 @@ $ pnpm install @retraigo/duration.js # PNPM
```js
const Duration = require("@retraigo/duration.js");

console.log(new Duration()); // Get duration since midnight
new Duration(); // Get duration since midnight

console.log(new Duration(3545346)); // A random duration
new Duration(3545346); // A random duration

console.log(new Duration(0)); // Just 0
new Duration(0); // Just 0

console.log(new Duration(-1)); // Negative duration returns 0 too
new Duration(-1); // Negative duration returns 0 too
```

### From Text
```js
Duration.fromString("1m2s") // Duration {d:0, h:0, m:1, s:2, ms:0}

Duration.fromString("4090 sec 4939 days 7342 hour 2324milliseconds 4344 min") // // Duration {d: 5246, h: 13, m: 52, s: 12, ms: 324 }
```

You can also get the entire Duration in milliseconds through the `raw` property.
```js
const dur = Duration.fromString("4090 sec 4939 days 7342 hour 2324milliseconds 4344 min")
dur.raw // 453304332324
```

### Properties
Expand Down
18 changes: 18 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const keyList = {
ms: "milliseconds",
};


/**
* Duration class. Yes, Duration. Ain't gonna give it a better name with my crappy naming sense.
*/
Expand Down Expand Up @@ -71,9 +72,26 @@ class Duration {
toString() {
return `[Duration ${this.stringify(['d', 'h', 'm', 's'], true)}]`
}
static fromString(str) {
str = str.replace(/\s/g, "")
const days = matchReg(str, "d") || matchReg(str, "days") || matchReg(str, "day")
const hours = matchReg(str, "h") || matchReg(str, "hours") || matchReg(str, "hour")
const minutes = matchReg(str, "m") || matchReg(str, "min") || matchReg(str, "minute") || matchReg(str, "mins") || matchReg(str, "minutes")
const seconds = matchReg(str, "s") || matchReg(str, "sec") || matchReg(str, "second") || matchReg(str, "secs") || matchReg(str, "seconds")
const milliseconds = matchReg(str, "ms") || matchReg(str, "millisecond") || matchReg(str, "milliseconds")
const ts = (days * 8.64e7) + (hours * 3600000) + (minutes * 60000) + (seconds * 1000) + milliseconds
return new Duration(ts)
}
static getCurrentDuration() {
return new Date().setHours(0, 0, 0, 0);
}
}

function matchReg(str, t) {
const reg = new RegExp(`(\\d+)${t}`, "i")
const matched = reg.exec(str)
if(!matched) return 0
return parseInt(matched[1].replace(t, ""))
}

module.exports = Duration;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@retraigo/duration.js",
"version": "1.0.5",
"version": "1.1.0",
"description": "Get the formatted time duration",
"main": "index.js",
"scripts": {
Expand Down
3 changes: 3 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const Duration = require("./index");

/*
const dur = new Duration(177013); // A random duration
console.log(dur);
Expand All @@ -12,3 +13,5 @@ console.log(new Duration(114750).json);
console.log(new Duration(245074).array);
*/
console.log(Duration.fromString("4090 sec 4939 days 7342 hour 2324milliseconds 4344 min").stringify(["m", "s"]))

0 comments on commit 2135b19

Please sign in to comment.