chaining-date
Advanced tools
Comparing version 0.6.2 to 0.7.1
@@ -1,1 +0,1 @@ | ||
var ChainingDate=function(){var t=["Time","Year","Month","Date","Day","Hours","Minutes","Seconds","Milliseconds"],e=Date,n=r.prototype,i=t.length;function r(t){this.date="number"==typeof t?new e(t):"string"==typeof t?new e(e.parse(t)):t||new e}for(;i--;)!function(t){var e="add"+t,i="get"+t,r="set"+t;n[i]=function(){return this.date[i]()},n[r]=function(t){return this.date[r](t),this},n[e]=function(t){return this[r](this[i]()+t),this}}(t[i]);return n.getYear=function(){return this.date.getFullYear()},n.getMonth=function(){return this.date.getMonth()+1},n.setMonth=function(t){return this.date.setMonth(t-1),this},n.setDay=function(t){return this.addDate(t-this.getDay()),this},r}(); | ||
var ChainingDate=function(){var t=function(t){return("0"+t).slice(-2)},n={YYYY:function(){return this.getYear()},YY:function(){return this.getYear()%100},MM:function(){return t(this.getMonth())},DD:function(){return t(this.getDate())},hh:function(){return t(this.getHours())},mm:function(){return t(this.getMinutes())},ss:function(){return t(this.getSeconds())}},e=["Time","Year","Month","Date","Day","Hours","Minutes","Seconds","Milliseconds"],i=Date,r=s.prototype,u=e.length;function s(t){this.date="number"==typeof t?new i(t):"string"==typeof t?new i(i.parse(t)):t||new i}for(s.setFormat=function(t,e){n[t]=e};u--;)!function(t){var n="add"+t,e="get"+t,i="set"+t;r[e]=function(){return this.date[e]()},r[i]=function(t){return this.date[i](t),this},r[n]=function(t){return this[i](this[e]()+t),this}}(e[u]);return r.getYear=function(){return this.date.getFullYear()},r.getMonth=function(){return this.date.getMonth()+1},r.setMonth=function(t){return this.date.setMonth(t-1),this},r.setDay=function(t){return this.addDate(t-this.getDay()),this},r.toString=function(t){var e=this;return t?t.replace(new RegExp(Object.keys(n).join("|"),"g"),(function(t){return n[t].call(e)})):this.date.toString()},s}(); |
/** | ||
* @author TroyTae | ||
* @version 0.6.2 | ||
* @version 0.7.1 | ||
* @name chaining-date | ||
@@ -8,2 +8,16 @@ */ | ||
var fillZero = function(number) { | ||
return ('0' + number).slice(-2); | ||
}; | ||
var DateFormat = { | ||
YYYY: function() { return this.getYear(); }, | ||
YY: function() { return this.getYear() % 100; }, | ||
MM: function() { return fillZero(this.getMonth()); }, | ||
DD: function() { return fillZero(this.getDate()); }, | ||
hh: function() { return fillZero(this.getHours()); }, | ||
mm: function() { return fillZero(this.getMinutes()); }, | ||
ss: function() { return fillZero(this.getSeconds()); } | ||
}; | ||
var DateMethods = [ | ||
@@ -27,3 +41,3 @@ 'Time', | ||
// Constructor | ||
// constructor | ||
function ChainingDate(date) { | ||
@@ -37,2 +51,7 @@ this.date = ( | ||
// static method | ||
ChainingDate.setFormat = function(format, callback) { | ||
DateFormat[format] = callback; | ||
}; | ||
// default prototype | ||
@@ -75,3 +94,16 @@ while (index--) { | ||
}; | ||
proto.toString = 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(); | ||
} | ||
}; | ||
module.exports = ChainingDate; |
{ | ||
"name": "chaining-date", | ||
"version": "0.6.2", | ||
"version": "0.7.1", | ||
"description": "A tiny module for using Date with chaining ⏱", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -1,2 +0,2 @@ | ||
import {DateMethods} from './constants'; | ||
import { DateFormat, DateMethods } from './constants'; | ||
@@ -9,3 +9,3 @@ var | ||
// Constructor | ||
// constructor | ||
function ChainingDate(date) { | ||
@@ -19,2 +19,7 @@ this.date = ( | ||
// static method | ||
ChainingDate.setFormat = function(format, callback) { | ||
DateFormat[format] = callback; | ||
}; | ||
// default prototype | ||
@@ -57,3 +62,16 @@ while (index--) { | ||
}; | ||
proto.toString = 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(); | ||
} | ||
}; | ||
export default ChainingDate; |
@@ -0,10 +1,13 @@ | ||
var fillZero = function(number) { | ||
return ('0' + number).slice(-2); | ||
}; | ||
var DateFormat = { | ||
year: 'YYYY', | ||
month: 'MM', | ||
date: 'DD', | ||
day: 'D', | ||
hour: 'hh', | ||
minute: 'mm', | ||
second: 'ss', | ||
millisecond: 'sss', | ||
YYYY: function() { return this.getYear(); }, | ||
YY: function() { return this.getYear() % 100; }, | ||
MM: function() { return fillZero(this.getMonth()); }, | ||
DD: function() { return fillZero(this.getDate()); }, | ||
hh: function() { return fillZero(this.getHours()); }, | ||
mm: function() { return fillZero(this.getMinutes()); }, | ||
ss: function() { return fillZero(this.getSeconds()); } | ||
}; | ||
@@ -11,0 +14,0 @@ |
@@ -52,1 +52,37 @@ 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('YY')).toBe((year % 100).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('custom format', () => { | ||
const chainingDate = new ChainingDate(); | ||
ChainingDate.setFormat('SSS', function () { | ||
return this.getMilliseconds(); | ||
}); | ||
expect(chainingDate.setMilliseconds(milliseconds).toString('SSS')) | ||
.toBe(milliseconds.toString()); | ||
ChainingDate.setFormat('SSS', function () { | ||
return this.getMilliseconds() + 'Z'; | ||
}); | ||
expect(chainingDate.setMilliseconds(milliseconds).toString('SSS')) | ||
.toBe(milliseconds + 'Z'); | ||
ChainingDate.setFormat('NY_TIMES', function () { | ||
return `${year}-${month}-${date}`; | ||
}); | ||
expect(chainingDate.toString('NY_TIMES')) | ||
.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
20522
435