Comparing version 0.1.6 to 0.1.7
@@ -64,6 +64,38 @@ (function (root, factory) { | ||
createInternal = function (value, unit) { | ||
var | ||
milliseconds = 0, | ||
days = 0, | ||
months = 0; | ||
switch(unit) { | ||
case 'year': | ||
months += value * 12; | ||
break; | ||
case 'month': | ||
months += value; | ||
break; | ||
case 'week': | ||
days += value * 7; | ||
break; | ||
case 'day': | ||
days += value; | ||
break; | ||
case 'hour': | ||
case 'minute': | ||
case 'second': | ||
case 'millisecond': | ||
milliseconds += value * unitToMilliseconds[unit]; | ||
break; | ||
default: | ||
} | ||
return new Internal(milliseconds, days, months); | ||
}, | ||
round = Math.round; | ||
function Expiry(value) { | ||
function Expiry(value, maybeUnit) { | ||
@@ -73,2 +105,9 @@ value = value || 0; | ||
var | ||
unit = unitToUnit[maybeUnit || 'millisecond']; | ||
if ( ! unit) { | ||
throw new Error('Invalid unit, '+maybeUnit); | ||
} | ||
var | ||
type = typeOf(value), | ||
@@ -78,8 +117,7 @@ internal; | ||
if (type === 'Number') { | ||
internal = new Internal(value); | ||
internal = createInternal(value, unit); | ||
} else if (type === 'String') { | ||
internal = Expiry.parse(value); | ||
internal = Expiry.parse(value, unit); | ||
} else { | ||
throw new Error('Wrong type for argument 1, value. Expected '+value+ | ||
' to be a number, string, or expiry.'); | ||
throw new Error('Invalid value, '+value); | ||
} | ||
@@ -90,14 +128,20 @@ | ||
Expiry.forge = function (value) { | ||
return new this(value); | ||
Expiry.forge = function (value, maybeUnit) { | ||
return new this(value, maybeUnit); | ||
}; | ||
Expiry.parse = function (value) { | ||
Expiry.parse = function (value, unit) { | ||
if (typeOf(value) !== 'String') { | ||
throw new Error('Wrong type for argument 1, value. String expected.'); | ||
throw new Error('Invalid value, '+value); | ||
} | ||
var | ||
re = /([\d\.]+)\s*([a-zA-Z]+)/g, | ||
re = /^[\d\.]+$/g; | ||
if (re.test(value)) { | ||
value += unit; | ||
} | ||
var | ||
result, | ||
@@ -113,2 +157,4 @@ | ||
re = /([\d\.]+)\s*([a-zA-Z]+)/g; | ||
while ((result = re.exec(value)) !== null) { | ||
@@ -140,3 +186,3 @@ | ||
default: | ||
throw new Error('Unexpected unit '+maybeUnit); | ||
throw new Error('Missing or invalid unit, '+maybeUnit); | ||
} | ||
@@ -179,5 +225,5 @@ } | ||
expiry.VERSION = '0.1.6'; | ||
expiry.VERSION = '0.1.7'; | ||
return expiry; | ||
})); |
@@ -1,1 +0,1 @@ | ||
!function(a,b){"function"==typeof define&&define.amd?define(function(){return a.expiry=b()}):"object"==typeof exports?module.exports=b():a.expiry=b()}(this,function(){function a(){return b.forge.apply(b,arguments)}var b=function(){function a(b){b=b||0;var c,d=i(b);if("Number"===d)c=new j(b);else{if("String"!==d)throw new Error("Wrong type for argument 1, value. Expected "+b+" to be a number, string, or expiry.");c=a.parse(b)}this._internal=c}var b=1,c=1e3*b,d=60*c,e=60*d,f=24*e,g={millisecond:b,second:c,minute:d,hour:e,day:f,month:function(a,b){var c=new Date,d=new Date(c);return d.setMonth(d.getMonth()+a),d.setDate(d.getDate()+b),d-c}},h={milliseconds:"millisecond",millisecond:"millisecond",ms:"millisecond",seconds:"second",second:"second",s:"second",minutes:"minute",minute:"minute",m:"minute",hours:"hour",hour:"hour",h:"hour",days:"day",day:"day",D:"day",weeks:"week",week:"week",W:"week",months:"month",month:"month",M:"month",years:"year",year:"year",Y:"year"},i=function(a){return toString.call(a).match(/\[object (\S+)\]/).pop()},j=function(a,b,c){this.milliseconds=a||0,this.days=b||0,this.months=c||0},k=Math.round;return a.forge=function(a){return new this(a)},a.parse=function(a){if("String"!==i(a))throw new Error("Wrong type for argument 1, value. String expected.");for(var b,c,d,e,f=/([\d\.]+)\s*([a-zA-Z]+)/g,k=0,l=0,m=0;null!==(b=f.exec(a));)switch(c=parseFloat(b[1]),d=b[2],e=h[d]){case"year":m+=12*c;break;case"month":m+=c;break;case"week":l+=7*c;break;case"day":l+=c;break;case"hour":case"minute":case"second":case"millisecond":k+=c*g[e];break;default:throw new Error("Unexpected unit "+d)}return new j(k,l,m)},a.prototype.valueOf=function(){var a=this._internal,b=a.milliseconds,c=a.days,d=a.months;return b+g.month(d,c)},a.prototype.asMilliseconds=function(){return this.valueOf()},a.prototype.asSeconds=function(){return k(this.valueOf()/c)},a}();return a.Expiry=b,a.VERSION="0.1.6",a}); | ||
!function(a,b){"function"==typeof define&&define.amd?define(function(){return a.expiry=b()}):"object"==typeof exports?module.exports=b():a.expiry=b()}(this,function(){function a(){return b.forge.apply(b,arguments)}var b=function(){function a(b,c){b=b||0;var d=h[c||"millisecond"];if(!d)throw new Error("Invalid unit, "+c);var e,f=i(b);if("Number"===f)e=k(b,d);else{if("String"!==f)throw new Error("Invalid value, "+b);e=a.parse(b,d)}this._internal=e}var b=1,c=1e3*b,d=60*c,e=60*d,f=24*e,g={millisecond:b,second:c,minute:d,hour:e,day:f,month:function(a,b){var c=new Date,d=new Date(c);return d.setMonth(d.getMonth()+a),d.setDate(d.getDate()+b),d-c}},h={milliseconds:"millisecond",millisecond:"millisecond",ms:"millisecond",seconds:"second",second:"second",s:"second",minutes:"minute",minute:"minute",m:"minute",hours:"hour",hour:"hour",h:"hour",days:"day",day:"day",D:"day",weeks:"week",week:"week",W:"week",months:"month",month:"month",M:"month",years:"year",year:"year",Y:"year"},i=function(a){return toString.call(a).match(/\[object (\S+)\]/).pop()},j=function(a,b,c){this.milliseconds=a||0,this.days=b||0,this.months=c||0},k=function(a,b){var c=0,d=0,e=0;switch(b){case"year":e+=12*a;break;case"month":e+=a;break;case"week":d+=7*a;break;case"day":d+=a;break;case"hour":case"minute":case"second":case"millisecond":c+=a*g[b]}return new j(c,d,e)},l=Math.round;return a.forge=function(a,b){return new this(a,b)},a.parse=function(a,b){if("String"!==i(a))throw new Error("Invalid value, "+a);var c=/^[\d\.]+$/g;c.test(a)&&(a+=b);var d,e,f,b,k=0,l=0,m=0;for(c=/([\d\.]+)\s*([a-zA-Z]+)/g;null!==(d=c.exec(a));)switch(e=parseFloat(d[1]),f=d[2],b=h[f]){case"year":m+=12*e;break;case"month":m+=e;break;case"week":l+=7*e;break;case"day":l+=e;break;case"hour":case"minute":case"second":case"millisecond":k+=e*g[b];break;default:throw new Error("Missing or invalid unit, "+f)}return new j(k,l,m)},a.prototype.valueOf=function(){var a=this._internal,b=a.milliseconds,c=a.days,d=a.months;return b+g.month(d,c)},a.prototype.asMilliseconds=function(){return this.valueOf()},a.prototype.asSeconds=function(){return l(this.valueOf()/c)},a}();return a.Expiry=b,a.VERSION="0.1.7",a}); |
@@ -51,6 +51,38 @@ var | ||
createInternal = function (value, unit) { | ||
var | ||
milliseconds = 0, | ||
days = 0, | ||
months = 0; | ||
switch(unit) { | ||
case 'year': | ||
months += value * 12; | ||
break; | ||
case 'month': | ||
months += value; | ||
break; | ||
case 'week': | ||
days += value * 7; | ||
break; | ||
case 'day': | ||
days += value; | ||
break; | ||
case 'hour': | ||
case 'minute': | ||
case 'second': | ||
case 'millisecond': | ||
milliseconds += value * unitToMilliseconds[unit]; | ||
break; | ||
default: | ||
} | ||
return new Internal(milliseconds, days, months); | ||
}, | ||
round = Math.round; | ||
function Expiry(value) { | ||
function Expiry(value, maybeUnit) { | ||
@@ -60,2 +92,9 @@ value = value || 0; | ||
var | ||
unit = unitToUnit[maybeUnit || 'millisecond']; | ||
if ( ! unit) { | ||
throw new Error('Invalid unit, '+maybeUnit); | ||
} | ||
var | ||
type = typeOf(value), | ||
@@ -65,8 +104,7 @@ internal; | ||
if (type === 'Number') { | ||
internal = new Internal(value); | ||
internal = createInternal(value, unit); | ||
} else if (type === 'String') { | ||
internal = Expiry.parse(value); | ||
internal = Expiry.parse(value, unit); | ||
} else { | ||
throw new Error('Wrong type for argument 1, value. Expected '+value+ | ||
' to be a number, string, or expiry.'); | ||
throw new Error('Invalid value, '+value); | ||
} | ||
@@ -77,14 +115,20 @@ | ||
Expiry.forge = function (value) { | ||
return new this(value); | ||
Expiry.forge = function (value, maybeUnit) { | ||
return new this(value, maybeUnit); | ||
}; | ||
Expiry.parse = function (value) { | ||
Expiry.parse = function (value, unit) { | ||
if (typeOf(value) !== 'String') { | ||
throw new Error('Wrong type for argument 1, value. String expected.'); | ||
throw new Error('Invalid value, '+value); | ||
} | ||
var | ||
re = /([\d\.]+)\s*([a-zA-Z]+)/g, | ||
re = /^[\d\.]+$/g; | ||
if (re.test(value)) { | ||
value += unit; | ||
} | ||
var | ||
result, | ||
@@ -100,2 +144,4 @@ | ||
re = /([\d\.]+)\s*([a-zA-Z]+)/g; | ||
while ((result = re.exec(value)) !== null) { | ||
@@ -127,3 +173,3 @@ | ||
default: | ||
throw new Error('Unexpected unit '+maybeUnit); | ||
throw new Error('Missing or invalid unit, '+maybeUnit); | ||
} | ||
@@ -130,0 +176,0 @@ } |
{ | ||
"name": "expiry-js", | ||
"version": "0.1.6", | ||
"version": "0.1.7", | ||
"description": "A lightweight Javascript Universal Module for parsing durations", | ||
@@ -5,0 +5,0 @@ "main": "dist/expiry.js", |
@@ -33,4 +33,6 @@ # expiry-js | ||
second = expiry('1s'), // or '1 second', '1 seconds' | ||
milliseconds = expiry('1ms'); // or '1 millisecond', '1 milliseconds' | ||
milliseconds = expiry('1ms'), // or '1 millisecond', '1 milliseconds' | ||
altMilliseconds = expiry('1', 'ms'); | ||
console.log(day.valueOf()); // 86400000 | ||
@@ -43,4 +45,8 @@ console.log(day.asMilliseconds()); // 86400000 | ||
### 0.1.7 (2015-06-20) | ||
* Adds parsing of digits only strings. Thanks, [@BigFunger](https://github.com/BigFunger) | ||
### 0.1.6 (2015-06-18) | ||
* Fixes error parsing seconds. Thanks, [@BigFunger](https://github.com/BigFunger) |
@@ -40,6 +40,11 @@ /* eslint-env mocha */ | ||
f = function forge(s, o) { | ||
f = function forge(s, u, o) { | ||
if ( ! o) { | ||
o = u; | ||
u = void 0; | ||
} | ||
var | ||
millisecond = expiry(s).valueOf(); | ||
millisecond = expiry(s, u).valueOf(); | ||
@@ -59,2 +64,9 @@ expect(millisecond).to.equal(d(o)); | ||
expect(expiry(1).valueOf()).to.equal(1); | ||
expect(expiry(2, 'ms').valueOf()).to.equal(2); | ||
expect(expiry(2, 's').valueOf()).to.equal(2 * 1000); | ||
expect(expiry(2, 'm').valueOf()).to.equal(2 * 1000 * 60); | ||
expect(expiry(2, 'h').valueOf()).to.equal(2 * 1000 * 60 * 60); | ||
expect(expiry(2, 'D').valueOf()).to.equal(d({ day: 2})); | ||
expect(expiry(2, 'M').valueOf()).to.equal(d({ month: 2})); | ||
expect(expiry(2, 'Y').valueOf()).to.equal(d({ year: 2})); | ||
}); | ||
@@ -64,2 +76,3 @@ | ||
it('millisecond', function () { | ||
@@ -72,2 +85,3 @@ | ||
f('2 ms', o); | ||
f('2', 'ms', o); | ||
}); | ||
@@ -81,2 +95,3 @@ | ||
f('2s', o); | ||
f('2', 's', o); | ||
}); | ||
@@ -90,2 +105,3 @@ | ||
f('2m', o); | ||
f('2', 'm', o); | ||
}); | ||
@@ -99,2 +115,3 @@ | ||
f('2h', o); | ||
f('2', 'h', o); | ||
}); | ||
@@ -108,2 +125,3 @@ | ||
f('2D', o); | ||
f('2', 'D', o); | ||
}); | ||
@@ -117,2 +135,3 @@ | ||
f('1W', o); | ||
f('1', 'W', o); | ||
}); | ||
@@ -126,2 +145,3 @@ | ||
f('2M', o); | ||
f('2', 'M', o); | ||
}); | ||
@@ -135,2 +155,3 @@ | ||
f('2Y', o); | ||
f('2', 'Y', o); | ||
}); | ||
@@ -137,0 +158,0 @@ |
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
21856
525
51