chaining-date
Advanced tools
Comparing version 0.8.2 to 0.8.3
@@ -1,1 +0,1 @@ | ||
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}(); | ||
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=s.prototype,u={};function s(t){this.date="number"==typeof t?new i(t):"string"==typeof t?new i(i.parse(t)):t||new i}function a(t,n){u[t]=n,r=new RegExp(Object.keys(u).reverse().join("|"),"g")}function c(t){return new s(t)}return t.forEach((function(t){var r="add"+t,i="set"+t,u="get"+t,s=n[t];o[u]=function(t){return e(this.date[u](),t)},o[i]=function(t){return this.date[i](t),this},o[r]=function(t){return this[i](this[u]()+t),this},a(s,(function(){return this["get"+t](s.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 u[t].call(n)}))},c.setFormat=a,c}(); |
/** | ||
* @author TroyTae | ||
* @version 0.8.2 | ||
* @version 0.8.3 | ||
* @name chaining-date | ||
@@ -44,10 +44,4 @@ */ | ||
proto = ChainingDate.prototype, | ||
FormatRegExp, | ||
DateFormat = {}, | ||
setFormatRegExp = function() { | ||
FormatRegExp = new RegExp( | ||
Object.keys(DateFormat).reverse().join('|'), | ||
'g' | ||
); | ||
} | ||
formatRegExp, | ||
dateFormat = {} | ||
; | ||
@@ -65,6 +59,9 @@ | ||
// static method | ||
ChainingDate.setFormat = function(format, callback) { | ||
DateFormat[format] = callback; | ||
setFormatRegExp(); | ||
}; | ||
function setFormat(format, callback) { | ||
dateFormat[format] = callback; | ||
formatRegExp = new RegExp( | ||
Object.keys(dateFormat).reverse().join('|'), | ||
'g' | ||
); | ||
} | ||
@@ -90,3 +87,3 @@ // default prototype | ||
}; | ||
ChainingDate.setFormat(format, function () { | ||
setFormat(format, function () { | ||
return this['get' + method](format.length); | ||
@@ -110,5 +107,5 @@ }); | ||
return format.replace( | ||
FormatRegExp, | ||
formatRegExp, | ||
function (match) { | ||
return DateFormat[match].call(instance); | ||
return dateFormat[match].call(instance); | ||
} | ||
@@ -118,2 +115,8 @@ ); | ||
export default ChainingDate; | ||
function index(date) { | ||
return new ChainingDate(date); | ||
} | ||
index.setFormat = setFormat; | ||
export default index; |
/** | ||
* @author TroyTae | ||
* @version 0.8.2 | ||
* @version 0.8.3 | ||
* @name chaining-date | ||
@@ -46,10 +46,4 @@ */ | ||
proto = ChainingDate.prototype, | ||
FormatRegExp, | ||
DateFormat = {}, | ||
setFormatRegExp = function() { | ||
FormatRegExp = new RegExp( | ||
Object.keys(DateFormat).reverse().join('|'), | ||
'g' | ||
); | ||
} | ||
formatRegExp, | ||
dateFormat = {} | ||
; | ||
@@ -67,6 +61,9 @@ | ||
// static method | ||
ChainingDate.setFormat = function(format, callback) { | ||
DateFormat[format] = callback; | ||
setFormatRegExp(); | ||
}; | ||
function setFormat(format, callback) { | ||
dateFormat[format] = callback; | ||
formatRegExp = new RegExp( | ||
Object.keys(dateFormat).reverse().join('|'), | ||
'g' | ||
); | ||
} | ||
@@ -92,3 +89,3 @@ // default prototype | ||
}; | ||
ChainingDate.setFormat(format, function () { | ||
setFormat(format, function () { | ||
return this['get' + method](format.length); | ||
@@ -112,5 +109,5 @@ }); | ||
return format.replace( | ||
FormatRegExp, | ||
formatRegExp, | ||
function (match) { | ||
return DateFormat[match].call(instance); | ||
return dateFormat[match].call(instance); | ||
} | ||
@@ -120,2 +117,8 @@ ); | ||
module.exports = ChainingDate; | ||
function index(date) { | ||
return new ChainingDate(date); | ||
} | ||
index.setFormat = setFormat; | ||
module.exports = index; |
{ | ||
"name": "chaining-date", | ||
"version": "0.8.2", | ||
"version": "0.8.3", | ||
"description": "A tiny module for using Date with chaining âą", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -14,3 +14,3 @@ const pkg = require('./package.json'); | ||
return { | ||
input: 'src/chaining-date.js', | ||
input: 'src/index.js', | ||
output: { | ||
@@ -17,0 +17,0 @@ banner: `/**\n * @author ${pkg.author.name}\n * @version ${pkg.version}\n * @name ${pkg.name}\n */`, |
@@ -7,10 +7,4 @@ import { DateMethods, DefaultDateFormat } from './constants'; | ||
proto = ChainingDate.prototype, | ||
FormatRegExp, | ||
DateFormat = {}, | ||
setFormatRegExp = function() { | ||
FormatRegExp = new RegExp( | ||
Object.keys(DateFormat).reverse().join('|'), | ||
'g' | ||
); | ||
} | ||
formatRegExp, | ||
dateFormat = {} | ||
; | ||
@@ -28,6 +22,9 @@ | ||
// static method | ||
ChainingDate.setFormat = function(format, callback) { | ||
DateFormat[format] = callback; | ||
setFormatRegExp(); | ||
}; | ||
function setFormat(format, callback) { | ||
dateFormat[format] = callback; | ||
formatRegExp = new RegExp( | ||
Object.keys(dateFormat).reverse().join('|'), | ||
'g' | ||
); | ||
} | ||
@@ -53,3 +50,3 @@ // default prototype | ||
}; | ||
ChainingDate.setFormat(format, function () { | ||
setFormat(format, function () { | ||
return this['get' + method](format.length); | ||
@@ -73,5 +70,5 @@ }); | ||
return format.replace( | ||
FormatRegExp, | ||
formatRegExp, | ||
function (match) { | ||
return DateFormat[match].call(instance); | ||
return dateFormat[match].call(instance); | ||
} | ||
@@ -81,2 +78,2 @@ ); | ||
export default ChainingDate; | ||
export { setFormat, ChainingDate }; |
@@ -13,3 +13,3 @@ const ChainingDate = require('../dist/index'); | ||
const today = new Date(); | ||
const chainingDate = new ChainingDate(); | ||
const chainingDate = ChainingDate(); | ||
expect(chainingDate.getYear()).toBe(today.getFullYear()); | ||
@@ -25,3 +25,3 @@ expect(chainingDate.getMonth()).toBe(today.getMonth() + 1); | ||
test('setter', () => { | ||
const chainingDate = new ChainingDate(); | ||
const chainingDate = ChainingDate(); | ||
expect(chainingDate.setYear(year).getYear()).toBe(year); | ||
@@ -37,3 +37,3 @@ expect(chainingDate.setMonth(month).getMonth()).toBe(month); | ||
test('adder', () => { | ||
const chainingDate = new ChainingDate(); | ||
const chainingDate = ChainingDate(); | ||
expect(chainingDate.setYear(year).addYear(3).getYear()).toBe(year + 3); | ||
@@ -49,3 +49,3 @@ expect(chainingDate.setMonth(month).addMonth(1).getMonth()).toBe(month + 1); | ||
test('format', () => { | ||
const chainingDate = new ChainingDate(`${year}-${month}-${date} ${hours}:${minutes}:${seconds}`); | ||
const chainingDate = ChainingDate(`${year}-${month}-${date} ${hours}:${minutes}:${seconds}`); | ||
expect(chainingDate.format('YYYY')).toBe(year.toString()); | ||
@@ -63,3 +63,3 @@ expect(chainingDate.format('MM')).toBe(`0${month}`); | ||
const customDate = new Date(`${year}-${month}-${date} ${hours}:${minutes}:${seconds}`); | ||
const chainingDate = new ChainingDate(customDate); | ||
const chainingDate = ChainingDate(customDate); | ||
ChainingDate.setFormat('dd', function () { | ||
@@ -66,0 +66,0 @@ switch (this.date.getDay()) { |
23818
18
570