annotate src/implab/text/format.js @ 24:f750c89976d3

Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
author cin
date Thu, 07 Dec 2017 19:19:29 +0300
parents 37e9e6bbe87a
children
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 }
24
f750c89976d3 Added trace tools, implab/log/trace!<channel-name>, <channel-name> is optional
cin
parents: 9
diff changeset
25 },2);
0
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 });
9
37e9e6bbe87a added iso formatter to Date type ex.: '{0:iso}'
cin
parents: 1
diff changeset
43 else if (pattern == "iso")
37e9e6bbe87a added iso formatter to Date type ex.: '{0:iso}'
cin
parents: 1
diff changeset
44 return value.toISOString();
0
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
45 else
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
46 return date.format(value, {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
47 selector : "date",
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
48 datePattern : pattern
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
49 });
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
50 } else {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
51 return value.toString(pattern);
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 };
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
54
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
55 function formatter(format) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
56 var data;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
57
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
58 if (arguments.length <= 1)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
59 return format;
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 data = Array.prototype.slice.call(arguments, 1);
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 var template = compile(format);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
64
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
65 return template(function(name, pattern) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
66 var value = data[name];
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
67 return !safe.isNull(value) ? convert(value, pattern) : "";
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 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
70
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
71 formatter.compile = function(format) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
72 var template = compile(format);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
73
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
74 return function() {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
75 var data = arguments;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
76
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
77 return template(function(name, pattern) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
78 var value = data[name];
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
79 return !safe.isNull(value) ? convert(value, pattern) : "";
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 };
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 formatter.convert = convert;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
85
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
86 return formatter;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
87 });