Skip to content

Commit

Permalink
support -ve values
Browse files Browse the repository at this point in the history
  • Loading branch information
Shashank3736 committed Jul 26, 2020
1 parent 8014870 commit b3828b6
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ ms("5 days, 4hours and 3 minutes."); //return 446580000
ms("446580000", {short: true}); //return 5d 4h 3m
ms(446580000, {extended: true}); //return 5 days 4 hours 3 minutes
ms(446580000); //return { days: 5, hours: 4, minutes: 3, seconds: 0}
ms("-86400000"); //return {days: -1, hours: -0, minutes: -0, seconds: -0}
ms("- 1 day"); //return -86400000
ms("-21 days and 24 hours"); //returns -1900800000
```

#### It supports almost every time units like
Expand All @@ -40,6 +43,8 @@ days, day, d etc.
###### For years:
years, year, yrs, y etc.


##### -ve values are also supported. As you see in above examples.
## Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Expand Down
7 changes: 5 additions & 2 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,12 @@ module.exports = (val, options = {}) => {
return timeValue;
} else {
const array = val.match(
/[0-9]+ *(milliseconds?|msecs?|ms|seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|weeks?|w|years?|yrs?|y)/gi
/(-?[0-9]+) *(milliseconds?|msecs?|ms|seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|weeks?|w|years?|yrs?|y)/gi
);
const result = array.reduce((a, b) => (isNaN(a) ? ms(a) : a) + ms(b), '0s');
const result = array.reduce((a, b,) =>
(isNaN(a) ? ms(a): a) +
((isNaN(a) ? ms(a): a) < 0 ? -ms(b):ms(b))
, '0s');
return result;
}
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@shreyash21/ms",
"version": "1.1.2",
"version": "1.2.0",
"description": "A better time converter for your projects.",
"main": "main.js",
"scripts": {
Expand Down

0 comments on commit b3828b6

Please sign in to comment.