annotate src/implab/text/format.js @ 5:3d124d0b9078

improved declare/override, added override.before, override.after, override.hide, inherited.arguments.
author cin
date Fri, 16 Jun 2017 02:14:25 +0300
parents 93fb6c09f2e1
children 37e9e6bbe87a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
1 define([
1
93fb6c09f2e1 minor fixes
cin
parents: 0
diff changeset
2 "../safe",
0
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
3 "./format-compile",
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
4 "dojo/number",
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
5 "dojo/date/locale",
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
6 "dojo/_base/array" ], function(safe, compile, number, date, array) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
7
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
8 // {short,medium,full,long}-{date,time}
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
9 var convert = function(value, pattern) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
10 if (!pattern)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
11 return value.toString();
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
12
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
13 if (pattern.toLocaleLowerCase() == "json") {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
14 var cache = [];
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
15 return JSON.stringify(value, function(k, v) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
16 if (!safe.isPrimitive(v)) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
17 var id = array.indexOf(cache, v);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
18 if (id >= 0)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
19 return "@ref-" + id;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
20 else
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
21 return v;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
22 } else {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
23 return v;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
24 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
25 });
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
26 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
27
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
28 if (safe.isNumber(value)) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
29 var nopt = {};
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
30 if (pattern.indexOf("!") === 0) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
31 nopt.round = -1;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
32 pattern = pattern.substr(1);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
33 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
34 nopt.pattern = pattern;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
35 return number.format(value, nopt);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
36 } else if (value instanceof Date) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
37 var m = pattern.match(/^(\w+)-(\w+)$/);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
38 if (m)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
39 return date.format(value, {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
40 selector : m[2],
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
41 formatLength : m[1]
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
42 });
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
43 else
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
44 return date.format(value, {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
45 selector : "date",
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
46 datePattern : pattern
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
47 });
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
48 } else {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
49 return value.toString(pattern);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
50 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
51 };
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
52
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
53 function formatter(format) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
54 var data;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
55
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
56 if (arguments.length <= 1)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
57 return format;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
58
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
59 data = Array.prototype.slice.call(arguments, 1);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
60
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
61 var template = compile(format);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
62
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
63 return template(function(name, pattern) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
64 var value = data[name];
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
65 return !safe.isNull(value) ? convert(value, pattern) : "";
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
66 });
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
67 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
68
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
69 formatter.compile = function(format) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
70 var template = compile(format);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
71
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
72 return function() {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
73 var data = arguments;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
74
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
75 return template(function(name, pattern) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
76 var value = data[name];
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
77 return !safe.isNull(value) ? convert(value, pattern) : "";
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
78 });
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
79 };
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
80 };
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
81
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
82 formatter.convert = convert;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
83
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
84 return formatter;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
85 });