-
Notifications
You must be signed in to change notification settings - Fork 1
/
lens.js
1065 lines (901 loc) · 30.8 KB
/
lens.js
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
var Lens = {};
;(function () {
var nil = function () { };
if (!console) {
console = { log: nil };
}
var log = {
debug: nil,
info: nil,
warn: nil,
error: nil,
level: function (level) {
log.debug = nil;
log.info = nil;
log.warn = nil;
log.error = nil;
switch (level) {
case 'debug': log.debug = console.log;
case 'info': log.info = console.log;
case 'warn': log.warn = console.log;
case 'error': log.error = console.log;
}
}
};
Lens.log = log;
})()
;(function () {
const SET_ATTRIBUTE = 1,
REMOVE_ATTRIBUTE = 2,
SET_PROPERTY = 3,
REMOVE_PROPERTY = 4,
REPLACE_NODE = 5,
APPEND_CHILD = 6,
REMOVE_CHILD = 7,
REPLACE_TEXT = 8;
/* diff the ATTRIBUTES of two nodes, and return a
(potentially empty) patch op list. */
var diffa = function (a, b) {
const ops = [];
const _a = {};
const _b = {};
for (let i = 0; i < a.attributes.length; i++) {
_a[a.attributes[i].nodeName] = a.attributes[i].nodeValue;
}
for (let i = 0; i < b.attributes.length; i++) {
_b[b.attributes[i].nodeName] = b.attributes[i].nodeValue;
}
/* if the attribute is only defined in (a), then
it has been removed in (b) and should be patched
as a REMOVE_ATTRIBUTE. */
for (let attr in _a) {
if (!(attr in _b)) {
ops.push({
op: REMOVE_ATTRIBUTE,
node: a,
key: attr,
});
}
}
/* if the attribute is only defined in (b), or is
defined in both with different values, patch as
a SET_ATTRIBUTE to get the correct value. */
for (let attr in _b) {
if (!(attr in _a) || _a[attr] !== _b[attr]) {
ops.push({
op: SET_ATTRIBUTE,
node: a,
key: attr,
value: _b[attr]
});
}
}
return ops;
};
var diffe = function (a, b) {
if (a.localName === b.localName) {
return diffa(a, b);
}
return null;
};
var difft = function (a, b) {
if (a.textContent === b.textContent) {
return [];
}
return [{
op: REPLACE_TEXT,
node: a,
with: b.textContent
}];
};
/* diff two NODEs, without recursing through child nodes */
var diffn1 = function (a, b) {
if (a.nodeType != b.nodeType) {
/* nothing in common */
return null;
}
if (a.nodeType == Node.ELEMENT_NODE) {
return diffe(a, b);
}
if (a.nodeType == Node.TEXT_NODE) {
return difft(a, b);
}
if (a.nodeType == Node.COMMENT_NODE) {
return null;
}
console.log('unrecognized a type %s', a.nodeType);
return null;
};
/* diff two NODEs, co-recursively with diff() */
var diffn = function (a, b) {
let ops = diffn1(a, b);
if (ops) {
return ops.concat(diff(a, b));
}
return [{
op: REPLACE_NODE,
node: a,
with: b
}];
};
window.diff = function (a, b) {
let ops = [];
const { childNodes: _a } = a;
const { childNodes: _b } = b;
const _al = _a ? _a.length : 0;
const _bl = _b ? _b.length : 0;
for (let i = 0; i < _bl; i++) {
if (!_a[i]) {
ops.push({
op: APPEND_CHILD,
node: a,
child: _b[i]
});
continue;
}
ops = ops.concat(diffn(_a[i], _b[i]));
}
for (var i = _bl; i < _al; i++) {
ops.push({
op: REMOVE_CHILD,
node: a,
child: _a[i]
});
}
return ops;
};
window.patch = function (e, ops) {
for (let i = 0; i < ops.length; i++) {
switch (ops[i].op) {
case SET_ATTRIBUTE: ops[i].node.setAttribute(ops[i].key, ops[i].value); break;
case REMOVE_ATTRIBUTE: ops[i].node.removeAttribute(ops[i].key); break;
case SET_PROPERTY: /* FIXME needs implemented! */ break;
case REMOVE_PROPERTY: /* FIXME needs implemented! */ break;
case REPLACE_NODE: ops[i].node.parentNode.replaceChild(ops[i].with, ops[i].node); break;
case APPEND_CHILD: ops[i].node.appendChild(ops[i].child); break;
case REMOVE_CHILD: ops[i].node.removeChild(ops[i].child); break;
case REPLACE_TEXT: ops[i].node.textContent = ops[i].with; break;
default:
console.log('unrecognized patch op %d for ', ops[i].op, ops[i]);
break;
}
}
};
window.explainPatch = function (ops) {
var l = [];
for (let i = 0; i < ops.length; i++) {
switch (ops[i].op) {
case SET_ATTRIBUTE: l.push(['SET_ATTRIBUTE', ops[i].node, ops[i].key+'='+ops[i].value]); break;
case REMOVE_ATTRIBUTE: l.push(['REMOVE_ATTRIBUTE', ops[i].node, ops[i].key]); break;
case SET_PROPERTY: l.push(['SET_PROPERTY', 'FIXME']); break;
case REMOVE_PROPERTY: l.push(['REMOVE_PROPERTY', 'FIXME']); break;
case REPLACE_NODE: l.push(['REPLACE_NODE', ops[i].node, { with: ops[i].with }]); break;
case APPEND_CHILD: l.push(['APPEND_CHILD', ops[i].node, { child: ops[i].child }]); break;
case REMOVE_CHILD: l.push(['REMOVE_CHILD', ops[i].node, { child: ops[i].child }]); break;
case REPLACE_TEXT: l.push(['REPLACE_TEXT', ops[i].node, { with: ops[i].with }]); break;
default: l.push(['**UNKNOWN**', ops[i]]); break;
}
}
return l;
};
})(window, document);
;(function () {
var __templates = {};
var template = function (name, data) {
if (!(name in __templates)) {
Lens.log.debug('template {%s} not found in the cache; compiling from source.', name);
__templates[name] = compile(name);
}
return __templates[name](data || {});
};
var parse = function (src) {
var tokenizer = new RegExp('([\\s\\S]*?)\\[\\[([\\s\\S]*?)\\]\\]([\\s\\S]*)');
var str = function (s) {
if (!s) { return "''"; }
return "'"+s.replace(/(['\\])/g, '\\$1').replace(/\n/g, "\\n")+"'";
};
var code = [];
for (;;) {
var tokens = tokenizer.exec(src)
if (!tokens) {
code.push('__ += '+str(src)+';');
break;
}
if (tokens[2][0] == ':') { /* trim preceeding literal */
tokens[1] = tokens[1].replace(/\s+$/, '');
tokens[2] = tokens[2].substr(1);
}
if (tokens[2][tokens[2].length - 1] == ':') { /* trim following literal */
tokens[3] = tokens[3].replace(/^\s+/, '');
tokens[2] = tokens[2].substr(0, tokens[2].length-2);
}
code.push('__ += '+str(tokens[1])+';');
if (tokens[2][0] == '=') {
code.push('__ += ('+tokens[2].replace(/^=\s*/, '')+');');
} else if (tokens[2][0] != '#') { /* skip comments */
code.push(tokens[2]);
}
src = tokens[3];
}
return code.join('');
};
var compile = function (name) {
name = name.toString();
var script = document.getElementById('template:'+name);
if (!script) {
Lens.log.error('unable to find a <script> element with id="template:%s"', name);
return function () {
throw "Template {"+name+"} not found";
};
}
var code = parse(script.innerHTML);
return function (_) {
/* the output variable */
var __ = '';
/* namespaced helper functions */
var lens = {
/* maybe(x,fallback)
fallback to a default value if a given variable
is undefined, or was not provided.
example:
[[= lens.maybe(x, "no x given") ]]
*/
maybe: function (a, b) {
return typeof(a) !== 'undefined' ? a : b;
},
/* escapeHTML(x)
return a sanitized version of x, with the dangerous HTML
entities like <, > and & replaced. Also replaces double
quote (") with the " representation, so that you can
embed values in form element attributes.
example:
<input type="text" name="display"
value="[[= lens.htmlEscape(_.display) ]]">
lens.h() is an alias, so you can also do this:
<input type="text" name="display"
value="[[= lens.h(_.display) ]]">
*/
escapeHTML: function (s) {
var t = document.createElement('textarea');
t.innerText = s;
return t.innerHTML.replace(/"/g, '"');
},
/* include(template)
include(template, _.other.data)
splices the output of another template into the current
output, at the calling site. This can be useful for
breaking up common elements of a UI into more manageable
chunks.
example:
<div id="login">[[ lens.include('signin'); ]]</div>
you can also provide a data object that will become the
`_` variable inside the called template:
[[ lens.include('alert', { alert: "something broke" }); ]]
as a "language construct", this function is also aliased
as (toplevel) `include()`, and it can be used in [[= ]]
constructs:
[[= include('other-template') ]]
*/
include: function (name, data) {
__ += template(name, data || _);
return '';
}
};
/* aliases ... */
lens.u = encodeURIComponent;
lens.h = lens.escapeHTML;
var include = lens.include;
Lens.log.debug('evaluating the {%s} template', name);
eval(code);
return __;
};
};
window.parseTemplate = parse;
if (typeof(jQuery) !== 'undefined') {
jQuery.template = template;
jQuery.fn.template = function (name, data, force) {
if (force || this.length == 0) {
this.html(template(name, data)).data('lens:template', {
template: name,
data: data
});
return this;
}
var was = this.data('lens:template');
if (was && (!name || was.template == name)) {
data = typeof(data) === 'undefined' ? was.data : data;
window.patch(this[0], diff(this[0], $('<div>'+template(was.template, data)+'</div>')[0]));
this.data('lens:template', {
template: was.template,
data: data
});
return this;
}
this.html(template(name, data)).data('lens:template', {
template: name,
data: data
});
return this;
};
} else if (typeof(window) !== 'undefined') {
window.template = template;
} else {
throw 'neither jQuery or top-level window object were found; unsure where to attach template()...';
}
})();
;(function () {
var twos = function (n, w) {
return n < 0 ? 2 ** w + n : n;
};
var lpad = function (s, p, n) {
while (s.length < n) {
s = p + s;
}
return s;
};
var num = function (n, prefix, sign, left, pad, width, ffmt) {
var p = '',
s = (ffmt ? ffmt : function (x) { return x.toString(); }).call(null, n);
if (sign && n > 0) { prefix = sign; }
if (n < 0) { prefix = '-'; s = s.substr(1); }
if (left) { pad = ' '; } /* all left-alignment is space-padded */
width -= s.length;
width -= prefix.length;
while (width > 0) {
p += pad;
width--; /* assumes pad is always 1 character */
}
if (pad == '0') {
s = prefix + p + s;
} else if (left) {
s = prefix + s + p;
} else {
s = p + prefix + s;
}
return s;
};
var sprintf = function () {
var s = '';
var fmt = arguments[0];
if (!fmt) { return ''; }
var n = 1;
for (var i = 0; i < fmt.length; i++) {
var c = fmt.charCodeAt(i);
if (c == 37) { // % - start of a format specificer
if (i + 1 >= fmt.length) {
throw '%: invalid format specifier (trailing %-sign)'
}
if (fmt.charCodeAt(i+1) == 37) {
i++;
s += '%';
continue;
}
var alt = false, /* alternate form (#) */
pad = ' '; /* pad char (changeable with 0) */
left = false, /* left-adjust (-) */
sign = ''; /* pre-positive character (' ' or +) */
comma = false; /* use thousands separator (') */
var width = -1, /* field minimum width */
prec = -1, /* field precision */
bits = 32; /* field length modifier */
/* parse format specifier flags */
for (;;) {
i++;
if (i >= fmt.length) {
throw '%: invalid format specifier (while parsing flags)'
}
c = fmt.charCodeAt(i);
switch (c) {
case 35: /* # */ alt = true; continue;
case 48: /* 0 */ pad = '0'; continue;
case 45: /* - */ left = true; continue;
case 32: /* */ sign = ' '; continue;
case 43: /* + */ sign = '+'; continue;
case 39: /* ' */ comma = true; continue;
}
break;
}
c = fmt.charCodeAt(i);
if (c >= 49 && c <= 57) {
/* parse field minimum width */
width = c - 48;
for (;;) {
i++;
if (i >= fmt.length) {
throw '%: invalid format specifier (while parsing field minimum width)'
}
c = fmt.charCodeAt(i);
if (c >= 48 && c <= 57) {
width = (width * 10) + (c - 48);
continue;
}
break;
}
}
c = fmt.charCodeAt(i);
if (c == 46) { /* . */
/* parse field precision */
prec = 0;
for (;;) {
i++;
if (i >= fmt.length) {
throw '%: invalid format specifier (while parsing field precision)'
}
c = fmt.charCodeAt(i);
if (c >= 48 && c <= 57) {
prec = (prec * 10) + (c - 48);
continue;
}
break;
}
}
/* parse length modifier (h, hh, l, ll) */
c = fmt.charCodeAt(i);
if (c == 104) { /* h */
i++;
if (i >= fmt.length) {
throw '%: invalid format specifier (while parsing length modifier)'
}
c = fmt.charCodeAt(i);
if (c == 104) { /* hh */
i++;
if (i >= fmt.length) {
throw '%: invalid format specifier (while parsing length modifier)'
}
bits = 8;
} else {
bits = 16;
}
} else if (c == 108) { /* l */
i++;
if (i >= fmt.length) {
throw '%: invalid format specifier (while parsing length modifier)'
}
c = fmt.charCodeAt(i);
if (c == 108) { /* ll */
i++;
if (i >= fmt.length) {
throw '%: invalid format specifier (while parsing length modifier)'
}
bits = 128;
} else {
bits = 64;
}
}
/* parse conversion specifier */
c = fmt.charCodeAt(i);
switch (c) {
case 100: /* d */
case 105: /* i */
if (n < arguments.length) {
s += num(parseInt(arguments[n]), '', sign, left, pad, width);
n++;
} else {
s += '(!missing)';
}
break;
case 111: /* o */
if (n < arguments.length) {
s += num(parseInt(arguments[n]),
'' /* ffmt (below) handles alt */,
'' /* ignore sign */, left, pad, width,
function (x) {
var v = lpad(x.toString(8), '0', prec);
return (alt && v.charCodeAt(0) != 48)
? '0' + v
: v;
});
n++;
} else {
s += '(!missing)';
}
break;
case 117: /* u */
if (n < arguments.length) {
s += num(twos(parseInt(arguments[n]), bits), '', sign, left, pad, width);
n++;
} else {
s += '(!missing)';
}
break;
case 120: /* x */
if (n < arguments.length) {
s += num(twos(parseInt(arguments[n]), bits),
(alt ? '0x' : ''),
'' /* ignore sign */, left, pad, width,
function (x) { return lpad(x.toString(16), '0', prec); });
n++;
} else {
s += '(!missing)';
}
break;
case 88: /* X */
if (n < arguments.length) {
s += num(twos(parseInt(arguments[n]), bits),
(alt ? '0X' : ''),
'' /* ignore sign */, left, pad, width,
function (x) { return lpad(x.toString(16).toUpperCase(), '0', prec); });
n++;
} else {
s += '(!missing)';
}
break;
case 101: /* e */
if (n < arguments.length) {
s += num(parseFloat(arguments[n]), '', sign, left, pad, width,
function (x) { return x.toExponential(); });
n++;
} else {
s += '(!missing)';
}
break;
case 69: /* E */
if (n < arguments.length) {
s += num(parseFloat(arguments[n]), '', sign, left, pad, width,
function (x) { return x.toExponential().toUpperCase(); });
n++;
} else {
s += '(!missing)';
}
break;
case 102: /* f */
case 103: /* g */
case 70: /* F */
case 71: /* G */
if (n < arguments.length) {
s += num(parseFloat(arguments[n]), '', sign, left, pad, width,
function (x) {
return prec >= 0 ? x.toFixed(prec)
: x.toString();
});
n++;
} else {
s += '(!missing)';
}
break;
case 99: /* c */
if (n < arguments.length) {
s += String.fromCharCode(parseInt(arguments[n])).toString();
n++;
} else {
s += '(!missing)';
}
break;
case 115: /* s */
if (n < arguments.length) {
var v;
if (typeof(arguments[n]) === 'undefined') {
v = '(undefined)';
} else if (arguments[n] == null) {
v = '(null)';
} else {
v = arguments[n].toString();
if (prec >= 0) {
v = v.substr(0, prec);
}
}
while (v.length < width) {
if (left) {
v += ' ';
} else {
v = pad + v;
}
}
s += v;
n++;
} else {
s += '(!missing)';
}
break;
case 106: /* j - NON-STANDARD json conversion */
if (n < arguments.length) {
s += JSON.stringify(arguments[n], undefined, alt ? (prec > 0 ? prec : 2) : 0);
} else {
s += '(!missing)';
}
break;
default:
s += '(!unrecognized)';
break;
}
} else {
s += fmt[i];
}
}
return s;
};
Lens.sprintf = sprintf;
window.sprintf = sprintf;
})();
;(function () {
var strftime = function (fmt, d) {
if (!(d instanceof Date)) {
var _d = new Date();
if (!isNaN(d)) {
_d.setTime(d * 1000); /* epoch s -> ms */
}
d = _d;
}
if (typeof(d) === 'undefined') {
return "";
}
en_US = {
pref: {
/* %c */ datetime: function (d) { return strftime("%a %b %e %H:%M:%S %Y", d); },
/* %x */ date: function (d) { return strftime("%m/%d/%Y", d); },
/* %X */ time: function (d) { return strftime("%H:%M:%S", d); }
},
weekday: {
abbr: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
full: ['Sunday',
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday']
},
month: {
abbr: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
full: ['January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December']
},
AM: "AM", am: "am", PM: "PM", pm: "pm",
ordinal: ['th', 'st', 'nd', 'rd', 'th', 'th', 'th', 'th', 'th', 'th', 'th', // 1 - 10
'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', // 11 - 20
'st', 'nd', 'rd', 'th', 'th', 'th', 'th', 'th', 'th', 'th', // 21 - 30
'st'],
zero: ['00', '01', '02', '03', '04', '05', '06', '07', '08', '09',
'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'],
space: [' 0', ' 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'],
};
var lc = en_US;
var inspec = false;
var alt_o = false;
var s = '';
for (var i = 0; i < fmt.length; i++) {
var c = fmt.charCodeAt(i);
if (inspec) {
switch (c) {
// %% A literal '%' character
case 37:
s += '%';
break;
// %a The abbreviated name of the day of the week according to the
// current locale.
case 97:
s += lc.weekday.abbr[d.getDay()];
break;
// %A The full name of the day of the week according to the current
// locale.
case 65:
s += lc.weekday.full[d.getDay()];
break;
// %b The abbreviated month name according to the current locale.
case 98:
s += lc.month.abbr[d.getMonth()];
break;
// %h Equivalent to %b.
case 104:
s += lc.month.abbr[d.getMonth()];
break;
// %B The full month name according to the current locale.
case 66:
s += lc.month.full[d.getMonth()];
break;
// %c The preferred date and time representation for the current
// locale.
case 99:
s += lc.pref.datetime(d);
break;
// %C The century number (year/100) as a 2-digit integer
case 67:
s += parseInt(d.getFullYear() / 100);
break;
// %d The day of the month as a decimal number (range 01 to 31).
case 100:
s += lc.zero[d.getDate()];
break;
// %D Equivalent to %m/%d/%y. (Yecch—for Americans only. Americans
// should note that in other countries %d/%m/%y is rather common.
// This means that in international context this format is
// ambiguous and should not be used.)
case 68:
s += strftime("%m/%d/%y", d);
break;
// %e Like %d, the day of the month as a decimal number, but a
// leading zero is replaced by a space.
case 101:
s += d.getDate().toString()+(alt_o ? lc.ordinal[d.getDate()] : '');
break;
// %E Modifier: use alternative format, see below.
case 69:
// not supported; just skip it
continue;
// %F Equivalent to %Y-%m-%d (the ISO 8601 date format).
case 70:
s += strftime("%Y-%m-%d", d);
break;
// %G The ISO 8601 week-based year (see NOTES) with century as a
// decimal number. The 4-digit year corresponding to the ISO
// week number (see %V). This has the same format and value as
// %Y, except that if the ISO week number belongs to the previous
// or next year, that year is used instead.
case 71:
throw "this strftime() does not support '%G'";
// %g Like %G, but without century, that is, with a 2-digit year
// (00-99).
case 103:
throw "this strftime() does not support '%g'";
// %H The hour as a decimal number using a 24-hour clock (range 00 to 23).
case 72:
s += lc.zero[d.getHours()]
break;
// %I The hour as a decimal number using a 12-hour clock (range 01 to 12)
case 73:
s += lc.zero[d.getHours() % 12 == 0 ? 12 : d.getHours() % 12];
break;
// %j The day of the year as a decimal number (range 001 to 366).
case 106:
throw "this strftime() does not support '%j'";
// %k The hour (24-hour clock) as a decimal number (range 0 to 23);
// single digits are preceded by a blank. (See also %H.)
case 107:
s += lc.space[d.getHours()];
break;
// %l The hour (12-hour clock) as a decimal number (range 1 to 12);
// single digits are preceded by a blank. (See also %I.)
case 108:
s += lc.space[d.getHours() % 12 == 0 ? 12 : d.getHours() % 12];
break;
// %m The month as a decimal number (range 01 to 12).
case 109:
s += lc.zero[d.getMonth()+1];
break;
// %M The minute as a decimal number (range 00 to 59).
case 77:
s += lc.zero[d.getMinutes()];
break;
// %n A newline character.
case 110:
s += "\n";
break;
// %O Modifier: use alternative format, see below.
case 79:
alt_o = true;
continue;
// %p Either "AM" or "PM" according to the given time value, or the
// corresponding strings for the current locale. Noon is treated
// as "PM" and midnight as "AM".
case 112:
s += (d.getHours() < 12 ? lc.AM : lc.PM);
break;
// %P Like %p but in lowercase: "am" or "pm" or a corresponding
// string for the current locale.
case 80:
s += (d.getHours() < 12 ? lc.am : lc.pm);
break;
// %r The time in a.m. or p.m. notation. In the POSIX locale this
// is equivalent to %I:%M:%S %p.
case 114:
s += lc.zero[d.getHours() % 12 == 0 ? 12 : d.getHours() % 12] + ":" +
lc.zero[d.getMinutes()] + ":" +
lc.zero[d.getSeconds()] + " " +
(d.getHours() < 12 ? lc.AM : lc.PM);
break;
// %R The time in 24-hour notation (%H:%M). For a version
// including the seconds, see %T below.
case 82:
s += lc.zero[d.getHours()] + ":" +
lc.zero[d.getMinutes()];
break;
// %s The number of seconds since the Epoch,
// 1970-01-01 00:00:00+0000 (UTC).
case 115:
s += d.getTime().toString();
break;
// %S The second as a decimal number (range 00 to 60). (The range
// is up to 60 to allow for occasional leap seconds.)
case 83:
s += lc.zero[d.getSeconds()];
break;
// %t A tab character.
case 116:
s += "\t";
break;
// %T The time in 24-hour notation (%H:%M:%S).
case 84:
s += lc.zero[d.getHours()] + ":" +
lc.zero[d.getMinutes()] + ":" +
lc.zero[d.getSeconds()];
break;
// %u The day of the week as a decimal, range 1 to 7, Monday being 1.
// See also %w.
case 117:
var wday = d.getDay();
if (wday == 0) { wday = 7 };
s += (wday).toString()+(alt_o ? lc.ordinal[wday] : '');
break;
// %U The week number of the current year as a decimal number, range
// 00 to 53, starting with the first Sunday as the first day of
// week 01. See also %V and %W.
case 85:
throw "this strftime() does not support '%U'";
// %V The ISO 8601 week number (see NOTES) of the current year as a
// decimal number, range 01 to 53, where week 1 is the first week
// that has at least 4 days in the new year. See also %U and %W.
case 86:
throw "this strftime() does not support '%V'";