chaining-date
Advanced tools
Comparing version 0.7.3 to 0.7.4
@@ -1,1 +0,1 @@ | ||
var ChainingDate=function(){var t={YYYY:function(){return this.getYear()},MM:function(){return this.getMonth()},DD:function(){return this.getDate()},hh:function(){return this.getHours()},mm:function(){return this.getMinutes()},ss:function(){return this.getSeconds()},sss:function(){return this.getMilliseconds()}},n=["Year","Month","Date","Hours","Minutes","Seconds","Milliseconds"],e=Date,i=s.prototype,r=n.length;function s(t){this.date="number"==typeof t?new e(t):"string"==typeof t?new e(e.parse(t)):t||new e}for(s.setFormat=function(n,e){t[n]=e};r--;)!function(t){var n="add"+t,e="get"+t,r="set"+t;i[e]=function(){return this.date[e]()},i[r]=function(t){return this.date[r](t),this},i[n]=function(t){return this[r](this[e]()+t),this}}(n[r]);return i.getYear=function(){return this.date.getFullYear()},i.getMonth=function(){return this.date.getMonth()+1},i.setMonth=function(t){return this.date.setMonth(t-1),this},i.toString=function(n){var e=this;return n?n.replace(new RegExp(Object.keys(t).join("|"),"g"),(function(n){return t[n].call(e)})):this.date.toString()},s}(); | ||
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}(); |
/** | ||
* @author TroyTae | ||
* @version 0.7.3 | ||
* @version 0.7.4 | ||
* @name chaining-date | ||
@@ -8,12 +8,2 @@ */ | ||
var DateFormat = { | ||
YYYY: function() { return this.getYear(); }, | ||
MM: function() { return this.getMonth(); }, | ||
DD: function() { return this.getDate(); }, | ||
hh: function() { return this.getHours(); }, | ||
mm: function() { return this.getMinutes(); }, | ||
ss: function() { return this.getSeconds(); }, | ||
sss: function() { return this.getMilliseconds(); } | ||
}; | ||
var DateMethods = [ | ||
@@ -29,6 +19,17 @@ 'Year', | ||
var DefaultDateFormat = { | ||
[DateMethods[0]]: 'YYYY', | ||
[DateMethods[1]]: 'MM', | ||
[DateMethods[2]]: 'DD', | ||
[DateMethods[3]]: 'hh', | ||
[DateMethods[4]]: 'mm', | ||
[DateMethods[5]]: 'ss', | ||
[DateMethods[6]]: 'sss' | ||
}; | ||
var | ||
FormatRegExp, | ||
DateFormat = {}, | ||
constructor = Date, | ||
proto = ChainingDate.prototype, | ||
index = DateMethods.length | ||
proto = ChainingDate.prototype | ||
; | ||
@@ -46,27 +47,35 @@ | ||
// static method | ||
function setFormatRegExp() { | ||
FormatRegExp = new RegExp( | ||
Object.keys(DateFormat).reverse().join('|'), | ||
'g' | ||
); | ||
} | ||
ChainingDate.setFormat = function(format, callback) { | ||
DateFormat[format] = callback; | ||
setFormatRegExp(); | ||
}; | ||
// default prototype | ||
while (index--) { | ||
(function(method) { | ||
var | ||
adder = 'add' + method, | ||
getter = 'get' + method, | ||
setter = 'set' + method | ||
; | ||
proto[getter] = function () { | ||
return this.date[getter](); | ||
}; | ||
proto[setter] = function (value) { | ||
this.date[setter](value); | ||
return this; | ||
}; | ||
proto[adder] = function (value) { | ||
this[setter](this[getter]() + value); | ||
return this; | ||
}; | ||
})(DateMethods[index]); | ||
} | ||
DateMethods.forEach(function(method) { | ||
var | ||
adder = 'add' + method, | ||
setter = 'set' + method, | ||
getter = 'get' + method | ||
; | ||
proto[getter] = function () { | ||
return this.date[getter](); | ||
}; | ||
proto[setter] = function (value) { | ||
this.date[setter](value); | ||
return this; | ||
}; | ||
proto[adder] = function (value) { | ||
this[setter](this[getter]() + value); | ||
return this; | ||
}; | ||
ChainingDate.setFormat(DefaultDateFormat[method], function () { | ||
return this['get' + method](); | ||
}); | ||
}); | ||
@@ -84,16 +93,12 @@ // custom prototype | ||
}; | ||
proto.toString = function(format) { | ||
proto.format = function(format) { | ||
var instance = this; | ||
if (format) { | ||
return format.replace( | ||
new RegExp(Object.keys(DateFormat).join('|'), 'g'), | ||
function (match) { | ||
return DateFormat[match].call(instance); | ||
} | ||
); | ||
} else { | ||
return this.date.toString(); | ||
} | ||
return format.replace( | ||
FormatRegExp, | ||
function (match) { | ||
return DateFormat[match].call(instance); | ||
} | ||
); | ||
}; | ||
module.exports = ChainingDate; |
{ | ||
"name": "chaining-date", | ||
"version": "0.7.3", | ||
"version": "0.7.4", | ||
"description": "A tiny module for using Date with chaining ⏱", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -1,7 +0,8 @@ | ||
import { DateFormat, DateMethods } from './constants'; | ||
import { DateMethods, DefaultDateFormat } from './constants'; | ||
var | ||
FormatRegExp, | ||
DateFormat = {}, | ||
constructor = Date, | ||
proto = ChainingDate.prototype, | ||
index = DateMethods.length | ||
proto = ChainingDate.prototype | ||
; | ||
@@ -19,27 +20,35 @@ | ||
// static method | ||
function setFormatRegExp() { | ||
FormatRegExp = new RegExp( | ||
Object.keys(DateFormat).reverse().join('|'), | ||
'g' | ||
); | ||
} | ||
ChainingDate.setFormat = function(format, callback) { | ||
DateFormat[format] = callback; | ||
setFormatRegExp(); | ||
}; | ||
// default prototype | ||
while (index--) { | ||
(function(method) { | ||
var | ||
adder = 'add' + method, | ||
getter = 'get' + method, | ||
setter = 'set' + method | ||
; | ||
proto[getter] = function () { | ||
return this.date[getter](); | ||
}; | ||
proto[setter] = function (value) { | ||
this.date[setter](value); | ||
return this; | ||
}; | ||
proto[adder] = function (value) { | ||
this[setter](this[getter]() + value); | ||
return this; | ||
}; | ||
})(DateMethods[index]); | ||
} | ||
DateMethods.forEach(function(method) { | ||
var | ||
adder = 'add' + method, | ||
setter = 'set' + method, | ||
getter = 'get' + method | ||
; | ||
proto[getter] = function () { | ||
return this.date[getter](); | ||
}; | ||
proto[setter] = function (value) { | ||
this.date[setter](value); | ||
return this; | ||
}; | ||
proto[adder] = function (value) { | ||
this[setter](this[getter]() + value); | ||
return this; | ||
}; | ||
ChainingDate.setFormat(DefaultDateFormat[method], function () { | ||
return this['get' + method](); | ||
}); | ||
}); | ||
@@ -57,16 +66,12 @@ // custom prototype | ||
}; | ||
proto.toString = function(format) { | ||
proto.format = function(format) { | ||
var instance = this; | ||
if (format) { | ||
return format.replace( | ||
new RegExp(Object.keys(DateFormat).join('|'), 'g'), | ||
function (match) { | ||
return DateFormat[match].call(instance); | ||
} | ||
); | ||
} else { | ||
return this.date.toString(); | ||
} | ||
return format.replace( | ||
FormatRegExp, | ||
function (match) { | ||
return DateFormat[match].call(instance); | ||
} | ||
); | ||
}; | ||
export default ChainingDate; |
@@ -1,11 +0,1 @@ | ||
var DateFormat = { | ||
YYYY: function() { return this.getYear(); }, | ||
MM: function() { return this.getMonth(); }, | ||
DD: function() { return this.getDate(); }, | ||
hh: function() { return this.getHours(); }, | ||
mm: function() { return this.getMinutes(); }, | ||
ss: function() { return this.getSeconds(); }, | ||
sss: function() { return this.getMilliseconds(); } | ||
}; | ||
var DateMethods = [ | ||
@@ -21,5 +11,15 @@ 'Year', | ||
var DefaultDateFormat = { | ||
[DateMethods[0]]: 'YYYY', | ||
[DateMethods[1]]: 'MM', | ||
[DateMethods[2]]: 'DD', | ||
[DateMethods[3]]: 'hh', | ||
[DateMethods[4]]: 'mm', | ||
[DateMethods[5]]: 'ss', | ||
[DateMethods[6]]: 'sss' | ||
}; | ||
export { | ||
DateFormat, | ||
DateMethods | ||
DateMethods, | ||
DefaultDateFormat | ||
}; |
@@ -45,23 +45,24 @@ const ChainingDate = require('../dist/index'); | ||
// test('format', () => { | ||
// const chainingDate = new ChainingDate(`${year}-${month}-${date} ${hours}:${minutes}:${seconds}`); | ||
// expect(chainingDate.toString('YYYY')).toBe(year.toString()); | ||
// expect(chainingDate.toString('MM')).toBe('0' + month); | ||
// expect(chainingDate.toString('DD')).toBe(date.toString()); | ||
// expect(chainingDate.toString('hh')).toBe('0' + hours); | ||
// expect(chainingDate.toString('mm')).toBe(minutes.toString()); | ||
// expect(chainingDate.toString('ss')).toBe(seconds.toString()); | ||
// | ||
// expect(chainingDate.toString('YYYY.MM.DD hh:mm:ss (Troy Time)')) | ||
// .toBe(`${year}.0${month}.${date} 0${hours}:${minutes}:${seconds} (Troy Time)`); | ||
// }); | ||
test('format', () => { | ||
const chainingDate = new ChainingDate(`${year}-${month}-${date} ${hours}:${minutes}:${seconds}`); | ||
expect(chainingDate.format('YYYY')).toBe(year.toString()); | ||
expect(chainingDate.format('MM')).toBe(month.toString()); | ||
expect(chainingDate.format('DD')).toBe(date.toString()); | ||
expect(chainingDate.format('hh')).toBe(hours.toString()); | ||
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)`); | ||
}); | ||
test('custom format', () => { | ||
const chainingDate = new ChainingDate(); | ||
ChainingDate.setFormat('YY', function () { | ||
return year % 100; | ||
}); | ||
ChainingDate.setFormat('YMD', function () { | ||
return `${year}-${month}-${date}`; | ||
}); | ||
expect(chainingDate.toString('YMD')) | ||
.toBe(`${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
411
18883