date-and-time 0.14.1 → 0.14.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -55
- package/date-and-time.js +1 -1
- package/date-and-time.min.js +7 -7
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -32,6 +32,9 @@ npm install date-and-time --save
|
|
|
32
32
|
|
|
33
33
|
## Recent Changes
|
|
34
34
|
|
|
35
|
+
- 0.14.2
|
|
36
|
+
- Fixed regular expression denial of service (ReDoS) vulnerability.
|
|
37
|
+
|
|
35
38
|
- 0.14.1
|
|
36
39
|
- Fixed a bug characters inside square brackets `[]` are not validated.
|
|
37
40
|
|
|
@@ -53,61 +56,6 @@ npm install date-and-time --save
|
|
|
53
56
|
date.format(today, 'M/D/YYYY'); // => '8/3/2020'
|
|
54
57
|
```
|
|
55
58
|
|
|
56
|
-
- 0.13.0
|
|
57
|
-
- The `format()` now supports a compiled formatString.
|
|
58
|
-
|
|
59
|
-
```javascript
|
|
60
|
-
const pattern = date.compile('MMM D YYYY');
|
|
61
|
-
|
|
62
|
-
date.format(new Date(2020, 2, 3), pattern); // => Mar 3 2020
|
|
63
|
-
date.format(new Date(2020, 3, 4), pattern); // => Apr 4 2020
|
|
64
|
-
date.format(new Date(2020, 4, 5), pattern); // => May 5 2020
|
|
65
|
-
```
|
|
66
|
-
|
|
67
|
-
- The `parse()` now supports `...` (ellipsis) token. The `preparse()` and the `isValid()` are too.
|
|
68
|
-
|
|
69
|
-
```javascript
|
|
70
|
-
// Cannot write like this even if you want to get only a date part.
|
|
71
|
-
date.parse('Mar 05 2020 10:42:29 GMT-0800', 'MMM D YYYY'); // => Invalid Date
|
|
72
|
-
|
|
73
|
-
// Previously, it was necessary to adjust the length of the format string by appending white spaces of the same length as a part to ignore.
|
|
74
|
-
date.parse('Mar 05 2020 10:42:29 GMT-0800', 'MMM D YYYY ');
|
|
75
|
-
|
|
76
|
-
// Can write simply like this using the ellipsis token.
|
|
77
|
-
date.parse('Mar 05 2020 10:42:29 GMT-0800', 'MMM D YYYY...');
|
|
78
|
-
```
|
|
79
|
-
|
|
80
|
-
- Added `day-of-week` plugin for the parser. However this is a dummy, not effective at all. See [PLUGINS.md](./PLUGINS.md) for details.
|
|
81
|
-
|
|
82
|
-
```javascript
|
|
83
|
-
// If a date string has day of week at the head, cannot parse it unless remove that part from it or fill white spaces that part of the format string.
|
|
84
|
-
date.parse('Thu Mar 05 2020 10:42:29 GMT-0800', ' MMM D YYYY...');
|
|
85
|
-
|
|
86
|
-
// This plugin provides `dd`, `ddd` and `dddd` tokens for such a case. However they are not effective at all because day of week has not information to identify a date.
|
|
87
|
-
date.parse('Thu Mar 05 2020 10:42:29 GMT-0800', 'ddd MMM D YYYY...');
|
|
88
|
-
```
|
|
89
|
-
|
|
90
|
-
- (**Breaking Change**) The `subtract()` now returns a **REAL** number. Previously, it returned values with truncated decimals.
|
|
91
|
-
|
|
92
|
-
```javascript
|
|
93
|
-
const now = new Date(2020, 2, 5, 1, 2, 3, 4);
|
|
94
|
-
const new_years_day = new Date(2020, 0, 1);
|
|
95
|
-
|
|
96
|
-
date.subtract(now, new_years_day).toDays(); // => 64.04309032407407
|
|
97
|
-
```
|
|
98
|
-
|
|
99
|
-
- Added `timespan` plugin. This plugin provides `timeSpan()` function to display a formatted elapsed time. This will might be integrated with the `subtract()`. See [PLUGINS.md](./PLUGINS.md) for details.
|
|
100
|
-
|
|
101
|
-
```javascript
|
|
102
|
-
const now = new Date(2020, 2, 5, 1, 2, 3, 4);
|
|
103
|
-
const new_years_day = new Date(2020, 0, 1);
|
|
104
|
-
|
|
105
|
-
date.timeSpan(now, new_years_day).toDays('D HH:mm:ss.SSS'); // => '64 01:02:03.004'
|
|
106
|
-
date.timeSpan(now, new_years_day).toHours('H [hours] m [minutes] s [seconds]'); // => '1537 hours 2 minutes 3 seconds'
|
|
107
|
-
```
|
|
108
|
-
|
|
109
|
-
- Added `microsecond` plugin for the parser. Microsecond is not supported by date objects so that it is rounded `millisecond` at the inside. See [PLUGINS.md](./PLUGINS.md) for details.
|
|
110
|
-
|
|
111
59
|
## Usage
|
|
112
60
|
|
|
113
61
|
- Node.js:
|
package/date-and-time.js
CHANGED
|
@@ -136,7 +136,7 @@
|
|
|
136
136
|
* @returns {Array.<string>} a compiled object
|
|
137
137
|
*/
|
|
138
138
|
date.compile = function (formatString) {
|
|
139
|
-
var re = /\[([^\[\]]
|
|
139
|
+
var re = /\[([^\[\]]|\[[^\[\]]*])*]|([A-Za-z])\2+|\.{3}|./g, keys, pattern = [formatString];
|
|
140
140
|
|
|
141
141
|
while ((keys = re.exec(formatString))) {
|
|
142
142
|
pattern[pattern.length] = keys[0];
|
package/date-and-time.min.js
CHANGED
|
@@ -7,10 +7,10 @@ h:function(a){return""+(a.getHours()%12||12)},mm:function(a){return("0"+a.getMin
|
|
|
7
7
|
dd:function(a){return this.res.dd[a.getDay()]},Z:function(a){return a.utc?"+0000":/[\+-]\d{4}/.exec(a.toTimeString())[0]},post:function(a){return a}},x={YYYY:function(a){return this.exec(/^\d{4}/,a)},Y:function(a){return this.exec(/^\d{1,4}/,a)},MMMM:function(a){a=this.find(this.res.MMMM,a);a.value++;return a},MMM:function(a){a=this.find(this.res.MMM,a);a.value++;return a},MM:function(a){return this.exec(/^\d\d/,a)},M:function(a){return this.exec(/^\d\d?/,a)},DD:function(a){return this.exec(/^\d\d/,
|
|
8
8
|
a)},D:function(a){return this.exec(/^\d\d?/,a)},HH:function(a){return this.exec(/^\d\d/,a)},H:function(a){return this.exec(/^\d\d?/,a)},A:function(a){return this.find(this.res.A,a)},hh:function(a){return this.exec(/^\d\d/,a)},h:function(a){return this.exec(/^\d\d?/,a)},mm:function(a){return this.exec(/^\d\d/,a)},m:function(a){return this.exec(/^\d\d?/,a)},ss:function(a){return this.exec(/^\d\d/,a)},s:function(a){return this.exec(/^\d\d?/,a)},SSS:function(a){return this.exec(/^\d{1,3}/,a)},SS:function(a){a=
|
|
9
9
|
this.exec(/^\d\d?/,a);a.value*=10;return a},S:function(a){a=this.exec(/^\d/,a);a.value*=100;return a},Z:function(a){a=this.exec(/^[\+-]\d{2}[0-5]\d/,a);a.value=-60*(a.value/100|0)-a.value%100;return a},h12:function(a,c){return(12===a?0:a)+12*c},exec:function(a,c){var b=(a.exec(c)||[""])[0];return{value:b|0,length:b.length}},find:function(a,c){for(var b=-1,e=0,g=0,h=a.length,f;g<h;g++)f=a[g],!c.indexOf(f)&&f.length>e&&(b=g,e=f.length);return{value:b,length:e}},pre:function(a){return a}},u=function(a,
|
|
10
|
-
c,b){var e=function(h,f,l){var p=function(n){n&&(this.res=n)};p.prototype=h;p.prototype.constructor=p;h=new p(l);for(var k in f||{})l=f[k],h[k]=l.slice?l.slice():l;return h},g={res:e(c.res,b.res)};g.formatter=e(c.formatter,b.formatter,g.res);g.parser=e(c.parser,b.parser,g.res);t[a]=g};d.compile=function(a){for(var c=/\[([^\[\]]
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
10
|
+
c,b){var e=function(h,f,l){var p=function(n){n&&(this.res=n)};p.prototype=h;p.prototype.constructor=p;h=new p(l);for(var k in f||{})l=f[k],h[k]=l.slice?l.slice():l;return h},g={res:e(c.res,b.res)};g.formatter=e(c.formatter,b.formatter,g.res);g.parser=e(c.parser,b.parser,g.res);t[a]=g};d.compile=function(a){for(var c=/\[([^\[\]]|\[[^\[\]]*])*]|([A-Za-z])\2+|\.{3}|./g,b,e=[a];b=c.exec(a);)e[e.length]=b[0];return e};d.format=function(a,c,b){c="string"===typeof c?d.compile(c):c;a=d.addMinutes(a,b?a.getTimezoneOffset():
|
|
11
|
+
0);var e=t[m].formatter,g="";a.utc=b||!1;b=1;for(var h=c.length,f;b<h;b++)f=c[b],g+=e[f]?e.post(e[f](a,c[0])):f.replace(/\[(.*)]/,"$1");return g};d.preparse=function(a,c){var b="string"===typeof c?d.compile(c):c,e={Y:1970,M:1,D:1,H:0,A:0,h:0,m:0,s:0,S:0,Z:0,_index:0,_length:0,_match:0},g=/\[(.*)]/,h=t[m].parser,f=0;a=h.pre(a);for(var l=1,p=b.length,k,n;l<p;l++)if(k=b[l],h[k]){n=h[k](a.slice(f),b[0]);if(!n.length)break;f+=n.length;e[k.charAt(0)]=n.value;e._match++}else if(k===a.charAt(f)||" "===k)f++;
|
|
12
|
+
else if(g.test(k)&&!a.slice(f).indexOf(g.exec(k)[1]))f+=k.length-2;else{"..."===k&&(f=a.length);break}e.H=e.H||h.h12(e.h,e.A);e._index=f;e._length=a.length;return e};d.isValid=function(a,c){var b="string"===typeof a?d.preparse(a,c):a,e=[31,28+d.isLeapYear(b.Y)|0,31,30,31,30,31,31,30,31,30,31][b.M-1];return!(1>b._index||1>b._length||b._index-b._length||1>b._match||1>b.Y||9999<b.Y||1>b.M||12<b.M||1>b.D||b.D>e||0>b.H||23<b.H||0>b.m||59<b.m||0>b.s||59<b.s||0>b.S||999<b.S||-720>b.Z||840<b.Z)};d.parse=
|
|
13
|
+
function(a,c,b){a=d.preparse(a,c);return d.isValid(a)?(a.M-=100>a.Y?22801:1,b||a.Z?new Date(Date.UTC(a.Y,a.M,a.D,a.H,a.m+a.Z,a.s,a.S)):new Date(a.Y,a.M,a.D,a.H,a.m,a.s,a.S)):new Date(NaN)};d.transform=function(a,c,b,e){return d.format(d.parse(a,c),b,e)};d.addYears=function(a,c){return d.addMonths(a,12*c)};d.addMonths=function(a,c){var b=new Date(a.getTime());b.setMonth(b.getMonth()+c);return b};d.addDays=function(a,c){var b=new Date(a.getTime());b.setDate(b.getDate()+c);return b};d.addHours=function(a,
|
|
14
|
+
c){return d.addMinutes(a,60*c)};d.addMinutes=function(a,c){return d.addSeconds(a,60*c)};d.addSeconds=function(a,c){return d.addMilliseconds(a,1E3*c)};d.addMilliseconds=function(a,c){return new Date(a.getTime()+c)};d.subtract=function(a,c){var b=a.getTime()-c.getTime();return{toMilliseconds:function(){return b},toSeconds:function(){return b/1E3},toMinutes:function(){return b/6E4},toHours:function(){return b/36E5},toDays:function(){return b/864E5}}};d.isLeapYear=function(a){return!(a%4)&&!!(a%100)||
|
|
15
|
+
!(a%400)};d.isSameDay=function(a,c){return a.toDateString()===c.toDateString()};d.locale=function(a,c){c?u(a,{res:v,formatter:w,parser:x},c):"function"===typeof a?m=a(d):a&&(q&&!q.date&&console.warn("This method of changing the locale is deprecated. See documentation for details."),m=a);return m};d.extend=function(a){var c=a.extender||{},b;for(b in c)d[b]||(d[b]=c[b]);(a.formatter||a.parser||a.res)&&u(m,t[m],a)};d.plugin=function(a,c){"function"===typeof a?d.extend(r[a(d)]):(r[a]=r[a]||c,!c&&r[a]&&
|
|
16
|
+
(d.extend(r[a]),q&&!q.date&&console.warn("This method of applying plugins is deprecated. See documentation for details.")))};d.locale(m,{});"object"===typeof module&&"object"===typeof module.exports?module.exports=d:"function"===typeof define&&define.amd?define([],function(){return d}):q.date=d})(this);
|