chaining-date
Advanced tools
Comparing version 0.7.4 to 0.8.1
@@ -1,1 +0,1 @@ | ||
var ChainingDate=function(){var t,e=["Year","Month","Date","Hours","Minutes","Seconds","Milliseconds"],n={[e[0]]:"YYYY",[e[1]]:"MM",[e[2]]:"DD",[e[3]]:"hh",[e[4]]:"mm",[e[5]]:"ss",[e[6]]:"sss"},r={},i=Date,s=o.prototype;function o(t){this.date="number"==typeof t?new i(t):"string"==typeof t?new i(i.parse(t)):t||new i}return o.setFormat=function(e,n){r[e]=n,t=new RegExp(Object.keys(r).reverse().join("|"),"g")},e.forEach((function(t){var e="add"+t,r="set"+t,i="get"+t;s[i]=function(){return this.date[i]()},s[r]=function(t){return this.date[r](t),this},s[e]=function(t){return this[r](this[i]()+t),this},o.setFormat(n[t],(function(){return this["get"+t]()}))})),s.getYear=function(){return this.date.getFullYear()},s.getMonth=function(){return this.date.getMonth()+1},s.setMonth=function(t){return this.date.setMonth(t-1),this},s.format=function(e){var n=this;return e.replace(t,(function(t){return r[t].call(n)}))},o}(); | ||
var ChainingDate=function(){var t=["Year","Month","Date","Hours","Minutes","Seconds","Milliseconds"],n={[t[0]]:"YYYY",[t[1]]:"MM",[t[2]]:"DD",[t[3]]:"hh",[t[4]]:"mm",[t[5]]:"ss",[t[6]]:"sss"};function e(t,n){return n?function(t,n){for(var e="",r=0;r++<n;)e+="0";return(e+t).slice(-n)}(t,n):t}var r,i=Date,o=u.prototype,s={};function u(t){this.date="number"==typeof t?new i(t):"string"==typeof t?new i(i.parse(t)):t||new i}return u.setFormat=function(t,n){s[t]=n,r=new RegExp(Object.keys(s).reverse().join("|"),"g")},t.forEach((function(t){var r="add"+t,i="set"+t,s="get"+t,a=n[t];o[s]=function(t){return e(this.date[s](),t)},o[i]=function(t){return this.date[i](t),this},o[r]=function(t){return this[i](this[s]()+t),this},u.setFormat(a,(function(){return this["get"+t](a.length)}))})),o.getYear=function(t){return e(this.date.getFullYear(),t)},o.getMonth=function(t){return e(this.date.getMonth()+1,t)},o.setMonth=function(t){return this.date.setMonth(t-1),this},o.format=function(t){var n=this;return t.replace(r,(function(t){return s[t].call(n)}))},u}(); |
/** | ||
* @author TroyTae | ||
* @version 0.7.4 | ||
* @version 0.8.1 | ||
* @name chaining-date | ||
@@ -28,7 +28,28 @@ */ | ||
function fillZero(value, digit) { | ||
var | ||
str = '', | ||
index = 0 | ||
; | ||
while (index++ < digit) { | ||
str += '0'; | ||
} | ||
return (str + value).slice(-digit); | ||
} | ||
function getValueWithZero(value, digit) { | ||
return digit ? fillZero(value, digit) : value; | ||
} | ||
var | ||
constructor = Date, | ||
proto = ChainingDate.prototype, | ||
FormatRegExp, | ||
DateFormat = {}, | ||
constructor = Date, | ||
proto = ChainingDate.prototype | ||
setFormatRegExp = function() { | ||
FormatRegExp = new RegExp( | ||
Object.keys(DateFormat).reverse().join('|'), | ||
'g' | ||
); | ||
} | ||
; | ||
@@ -46,8 +67,2 @@ | ||
// static method | ||
function setFormatRegExp() { | ||
FormatRegExp = new RegExp( | ||
Object.keys(DateFormat).reverse().join('|'), | ||
'g' | ||
); | ||
} | ||
ChainingDate.setFormat = function(format, callback) { | ||
@@ -63,6 +78,7 @@ DateFormat[format] = callback; | ||
setter = 'set' + method, | ||
getter = 'get' + method | ||
getter = 'get' + method, | ||
format = DefaultDateFormat[method] | ||
; | ||
proto[getter] = function () { | ||
return this.date[getter](); | ||
proto[getter] = function (digit) { | ||
return getValueWithZero(this.date[getter](), digit); | ||
}; | ||
@@ -77,4 +93,4 @@ proto[setter] = function (value) { | ||
}; | ||
ChainingDate.setFormat(DefaultDateFormat[method], function () { | ||
return this['get' + method](); | ||
ChainingDate.setFormat(format, function () { | ||
return this['get' + method](format.length); | ||
}); | ||
@@ -84,7 +100,7 @@ }); | ||
// custom prototype | ||
proto.getYear = function() { | ||
return this.date.getFullYear(); | ||
proto.getYear = function(digit) { | ||
return getValueWithZero(this.date.getFullYear(), digit); | ||
}; | ||
proto.getMonth = function() { | ||
return this.date.getMonth() + 1; | ||
proto.getMonth = function(digit) { | ||
return getValueWithZero(this.date.getMonth() + 1, digit); | ||
}; | ||
@@ -91,0 +107,0 @@ proto.setMonth = function(value) { |
{ | ||
"name": "chaining-date", | ||
"version": "0.7.4", | ||
"version": "0.8.1", | ||
"description": "A tiny module for using Date with chaining ⏱", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
import { DateMethods, DefaultDateFormat } from './constants'; | ||
import { getValueWithZero } from './utility'; | ||
var | ||
constructor = Date, | ||
proto = ChainingDate.prototype, | ||
FormatRegExp, | ||
DateFormat = {}, | ||
constructor = Date, | ||
proto = ChainingDate.prototype | ||
setFormatRegExp = function() { | ||
FormatRegExp = new RegExp( | ||
Object.keys(DateFormat).reverse().join('|'), | ||
'g' | ||
); | ||
} | ||
; | ||
@@ -20,8 +27,2 @@ | ||
// static method | ||
function setFormatRegExp() { | ||
FormatRegExp = new RegExp( | ||
Object.keys(DateFormat).reverse().join('|'), | ||
'g' | ||
); | ||
} | ||
ChainingDate.setFormat = function(format, callback) { | ||
@@ -37,6 +38,7 @@ DateFormat[format] = callback; | ||
setter = 'set' + method, | ||
getter = 'get' + method | ||
getter = 'get' + method, | ||
format = DefaultDateFormat[method] | ||
; | ||
proto[getter] = function () { | ||
return this.date[getter](); | ||
proto[getter] = function (digit) { | ||
return getValueWithZero(this.date[getter](), digit); | ||
}; | ||
@@ -51,4 +53,4 @@ proto[setter] = function (value) { | ||
}; | ||
ChainingDate.setFormat(DefaultDateFormat[method], function () { | ||
return this['get' + method](); | ||
ChainingDate.setFormat(format, function () { | ||
return this['get' + method](format.length); | ||
}); | ||
@@ -58,7 +60,7 @@ }); | ||
// custom prototype | ||
proto.getYear = function() { | ||
return this.date.getFullYear(); | ||
proto.getYear = function(digit) { | ||
return getValueWithZero(this.date.getFullYear(), digit); | ||
}; | ||
proto.getMonth = function() { | ||
return this.date.getMonth() + 1; | ||
proto.getMonth = function(digit) { | ||
return getValueWithZero(this.date.getMonth() + 1, digit); | ||
}; | ||
@@ -65,0 +67,0 @@ proto.setMonth = function(value) { |
@@ -48,21 +48,21 @@ const ChainingDate = require('../dist/index'); | ||
expect(chainingDate.format('YYYY')).toBe(year.toString()); | ||
expect(chainingDate.format('MM')).toBe(month.toString()); | ||
expect(chainingDate.format('MM')).toBe(`0${month}`); | ||
expect(chainingDate.format('DD')).toBe(date.toString()); | ||
expect(chainingDate.format('hh')).toBe(hours.toString()); | ||
expect(chainingDate.format('hh')).toBe(`0${hours}`); | ||
expect(chainingDate.format('mm')).toBe(minutes.toString()); | ||
expect(chainingDate.format('ss')).toBe(seconds.toString()); | ||
expect(chainingDate.format('YYYY.MM.DD hh:mm:ss (Troy Time)')) | ||
.toBe(`${year}.${month}.${date} ${hours}:${minutes}:${seconds} (Troy Time)`); | ||
.toBe(`${year}.0${month}.${date} 0${hours}:${minutes}:${seconds} (Troy Time)`); | ||
}); | ||
test('custom format', () => { | ||
const chainingDate = new ChainingDate(); | ||
ChainingDate.setFormat('YY', function () { | ||
return year % 100; | ||
}); | ||
ChainingDate.setFormat('YMD', function () { | ||
return `${year}-${month}-${date}`; | ||
}); | ||
expect(chainingDate.format('YY')).toBe(`${year % 100}`); | ||
expect(chainingDate.format('YMD')).toBe(`${year}-${month}-${date}`); | ||
}); | ||
// test('custom format', () => { | ||
// const chainingDate = new ChainingDate(); | ||
// ChainingDate.setFormat('YY', function () { | ||
// return year % 100; | ||
// }); | ||
// ChainingDate.setFormat('YMD', function () { | ||
// return `${year}-${month}-${date}`; | ||
// }); | ||
// expect(chainingDate.format('YY')).toBe(`${year % 100}`); | ||
// expect(chainingDate.format('YMD')).toBe(`${year}-${month}-${date}`); | ||
// }); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
26053
16
441