+61
| 'use strict'; | ||
| const MODULE_REQUIRE = 1 | ||
| /* built-in */ | ||
| /* NPM */ | ||
| /* in-package */ | ||
| ; | ||
| /** | ||
| * Generally, the expression is used to describe age of (HTTP) cache. | ||
| * 因为这种表达式通常于代表缓存时间,故命名为 age。 | ||
| */ | ||
| const reAges = /(?<num>\d+)(?<unit>ms|s|m|h|d|D|w|W|M|Q|S|H|y|Y)/g; | ||
| const MULTI = { | ||
| ms : 0.001, // MicroSecond | ||
| s : 1, // Second | ||
| m : 60, // Minute | ||
| h : 3600, // Hour | ||
| d : 86400, // Day | ||
| D : 86400, // Day | ||
| w : 604800, // Week = 86400 * 7 | ||
| W : 604800, // Week = 86400 * 7 | ||
| M : 2592000, // Month = 86400 * 30 | ||
| S : 7776000, // Season = 86400 * 90 | ||
| Q : 7776000, // Quarter = 86400 * 91.25 | ||
| H : 15768000, // Half = 86400 * 182.5 | ||
| y : 31536000, // Year = 86400 * 365 | ||
| Y : 31622400, // leapYear = 86400 * 366 | ||
| }; | ||
| /** | ||
| * @param {string|number} age | ||
| * @returns {number} | ||
| */ | ||
| function toSeconds(age) { | ||
| if (typeof age == 'number') { | ||
| return age | ||
| } | ||
| if (typeof age != 'string') { | ||
| throw new Error(`invalid argument "age": ${age}`); | ||
| } | ||
| let matches = Array.from(age.matchAll(reAges)) | ||
| if (matches.length == 0) { | ||
| throw new Error(`invalid argument "age": ${age}`); | ||
| } | ||
| let seconds = 0; | ||
| matches.forEach(match => { | ||
| let { num, unit } = match.groups; | ||
| num = parseInt(num); | ||
| seconds += Math.ceil(num * MULTI[unit]); | ||
| }) | ||
| return seconds; | ||
| }; | ||
| module.exports = toSeconds; |
+1
-0
@@ -19,2 +19,3 @@ 'use strict'; | ||
| parse : require('./parse'), | ||
| toSeconds : require('./toSeconds'), | ||
| }; |
+1
-1
@@ -14,3 +14,3 @@ { | ||
| "name": "dafo", | ||
| "version": "0.1.1", | ||
| "version": "0.1.2-majon.0", | ||
| "main": "index.js", | ||
@@ -17,0 +17,0 @@ "keywords": [ |
+21
-0
@@ -21,2 +21,3 @@ # dafo | ||
| * [dafo.parse](#dafoparse) | ||
| * [dafo.toSeconds](#dafotoseconds) | ||
| * [Others](#others) | ||
@@ -198,2 +199,22 @@ * [Why dafo](#why-dafo) | ||
| ### dafo.toSeconds | ||
| ```javascript | ||
| const dafo = require('dafo'); | ||
| let secondCount = dafo.toSeconds('1d2h'); | ||
| ``` | ||
| * Date __dafo.toSeconds__(string *age*) | ||
| Argument *age* is an expression used to describe age, generally that of HTTP cache. | ||
| | Unit | Meaning | | ||
| | :---- | :------------------------------------------ | | ||
| | __s__ | second(s) | | ||
| | __m__ | minute(s) | | ||
| | __h__ | hour(s) | | ||
| | __d__ | day(s) | | ||
| | __M__ | month(es) | | ||
| | __Y__ | year(s) | | ||
| ### Others | ||
@@ -200,0 +221,0 @@ |
+5
-1
@@ -0,1 +1,5 @@ | ||
| /** | ||
| * 本例中,部分测试用例依赖的占位符尚未实现。 | ||
| */ | ||
| 'use strict'; | ||
@@ -164,3 +168,3 @@ | ||
| it('Y/m/d/H\\H/is', () => { | ||
| console.log(date_format(d1, 'Y/m/d/H\\H/is')); | ||
| assert.strictEqual(date_format(d1, 'Y/m/d/H\\H/is'), '2000/01/01/13H/1406'); | ||
| }); | ||
@@ -167,0 +171,0 @@ |
+10
-0
@@ -118,2 +118,12 @@ 'use strict'; | ||
| it.only('toSeconds()', () => { | ||
| assert.equal(dafo.toSeconds( '30s'), 30); | ||
| assert.equal(dafo.toSeconds( '1m'), 60); | ||
| assert.equal(dafo.toSeconds( '1h'), 3600); | ||
| assert.equal(dafo.toSeconds( '1d'), 86400); | ||
| assert.equal(dafo.toSeconds( '1d1m'), 86400 + 60); | ||
| assert.equal(dafo.toSeconds( ' 1M'), 86400 * 30); | ||
| assert.equal(dafo.toSeconds( ' 1y'), 86400 * 365); | ||
| assert.equal(dafo.toSeconds( ' 1Q'), 86400 * 90); | ||
| }) | ||
| }); |
-18
| # dafo Change Log | ||
| Notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning 2.0.0](http://semver.org/). | ||
| ## [0.1.1] - Sep 11th, 2019 | ||
| * Fixed the bug in `dafo/mysql` that all place holders after escape character `\` are keep raw. | ||
| ## [0.1.0] - May 12th, 2019 | ||
| * Utility function `dafo.parse()` added. | ||
| ## [0.0.1] - 2018-1-17 | ||
| Released. | ||
| --- | ||
| This CHANGELOG.md follows [*Keep a CHANGELOG*](http://keepachangelog.com/). |
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
57503
3.74%1229
5.49%273
8.33%1
Infinity%