-
Notifications
You must be signed in to change notification settings - Fork 595
/
Copy pathcommonutil.ts
292 lines (249 loc) · 10.1 KB
/
commonutil.ts
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
/// <reference path="./tickEvent.ts" />
/// <reference path="./apptarget.ts" />
/// <reference path="./logger.ts" />
namespace ts.pxtc {
export let __dummy = 42;
}
import pxtc = ts.pxtc
namespace ts.pxtc.Util {
export function assert(cond: boolean, msg = "Assertion failed") {
if (!cond) {
debugger
throw new Error(msg)
}
}
export function flatClone<T extends Object>(obj: T): T {
if (obj == null) return null
let r: any = {}
Object.keys(obj).forEach((k) => { r[k] = (obj as any)[k] })
return r;
}
export function clone<T>(v: T): T {
if (v == null) return null
return JSON.parse(JSON.stringify(v))
}
export function htmlEscape(_input: string) {
if (!_input) return _input; // null, undefined, empty string test
return _input.replace(/([^\w .!?\-$])/g, c => "&#" + c.charCodeAt(0) + ";");
}
export function htmlUnescape(_input: string) {
if (!_input) return _input; // null, undefined, empty string test
return _input.replace(/(&#\d+;)/g, c => String.fromCharCode(Number(c.substr(2, c.length - 3))));
}
export function jsStringQuote(s: string) {
return s.replace(/[^\w .!?\-$]/g,
(c) => {
let h = c.charCodeAt(0).toString(16);
return "\\u" + "0000".substr(0, 4 - h.length) + h;
});
}
export function jsStringLiteral(s: string) {
return "\"" + jsStringQuote(s) + "\"";
}
export function initials(username: string): string {
if (/^\w+@/.test(username)) {
// Looks like an email address. Return first two characters.
const initials = username.match(/^\w\w/);
return initials.shift().toUpperCase();
} else {
// Parse the user name for user initials
const initials = username.match(/\b\w/g) || [];
return ((initials.shift() || '') + (initials.pop() || '')).toUpperCase();
}
}
// Localization functions. Please port any modifications over to pxtsim/localization.ts
let _localizeLang: string = "en";
let _localizeStrings: pxt.Map<string> = {};
let _translationsCache: pxt.Map<pxt.Map<string>> = {};
//let _didSetlocalizations = false;
//let _didReportLocalizationsNotSet = false;
let localizeLive = false;
export function enableLiveLocalizationUpdates() {
localizeLive = true;
}
export function liveLocalizationEnabled() {
return localizeLive;
}
/**
* Returns the current user language, prepended by "live-" if in live mode
*/
export function localeInfo(): string {
return `${localizeLive ? "live-" : ""}${userLanguage()}`;
}
/**
* Returns current user language iSO-code. Default is `en`.
*/
export function userLanguage(): string {
return _localizeLang;
}
// This function returns normalized language code
// For example: zh-CN this returns ["zh-CN", "zh", "zh-cn"]
// First two are valid crowdin\makecode locale code,
// Last all lowercase one is just for the backup when reading user defined extensions & tutorials.
export function normalizeLanguageCode(code: string): string[] {
const langParts = /^(\w{2,3})-(\w{2,4}$)/i.exec(code);
if (langParts && langParts[1] && langParts[2]) {
return [`${langParts[1].toLowerCase()}-${langParts[2].toUpperCase()}`, langParts[1].toLowerCase(),
`${langParts[1].toLowerCase()}-${langParts[2].toLowerCase()}`];
} else {
return [(code || "en").toLowerCase()];
}
}
export function setUserLanguage(localizeLang: string) {
_localizeLang = normalizeLanguageCode(localizeLang)[0];
}
export function isUserLanguageRtl(): boolean {
return /^ar|dv|fa|ha|he|ks|ku|ps|ur|yi/i.test(_localizeLang);
}
export const TRANSLATION_LOCALE = "pxt";
export function isTranslationMode(): boolean {
return userLanguage() == TRANSLATION_LOCALE;
}
export function _localize(s: string) {
// Needs to be test in localhost / CLI
/*if (!_didSetlocalizations && !_didReportLocalizationsNotSet) {
_didReportLocalizationsNotSet = true;
pxt.tickEvent("locale.localizationsnotset");
// pxt.reportError can't be used here because of order of file imports
// Just use pxt.error instead, and use an Error so stacktrace is reported
pxt.error(new Error("Attempted to translate a string before localizations were set"));
}*/
return _localizeStrings[s] || s;
}
export function getLocalizedStrings() {
return _localizeStrings;
}
export function setLocalizedStrings(strs: pxt.Map<string>) {
//_didSetlocalizations = true;
_localizeStrings = strs;
}
export function translationsCache() {
return _translationsCache;
}
export function fmt_va(f: string, args: any[]): string {
if (args.length == 0) return f;
return f.replace(/\{([0-9]+)(\:[^\}]+)?\}/g, function (s: string, n: string, spec: string): string {
let v = args[parseInt(n)];
let r = "";
let fmtMatch = /^:f(\d*)\.(\d+)/.exec(spec);
if (fmtMatch) {
let precision = parseInt(fmtMatch[2])
let len = parseInt(fmtMatch[1]) || 0
let fillChar = /^0/.test(fmtMatch[1]) ? "0" : " ";
let num = (<number>v).toFixed(precision)
if (len > 0 && precision > 0) len += precision + 1;
if (len > 0) {
while (num.length < len) {
num = fillChar + num;
}
}
r = num;
} else if (spec == ":x") {
r = "0x" + v.toString(16);
} else if (v === undefined) r = "(undef)";
else if (v === null) r = "(null)";
else if (v.toString) r = v.toString();
else r = v + "";
if (spec == ":a") {
if (/^\s*[euioah]/.test(r.toLowerCase()))
r = "an " + r;
else if (/^\s*[bcdfgjklmnpqrstvwxz]/.test(r.toLowerCase()))
r = "a " + r;
} else if (spec == ":s") {
if (v == 1) r = ""
else r = "s"
} else if (spec == ":q") {
r = Util.htmlEscape(r);
} else if (spec == ":jq") {
r = Util.jsStringQuote(r);
} else if (spec == ":uri") {
r = encodeURIComponent(r).replace(/'/g, "%27").replace(/"/g, "%22");
} else if (spec == ":url") {
r = encodeURI(r).replace(/'/g, "%27").replace(/"/g, "%22");
} else if (spec == ":%") {
r = (v * 100).toFixed(1).toString() + '%';
}
return r;
});
}
export function fmt(f: string, ...args: any[]) { return fmt_va(f, args); }
const locStats: { [index: string]: number; } = {};
export function dumpLocStats() {
const r: { [index: string]: string; } = {};
Object.keys(locStats).sort((a, b) => locStats[b] - locStats[a])
.forEach(k => r[k] = k);
pxt.log('prioritized list of strings:')
pxt.log(JSON.stringify(r, null, 2));
}
let sForPlural = true;
export function lf_va(format: string, args: any[]): string { // @ignorelf@
if (!format) return format;
locStats[format] = (locStats[format] || 0) + 1;
let lfmt = Util._localize(format)
if (!sForPlural && lfmt != format && /\d:s\}/.test(lfmt)) {
lfmt = lfmt.replace(/\{\d+:s\}/g, "")
}
lfmt = lfmt.replace(/^\{(id|loc):[^\}]+\}/g, '');
return fmt_va(lfmt, args);
}
export function lf(format: string, ...args: any[]): string { // @ignorelf@
return lf_va(format, args); // @ignorelf@
}
/**
* Similar to lf but the string do not get extracted into the loc file.
*/
export function rlf(format: string, ...args: any[]): string {
return lf_va(format, args); // @ignorelf@
}
export function lookup<T>(m: pxt.Map<T>, key: string): T {
if (m.hasOwnProperty(key))
return m[key]
return null
}
export function isoTime(time: number) {
let d = new Date(time * 1000)
return Util.fmt("{0}-{1:f02.0}-{2:f02.0} {3:f02.0}:{4:f02.0}:{5:f02.0}",
d.getFullYear(), d.getMonth() + 1, d.getDate(), d.getHours(), d.getMinutes(), d.getSeconds())
}
export function userError(msg: string): never {
let e = new Error(msg);
(<any>e).isUserError = true;
throw e
}
// small deep equals for primitives, objects, arrays. returns error message
export function deq(a: any, b: any): string {
if (a === b) return null;
if (!a || !b) return "Null value";
if (typeof a == 'object' && typeof b == 'object') {
if (Array.isArray(a)) {
if (!Array.isArray(b)) {
return "Expected array";
}
if (a.length != b.length) {
return "Expected array of length " + a.length + ", got " + b.length;
}
for (let i = 0; i < a.length; i++) {
if (deq(a[i], b[i]) != null) {
return "Expected array value " + a[i] + " got " + b[i];
}
}
return null;
}
let ak = Object.keys(a);
let bk = Object.keys(a);
if (ak.length != bk.length) {
return "Expected " + ak.length + " keys, got " + bk.length;
}
for (let i = 0; i < ak.length; i++) {
if (!Object.prototype.hasOwnProperty.call(b, ak[i])) {
return "Missing key " + ak[i];
} else if (deq(a[ak[i]], b[ak[i]]) != null) {
return "Expected value of " + ak[i] + " to be " + a[ak[i]] + ", got " + b[ak[i]];
}
}
return null;
}
return "Unable to compare " + a + ", " + b;
}
}
const lf = ts.pxtc.Util.lf;