| @@ -0,0 +1,8 | |||
|
|
1 | import * as module from "module"; | |
|
|
2 | import { TraceSource } from "../log/TraceSource"; | |
|
|
3 | ||
|
|
4 | const logger = TraceSource.get(module.id); | |
|
|
5 | ||
|
|
6 | logger.warn("The module is deprecated, use StringFormat.compile() method directly"); | |
|
|
7 | ||
|
|
8 | export { compile } from "./StringFormat"; | |
| @@ -0,0 +1,47 | |||
|
|
1 | import { format as dojoFormatNumber } from "dojo/number"; | |
|
|
2 | import { format as dojoFormatDate } from "dojo/date/locale"; | |
|
|
3 | import { Formatter } from "./StringFormat"; | |
|
|
4 | ||
|
|
5 | import { isNumber } from "../safe"; | |
|
|
6 | ||
|
|
7 | interface NumberFormatOptions { | |
|
|
8 | round?: number; | |
|
|
9 | pattern?: string; | |
|
|
10 | } | |
|
|
11 | ||
|
|
12 | function convertNumber(value: any, pattern: string) { | |
|
|
13 | if (isNumber(value)) { | |
|
|
14 | const nopt = {} as NumberFormatOptions; | |
|
|
15 | if (pattern.indexOf("!") === 0) { | |
|
|
16 | nopt.round = -1; | |
|
|
17 | pattern = pattern.substr(1); | |
|
|
18 | } | |
|
|
19 | nopt.pattern = pattern; | |
|
|
20 | ||
|
|
21 | return dojoFormatNumber(value, nopt); | |
|
|
22 | } | |
|
|
23 | } | |
|
|
24 | ||
|
|
25 | function convertDate(value: any, pattern: string) { | |
|
|
26 | if (value instanceof Date) { | |
|
|
27 | const m = pattern.match(/^(\w+)-(\w+)$/); | |
|
|
28 | if (m) | |
|
|
29 | return dojoFormatDate(value, { | |
|
|
30 | selector: m[2], | |
|
|
31 | formatLength: m[1] | |
|
|
32 | }); | |
|
|
33 | else if (pattern === "iso") | |
|
|
34 | return value.toISOString(); | |
|
|
35 | else | |
|
|
36 | return dojoFormatDate(value, { | |
|
|
37 | selector: "date", | |
|
|
38 | datePattern: pattern | |
|
|
39 | }); | |
|
|
40 | } | |
|
|
41 | } | |
|
|
42 | ||
|
|
43 | const _formatter = new Formatter([convertNumber, convertDate]); | |
|
|
44 | ||
|
|
45 | export = function format(msg: string, ...args: any[]) { | |
|
|
46 | return _formatter.format.apply(msg, ...args); | |
|
|
47 | }; | |
| @@ -0,0 +1,173 | |||
|
|
1 | import { isPrimitive, isNull, each } from "../safe"; | |
|
|
2 | import { MapOf } from "../interfaces"; | |
|
|
3 | ||
|
|
4 | type SubstFn = (name: string, format?: string) => string; | |
|
|
5 | type TemplateFn = (subst: SubstFn) => string; | |
|
|
6 | type ConvertFn = (value: any, format?: string) => string; | |
|
|
7 | ||
|
|
8 | const map = { | |
|
|
9 | "\\{": "&curlopen;", | |
|
|
10 | "\\}": "&curlclose;", | |
|
|
11 | "&": "&", | |
|
|
12 | "\\:": ":" | |
|
|
13 | }; | |
|
|
14 | ||
|
|
15 | const rev = { | |
|
|
16 | curlopen: "{", | |
|
|
17 | curlclose: "}", | |
|
|
18 | amp: "&", | |
|
|
19 | colon: ":" | |
|
|
20 | }; | |
|
|
21 | ||
|
|
22 | function espaceString(s: string) { | |
|
|
23 | if (!s) | |
|
|
24 | return s; | |
|
|
25 | return "'" + s.replace(/('|\\)/g, "\\$1").replace("\n", "\\n") + "'"; | |
|
|
26 | } | |
|
|
27 | ||
|
|
28 | function encode(s: string) { | |
|
|
29 | if (!s) | |
|
|
30 | return s; | |
|
|
31 | return s.replace(/\\{|\\}|&|\\:|\n/g, m => map[m] || m); | |
|
|
32 | } | |
|
|
33 | ||
|
|
34 | function decode(s: string) { | |
|
|
35 | if (!s) | |
|
|
36 | return s; | |
|
|
37 | return s.replace(/&(\w+);/g, (m, $1) => rev[$1] || m); | |
|
|
38 | } | |
|
|
39 | ||
|
|
40 | function subst(s: string) { | |
|
|
41 | const i = s.indexOf(":"); | |
|
|
42 | let name: string; | |
|
|
43 | let pattern: string; | |
|
|
44 | if (i >= 0) { | |
|
|
45 | name = s.substr(0, i); | |
|
|
46 | pattern = s.substr(i + 1); | |
|
|
47 | } else { | |
|
|
48 | name = s; | |
|
|
49 | } | |
|
|
50 | ||
|
|
51 | if (pattern) | |
|
|
52 | return [ | |
|
|
53 | espaceString(decode(name)), | |
|
|
54 | espaceString(decode(pattern))]; | |
|
|
55 | else | |
|
|
56 | return [espaceString(decode(name))]; | |
|
|
57 | } | |
|
|
58 | ||
|
|
59 | function _compile(str: string) { | |
|
|
60 | if (!str) | |
|
|
61 | return () => void 0; | |
|
|
62 | ||
|
|
63 | const chunks = encode(str).split("{"); | |
|
|
64 | let chunk: string; | |
|
|
65 | ||
|
|
66 | const code = ["var result=[];"]; | |
|
|
67 | ||
|
|
68 | for (let i = 0; i < chunks.length; i++) { | |
|
|
69 | chunk = chunks[i]; | |
|
|
70 | ||
|
|
71 | if (i === 0) { | |
|
|
72 | if (chunk) | |
|
|
73 | code.push("result.push(" + espaceString(decode(chunk)) + | |
|
|
74 | ");"); | |
|
|
75 | } else { | |
|
|
76 | const len = chunk.indexOf("}"); | |
|
|
77 | if (len < 0) | |
|
|
78 | throw new Error("Unbalanced substitution #" + i); | |
|
|
79 | ||
|
|
80 | code.push("result.push(subst(" + | |
|
|
81 | subst(chunk.substr(0, len)).join(",") + "));"); | |
|
|
82 | if (chunk.length > len + 1) | |
|
|
83 | code.push("result.push(" + | |
|
|
84 | espaceString(decode(chunk.substr(len + 1))) + ");"); | |
|
|
85 | } | |
|
|
86 | } | |
|
|
87 | ||
|
|
88 | code.push("return result.join('');"); | |
|
|
89 | ||
|
|
90 | // the code for this function is generated from the template | |
|
|
91 | // tslint:disable-next-line:function-constructor | |
|
|
92 | return new Function("subst", code.join("\n")) as TemplateFn; | |
|
|
93 | } | |
|
|
94 | ||
|
|
95 | const cache: MapOf<TemplateFn> = {}; | |
|
|
96 | ||
|
|
97 | export function compile(template: string) { | |
|
|
98 | let compiled = cache[template]; | |
|
|
99 | if (!compiled) { | |
|
|
100 | compiled = _compile(template); | |
|
|
101 | cache[template] = compiled; | |
|
|
102 | } | |
|
|
103 | return compiled; | |
|
|
104 | } | |
|
|
105 | ||
|
|
106 | function defaultConverter(value: any, pattern: string) { | |
|
|
107 | if (pattern && pattern.toLocaleLowerCase() === "json") { | |
|
|
108 | const seen = []; | |
|
|
109 | return JSON.stringify(value, (k, v) => { | |
|
|
110 | if (!isPrimitive(v)) { | |
|
|
111 | const id = seen.indexOf(v); | |
|
|
112 | if (id >= 0) | |
|
|
113 | return "@ref-" + id; | |
|
|
114 | else { | |
|
|
115 | seen.push(v); | |
|
|
116 | return v; | |
|
|
117 | } | |
|
|
118 | } else { | |
|
|
119 | return v; | |
|
|
120 | } | |
|
|
121 | }, 2); | |
|
|
122 | } else if (isNull(value)) { | |
|
|
123 | return ""; | |
|
|
124 | } else if (value instanceof Date) { | |
|
|
125 | return value.toISOString(); | |
|
|
126 | } else { | |
|
|
127 | return pattern ? value.toString(pattern) : value.toString(); | |
|
|
128 | } | |
|
|
129 | } | |
|
|
130 | ||
|
|
131 | export class Formatter { | |
|
|
132 | _converters: ConvertFn[]; | |
|
|
133 | ||
|
|
134 | constructor(converters?: ConvertFn[]) { | |
|
|
135 | this._converters = converters || []; | |
|
|
136 | this._converters.push(defaultConverter); | |
|
|
137 | } | |
|
|
138 | ||
|
|
139 | convert(value: any, pattern: string) { | |
|
|
140 | for (const c of this._converters) { | |
|
|
141 | const res = c(value, pattern); | |
|
|
142 | if (!isNull(res)) | |
|
|
143 | return res; | |
|
|
144 | } | |
|
|
145 | return ""; | |
|
|
146 | } | |
|
|
147 | ||
|
|
148 | format(msg: string, ...args: any[]) { | |
|
|
149 | const template = compile(msg); | |
|
|
150 | ||
|
|
151 | return template((name, pattern) => { | |
|
|
152 | const value = args[name]; | |
|
|
153 | return !isNull(value) ? this.convert(value, pattern) : ""; | |
|
|
154 | }); | |
|
|
155 | ||
|
|
156 | } | |
|
|
157 | ||
|
|
158 | compile(msg: string) { | |
|
|
159 | const template = compile(msg); | |
|
|
160 | return (...args: any[]) => { | |
|
|
161 | return template((name, pattern) => { | |
|
|
162 | const value = args[name]; | |
|
|
163 | return !isNull(value) ? this.convert(value, pattern) : ""; | |
|
|
164 | }); | |
|
|
165 | }; | |
|
|
166 | } | |
|
|
167 | } | |
|
|
168 | ||
|
|
169 | const _default = new Formatter(); | |
|
|
170 | ||
|
|
171 | export function format(msg: string, ...args: any[]) { | |
|
|
172 | return _default.format(msg, ...args); | |
|
|
173 | } | |
| @@ -2,6 +2,10 export type Constructor<T = {}> = new (. | |||
|
|
2 | 2 | |
|
|
3 | 3 | export type Factory<T = {}> = (...args: any[]) => T; |
|
|
4 | 4 | |
|
|
5 | export interface MapOf<T> { | |
|
|
6 | [key: string]: T; | |
|
|
7 | } | |
|
|
8 | ||
|
|
5 | 9 | export interface IDestroyable { |
|
|
6 | 10 | destroy(); |
|
|
7 | 11 | } |
| @@ -1,6 +1,6 | |||
|
|
1 | 1 | import { Observable } from "../Observable"; |
|
|
2 | 2 | import { Registry } from "./Registry"; |
|
|
3 |
import { format } from "../text/Format |
|
|
|
3 | import { format } from "../text/StringFormat"; | |
|
|
4 | 4 | |
|
|
5 | 5 | export const DebugLevel = 400; |
|
|
6 | 6 | |
| @@ -38,7 +38,7 export class TraceSource { | |||
|
|
38 | 38 | } |
|
|
39 | 39 | |
|
|
40 | 40 | protected emit(level: number, arg: any) { |
|
|
41 | this._notifyNext({ source: this, level, arg}); | |
|
|
41 | this._notifyNext({ source: this, level, arg }); | |
|
|
42 | 42 | } |
|
|
43 | 43 | |
|
|
44 | 44 | isDebugEnabled() { |
|
|
1 | NO CONTENT: file was removed |
|
|
1 | NO CONTENT: file was removed |
|
|
1 | NO CONTENT: file was removed |
|
|
1 | NO CONTENT: file was removed |
General Comments 0
You need to be logged in to leave comments.
Login now
