This repository was archived by the owner on Dec 9, 2022. It is now read-only.
forked from Marak/javascript-fu
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjs-fu.js
More file actions
1861 lines (1670 loc) · 58.9 KB
/
js-fu.js
File metadata and controls
1861 lines (1670 loc) · 58.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*************** AUTOGENERATED @ 1275621939053 ***************
WARNING: THIS FILE WAS AUTOGENERATED BY THE JS-FU BUILD SCRIPT
MODIFYING THIS FILE IS FINE, BUT YOU REALLY SHOULD BE MODIFYING
THE LIBRARY DIRECTLY AND REGENERATING THIS FILE USING BUILD.js!!!!
Javascript-fu - Written by Marak Squires
*/
var fu = {};
fu.version = "0.0.1";
fu.isDefined = function ( objecty ){
if(typeof objecty == 'undefined'){
return false;
}
if(objecty == null || objecty == 'null'){
return false;
}
if(objecty.toString() == 'NaN'){
return false;
}
return true;
};
fu.isNull = function (obj) {
return obj === null || obj == 'null';
};
fu.isNumber = function ( obj ){
return (obj === +obj) || (toString.call(obj) === '[object Number]');
};
fu.isString = function ( stringy ){
return !!(stringy === '' || (stringy && stringy.charCodeAt && stringy.substr));
};
fu.isRegExp = function (obj){
return !!(obj && obj.test && obj.exec && (obj.ignoreCase || obj.ignoreCase === false));
};
fu.isText = function ( texty ){
if(!(this.isString(texty))) {
return false;
}
var textyLength = texty.length;
if(texty.replace(/[^\w\s\.\?\!\,\;\:\'\"]/g, "").length == textyLength){
return true;
}
return false;
};
fu.isDate = function ( datey ){
return !!(datey && datey.getTimezoneOffset && datey.setUTCFullYear);
};
fu.isArray = function (obj){
return !!(obj && obj.concat && obj.unshift && !obj.callee);
};
fu.isJSON = function (jsony){
try{JSON.parse(jsony);return true;}catch(err){return false;}
};
fu.isObject = function (objecty){
if(this.isFunction(objecty)) {
return false;
}
if(this.isArray(objecty)) {
return false;
}
return typeof objecty == 'object';
};
fu.isFunction = function (functiony){
return !!(functiony && functiony.constructor && functiony.call && functiony.apply);
};
fu.isEmpty = function (obj){
if (this.isString(obj)) return obj.length === 0;
if (this.isArray(obj)) return obj.length === 0;
for (var key in obj) {
if (hasOwnProperty.call(obj, key)){
return false;
}
};
return true;
};
fu.isNode = function (){
return !!(obj && obj.nodeType == 1);
};
fu.isBoolean = function (obj){
return obj === true || obj === false;
};
fu.isEqual = function (a , b){
// Perform a deep comparison to check if two objects are equal.
// Check object identity.
if (a === b) return true;
// Different types?
var atype = typeof(a), btype = typeof(b);
if (atype != btype) return false;
// Basic equality test (watch out for coercions).
if (a == b) return true;
// One is falsy and the other truthy.
if ((!a && b) || (a && !b)) return false;
// One of them implements an isEqual()?
//if (a.isEqual) return a.isEqual(b);
// Check dates' integer values.
if (this.isDate(a) && this.isDate(b)) return a.getTime() === b.getTime();
// Both are NaN?
if (this.isNaN(a) && this.isNaN(b)) return true;
// Compare regular expressions.
if (this.isRegExp(a) && this.isRegExp(b)) {
return a.source === b.source &&
a.global === b.global &&
a.ignoreCase === b.ignoreCase &&
a.multiline === b.multiline;
}
/**/
// If a is not an object by this point, we can't handle it.
if (a instanceof Object != true) return false;
// Check for different array lengths before comparing contents.
if (a.length && (a.length != b.length)) return false;
// Nothing else worked, deep compare the contents.
var aKeys = getFu.getKeys(a), bKeys = getFu.getKeys(b);
// Different object sizes?
if (aKeys.length != bKeys.length) return false;
// Recursive comparison of contents.
for (var key in a) {
if (!(key in b) || !this.isEqual(a[key], b[key]))
return false;
}
return true;
};
fu.example = isDefined( anything );;
fu.message = checks if anything is defined;
fu.code = function ( objecty ){
if(typeof objecty == 'undefined'){
return false;
}
if(objecty == null || objecty == 'null'){
return false;
}
if(objecty.toString() == 'NaN'){
return false;
}
return true;
};
fu.example = isDefined( anything );;
fu.message = checks if anything is defined;
fu.code = function ( objecty ){
if(typeof objecty == 'undefined'){
return false;
}
if(objecty == null || objecty == 'null'){
return false;
}
if(objecty.toString() == 'NaN'){
return false;
}
return true;
};
fu.example = isNumber( anything );;
fu.message = checks if anything is a number;
fu.code = function ( obj ){
return (obj === +obj) || (toString.call(obj) === '[object Number]');
};
fu.example = isNumber( anything );;
fu.message = checks if anything is a string;
fu.code = function ( stringy ){
return !!(stringy === '' || (stringy && stringy.charCodeAt && stringy.substr));
};
fu.example = isRegExp( anything );;
fu.message = checks if anything is a regular expression;
fu.code = function (obj){
return !!(obj && obj.test && obj.exec && (obj.ignoreCase || obj.ignoreCase === false));
};
fu.example = istext( anything );;
fu.message = checks if anything is text;
fu.code = function ( texty ){
if(!(this.isString(texty))) {
return false;
}
var textyLength = texty.length;
if(texty.replace(/[^\w\s\.\?\!\,\;\:\'\"]/g, "").length == textyLength){
return true;
}
return false;
};
fu.example = isDate( anything );;
fu.message = checks if anything is date;
fu.code = function ( datey ){
return !!(datey && datey.getTimezoneOffset && datey.setUTCFullYear);
};
fu.example = isArray( anything );;
fu.message = checks if anything is array;
fu.code = function (obj){
return !!(obj && obj.concat && obj.unshift && !obj.callee);
};
fu.example = isJSON( anything );;
fu.message = checks if anything is a JSON string;
fu.code = function (jsony){
try{JSON.parse(jsony);return true;}catch(err){return false;}
};
fu.example = isObject( anything );;
fu.message = checks if anything is an object;
fu.code = function (objecty){
if(this.isFunction(objecty)) {
return false;
}
if(this.isArray(objecty)) {
return false;
}
return typeof objecty == 'object';
};
fu.example = isEmpty( anything );;
fu.message = checks if anything is empty;
fu.code = function (functiony){
return !!(functiony && functiony.constructor && functiony.call && functiony.apply);
};
fu.toLink = function ( str ){
return str.replace(/(^|\s)((?:f|ht)tps?:\/\/[^\s]+)/g, replacement || '$1<a href="$2">$2</a>');
};
fu.toJSON = function ( str ){
return (JSON.stringify(str));
};
fu.toNumber = function (numbery){
// currently not using parseFloat, parseInt, or toFixed
var n = numbery;
n = n.toString();
n = n.replace( /\,/g, '' );
n = n.replace( /\$/g, '' ); // replace with format.currency.toCurrency call
var number = new Number(n);
if(number.toString() == 'NaN'){
// since we failed at getting a number, we can try to extract the digits out of the input
//number = fu.getNumbers(number);
return false;
}
else{
return n;
}
};
fu.compact = function (array) {
return _.filter(array, function(value){ return !!value; });
};
fu.flatten = function (array) {
return _.reduce(array, [], function(memo, value) {
if (_.isArray(value)) return memo.concat(_.flatten(value));
memo.push(value);
return memo;
});
};
fu.uniq = function (array, isSorted) {
return _.reduce(array, [], function(memo, el, i) {
if (0 == i || (isSorted === true ? _.last(memo) != el : !_.include(memo, el))) memo.push(el);
return memo;
});
};
fu.intersect = function (array) {
var rest = _.rest(arguments);
return _.filter(_.uniq(array), function(item) {
return _.every(rest, function(other) {
return _.indexOf(other, item) >= 0;
});
});
};
fu.lastIndexOf = function (array, item) {
if (nativeLastIndexOf && array.lastIndexOf === nativeLastIndexOf) return array.lastIndexOf(item);
var i = array.length;
while (i--) {
if(array[i] === item) {
return i;
}
}
return -1;
};
fu.range = function (start, stop, step) {
var a = _.toArray(arguments);
var solo = a.length <= 1;
var start = solo ? 0 : a[0], stop = solo ? a[0] : a[1], step = a[2] || 1;
var len = Math.ceil((stop - start) / step);
if (len <= 0) return [];
var range = new Array(len);
for (var i = start, idx = 0; true; i += step) {
if ((step > 0 ? i - stop : stop - i) >= 0) return range;
range[idx++] = i;
}
};
fu.toPercent = function (number){
// TODO: add more stripping and formatting logic
return number;
};
fu.toCamel = function (str) {
return exports.toTitle(str).replace(/[^\w]/, '');
};
fu.toDash = function (str) {
str = str.replace(/_/g, '-');
return str;
};
fu.toHuman = function (str) {
str = str.replace(/_id$/, "").replace(/_/, " ");
return str.charAt(0).toUpperCase() + str.slice(1);
};
fu.toOrdinal = function (str) {
str = str.toString();
var num = parseInt(str, 10),
mod100 = num % 100,
mod10 = num % 10;
switch(mod100){
case 11:
case 12:
case 13:
return str + "th";
}
switch(mod10){
case 1:
return str + "st";
case 2:
return str + "nd";
case 3:
return str + "rd";
}
return str + "th";
};
fu.toTitle = function (str) {
str = exports.toUnderscore(str);
str = exports.toHuman(str);
var parts = str.split(/\b('?[a-z])/);
str = '';
for (var i = 0; i < parts.length; i = i + 1) {
if ((i % 2) === 0) {
str = str + parts[i];
} else {
str = str + parts[i].charAt(0).toUpperCase() + parts[i].slice(1);
}
};
return str;
};
fu.toParam = function (str){
var separator = '-';
str = str.replace(/[^a-z0-9\-_]+/ig, separator);
if(separator.length){
str = str.replace(/-{2,}/g, separator);
str = str.replace(/^-|-$/ig, '');
}
return str.toLowerCase();
};
fu.toUnderscore = function (str) {
str = str.replace(/::/g, '/').replace(/([A-Z]+)([A-Z][a-z])/g, '$1_$2');
str = str.replace(/([a-z\d])([A-Z])/g, '$1_$2').replace(/-/g, "_");
str = str.toLowerCase();
return str;
};
fu.toPlural = function (str) {
for (var i = inflections.uncountables.length - 1; i >= 0; i--) {
if (str.match(inflections.uncountables[i])) return str;
};
var pairs = inflections.plurals,
pair = [];
//go from the end of the array to the front so the last pairs have priority
for (i = pairs.length - 1; i >= 0; i--) {
pair = pairs[i];
var result = str.replace(pair[0], pair[1]);
if (result === str) {
continue;
} else return result;
};
return str.replace(/([^s])$/i, '$1s');
};
fu.toSingle = function (str) {
for (var u = inflections.uncountables.length - 1; u >= 0; u--) {
if (str.match(inflections.uncountables[u])) return str;
};
var pairs = inflections.singulars,
pair = [];
//go from the end of the array to the front so the last pairs have priority
for (var i = pairs.length - 1; i >= 0; i--) {
pair = pairs[i];
var result = str.replace(pair[0], pair[1]);
if (result === str) {
continue;
} else return result;
};
return str.replace(/s$/i, '');
};
fu.toReverse = function ( object ){
if(isFu.isArray(object)) {
return object.reverse();
}
if(isFu.isString(object)) {
return object.split("").reverse().join("");
}
if(isFu.isNumber(object)) {
return this.toNumber(("" + object).split("").reverse().join(""));
}
};
fu.toWrap = function ( m, b, c ){
var i, j, l, s, r;
if(m < 1)
return this;
for(i = -1, l = (r = this.split("\n")).length; ++i < l; r[i] += s)
for(s = r[i], r[i] = ""; s.length > m; r[i] += s.slice(0, j) + ((s = s.slice(j)).length ? b : ""))
j = c == 2 || (j = s.slice(0, m + 1).match(/\S*(\s)?$/))[1] ? m : j.input.length - j[0].length
|| c == 1 && m || j.input.length + (j = s.slice(m).match(/^\S*/)).input.length;
return r.join("\n");
};
fu.toTrim = function ( str ){
return str;
};
fu._arrayShuffle = function (o){
for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
return o;
};
fu.toShuffle = function ( object ){
if(isFu.isArray(object)) {
return this._arrayShuffle(object);
}
if(isFu.isString(object)) {
return this._arrayShuffle(object.split("")).join("");
}
if(isFu.isNumber(object)) {
return this.toNumber(this._arrayShuffle(("" + object).split("")).join(""));
}
};
fu.toChain = function (){
//TODO: add chain >.<
};
fu.toMix = function (){
// TODO: write tests and better possibly better shallow copy. add option for deep copy
// copy reference to target object
var target = arguments[0] || {}, i = 1, length = arguments.length, deep = false, options, name, src, copy;
// Handle a deep copy situation
if ( isFu.isBoolean(target)) {
deep = target;
target = arguments[1] || {};
// skip the boolean and the target
i = 2;
}
// Handle case when target is a string or something (possible in deep copy)
if ( typeof target !== "object" && !isFu.isFunction(target) ) {
target = {};
}
for ( ; i < length; i++ ) {
// Only deal with non-null/undefined values
if ( (options = arguments[ i ]) != null ) {
// Extend the base object
for ( name in options ) {
src = target[ name ];
copy = options[ name ];
// Prevent never-ending loop
if ( target === copy ) {
continue;
}
// Recurse if we're merging object literal values or arrays
if ( deep && copy && ( isFu.isObject(copy) || isFu.isArray(copy) ) ) {
var clone = src && ( isFu.isObject(src) || isFu.isArray(src) ) ? src
: isFu.isArray(copy) ? [] : {};
// Never move original objects, clone them
target[ name ] = this.toMix( deep, clone, copy );
// Don't bring in undefined values
} else if ( copy !== undefined ) {
target[ name ] = copy;
}
}
}
}
// Return the modified object
return target;
};
fu.pad = function (val, len) {
val = String(val);
len = len || 2;
while (val.length < len) val = "0" + val;
return val;
};
fu.token = /d{1,4}|m{1,4}|yy(?:yy)?|([HhMsTt])\1?|[LloSZ]|"[^"]*"|'[^']*'/g;
fu.timezone = /\b(?:[PMCEA][SDP]T|(?:Pacific|Mountain|Central|Eastern|Atlantic) (?:Standard|Daylight|Prevailing) Time|(?:GMT|UTC)(?:[-+]\d{4})?)\b/g;
fu.timezoneClip = /[^-+\dA-Z]/g;
fu.dateFormat = function (date, mask, utc) {
var dF = fu.dateFormat;
// You can't provide utc if you skip other args (use the "UTC:" mask prefix)
if (arguments.length == 1 && Object.prototype.toString.call(date) == "[object String]" && !/\d/.test(date)) {
mask = date;
date = undefined;
}
// Passing date through Date applies Date.parse, if necessary
date = date ? new Date(date) : new Date;
if (isNaN(date)) throw SyntaxError("invalid date");
mask = String(fu.masks[mask] || mask || fu.masks["default"]);
// Allow setting the utc argument via the mask
if (mask.slice(0, 4) == "UTC:") {
mask = mask.slice(4);
utc = true;
}
var _ = utc ? "getUTC" : "get",
d = date[_ + "Date"](),
D = date[_ + "Day"](),
m = date[_ + "Month"](),
y = date[_ + "FullYear"](),
H = date[_ + "Hours"](),
M = date[_ + "Minutes"](),
s = date[_ + "Seconds"](),
L = date[_ + "Milliseconds"](),
o = utc ? 0 : date.getTimezoneOffset(),
flags = {
d: d,
dd: fu.pad(d),
ddd: fu.i18n().dayNames[D],
dddd: fu.i18n().dayNames[D + 7],
m: m + 1,
mm: fu.pad(m + 1),
mmm: fu.i18n().monthNames[m],
mmmm: fu.i18n().monthNames[m + 12],
yy: String(y).slice(2),
yyyy: y,
h: H % 12 || 12,
hh: fu.pad(H % 12 || 12),
H: H,
HH: fu.pad(H),
M: M,
MM: fu.pad(M),
s: s,
ss: fu.pad(s),
l: fu.pad(L, 3),
L: fu.pad(L > 99 ? Math.round(L / 10) : L),
t: H < 12 ? "a" : "p",
tt: H < 12 ? "am" : "pm",
T: H < 12 ? "A" : "P",
TT: H < 12 ? "AM" : "PM",
Z: utc ? "UTC" : (String(date).match(fu.timezone) || [""]).pop().replace(fu.timezoneClip, ""),
o: (o > 0 ? "-" : "+") + fu.pad(Math.floor(Math.abs(o) / 60) * 100 + Math.abs(o) % 60, 4),
S: ["th", "st", "nd", "rd"][d % 10 > 3 ? 0 : (d % 100 - d % 10 != 10) * d % 10]
};
return mask.replace(fu.token, function ($0) {
return $0 in flags ? flags[$0] : $0.slice(1, $0.length - 1);
});
};
fu.masks = function (){return{
"default": "ddd mmm dd yyyy HH:MM:ss",
shortDate: "m/d/yy",
mediumDate: "mmm d, yyyy",
longDate: "mmmm d, yyyy",
fullDate: "dddd, mmmm d, yyyy",
shortTime: "h:MM TT",
mediumTime: "h:MM:ss TT",
longTime: "h:MM:ss TT Z",
isoDate: "yyyy-mm-dd",
isoTime: "HH:MM:ss",
isoDateTime: "yyyy-mm-dd'T'HH:MM:ss",
isoUtcDateTime: "UTC:yyyy-mm-dd'T'HH:MM:ss'Z'"
};
};
fu.cultureinfo = function (){
return{
/* Culture Name */
name: "en-US",
englishName: "English (United States)",
nativeName: "English (United States)",
/* Day Name Strings */
dayNames: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
abbreviatedDayNames: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
shortestDayNames: ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"],
firstLetterDayNames: ["S", "M", "T", "W", "T", "F", "S"],
/* Month Name Strings */
monthNames: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
abbreviatedMonthNames: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
/* AM/PM Designators */
amDesignator: "AM",
pmDesignator: "PM",
firstDayOfWeek: 0,
twoDigitYearMax: 2029,
/**
* The dateElementOrder is based on the order of the
* format specifiers in the formatPatterns.DatePattern.
*
* Example:
<pre>
shortDatePattern dateElementOrder
------------------ ----------------
"M/d/yyyy" "mdy"
"dd/MM/yyyy" "dmy"
"yyyy-MM-dd" "ymd"
</pre>
*
* The correct dateElementOrder is required by the parser to
* determine the expected order of the date elements in the
* string being parsed.
*/
dateElementOrder: "mdy",
/* Standard date and time format patterns */
formatPatterns: {
shortDate: "M/d/yyyy",
longDate: "dddd, MMMM dd, yyyy",
shortTime: "h:mm tt",
longTime: "h:mm:ss tt",
fullDateTime: "dddd, MMMM dd, yyyy h:mm:ss tt",
sortableDateTime: "yyyy-MM-ddTHH:mm:ss",
universalSortableDateTime: "yyyy-MM-dd HH:mm:ssZ",
rfc1123: "ddd, dd MMM yyyy HH:mm:ss GMT",
monthDay: "MMMM dd",
yearMonth: "MMMM, yyyy"
},
/**
* NOTE: If a string format is not parsing correctly, but
* you would expect it parse, the problem likely lies below.
*
* The following regex patterns control most of the string matching
* within the parser.
*
* The Month name and Day name patterns were automatically generated
* and in general should be (mostly) correct.
*
* Beyond the month and day name patterns are natural language strings.
* Example: "next", "today", "months"
*
* These natural language string may NOT be correct for this culture.
* If they are not correct, please translate and edit this file
* providing the correct regular expression pattern.
*
* If you modify this file, please post your revised CultureInfo file
* to the Datejs Forum located at http://www.datejs.com/forums/.
*
* Please mark the subject of the post with [CultureInfo]. Example:
* Subject: [CultureInfo] Translated "da-DK" Danish(Denmark)
*
* We will add the modified patterns to the master source files.
*
* As well, please review the list of "Future Strings" section below.
*/
regexPatterns: {
jan: /^jan(uary)?/i,
feb: /^feb(ruary)?/i,
mar: /^mar(ch)?/i,
apr: /^apr(il)?/i,
may: /^may/i,
jun: /^jun(e)?/i,
jul: /^jul(y)?/i,
aug: /^aug(ust)?/i,
sep: /^sep(t(ember)?)?/i,
oct: /^oct(ober)?/i,
nov: /^nov(ember)?/i,
dec: /^dec(ember)?/i,
sun: /^su(n(day)?)?/i,
mon: /^mo(n(day)?)?/i,
tue: /^tu(e(s(day)?)?)?/i,
wed: /^we(d(nesday)?)?/i,
thu: /^th(u(r(s(day)?)?)?)?/i,
fri: /^fr(i(day)?)?/i,
sat: /^sa(t(urday)?)?/i,
future: /^next/i,
past: /^last|past|prev(ious)?/i,
add: /^(\+|aft(er)?|from|hence)/i,
subtract: /^(\-|bef(ore)?|ago)/i,
yesterday: /^yes(terday)?/i,
today: /^t(od(ay)?)?/i,
tomorrow: /^tom(orrow)?/i,
now: /^n(ow)?/i,
millisecond: /^ms|milli(second)?s?/i,
second: /^sec(ond)?s?/i,
minute: /^mn|min(ute)?s?/i,
hour: /^h(our)?s?/i,
week: /^w(eek)?s?/i,
month: /^m(onth)?s?/i,
day: /^d(ay)?s?/i,
year: /^y(ear)?s?/i,
shortMeridian: /^(a|p)/i,
longMeridian: /^(a\.?m?\.?|p\.?m?\.?)/i,
timezone: /^((e(s|d)t|c(s|d)t|m(s|d)t|p(s|d)t)|((gmt)?\s*(\+|\-)\s*\d\d\d\d?)|gmt|utc)/i,
ordinalSuffix: /^\s*(st|nd|rd|th)/i,
timeContext: /^\s*(\:|a(?!u|p)|p)/i
},
timezones: [{name:"UTC", offset:"-000"}, {name:"GMT", offset:"-000"}, {name:"EST", offset:"-0500"}, {name:"EDT", offset:"-0400"}, {name:"CST", offset:"-0600"}, {name:"CDT", offset:"-0500"}, {name:"MST", offset:"-0700"}, {name:"MDT", offset:"-0600"}, {name:"PST", offset:"-0800"}, {name:"PDT", offset:"-0700"}]
}
};
fu.parseLibrary = function () {
/**
* @version: 1.0 Alpha-1
* @author: Coolite Inc. http://www.coolite.com/
* @date: 2008-04-13
* @copyright: Copyright (c) 2006-2008, Coolite Inc. (http://www.coolite.com/). All rights reserved.
* @license: Licensed under The MIT License. See license.txt and http://www.datejs.com/license/.
* @website: http://www.datejs.com/
*/
Date.Parsing = {
Exception: function (s) {
this.message = "Parse error at '" + s.substring(0, 10) + " ...'";
}
};
var $P = Date.Parsing;
var _ = $P.Operators = {
//
// Tokenizers
//
rtoken: function (r) { // regex token
return function (s) {
var mx = s.match(r);
if (mx) {
return ([ mx[0], s.substring(mx[0].length) ]);
} else {
throw new $P.Exception(s);
}
};
},
token: function (s) { // whitespace-eating token
return function (s) {
return _.rtoken(new RegExp("^\s*" + s + "\s*"))(s);
// Removed .strip()
// return _.rtoken(new RegExp("^\s*" + s + "\s*"))(s).strip();
};
},
stoken: function (s) { // string token
return _.rtoken(new RegExp("^" + s));
},
//
// Atomic Operators
//
until: function (p) {
return function (s) {
var qx = [], rx = null;
while (s.length) {
try {
rx = p.call(this, s);
} catch (e) {
qx.push(rx[0]);
s = rx[1];
continue;
}
break;
}
return [ qx, s ];
};
},
many: function (p) {
return function (s) {
var rx = [], r = null;
while (s.length) {
try {
r = p.call(this, s);
} catch (e) {
return [ rx, s ];
}
rx.push(r[0]);
s = r[1];
}
return [ rx, s ];
};
},
// generator operators -- see below
optional: function (p) {
return function (s) {
var r = null;
try {
r = p.call(this, s);
} catch (e) {
return [ null, s ];
}
return [ r[0], r[1] ];
};
},
not: function (p) {
return function (s) {
try {
p.call(this, s);
} catch (e) {
return [null, s];
}
throw new $P.Exception(s);
};
},
ignore: function (p) {
return p ?
function (s) {
var r = null;
r = p.call(this, s);
return [null, r[1]];
} : null;
},
product: function () {
var px = arguments[0],
qx = Array.prototype.slice.call(arguments, 1), rx = [];
for (var i = 0 ; i < px.length ; i++) {
rx.push(_.each(px[i], qx));
}
return rx;
},
cache: function (rule) {
var cache = {}, r = null;
return function (s) {
try {
r = cache[s] = (cache[s] || rule.call(this, s));
} catch (e) {
r = cache[s] = e;
}
if (r instanceof $P.Exception) {
throw r;
} else {
return r;
}
};
},
// vector operators -- see below
any: function () {
var px = arguments;
return function (s) {
var r = null;
for (var i = 0; i < px.length; i++) {
if (px[i] == null) {
continue;
}
try {
r = (px[i].call(this, s));
} catch (e) {
r = null;
}
if (r) {
return r;
}
}
throw new $P.Exception(s);
};
},
each: function () {
var px = arguments;
return function (s) {
var rx = [], r = null;
for (var i = 0; i < px.length ; i++) {
if (px[i] == null) {
continue;
}
try {
r = (px[i].call(this, s));
} catch (e) {
throw new $P.Exception(s);
}
rx.push(r[0]);
s = r[1];
}
return [ rx, s];
};
},
all: function () {
var px = arguments, _ = _;
return _.each(_.optional(px));
},
// delimited operators
sequence: function (px, d, c) {
d = d || _.rtoken(/^\s*/);
c = c || null;
if (px.length == 1) {
return px[0];
}
return function (s) {
var r = null, q = null;
var rx = [];
for (var i = 0; i < px.length ; i++) {
try {
r = px[i].call(this, s);
} catch (e) {
break;
}
rx.push(r[0]);
try {
q = d.call(this, r[1]);
} catch (ex) {
q = null;
break;
}
s = q[1];
}
if (!r) {
throw new $P.Exception(s);
}
if (q) {
throw new $P.Exception(q[1]);
}
if (c) {
try {
r = c.call(this, r[1]);
} catch (ey) {
throw new $P.Exception(r[1]);
}
}
return [ rx, (r?r[1]:s) ];
};
},
//
// Composite Operators
//
between: function (d1, p, d2) {
d2 = d2 || d1;
var _fn = _.each(_.ignore(d1), p, _.ignore(d2));
return function (s) {
var rx = _fn.call(this, s);
return [[rx[0][0], r[0][2]], rx[1]];
};
},
list: function (p, d, c) {
d = d || _.rtoken(/^\s*/);
c = c || null;
return (p instanceof Array ?
_.each(_.product(p.slice(0, -1), _.ignore(d)), p.slice(-1), _.ignore(c)) :
_.each(_.many(_.each(p, _.ignore(d))), px, _.ignore(c)));
},
set: function (px, d, c) {
d = d || _.rtoken(/^\s*/);
c = c || null;
return function (s) {
// r is the current match, best the current 'best' match
// which means it parsed the most amount of input
var r = null, p = null, q = null, rx = null, best = [[], s], last = false;
// go through the rules in the given set
for (var i = 0; i < px.length ; i++) {
// last is a flag indicating whether this must be the last element
// if there is only 1 element, then it MUST be the last one
q = null;
p = null;
r = null;
last = (px.length == 1);
// first, we try simply to match the current pattern
// if not, try the next pattern
try {
r = px[i].call(this, s);
} catch (e) {
continue;
}
// since we are matching against a set of elements, the first
// thing to do is to add r[0] to matched elements
rx = [[r[0]], r[1]];
// if we matched and there is still input to parse and
// we don't already know this is the last element,
// we're going to next check for the delimiter ...
// if there's none, or if there's no input left to parse
// than this must be the last element after all ...
if (r[1].length > 0 && ! last) {
try {
q = d.call(this, r[1]);
} catch (ex) {
last = true;
}
} else {
last = true;
}
// if we parsed the delimiter and now there's no more input,
// that means we shouldn't have parsed the delimiter at all