cx 24.11.4 → 25.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,92 +1,92 @@
1
- import { expression } from "./Expression";
2
-
3
- import { quoteStr } from "../util/quote";
4
-
5
- function plus(str) {
6
- return str.length ? str + " + " : str;
7
- }
8
-
9
- export function stringTemplate(str) {
10
- let tplCache = getTplCache();
11
- let expr = tplCache[str];
12
- if (expr) return expr;
13
-
14
- expr = "";
15
-
16
- let termStart = -1,
17
- quoteStart = 0,
18
- term,
19
- bracketsOpen = 0,
20
- percentSign;
21
-
22
- for (let i = 0; i < str.length; i++) {
23
- let c = str[i];
24
- switch (c) {
25
- case "{":
26
- if (termStart < 0) {
27
- if (str[i + 1] == "{" && str[i - 1] != "%") {
28
- expr = plus(expr) + quoteStr(str.substring(quoteStart, i) + "{");
29
- i++;
30
- quoteStart = i + 1;
31
- } else {
32
- termStart = i + 1;
33
- percentSign = str[i - 1] == "%";
34
- if (i > quoteStart) expr = plus(expr) + quoteStr(str.substring(quoteStart, percentSign ? i - 1 : i));
35
- bracketsOpen = 1;
36
- }
37
- } else bracketsOpen++;
38
- break;
39
-
40
- case "}":
41
- if (termStart >= 0) {
42
- if (--bracketsOpen == 0) {
43
- term = str.substring(termStart, i);
44
- if (term.indexOf(":") == -1) {
45
- let nullSepIndex = term.indexOf("|");
46
- if (nullSepIndex == -1) term += ":s";
47
- else term = term.substring(0, nullSepIndex) + ":s" + term.substring(nullSepIndex);
48
- }
49
- expr = plus(expr) + (percentSign ? "%{" : "{") + term + "}";
50
- termStart = -1;
51
- quoteStart = i + 1;
52
- bracketsOpen = 0;
53
- }
54
- } else if (str[i + 1] == "}") {
55
- expr = plus(expr) + quoteStr(str.substring(quoteStart, i) + "}");
56
- i++;
57
- quoteStart = i + 1;
58
- }
59
- break;
60
- }
61
- }
62
-
63
- if (quoteStart < str.length) expr = plus(expr) + quoteStr(str.substring(quoteStart));
64
-
65
- return (tplCache[str] = expression(expr));
66
- }
67
-
68
- export const StringTemplate = {
69
- get: function (str) {
70
- return stringTemplate(str);
71
- },
72
-
73
- compile: function (str) {
74
- return stringTemplate(str).memoize();
75
- },
76
-
77
- format: function (format, ...args) {
78
- return stringTemplate(format)(args);
79
- },
80
- };
81
-
82
- let tplCache = {};
83
-
84
- let getTplCache = () => tplCache;
85
-
86
- export function invalidateStringTemplateCache() {
87
- tplCache = {};
88
- }
89
-
90
- export function setGetStringTemplateCacheCallback(callback) {
91
- getTplCache = callback;
92
- }
1
+ import { expression } from "./Expression";
2
+
3
+ import { quoteStr } from "../util/quote";
4
+
5
+ function plus(str) {
6
+ return str.length ? str + " + " : str;
7
+ }
8
+
9
+ export function stringTemplate(str) {
10
+ let tplCache = getTplCache();
11
+ let expr = tplCache[str];
12
+ if (expr) return expr;
13
+
14
+ expr = "";
15
+
16
+ let termStart = -1,
17
+ quoteStart = 0,
18
+ term,
19
+ bracketsOpen = 0,
20
+ percentSign;
21
+
22
+ for (let i = 0; i < str.length; i++) {
23
+ switch (str[i]) {
24
+ case "{":
25
+ if (termStart < 0) {
26
+ if (str[i + 1] == "{" && str[i - 1] != "%") {
27
+ expr = plus(expr) + quoteStr(str.substring(quoteStart, i) + "{");
28
+ i++;
29
+ quoteStart = i + 1;
30
+ } else {
31
+ termStart = i + 1;
32
+ percentSign = str[i - 1] == "%";
33
+ if (i > quoteStart) expr = plus(expr) + quoteStr(str.substring(quoteStart, percentSign ? i - 1 : i));
34
+ bracketsOpen = 1;
35
+ quoteStart = i; // for the case where the brackets are not closed
36
+ }
37
+ } else bracketsOpen++;
38
+ break;
39
+
40
+ case "}":
41
+ if (termStart >= 0) {
42
+ if (--bracketsOpen == 0) {
43
+ term = str.substring(termStart, i);
44
+ if (term.indexOf(":") == -1) {
45
+ let nullSepIndex = term.indexOf("|");
46
+ if (nullSepIndex == -1) term += ":s";
47
+ else term = term.substring(0, nullSepIndex) + ":s" + term.substring(nullSepIndex);
48
+ }
49
+ expr = plus(expr) + (percentSign ? "%{" : "{") + term + "}";
50
+ termStart = -1;
51
+ quoteStart = i + 1;
52
+ bracketsOpen = 0;
53
+ }
54
+ } else if (str[i + 1] == "}") {
55
+ expr = plus(expr) + quoteStr(str.substring(quoteStart, i) + "}");
56
+ i++;
57
+ quoteStart = i + 1;
58
+ }
59
+ break;
60
+ }
61
+ }
62
+
63
+ if (quoteStart < str.length) expr = plus(expr) + quoteStr(str.substring(quoteStart));
64
+
65
+ return (tplCache[str] = expression(expr));
66
+ }
67
+
68
+ export const StringTemplate = {
69
+ get: function (str) {
70
+ return stringTemplate(str);
71
+ },
72
+
73
+ compile: function (str) {
74
+ return stringTemplate(str).memoize();
75
+ },
76
+
77
+ format: function (format, ...args) {
78
+ return stringTemplate(format)(args);
79
+ },
80
+ };
81
+
82
+ let tplCache = {};
83
+
84
+ let getTplCache = () => tplCache;
85
+
86
+ export function invalidateStringTemplateCache() {
87
+ tplCache = {};
88
+ }
89
+
90
+ export function setGetStringTemplateCacheCallback(callback) {
91
+ getTplCache = callback;
92
+ }
@@ -18,6 +18,11 @@ describe("StringTemplate", function () {
18
18
  assert.equal(e({}), 'It\'s "working"!');
19
19
  });
20
20
 
21
+ it("allows \\ before a binding", function () {
22
+ var e = StringTemplate.compile("t\\{person.name}");
23
+ assert.equal(e({ person: { name: "Ogi" } }), "t\\Ogi");
24
+ });
25
+
21
26
  it("supports multi-line strings", function () {
22
27
  var e = StringTemplate.compile("a\nb");
23
28
  assert.equal(e(), "a\nb");
@@ -47,6 +52,11 @@ describe("StringTemplate", function () {
47
52
  };
48
53
  assert.equal(e(state), "Hello {Jim}");
49
54
  });
55
+
56
+ it("open brackets are ignored", function () {
57
+ var e = StringTemplate.compile("B { A");
58
+ assert.equal(e({}), "B { A");
59
+ });
50
60
  });
51
61
 
52
62
  describe("supports formatting", function () {
@@ -71,6 +81,18 @@ describe("StringTemplate", function () {
71
81
  });
72
82
  });
73
83
 
84
+ describe("properly handles backslashes", function () {
85
+ it("in a string", function () {
86
+ var e = StringTemplate.compile("a\\b");
87
+ assert.equal(e(), "a\\b");
88
+ });
89
+
90
+ it.only("before a special character", function () {
91
+ var e = StringTemplate.compile("\\t");
92
+ assert.equal(e(), "\\t");
93
+ });
94
+ });
95
+
74
96
  describe("supports expressions", function () {
75
97
  it("using []", function () {
76
98
  var e = StringTemplate.compile("1 + 2 = {[1+2]}");
@@ -22,6 +22,8 @@ export class Culture {
22
22
  static getDefaultDateEncoding(): DateEncoding;
23
23
 
24
24
  static setDefaultDateEncoding(encoding: DateEncoding): void;
25
+
26
+ static invalidateCache(): void;
25
27
  }
26
28
 
27
29
  export interface CultureSpecs {
package/src/ui/Format.js CHANGED
@@ -1,107 +1,108 @@
1
- import { Culture, getCurrentCultureCache } from "./Culture";
2
- import { Format as Fmt, resolveMinMaxFractionDigits, setGetFormatCacheCallback } from "../util/Format";
3
- import { GlobalCacheIdentifier } from "../util/GlobalCacheIdentifier";
4
- import { setGetExpressionCacheCallback } from "../data/Expression";
5
- import { setGetStringTemplateCacheCallback } from "../data/StringTemplate";
6
-
7
- export const Format = Fmt;
8
-
9
- let cultureSensitiveFormatsRegistered = false;
10
-
11
- export function resolveNumberFormattingFlags(flags) {
12
- if (!flags) return null;
13
- let result = {};
14
- if (flags.indexOf("+") >= 0) result.signDisplay = "exceptZero";
15
- if (flags.indexOf("c") >= 0) result.notation = "compact";
16
- if (flags.indexOf("a") >= 0) result.currencySign = "accounting";
17
- return result;
18
- }
19
-
20
- export function enableCultureSensitiveFormatting() {
21
- if (cultureSensitiveFormatsRegistered) return;
22
-
23
- cultureSensitiveFormatsRegistered = true;
24
-
25
- Fmt.registerFactory(["number", "n"], (format, minimumFractionDigits, maximumFractionDigits, flags) => {
26
- let culture = Culture.getNumberCulture();
27
-
28
- let formatter = culture.getFormatter({
29
- ...resolveMinMaxFractionDigits(minimumFractionDigits, maximumFractionDigits),
30
- ...resolveNumberFormattingFlags(flags),
31
- });
32
-
33
- return (value) => formatter.format(value);
34
- });
35
-
36
- Fmt.registerFactory("currency", (format, currency, minimumFractionDigits, maximumFractionDigits, flags) => {
37
- let culture = Culture.getNumberCulture();
38
- currency = currency || Culture.defaultCurrency;
39
-
40
- let formatter = culture.getFormatter({
41
- style: "currency",
42
- currency: currency,
43
- ...resolveMinMaxFractionDigits(minimumFractionDigits, maximumFractionDigits),
44
- ...resolveNumberFormattingFlags(flags),
45
- });
46
-
47
- return (value) => formatter.format(value);
48
- });
49
-
50
- Fmt.registerFactory(["percentage", "p", "%"], (format, minimumFractionDigits, maximumFractionDigits, flags) => {
51
- let culture = Culture.getNumberCulture();
52
- let formatter = culture.getFormatter({
53
- style: "percent",
54
- ...resolveMinMaxFractionDigits(minimumFractionDigits, maximumFractionDigits),
55
- ...resolveNumberFormattingFlags(flags),
56
- });
57
- return (value) => formatter.format(value);
58
- });
59
-
60
- Fmt.registerFactory(["percentSign", "ps"], (format, minimumFractionDigits, maximumFractionDigits, flags) => {
61
- let culture = Culture.getNumberCulture();
62
- let formatter = culture.getFormatter({
63
- style: "percent",
64
- ...resolveMinMaxFractionDigits(minimumFractionDigits, maximumFractionDigits),
65
- ...resolveNumberFormattingFlags(flags),
66
- });
67
- return (value) => formatter.format(value / 100);
68
- });
69
-
70
- Fmt.registerFactory(["date", "d"], (fmt, format = "yyyyMMdd") => {
71
- let culture = Culture.getDateTimeCulture();
72
- let formatter = culture.getFormatter(format);
73
- return (value) => formatter.format(new Date(value));
74
- });
75
-
76
- Fmt.registerFactory(["time", "t"], (fmt, format = "hhmmss") => {
77
- let culture = Culture.getDateTimeCulture();
78
- let formatter = culture.getFormatter(format);
79
- return (value) => formatter.format(new Date(value));
80
- });
81
-
82
- Fmt.registerFactory(["datetime", "dt"], (fmt, format = "yyyyMd hhmm") => {
83
- let culture = Culture.getDateTimeCulture();
84
- let formatter = culture.getFormatter(format);
85
- return (value) => formatter.format(new Date(value));
86
- });
87
-
88
- setGetFormatCacheCallback(() => {
89
- let cache = getCurrentCultureCache();
90
- if (!cache.formatCache) cache.formatCache = {};
91
- return cache.formatCache;
92
- });
93
-
94
- setGetExpressionCacheCallback(() => {
95
- let cache = getCurrentCultureCache();
96
- if (!cache.exprCache) cache.exprCache = {};
97
- return cache.exprCache;
98
- });
99
-
100
- setGetStringTemplateCacheCallback(() => {
101
- let cache = getCurrentCultureCache();
102
- if (!cache.strTplCache) cache.strTplCache = {};
103
- return cache.strTplCache;
104
- });
105
-
106
- GlobalCacheIdentifier.change();
107
- }
1
+ import { Culture, getCurrentCultureCache } from "./Culture";
2
+ import { Format as Fmt, resolveMinMaxFractionDigits, setGetFormatCacheCallback } from "../util/Format";
3
+ import { setGetExpressionCacheCallback } from "../data/Expression";
4
+ import { setGetStringTemplateCacheCallback } from "../data/StringTemplate";
5
+ import { parseDateInvariant } from "../util";
6
+ import { GlobalCacheIdentifier } from "../util/GlobalCacheIdentifier";
7
+
8
+ export const Format = Fmt;
9
+
10
+ let cultureSensitiveFormatsRegistered = false;
11
+
12
+ export function resolveNumberFormattingFlags(flags) {
13
+ if (!flags) return null;
14
+ let result = {};
15
+ if (flags.indexOf("+") >= 0) result.signDisplay = "exceptZero";
16
+ if (flags.indexOf("c") >= 0) result.notation = "compact";
17
+ if (flags.indexOf("a") >= 0) result.currencySign = "accounting";
18
+ return result;
19
+ }
20
+
21
+ export function enableCultureSensitiveFormatting() {
22
+ if (cultureSensitiveFormatsRegistered) return;
23
+
24
+ cultureSensitiveFormatsRegistered = true;
25
+
26
+ Fmt.registerFactory(["number", "n"], (format, minimumFractionDigits, maximumFractionDigits, flags) => {
27
+ let culture = Culture.getNumberCulture();
28
+
29
+ let formatter = culture.getFormatter({
30
+ ...resolveMinMaxFractionDigits(minimumFractionDigits, maximumFractionDigits),
31
+ ...resolveNumberFormattingFlags(flags),
32
+ });
33
+
34
+ return (value) => formatter.format(value);
35
+ });
36
+
37
+ Fmt.registerFactory("currency", (format, currency, minimumFractionDigits, maximumFractionDigits, flags) => {
38
+ let culture = Culture.getNumberCulture();
39
+ currency = currency || Culture.defaultCurrency;
40
+
41
+ let formatter = culture.getFormatter({
42
+ style: "currency",
43
+ currency: currency,
44
+ ...resolveMinMaxFractionDigits(minimumFractionDigits, maximumFractionDigits),
45
+ ...resolveNumberFormattingFlags(flags),
46
+ });
47
+
48
+ return (value) => formatter.format(value);
49
+ });
50
+
51
+ Fmt.registerFactory(["percentage", "p", "%"], (format, minimumFractionDigits, maximumFractionDigits, flags) => {
52
+ let culture = Culture.getNumberCulture();
53
+ let formatter = culture.getFormatter({
54
+ style: "percent",
55
+ ...resolveMinMaxFractionDigits(minimumFractionDigits, maximumFractionDigits),
56
+ ...resolveNumberFormattingFlags(flags),
57
+ });
58
+ return (value) => formatter.format(value);
59
+ });
60
+
61
+ Fmt.registerFactory(["percentSign", "ps"], (format, minimumFractionDigits, maximumFractionDigits, flags) => {
62
+ let culture = Culture.getNumberCulture();
63
+ let formatter = culture.getFormatter({
64
+ style: "percent",
65
+ ...resolveMinMaxFractionDigits(minimumFractionDigits, maximumFractionDigits),
66
+ ...resolveNumberFormattingFlags(flags),
67
+ });
68
+ return (value) => formatter.format(value / 100);
69
+ });
70
+
71
+ Fmt.registerFactory(["date", "d"], (fmt, format = "yyyyMMdd") => {
72
+ let culture = Culture.getDateTimeCulture();
73
+ let formatter = culture.getFormatter(format);
74
+ return (value) => formatter.format(parseDateInvariant(value));
75
+ });
76
+
77
+ Fmt.registerFactory(["time", "t"], (fmt, format = "hhmmss") => {
78
+ let culture = Culture.getDateTimeCulture();
79
+ let formatter = culture.getFormatter(format);
80
+ return (value) => formatter.format(parseDateInvariant(value));
81
+ });
82
+
83
+ Fmt.registerFactory(["datetime", "dt"], (fmt, format = "yyyyMd hhmm") => {
84
+ let culture = Culture.getDateTimeCulture();
85
+ let formatter = culture.getFormatter(format);
86
+ return (value) => formatter.format(parseDateInvariant(value));
87
+ });
88
+
89
+ setGetFormatCacheCallback(() => {
90
+ let cache = getCurrentCultureCache();
91
+ if (!cache.formatCache) cache.formatCache = {};
92
+ return cache.formatCache;
93
+ });
94
+
95
+ setGetExpressionCacheCallback(() => {
96
+ let cache = getCurrentCultureCache();
97
+ if (!cache.exprCache) cache.exprCache = {};
98
+ return cache.exprCache;
99
+ });
100
+
101
+ setGetStringTemplateCacheCallback(() => {
102
+ let cache = getCurrentCultureCache();
103
+ if (!cache.strTplCache) cache.strTplCache = {};
104
+ return cache.strTplCache;
105
+ });
106
+
107
+ GlobalCacheIdentifier.change();
108
+ }