Comparing version
@@ -12,135 +12,199 @@ | ||
(function() { | ||
var formattingTokens, getParseRegexForToken, isArray, isString, isValidFormatParam, isValidStringParam, parseTokenFourDigits, parseTokenOneOrTwoDigits, parseTokenOneToThreeDigits, parseTokenT, parseTokenThreeDigits, parseTokenTimezone, parseTokenWord, singleTest, test; | ||
var FormatJS; | ||
formattingTokens = /(\[[^\[]*\])|(\\)?(Mo|MM?M?M?|Do|DDDo|DD?D?D?|dddd?|do?|w[o|w]?|YYYY|YY|a|A|hh?|HH?|mm?|ss?|SS?S?|zz?|ZZ?|LT|LL?L?L?)/g; | ||
FormatJS = (function() { | ||
parseTokenOneOrTwoDigits = /\d\d?/; | ||
function FormatJS() { | ||
this._parseTokenWord = /^([0-9a-z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+)$/i; | ||
this._parseTokenNumber1_12 = /^([1-9]|1[0-2])$/; | ||
this._parseTokenNumber01_12 = /^(0[1-9]|1[0-2])$/; | ||
this._parseTokenNumber0_59 = /^([0-9]|[1-5][0-9])$/; | ||
this._parseTokenNumber00_59 = /^(0[0-9]|[1-5][0-9])$/; | ||
} | ||
parseTokenOneToThreeDigits = /\d{1,3}/; | ||
FormatJS.prototype._isArray = function(obj) { | ||
return Object.prototype.toString.call(obj) === '[object Array]'; | ||
}; | ||
parseTokenThreeDigits = /\d{3}/; | ||
FormatJS.prototype._isString = function(obj) { | ||
return Object.prototype.toString.call(obj) === '[object String]'; | ||
}; | ||
parseTokenFourDigits = /\d{4}/; | ||
FormatJS.prototype._isValidStringParam = function(string) { | ||
return string && this._isString(string); | ||
}; | ||
parseTokenWord = /[0-9a-z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+/i; | ||
FormatJS.prototype._isValidFormatParam = function(format) { | ||
if (this._isString(format)) { | ||
return this._isValidStringParam(format); | ||
} | ||
if (this._isArray(format)) { | ||
return format.length; | ||
} | ||
return false; | ||
}; | ||
parseTokenTimezone = /Z|[\+\-]\d\d:?\d\d/i; | ||
FormatJS.prototype._getParseRegexForToken = function(token) { | ||
switch (token) { | ||
case 'M': | ||
return this._parseTokenNumber1_12; | ||
case 'Mo': | ||
return this._parseTokenWord; | ||
case 'MM': | ||
return this._parseTokenNumber01_12; | ||
case 'MMM': | ||
return this._parseTokenWord; | ||
case 'MMMM': | ||
return this._parseTokenWord; | ||
case 'D': | ||
return /^([1-9]|[12][0-9]|3[01])$/; | ||
case 'Do': | ||
return this._parseTokenWord; | ||
case 'DD': | ||
return /^(0[1-9]|[12][0-9]|3[01])$/; | ||
case 'DDD': | ||
return /^([1-9]|[1-9][0-9]|[1-3][0-6][0-6])$/; | ||
case 'DDDo': | ||
return this._parseTokenWord; | ||
case 'DDDD': | ||
return /^(00[1-9]|0[1-9][0-9]|[1-3][0-6][0-6])$/; | ||
case 'd': | ||
return /^([0-6])$/; | ||
case 'do': | ||
return this._parseTokenWord; | ||
case 'ddd': | ||
return this._parseTokenWord; | ||
case 'dddd': | ||
return this._parseTokenWord; | ||
case 'w': | ||
return /^([1-9]|[1-4][0-9]|5[0-3])$/; | ||
case 'wo': | ||
return this._parseTokenWord; | ||
case 'ww': | ||
return /^(0[1-9]|[1-4][0-9]|5[0-3])$/; | ||
case 'YY': | ||
return /^(\d{2})$/; | ||
case 'YYYY': | ||
return /^(\d{4})$/; | ||
case 'H': | ||
return /^([0-9]|[12][0-3])$/; | ||
case 'HH': | ||
return /^(0[0-9]|[12][0-3])$/; | ||
case 'h': | ||
return this._parseTokenNumber1_12; | ||
case 'hh': | ||
return this._parseTokenNumber01_12; | ||
case 'm': | ||
return this._parseTokenNumber0_59; | ||
case 'mm': | ||
return this._parseTokenNumber00_59; | ||
case 's': | ||
return this._parseTokenNumber0_59; | ||
case 'ss': | ||
return this._parseTokenNumber00_59; | ||
default: | ||
return new RegExp(token.replace('\\', '')); | ||
} | ||
}; | ||
parseTokenT = /T/i; | ||
FormatJS.prototype._getTokens = function(format) { | ||
return format.match(/(\[[^\[]*\])|(\\)?(Mo|MM?M?M?|Do|DDDo|DD?D?D?|dddd?|do?|w[o|w]?|YYYY|YY|hh?|HH?|mm?|ss?)/g); | ||
}; | ||
getParseRegexForToken = function(token) { | ||
switch (token) { | ||
case 'DDDD': | ||
return parseTokenThreeDigits; | ||
case 'YYYY': | ||
return parseTokenFourDigits; | ||
case 'S': | ||
case 'SS': | ||
case 'SSS': | ||
case 'DDD': | ||
return parseTokenOneToThreeDigits; | ||
case 'MMM': | ||
case 'MMMM': | ||
case 'ddd': | ||
case 'dddd': | ||
case 'a': | ||
case 'A': | ||
return parseTokenWord; | ||
case 'Z': | ||
case 'ZZ': | ||
return parseTokenTimezone; | ||
case 'T': | ||
return parseTokenT; | ||
case 'MM': | ||
case 'DD': | ||
case 'dd': | ||
case 'YY': | ||
case 'HH': | ||
case 'hh': | ||
case 'mm': | ||
case 'ss': | ||
case 'M': | ||
case 'D': | ||
case 'd': | ||
case 'H': | ||
case 'h': | ||
case 'm': | ||
case 's': | ||
return parseTokenOneOrTwoDigits; | ||
default: | ||
return new RegExp(token.replace('\\', '')); | ||
} | ||
}; | ||
FormatJS.prototype._getTokenAffix = function(format, tokens) { | ||
var end, i, out, prefix, start, token, _i, _len; | ||
out = []; | ||
for (i = _i = 0, _len = tokens.length; _i < _len; i = ++_i) { | ||
token = tokens[i]; | ||
start = format.indexOf(token); | ||
end = start + token.length; | ||
prefix = start === 0 ? null : format.slice(0, start); | ||
out.push({ | ||
token: token, | ||
prefix: prefix | ||
}); | ||
format = format.slice(end, format.length); | ||
if (i !== 0) { | ||
out[i - 1]['suffix'] = prefix; | ||
} | ||
out[i]['suffix'] = format === '' ? null : i === tokens.length - 1 ? format : void 0; | ||
} | ||
return out; | ||
}; | ||
isArray = function(obj) { | ||
return toString.call(obj) === '[object Array]'; | ||
}; | ||
FormatJS.prototype._test = function(string, format) { | ||
var affix, affixes, i, testPrefix, testToken, _i, _len, | ||
_this = this; | ||
testPrefix = function(prefix) { | ||
var result; | ||
if (!prefix) { | ||
return true; | ||
} | ||
result = string.slice(0, prefix.length) === prefix; | ||
string = string.slice(prefix.length, string.length); | ||
return result; | ||
}; | ||
testToken = function(token, suffix, isLast) { | ||
var position, result, tokenStr; | ||
position = suffix ? string.indexOf(suffix) : string.length; | ||
tokenStr = string.slice(0, position); | ||
result = Boolean((tokenStr.match(_this._getParseRegexForToken(token)) || []).length); | ||
if (!result) { | ||
return false; | ||
} | ||
string = string.slice(position, string.length); | ||
if (isLast && string) { | ||
return string === (suffix || ''); | ||
} else { | ||
return true; | ||
} | ||
}; | ||
affixes = this._getTokenAffix(format, this._getTokens(format) || []); | ||
if (!affixes.length) { | ||
return string === format; | ||
} | ||
for (i = _i = 0, _len = affixes.length; _i < _len; i = ++_i) { | ||
affix = affixes[i]; | ||
if (!testPrefix(affix.prefix)) { | ||
return false; | ||
} | ||
if (!testToken(affix.token, affix.suffix, i === affixes.length - 1)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
}; | ||
isString = function(obj) { | ||
return toString.call(obj) === '[object String]'; | ||
}; | ||
isValidStringParam = function(string) { | ||
return string && isString(string); | ||
}; | ||
isValidFormatParam = function(format) { | ||
if (isString(format)) { | ||
return isValidStringParam(format); | ||
} | ||
if (isArray(format)) { | ||
return format.length; | ||
} | ||
return false; | ||
}; | ||
singleTest = function(string, format) { | ||
var parsedInput, token, _i, _len, _ref; | ||
_ref = format.match(formattingTokens); | ||
for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
token = _ref[_i]; | ||
parsedInput = (getParseRegexForToken(token).exec(string) || [])[0]; | ||
string = string.replace(getParseRegexForToken(token), ''); | ||
format = format.replace(token, ''); | ||
if (parsedInput === void 0) { | ||
FormatJS.prototype.test = function(string, format) { | ||
var formatItem, _i, _len; | ||
if (!(this._isValidStringParam(string) && this._isValidFormatParam(format))) { | ||
return false; | ||
} | ||
} | ||
return string === format; | ||
}; | ||
test = function(string, format) { | ||
var formatItem, _i, _len; | ||
if (!(isValidStringParam(string) && isValidFormatParam(format))) { | ||
if (this._isString(format)) { | ||
return this._test(string, format); | ||
} | ||
for (_i = 0, _len = format.length; _i < _len; _i++) { | ||
formatItem = format[_i]; | ||
if (this._test(string, formatItem)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
if (isString(format)) { | ||
return singleTest(string, format); | ||
} | ||
for (_i = 0, _len = format.length; _i < _len; _i++) { | ||
formatItem = format[_i]; | ||
if (singleTest(string, formatItem)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
}; | ||
}; | ||
return FormatJS; | ||
})(); | ||
if (typeof module !== 'undefined') { | ||
module.exports = { | ||
test: test | ||
}; | ||
module.exports = FormatJS; | ||
} | ||
if (typeof window !== 'undefined') { | ||
window.formatjs = { | ||
test: test | ||
}; | ||
window.FormatJS = FormatJS; | ||
} | ||
if (typeof define === "function" && define.amd) { | ||
define("formatjs", [], function() { | ||
return { | ||
test: test | ||
}; | ||
define("FormatJS", [], function() { | ||
return FormatJS; | ||
}); | ||
@@ -147,0 +211,0 @@ } |
@@ -1,4 +0,4 @@ | ||
/*! formatjs - v0.1.0 - 2012-06-30 | ||
/*! formatjs - v0.1.1 - 2012-07-04 | ||
* https://github.com/nowamasa/formatjs | ||
* Copyright (c) 2012 nowamasa; Licensed MIT */ | ||
(function(){var a,b,c,d,e,f,g,h,i,j,k,l,m,n,o;a=/(\[[^\[]*\])|(\\)?(Mo|MM?M?M?|Do|DDDo|DD?D?D?|dddd?|do?|w[o|w]?|YYYY|YY|a|A|hh?|HH?|mm?|ss?|SS?S?|zz?|ZZ?|LT|LL?L?L?)/g,h=/\d\d?/,i=/\d{1,3}/,k=/\d{3}/,g=/\d{4}/,m=/[0-9a-z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+/i,l=/Z|[\+\-]\d\d:?\d\d/i,j=/T/i,b=function(a){switch(a){case"DDDD":return k;case"YYYY":return g;case"S":case"SS":case"SSS":case"DDD":return i;case"MMM":case"MMMM":case"ddd":case"dddd":case"a":case"A":return m;case"Z":case"ZZ":return l;case"T":return j;case"MM":case"DD":case"dd":case"YY":case"HH":case"hh":case"mm":case"ss":case"M":case"D":case"d":case"H":case"h":case"m":case"s":return h;default:return new RegExp(a.replace("\\",""))}},c=function(a){return toString.call(a)==="[object Array]"},d=function(a){return toString.call(a)==="[object String]"},f=function(a){return a&&d(a)},e=function(a){return d(a)?f(a):c(a)?a.length:!1},n=function(c,d){var e,f,g,h,i;i=d.match(a);for(g=0,h=i.length;g<h;g++){f=i[g],e=(b(f).exec(c)||[])[0],c=c.replace(b(f),""),d=d.replace(f,"");if(e===void 0)return!1}return c===d},o=function(a,b){var c,g,h;if(!f(a)||!e(b))return!1;if(d(b))return n(a,b);for(g=0,h=b.length;g<h;g++){c=b[g];if(n(a,c))return!0}return!1},typeof module!="undefined"&&(module.exports={test:o}),typeof window!="undefined"&&(window.formatjs={test:o}),typeof define=="function"&&define.amd&&define("formatjs",[],function(){return{test:o}})}).call(this); | ||
(function(){var a;a=function(){function a(){this._parseTokenWord=/^([0-9a-z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+)$/i,this._parseTokenNumber1_12=/^([1-9]|1[0-2])$/,this._parseTokenNumber01_12=/^(0[1-9]|1[0-2])$/,this._parseTokenNumber0_59=/^([0-9]|[1-5][0-9])$/,this._parseTokenNumber00_59=/^(0[0-9]|[1-5][0-9])$/}return a.prototype._isArray=function(a){return Object.prototype.toString.call(a)==="[object Array]"},a.prototype._isString=function(a){return Object.prototype.toString.call(a)==="[object String]"},a.prototype._isValidStringParam=function(a){return a&&this._isString(a)},a.prototype._isValidFormatParam=function(a){return this._isString(a)?this._isValidStringParam(a):this._isArray(a)?a.length:!1},a.prototype._getParseRegexForToken=function(a){switch(a){case"M":return this._parseTokenNumber1_12;case"Mo":return this._parseTokenWord;case"MM":return this._parseTokenNumber01_12;case"MMM":return this._parseTokenWord;case"MMMM":return this._parseTokenWord;case"D":return/^([1-9]|[12][0-9]|3[01])$/;case"Do":return this._parseTokenWord;case"DD":return/^(0[1-9]|[12][0-9]|3[01])$/;case"DDD":return/^([1-9]|[1-9][0-9]|[1-3][0-6][0-6])$/;case"DDDo":return this._parseTokenWord;case"DDDD":return/^(00[1-9]|0[1-9][0-9]|[1-3][0-6][0-6])$/;case"d":return/^([0-6])$/;case"do":return this._parseTokenWord;case"ddd":return this._parseTokenWord;case"dddd":return this._parseTokenWord;case"w":return/^([1-9]|[1-4][0-9]|5[0-3])$/;case"wo":return this._parseTokenWord;case"ww":return/^(0[1-9]|[1-4][0-9]|5[0-3])$/;case"YY":return/^(\d{2})$/;case"YYYY":return/^(\d{4})$/;case"H":return/^([0-9]|[12][0-3])$/;case"HH":return/^(0[0-9]|[12][0-3])$/;case"h":return this._parseTokenNumber1_12;case"hh":return this._parseTokenNumber01_12;case"m":return this._parseTokenNumber0_59;case"mm":return this._parseTokenNumber00_59;case"s":return this._parseTokenNumber0_59;case"ss":return this._parseTokenNumber00_59;default:return new RegExp(a.replace("\\",""))}},a.prototype._getTokens=function(a){return a.match(/(\[[^\[]*\])|(\\)?(Mo|MM?M?M?|Do|DDDo|DD?D?D?|dddd?|do?|w[o|w]?|YYYY|YY|hh?|HH?|mm?|ss?)/g)},a.prototype._getTokenAffix=function(a,b){var c,d,e,f,g,h,i,j;e=[];for(d=i=0,j=b.length;i<j;d=++i)h=b[d],g=a.indexOf(h),c=g+h.length,f=g===0?null:a.slice(0,g),e.push({token:h,prefix:f}),a=a.slice(c,a.length),d!==0&&(e[d-1].suffix=f),e[d].suffix=a===""?null:d===b.length-1?a:void 0;return e},a.prototype._test=function(a,b){var c,d,e,f,g,h,i,j=this;f=function(b){var c;return b?(c=a.slice(0,b.length)===b,a=a.slice(b.length,a.length),c):!0},g=function(b,c,d){var e,f,g;return e=c?a.indexOf(c):a.length,g=a.slice(0,e),f=Boolean((g.match(j._getParseRegexForToken(b))||[]).length),f?(a=a.slice(e,a.length),d&&a?a===(c||""):!0):!1},d=this._getTokenAffix(b,this._getTokens(b)||[]);if(!d.length)return a===b;for(e=h=0,i=d.length;h<i;e=++h){c=d[e];if(!f(c.prefix))return!1;if(!g(c.token,c.suffix,e===d.length-1))return!1}return!0},a.prototype.test=function(a,b){var c,d,e;if(!this._isValidStringParam(a)||!this._isValidFormatParam(b))return!1;if(this._isString(b))return this._test(a,b);for(d=0,e=b.length;d<e;d++){c=b[d];if(this._test(a,c))return!0}return!1},a}(),typeof module!="undefined"&&(module.exports=a),typeof window!="undefined"&&(window.FormatJS=a),typeof define=="function"&&define.amd&&define("FormatJS",[],function(){return a})}).call(this); |
{ | ||
"name":"formatjs", | ||
"description":"JavaScript date format tester.", | ||
"version":"0.1.0", | ||
"version":"0.1.1", | ||
"homepage":"https://github.com/nowamasa/formatjs", | ||
@@ -35,3 +35,3 @@ "author":{ | ||
}, | ||
"keywords":["validator", "date formats"] | ||
"keywords":["validator", "dates", "testing", "format", "date formats"] | ||
} |
@@ -7,2 +7,51 @@ # formatjs | ||
Very [lightweight](https://raw.github.com/nowamasa/formatjs/master/dist/formatjs.min.js) (~2k min & gzip) utility function for testing if the string matches the date format. | ||
It has only one public function `test` which takes 2 parameters (`string` and `format`|`formats`) and returns `true` if string matches or `false` if it doesn't. | ||
See the example below: | ||
```javascript | ||
test('12/04', 'YYYY/MM'); // false, doesn't match | ||
test('2012/04', 'YYYY/MM'); // true, match | ||
``` | ||
You can pass multiple formats at once: | ||
```javascript | ||
test('12/04', ['YYYY/MM', 'YYYY/MM/DD']); // false, doesn't match | ||
test('2012/04', ['YYYY/MM', 'YYYY/MM/DD']); // true, match | ||
``` | ||
## Available format tokens | ||
<pre> | ||
Token | ||
Month M 1 2 ... 11 12 | ||
Mo 1st 2nd ... 11th 12th | ||
MM 01 02 ... 11 12 | ||
MMM Jan Feb ... Nov Dec | ||
MMMM January February ... November December | ||
Day of Month D 1 2 ... 30 30 | ||
Do 1st 2nd ... 30th 31st | ||
DD 01 02 ... 30 31 | ||
Day of Year DDD 1 2 ... 364 365 | ||
DDDo 1st 2nd ... 364th 365th | ||
DDDD 001 002 ... 364 365 | ||
Day of Week d 0 1 ... 5 6 | ||
do 0th 1st ... 5th 6th | ||
ddd Sun Mon ... Fri Sat | ||
dddd Sunday Monday ... Friday Saturday | ||
Week of Year w 1 2 ... 52 53 | ||
wo 1st 2nd ... 52nd 53rd | ||
ww 01 02 ... 52 53 | ||
Year YY 70 71 ... 29 30 | ||
YYYY 1970 1971 ... 2029 2030 | ||
Hour H 0 1 ... 22 23 | ||
HH 00 01 ... 22 23 | ||
h 1 2 ... 11 12 | ||
hh 01 02 ... 11 12 | ||
Minute m 0 1 ... 58 59 | ||
mm 00 01 ... 58 59 | ||
Second s 0 1 ... 58 59 | ||
ss 00 01 ... 58 59 | ||
</pre> | ||
## Getting Started | ||
@@ -13,5 +62,6 @@ ### On the server | ||
```javascript | ||
var formatjs = require('./formatjs'); | ||
formatjs.test('12/04', 'YYYY/MM'); // false | ||
formatjs.test('2012/04', 'YYYY/MM'); // true | ||
var FormatJS = require('formatjs'); | ||
var format = new FormatJS(); | ||
format.test('12/04', 'YYYY/MM'); // false | ||
format.test('2012/04', 'YYYY/MM'); // true | ||
``` | ||
@@ -30,4 +80,5 @@ | ||
<script> | ||
formatjs.test('12/04', 'YYYY/MM'); // false | ||
formatjs.test('2012/04', 'YYYY/MM'); // true | ||
var format = new FormatJS(); | ||
format.test('12/04', 'YYYY/MM'); // false | ||
format.test('2012/04', 'YYYY/MM'); // true | ||
</script> | ||
@@ -40,5 +91,6 @@ ``` | ||
<script> | ||
require(['formatjs'], function(formatjs) { | ||
formatjs.test('12/04', 'YYYY/MM'); // false | ||
formatjs.test('2012/04', 'YYYY/MM'); // true | ||
require(['FormatJS'], function(FormatJS) { | ||
var format = new FormatJS(); | ||
format.test('12/04', 'YYYY/MM'); // false | ||
format.test('2012/04', 'YYYY/MM'); // true | ||
}); | ||
@@ -49,4 +101,5 @@ </script> | ||
## Contributing | ||
Install [nodejs](http://nodejs.org/).<br> | ||
Install [coffee-script](http://coffeescript.org/) with: `npm install -g coffee-script`.<br> | ||
Install [Node.js](http://nodejs.org/).<br> | ||
Install [CoffeeScript](http://coffeescript.org/) with: `npm install -g coffee-script`.<br> | ||
Install [grunt](https://github.com/cowboy/grunt) with `npm install -g grunt`.<br> | ||
Install dependencies with: `npm install`.<br> | ||
@@ -53,0 +106,0 @@ |
@@ -1,35 +0,615 @@ | ||
/*global require:true */ | ||
var format = require('../dist/formatjs.js'); | ||
var FormatJS = require('../dist/formatjs'); | ||
var formatjs = new FormatJS(); | ||
/* | ||
======== A Handy Little Nodeunit Reference ======== | ||
https://github.com/caolan/nodeunit | ||
======== A Handy Little Nodeunit Reference ======== | ||
https://github.com/caolan/nodeunit | ||
Test methods: | ||
test.expect(numAssertions) | ||
test.done() | ||
Test assertions: | ||
test.ok(value, [message]) | ||
test.equal(actual, expected, [message]) | ||
test.notEqual(actual, expected, [message]) | ||
test.deepEqual(actual, expected, [message]) | ||
test.notDeepEqual(actual, expected, [message]) | ||
test.strictEqual(actual, expected, [message]) | ||
test.notStrictEqual(actual, expected, [message]) | ||
test.throws(block, [error], [message]) | ||
test.doesNotThrow(block, [error], [message]) | ||
test.ifError(value) | ||
*/ | ||
Test methods: | ||
test.expect(numAssertions) | ||
test.done() | ||
Test assertions: | ||
test.ok(value, [message]) | ||
test.equal(actual, expected, [message]) | ||
test.notEqual(actual, expected, [message]) | ||
test.deepEqual(actual, expected, [message]) | ||
test.notDeepEqual(actual, expected, [message]) | ||
test.strictEqual(actual, expected, [message]) | ||
test.notStrictEqual(actual, expected, [message]) | ||
test.throws(block, [error], [message]) | ||
test.doesNotThrow(block, [error], [message]) | ||
test.ifError(value) | ||
*/ | ||
exports['awesome'] = { | ||
setUp: function(done) { | ||
// setup here | ||
done(); | ||
}, | ||
'no args': function(test) { | ||
test.expect(1); | ||
// tests here | ||
test.equal('awesome', 'awesome', 'should be awesome.'); | ||
test.done(); | ||
} | ||
exports['utils'] = { | ||
'_isArray':function (test) { | ||
test.expect(11); | ||
test.equal(formatjs._isArray(), false); | ||
test.equal(formatjs._isArray(null), false); | ||
test.equal(formatjs._isArray(false), false); | ||
test.equal(formatjs._isArray({}), false); | ||
test.equal(formatjs._isArray(''), false); | ||
test.equal(formatjs._isArray(1), false); | ||
test.equal(formatjs._isArray('abc'), false); | ||
test.equal(formatjs._isArray([]), true); | ||
test.equal(formatjs._isArray(['a']), true); | ||
test.equal(formatjs._isArray(new Array()), true); | ||
test.equal(formatjs._isArray(new Array(3)), true); | ||
test.done(); | ||
}, | ||
'_isString':function (test) { | ||
test.expect(9); | ||
test.equal(formatjs._isString(), false); | ||
test.equal(formatjs._isString({}), false); | ||
test.equal(formatjs._isString(false), false); | ||
test.equal(formatjs._isString(1), false); | ||
test.equal(formatjs._isString([]), false); | ||
test.equal(formatjs._isString(['a']), false); | ||
test.equal(formatjs._isString(''), true); | ||
test.equal(formatjs._isString(' '), true); | ||
test.equal(formatjs._isString('a'), true); | ||
test.done(); | ||
}, | ||
'_isValidStringParam':function (test) { | ||
test.expect(8); | ||
test.equal(formatjs._isValidStringParam(1), false); | ||
test.equal(formatjs._isValidStringParam([]), false); | ||
test.equal(formatjs._isValidStringParam({}), false); | ||
test.equal(formatjs._isValidStringParam(''), false); | ||
test.equal(formatjs._isValidStringParam(true), false); | ||
test.equal(formatjs._isValidStringParam('abc'), true); | ||
test.equal(formatjs._isValidStringParam(' '), true); | ||
test.equal(formatjs._isValidStringParam('a b'), true); | ||
test.done(); | ||
}, | ||
'_isValidFormatParam':function (test) { | ||
test.expect(7); | ||
test.equal(formatjs._isValidFormatParam(1), false); | ||
test.equal(formatjs._isValidFormatParam({}), false); | ||
test.equal(formatjs._isValidFormatParam(false), false); | ||
test.equal(formatjs._isValidFormatParam(''), false); | ||
test.equal(formatjs._isValidFormatParam([]), false); | ||
test.equal(formatjs._isValidFormatParam([1]), true); | ||
test.equal(formatjs._isValidFormatParam('a'), true); | ||
test.done(); | ||
} | ||
}; | ||
exports['tokens'] = { | ||
'_getParseRegexForToken':function (test) { | ||
test.expect(29); | ||
test.strictEqual(formatjs._getParseRegexForToken('M').toString(), formatjs._parseTokenNumber1_12.toString()); | ||
test.strictEqual(formatjs._getParseRegexForToken('Mo').toString(), formatjs._parseTokenWord.toString()); | ||
test.strictEqual(formatjs._getParseRegexForToken('MM').toString(), formatjs._parseTokenNumber01_12.toString()); | ||
test.strictEqual(formatjs._getParseRegexForToken('MMM').toString(), formatjs._parseTokenWord.toString()); | ||
test.strictEqual(formatjs._getParseRegexForToken('MMMM').toString(), formatjs._parseTokenWord.toString()); | ||
test.strictEqual(formatjs._getParseRegexForToken('D').toString(), /^([1-9]|[12][0-9]|3[01])$/.toString()); | ||
test.strictEqual(formatjs._getParseRegexForToken('Do').toString(), formatjs._parseTokenWord.toString()); | ||
test.strictEqual(formatjs._getParseRegexForToken('DD').toString(), /^(0[1-9]|[12][0-9]|3[01])$/.toString()); | ||
test.strictEqual(formatjs._getParseRegexForToken('DDD').toString(), /^([1-9]|[1-9][0-9]|[1-3][0-6][0-6])$/.toString()); | ||
test.strictEqual(formatjs._getParseRegexForToken('DDDo').toString(), formatjs._parseTokenWord.toString()); | ||
test.strictEqual(formatjs._getParseRegexForToken('DDDD').toString(), /^(00[1-9]|0[1-9][0-9]|[1-3][0-6][0-6])$/.toString()); | ||
test.strictEqual(formatjs._getParseRegexForToken('d').toString(), /^([0-6])$/.toString()); | ||
test.strictEqual(formatjs._getParseRegexForToken('do').toString(), formatjs._parseTokenWord.toString()); | ||
test.strictEqual(formatjs._getParseRegexForToken('ddd').toString(), formatjs._parseTokenWord.toString()); | ||
test.strictEqual(formatjs._getParseRegexForToken('dddd').toString(), formatjs._parseTokenWord.toString()); | ||
test.strictEqual(formatjs._getParseRegexForToken('w').toString(), /^([1-9]|[1-4][0-9]|5[0-3])$/.toString()); | ||
test.strictEqual(formatjs._getParseRegexForToken('wo').toString(), formatjs._parseTokenWord.toString()); | ||
test.strictEqual(formatjs._getParseRegexForToken('ww').toString(), /^(0[1-9]|[1-4][0-9]|5[0-3])$/.toString()); | ||
test.strictEqual(formatjs._getParseRegexForToken('YY').toString(), /^(\d{2})$/.toString()); | ||
test.strictEqual(formatjs._getParseRegexForToken('YYYY').toString(), /^(\d{4})$/.toString()); | ||
test.strictEqual(formatjs._getParseRegexForToken('H').toString(), /^([0-9]|[12][0-3])$/.toString()); | ||
test.strictEqual(formatjs._getParseRegexForToken('HH').toString(), /^(0[0-9]|[12][0-3])$/.toString()); | ||
test.strictEqual(formatjs._getParseRegexForToken('h').toString(), formatjs._parseTokenNumber1_12.toString()); | ||
test.strictEqual(formatjs._getParseRegexForToken('hh').toString(), formatjs._parseTokenNumber01_12.toString()); | ||
test.strictEqual(formatjs._getParseRegexForToken('m').toString(), formatjs._parseTokenNumber0_59.toString()); | ||
test.strictEqual(formatjs._getParseRegexForToken('mm').toString(), formatjs._parseTokenNumber00_59.toString()); | ||
test.strictEqual(formatjs._getParseRegexForToken('s').toString(), formatjs._parseTokenNumber0_59.toString()); | ||
test.strictEqual(formatjs._getParseRegexForToken('ss').toString(), formatjs._parseTokenNumber00_59.toString()); | ||
test.strictEqual(formatjs._getParseRegexForToken('xxx').toString(), new RegExp('xxx'.replace('\\', '')).toString()); | ||
test.done(); | ||
}, | ||
'_getTokens':function (test) { | ||
test.expect(5); | ||
test.deepEqual(formatjs._getTokens(''), null); | ||
test.deepEqual(formatjs._getTokens('a'), null); | ||
test.deepEqual(formatjs._getTokens('YY YY YYYY'), ['YY','YY','YYYY']); | ||
test.deepEqual(formatjs._getTokens('YY DD MM'), ['YY','DD','MM']); | ||
test.deepEqual(formatjs._getTokens('dddd, MMMM Do YYYY, h:mm:ss a'), ['dddd','MMMM','Do','YYYY','h','mm','ss']); | ||
test.done(); | ||
}, | ||
'_getTokenAffix':function (test) { | ||
test.expect(3); | ||
test.deepEqual(formatjs._getTokenAffix('YY/DD/MM', ['YY','DD','MM']), [ | ||
{ token : 'YY', suffix : '/', prefix : null }, | ||
{ token : 'DD', suffix : '/', prefix : '/' }, | ||
{ token : 'MM', suffix : null, prefix : '/' } | ||
]); | ||
test.deepEqual(formatjs._getTokenAffix('*YY**DD***MM****', ['YY','DD','MM']), [ | ||
{ token : 'YY', suffix : '**', prefix : '*' }, | ||
{ token : 'DD', suffix : '***', prefix : '**' }, | ||
{ token : 'MM', suffix : '****', prefix : '***' } | ||
]); | ||
test.deepEqual(formatjs._getTokenAffix('*YY**DD***MM****', ['DD','MM']), [ | ||
{ token : 'DD', suffix : '***', prefix : '*YY**' }, | ||
{ token : 'MM', suffix : '****', prefix : '***' } | ||
]); | ||
test.done(); | ||
} | ||
}; | ||
exports['test'] = { | ||
'one format':function (test) { | ||
test.expect(11); | ||
test.equal(formatjs.test('2012-04', 'YYYY-MM-DD'), false); | ||
test.equal(formatjs.test('2012-04-12', 'YYYY-MM-DD'), true); | ||
test.equal(formatjs.test('2012&04', 'YYYY&MM-DD'), false); | ||
test.equal(formatjs.test('2012&04-12', 'YYYY&MM-DD'), true); | ||
test.equal(formatjs.test('12/04', 'YYYY/MM'), false); | ||
test.equal(formatjs.test('2012/04', 'YYYY/MM'), true); | ||
test.equal(formatjs.test('2012/04', 'YYYY/MM'), true); | ||
test.equal(formatjs.test('2012 04', 'YYYY/MM'), false); | ||
test.equal(formatjs.test('2012/04/1', 'YYYY/MM/D'), true); | ||
test.equal(formatjs.test('2012/04/01', 'YYYY/MM/D'), false); | ||
test.equal(formatjs.test('2012/04/32', 'YYYY/MM/DD'), false); | ||
test.done(); | ||
}, | ||
'many format':function (test) { | ||
test.expect(4); | ||
test.equal(formatjs.test('12/04', ['YYYY/MM', 'YY', 'Do']), false); | ||
test.equal(formatjs.test('12', ['YYYY/MM', 'YY', 'Do']), true); | ||
test.equal(formatjs.test('2012/04/1', ['YYYY/MM', 'YYYY/MM/D']), true); | ||
test.equal(formatjs.test('2012/04/01', ['YYYY/MM', 'YYYY/MM/D']), false); | ||
test.done(); | ||
}, | ||
'token xxx':function (test) { | ||
test.expect(4); | ||
test.equal(formatjs.test('abv', 'xx'), false); | ||
test.equal(formatjs.test('45', 'aaaaaaaa'), false); | ||
test.equal(formatjs.test('45', '123'), false); | ||
test.equal(formatjs.test('45', 'rrrrrrr'), false); | ||
test.done(); | ||
} | ||
}; | ||
// specific tokens | ||
exports['Month'] = { | ||
'token M':function (test) { | ||
test.expect(6); | ||
test.equal(formatjs.test('1', 'M'), true); | ||
test.equal(formatjs.test('6', 'M'), true); | ||
test.equal(formatjs.test('12', 'M'), true); | ||
test.equal(formatjs.test('a', 'M'), false); | ||
test.equal(formatjs.test('01', 'M'), false); | ||
test.equal(formatjs.test('13', 'M'), false); | ||
test.done(); | ||
}, | ||
'token Mo':function (test) { | ||
test.expect(6); | ||
test.equal(formatjs.test('1st', 'Mo'), true); | ||
test.equal(formatjs.test('6s', 'Mo'), true); | ||
test.equal(formatjs.test('12abc', 'Mo'), true); | ||
test.equal(formatjs.test('01', 'Mo'), true); | ||
test.equal(formatjs.test('01st', 'Mo'), true); | ||
test.equal(formatjs.test('13 st', 'Mo'), false); | ||
test.done(); | ||
}, | ||
'token MM':function (test) { | ||
test.expect(6); | ||
test.equal(formatjs.test('01', 'MM'), true); | ||
test.equal(formatjs.test('10', 'MM'), true); | ||
test.equal(formatjs.test('12', 'MM'), true); | ||
test.equal(formatjs.test('1', 'MM'), false); | ||
test.equal(formatjs.test('13', 'MM'), false); | ||
test.equal(formatjs.test('a', 'MM'), false); | ||
test.done(); | ||
}, | ||
'token MMM':function (test) { | ||
test.expect(6); | ||
test.equal(formatjs.test('Jan', 'MMM'), true); | ||
test.equal(formatjs.test('feb', 'MMM'), true); | ||
test.equal(formatjs.test('abc', 'MMM'), true); | ||
test.equal(formatjs.test('Jann', 'MMM'), true); | ||
test.equal(formatjs.test('123', 'MMM'), true); | ||
test.equal(formatjs.test('Ja gf', 'MMM'), false); | ||
test.done(); | ||
}, | ||
'token MMMM':function (test) { | ||
test.expect(6); | ||
test.equal(formatjs.test('Jan', 'MMMM'), true); | ||
test.equal(formatjs.test('feb', 'MMMM'), true); | ||
test.equal(formatjs.test('abcdef', 'MMMM'), true); | ||
test.equal(formatjs.test('123', 'MMMM'), true); | ||
test.equal(formatjs.test('dfg', 'MMMM'), true); | ||
test.equal(formatjs.test('abc def', 'MMMM'), false); | ||
test.done(); | ||
} | ||
}; | ||
exports['Day of Month'] = { | ||
'token D':function (test) { | ||
test.expect(9); | ||
test.equal(formatjs.test('1', 'D'), true); | ||
test.equal(formatjs.test('10', 'D'), true); | ||
test.equal(formatjs.test('20', 'D'), true); | ||
test.equal(formatjs.test('29', 'D'), true); | ||
test.equal(formatjs.test('30', 'D'), true); | ||
test.equal(formatjs.test('31', 'D'), true); | ||
test.equal(formatjs.test('0', 'D'), false); | ||
test.equal(formatjs.test('01', 'D'), false); | ||
test.equal(formatjs.test('32', 'D'), false); | ||
test.done(); | ||
}, | ||
'token Do':function (test) { | ||
test.expect(6); | ||
test.equal(formatjs.test('a', 'Do'), true); | ||
test.equal(formatjs.test('10', 'Do'), true); | ||
test.equal(formatjs.test('avc', 'Do'), true); | ||
test.equal(formatjs.test('av-c', 'Do'), false); | ||
test.equal(formatjs.test('234', 'Do'), true); | ||
test.equal(formatjs.test('an de', 'Do'), false); | ||
test.done(); | ||
}, | ||
'token DD':function (test) { | ||
test.expect(9); | ||
test.equal(formatjs.test('1', 'DD'), false); | ||
test.equal(formatjs.test('10', 'DD'), true); | ||
test.equal(formatjs.test('20', 'DD'), true); | ||
test.equal(formatjs.test('29', 'DD'), true); | ||
test.equal(formatjs.test('30', 'DD'), true); | ||
test.equal(formatjs.test('31', 'DD'), true); | ||
test.equal(formatjs.test('0', 'DD'), false); | ||
test.equal(formatjs.test('01', 'DD'), true); | ||
test.equal(formatjs.test('32', 'DD'), false); | ||
test.done(); | ||
} | ||
}; | ||
exports['Day of Year'] = { | ||
'token DDD':function (test) { | ||
test.expect(9); | ||
test.equal(formatjs.test('1', 'DDD'), true); | ||
test.equal(formatjs.test('99', 'DDD'), true); | ||
test.equal(formatjs.test('150', 'DDD'), true); | ||
test.equal(formatjs.test('300', 'DDD'), true); | ||
test.equal(formatjs.test('366', 'DDD'), true); | ||
test.equal(formatjs.test('367', 'DDD'), false); | ||
test.equal(formatjs.test('400', 'DDD'), false); | ||
test.equal(formatjs.test('400', 'DDD'), false); | ||
test.equal(formatjs.test('01', 'DDD'), false); | ||
test.done(); | ||
}, | ||
'token DDDo':function (test) { | ||
test.expect(4); | ||
test.equal(formatjs.test('1', 'DDDo'), true); | ||
test.equal(formatjs.test('12', 'DDDo'), true); | ||
test.equal(formatjs.test('av', 'DDDo'), true); | ||
test.equal(formatjs.test('adbf', 'DDDo'), true); | ||
test.done(); | ||
}, | ||
'token DDDD':function (test) { | ||
test.expect(6); | ||
test.equal(formatjs.test('001', 'DDDD'), true); | ||
test.equal(formatjs.test('099', 'DDDD'), true); | ||
test.equal(formatjs.test('300', 'DDDD'), true); | ||
test.equal(formatjs.test('366', 'DDDD'), true); | ||
test.equal(formatjs.test('367', 'DDDD'), false); | ||
test.equal(formatjs.test('7', 'DDDD'), false); | ||
test.done(); | ||
} | ||
}; | ||
exports['Day of Week'] = { | ||
'token d':function (test) { | ||
test.expect(4); | ||
test.equal(formatjs.test('0', 'd'), true); | ||
test.equal(formatjs.test('1', 'd'), true); | ||
test.equal(formatjs.test('6', 'd'), true); | ||
test.equal(formatjs.test('7', 'd'), false); | ||
test.done(); | ||
}, | ||
'token do':function (test) { | ||
test.expect(4); | ||
test.equal(formatjs.test('0', 'do'), true); | ||
test.equal(formatjs.test('acb', 'do'), true); | ||
test.equal(formatjs.test('Fri', 'do'), true); | ||
test.equal(formatjs.test('a', 'do'), true); | ||
test.done(); | ||
}, | ||
'token ddd':function (test) { | ||
test.expect(4); | ||
test.equal(formatjs.test('0', 'ddd'), true); | ||
test.equal(formatjs.test('acb', 'ddd'), true); | ||
test.equal(formatjs.test('Fri', 'ddd'), true); | ||
test.equal(formatjs.test('a', 'ddd'), true); | ||
test.done(); | ||
}, | ||
'token dddd':function (test) { | ||
test.expect(4); | ||
test.equal(formatjs.test('0', 'dddd'), true); | ||
test.equal(formatjs.test('acb', 'dddd'), true); | ||
test.equal(formatjs.test('Fri', 'dddd'), true); | ||
test.equal(formatjs.test('a', 'dddd'), true); | ||
test.done(); | ||
} | ||
}; | ||
exports['Week of Year'] = { | ||
'token w':function (test) { | ||
test.expect(6); | ||
test.equal(formatjs.test('0', 'w'), false); | ||
test.equal(formatjs.test('1', 'w'), true); | ||
test.equal(formatjs.test('01', 'w'), false); | ||
test.equal(formatjs.test('45', 'w'), true); | ||
test.equal(formatjs.test('53', 'w'), true); | ||
test.equal(formatjs.test('54', 'w'), false); | ||
test.done(); | ||
}, | ||
'token wo':function (test) { | ||
test.expect(4); | ||
test.equal(formatjs.test('0', 'wo'), true); | ||
test.equal(formatjs.test('avc', 'wo'), true); | ||
test.equal(formatjs.test('51rd', 'wo'), true); | ||
test.equal(formatjs.test('aaa', 'wo'), true); | ||
test.done(); | ||
}, | ||
'token ww':function (test) { | ||
test.expect(5); | ||
test.equal(formatjs.test('01', 'ww'), true); | ||
test.equal(formatjs.test('11', 'ww'), true); | ||
test.equal(formatjs.test('53', 'ww'), true); | ||
test.equal(formatjs.test('54', 'ww'), false); | ||
test.equal(formatjs.test('4', 'ww'), false); | ||
test.done(); | ||
} | ||
}; | ||
exports['Year'] = { | ||
'token YY':function (test) { | ||
test.expect(5); | ||
test.equal(formatjs.test('01', 'YY'), true); | ||
test.equal(formatjs.test('99', 'YY'), true); | ||
test.equal(formatjs.test('00', 'YY'), true); | ||
test.equal(formatjs.test('000', 'YY'), false); | ||
test.equal(formatjs.test('100', 'YY'), false); | ||
test.done(); | ||
}, | ||
'token YYYY':function (test) { | ||
test.expect(5); | ||
test.equal(formatjs.test('0100', 'YYYY'), true); | ||
test.equal(formatjs.test('2000', 'YYYY'), true); | ||
test.equal(formatjs.test('9999', 'YYYY'), true); | ||
test.equal(formatjs.test('10000', 'YYYY'), false); | ||
test.equal(formatjs.test('00000', 'YYYY'), false); | ||
test.done(); | ||
} | ||
}; | ||
exports['Hour'] = { | ||
'token H':function (test) { | ||
test.expect(5); | ||
test.equal(formatjs.test('0', 'H'), true); | ||
test.equal(formatjs.test('1', 'H'), true); | ||
test.equal(formatjs.test('23', 'H'), true); | ||
test.equal(formatjs.test('24', 'H'), false); | ||
test.equal(formatjs.test('01', 'H'), false); | ||
test.done(); | ||
}, | ||
'token HH':function (test) { | ||
test.expect(5); | ||
test.equal(formatjs.test('01', 'HH'), true); | ||
test.equal(formatjs.test('23', 'HH'), true); | ||
test.equal(formatjs.test('24', 'HH'), false); | ||
test.equal(formatjs.test('1', 'HH'), false); | ||
test.equal(formatjs.test('00', 'HH'), true); | ||
test.done(); | ||
}, | ||
'token h':function (test) { | ||
test.expect(6); | ||
test.equal(formatjs.test('1', 'h'), true); | ||
test.equal(formatjs.test('5', 'h'), true); | ||
test.equal(formatjs.test('12', 'h'), true); | ||
test.equal(formatjs.test('0', 'h'), false); | ||
test.equal(formatjs.test('13', 'h'), false); | ||
test.equal(formatjs.test('01', 'h'), false); | ||
test.done(); | ||
}, | ||
'token hh':function (test) { | ||
test.expect(6); | ||
test.equal(formatjs.test('01', 'hh'), true); | ||
test.equal(formatjs.test('11', 'hh'), true); | ||
test.equal(formatjs.test('12', 'hh'), true); | ||
test.equal(formatjs.test('1', 'hh'), false); | ||
test.equal(formatjs.test('00', 'hh'), false); | ||
test.equal(formatjs.test('13', 'hh'), false); | ||
test.done(); | ||
} | ||
}; | ||
exports['Minute'] = { | ||
'token m':function (test) { | ||
test.expect(6); | ||
test.equal(formatjs.test('0', 'm'), true); | ||
test.equal(formatjs.test('45', 'm'), true); | ||
test.equal(formatjs.test('59', 'm'), true); | ||
test.equal(formatjs.test('60', 'm'), false); | ||
test.equal(formatjs.test('01', 'm'), false); | ||
test.equal(formatjs.test('00', 'm'), false); | ||
test.done(); | ||
}, | ||
'token mm':function (test) { | ||
test.expect(7); | ||
test.equal(formatjs.test('00', 'mm'), true); | ||
test.equal(formatjs.test('01', 'mm'), true); | ||
test.equal(formatjs.test('45', 'mm'), true); | ||
test.equal(formatjs.test('59', 'mm'), true); | ||
test.equal(formatjs.test('60', 'mm'), false); | ||
test.equal(formatjs.test('0', 'mm'), false); | ||
test.equal(formatjs.test('1', 'mm'), false); | ||
test.done(); | ||
} | ||
}; | ||
exports['Second'] = { | ||
'token s':function (test) { | ||
test.expect(6); | ||
test.equal(formatjs.test('0', 's'), true); | ||
test.equal(formatjs.test('45', 's'), true); | ||
test.equal(formatjs.test('59', 's'), true); | ||
test.equal(formatjs.test('60', 's'), false); | ||
test.equal(formatjs.test('01', 's'), false); | ||
test.equal(formatjs.test('00', 's'), false); | ||
test.done(); | ||
}, | ||
'token ss':function (test) { | ||
test.expect(7); | ||
test.equal(formatjs.test('00', 'ss'), true); | ||
test.equal(formatjs.test('01', 'ss'), true); | ||
test.equal(formatjs.test('45', 'ss'), true); | ||
test.equal(formatjs.test('59', 'ss'), true); | ||
test.equal(formatjs.test('60', 'ss'), false); | ||
test.equal(formatjs.test('0', 'ss'), false); | ||
test.equal(formatjs.test('1', 'ss'), false); | ||
test.done(); | ||
} | ||
}; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
68726
98.92%721
217.62%109
94.64%1
Infinity%