cx 26.1.4 → 26.1.5
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.
- package/build/ui/Widget.d.ts +1 -1
- package/build/ui/exprHelpers.d.ts +6 -2
- package/build/ui/exprHelpers.js +9 -3
- package/build/ui/tpl.d.ts +5 -3
- package/build/ui/tpl.js +11 -4
- package/build/widgets/overlay/Overlay.d.ts +3 -3
- package/dist/manifest.js +827 -824
- package/dist/ui.js +30 -22
- package/dist/util.js +4 -0
- package/dist/widgets.js +395 -320
- package/package.json +1 -1
- package/src/ui/exprHelpers.ts +96 -95
- package/src/ui/tpl.spec.ts +69 -0
- package/src/ui/tpl.ts +17 -4
package/package.json
CHANGED
package/src/ui/exprHelpers.ts
CHANGED
|
@@ -1,95 +1,96 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
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
|
-
* @param
|
|
85
|
-
* @param
|
|
86
|
-
* @
|
|
87
|
-
*
|
|
88
|
-
* format(m.
|
|
89
|
-
* format(m.
|
|
90
|
-
* format(m.value, "
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
1
|
+
import { computable } from "../data/computable";
|
|
2
|
+
import { AccessorChain } from "../data/createAccessorModelProxy";
|
|
3
|
+
import { Selector } from "../data/Selector";
|
|
4
|
+
import { Format } from "../util/Format";
|
|
5
|
+
import { expr } from "./expr";
|
|
6
|
+
|
|
7
|
+
/** Returns a selector that converts the value to boolean using !! */
|
|
8
|
+
export function truthy<V>(arg: AccessorChain<V>): Selector<boolean> {
|
|
9
|
+
return expr(arg, (x) => !!x);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** Returns a selector that checks if the value is falsy using ! */
|
|
13
|
+
export function falsy<V>(arg: AccessorChain<V>): Selector<boolean> {
|
|
14
|
+
return expr(arg, (x) => !x);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** Returns a selector that checks if the value is strictly true (=== true) */
|
|
18
|
+
export function isTrue(arg: AccessorChain<any>): Selector<boolean> {
|
|
19
|
+
return expr(arg, (x) => x === true);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Returns a selector that checks if the value is strictly false (=== false) */
|
|
23
|
+
export function isFalse(arg: AccessorChain<any>): Selector<boolean> {
|
|
24
|
+
return expr(arg, (x) => x === false);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Returns a selector that checks if the value is not null or undefined (x != null) */
|
|
28
|
+
export function hasValue<V>(arg: AccessorChain<V>): Selector<boolean> {
|
|
29
|
+
return expr(arg, (x) => x != null);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Returns a selector that checks if a string or array is empty (null, undefined, or length === 0) */
|
|
33
|
+
export function isEmpty(arg: AccessorChain<string | any[] | null | undefined>): Selector<boolean> {
|
|
34
|
+
return expr(arg, (x) => x == null || x.length === 0);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Returns a selector that checks if a string or array is non-empty (not null/undefined and length > 0) */
|
|
38
|
+
export function isNonEmpty(arg: AccessorChain<string | any[] | null | undefined>): Selector<boolean> {
|
|
39
|
+
return expr(arg, (x) => x != null && x.length > 0);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Returns a selector that checks if the value is less than the given value */
|
|
43
|
+
export function lessThan<V>(arg: AccessorChain<V>, value: V): Selector<boolean> {
|
|
44
|
+
return expr(arg, (x) => x < value);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Returns a selector that checks if the value is less than or equal to the given value */
|
|
48
|
+
export function lessThanOrEqual<V>(arg: AccessorChain<V>, value: V): Selector<boolean> {
|
|
49
|
+
return expr(arg, (x) => x <= value);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Returns a selector that checks if the value is greater than the given value */
|
|
53
|
+
export function greaterThan<V>(arg: AccessorChain<V>, value: V): Selector<boolean> {
|
|
54
|
+
return expr(arg, (x) => x > value);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Returns a selector that checks if the value is greater than or equal to the given value */
|
|
58
|
+
export function greaterThanOrEqual<V>(arg: AccessorChain<V>, value: V): Selector<boolean> {
|
|
59
|
+
return expr(arg, (x) => x >= value);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Returns a selector that checks if the value equals the given value using == */
|
|
63
|
+
export function equal<V>(arg: AccessorChain<V>, value: V): Selector<boolean> {
|
|
64
|
+
return expr(arg, (x) => x == value);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Returns a selector that checks if the value does not equal the given value using != */
|
|
68
|
+
export function notEqual<V>(arg: AccessorChain<V>, value: V): Selector<boolean> {
|
|
69
|
+
return expr(arg, (x) => x != value);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Returns a selector that checks if the value strictly equals the given value using === */
|
|
73
|
+
export function strictEqual<V>(arg: AccessorChain<V>, value: V): Selector<boolean> {
|
|
74
|
+
return expr(arg, (x) => x === value);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** Returns a selector that checks if the value strictly does not equal the given value using !== */
|
|
78
|
+
export function strictNotEqual<V>(arg: AccessorChain<V>, value: V): Selector<boolean> {
|
|
79
|
+
return expr(arg, (x) => x !== value);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Returns a selector that formats the value using the specified format string.
|
|
83
|
+
* Format strings use semicolon-separated syntax: "formatType;param1;param2"
|
|
84
|
+
* @param arg - The accessor chain to the value
|
|
85
|
+
* @param fmt - The format string
|
|
86
|
+
* @param nullText - Optional text to display for null/undefined values
|
|
87
|
+
* @example
|
|
88
|
+
* format(m.price, "n;2") // formats as number with 2 decimal places
|
|
89
|
+
* format(m.date, "d") // formats as date
|
|
90
|
+
* format(m.value, "p;0;2") // formats as percentage with 0-2 decimal places
|
|
91
|
+
* format(m.value, "n;2", "N/A") // shows "N/A" for null values
|
|
92
|
+
*/
|
|
93
|
+
export function format<V>(arg: AccessorChain<V>, fmt: string, nullText?: string): Selector<string> {
|
|
94
|
+
let f = nullText != null ? `${fmt}|${nullText}` : fmt;
|
|
95
|
+
return computable(arg, (x) => Format.value(x, f));
|
|
96
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { tpl } from "./tpl";
|
|
2
|
+
import assert from "assert";
|
|
3
|
+
import { createAccessorModelProxy } from "../data/createAccessorModelProxy";
|
|
4
|
+
|
|
5
|
+
interface Model {
|
|
6
|
+
firstName: string;
|
|
7
|
+
lastName: string;
|
|
8
|
+
age: number;
|
|
9
|
+
city: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
describe("tpl", function () {
|
|
13
|
+
const m = createAccessorModelProxy<Model>();
|
|
14
|
+
|
|
15
|
+
describe("string-only form", function () {
|
|
16
|
+
it("returns a tpl object", function () {
|
|
17
|
+
const result = tpl("{firstName} {lastName}");
|
|
18
|
+
assert.deepStrictEqual(result, { tpl: "{firstName} {lastName}" });
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
describe("accessor chain form", function () {
|
|
23
|
+
it("formats with a single accessor", function () {
|
|
24
|
+
const selector = tpl(m.firstName, "Hello, {0}!");
|
|
25
|
+
assert.strictEqual(selector({ firstName: "John" }), "Hello, John!");
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it("formats with two accessors", function () {
|
|
29
|
+
const selector = tpl(m.firstName, m.lastName, "{0} {1}");
|
|
30
|
+
assert.strictEqual(selector({ firstName: "John", lastName: "Doe" }), "John Doe");
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it("formats with three accessors", function () {
|
|
34
|
+
const selector = tpl(m.firstName, m.lastName, m.age, "{0} {1} is {2} years old");
|
|
35
|
+
assert.strictEqual(
|
|
36
|
+
selector({ firstName: "John", lastName: "Doe", age: 30 }),
|
|
37
|
+
"John Doe is 30 years old",
|
|
38
|
+
);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("formats with four accessors", function () {
|
|
42
|
+
const selector = tpl(m.firstName, m.lastName, m.age, m.city, "{0} {1}, {2}, from {3}");
|
|
43
|
+
assert.strictEqual(
|
|
44
|
+
selector({ firstName: "John", lastName: "Doe", age: 30, city: "NYC" }),
|
|
45
|
+
"John Doe, 30, from NYC",
|
|
46
|
+
);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it("handles null values", function () {
|
|
50
|
+
const selector = tpl(m.firstName, m.lastName, "{0} {1}");
|
|
51
|
+
assert.strictEqual(selector({ firstName: "John", lastName: null }), "John ");
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("handles undefined values", function () {
|
|
55
|
+
const selector = tpl(m.firstName, m.lastName, "{0} {1}");
|
|
56
|
+
assert.strictEqual(selector({ firstName: undefined, lastName: "Doe" }), " Doe");
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it("supports formatting in template", function () {
|
|
60
|
+
const selector = tpl(m.age, "Age: {0:n;0}");
|
|
61
|
+
assert.strictEqual(selector({ age: 30 }), "Age: 30");
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("supports null text in template", function () {
|
|
65
|
+
const selector = tpl(m.firstName, "Name: {0|N/A}");
|
|
66
|
+
assert.strictEqual(selector({ firstName: null }), "Name: N/A");
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
});
|
package/src/ui/tpl.ts
CHANGED
|
@@ -1,5 +1,18 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import { computable, ComputableSelector } from "../data/computable";
|
|
2
|
+
import { MemoSelector } from "../data/Selector";
|
|
3
|
+
import { StringTemplate } from "../data/StringTemplate";
|
|
4
|
+
import { Tpl } from "./Prop";
|
|
5
|
+
|
|
6
|
+
export function tpl(text: string): Tpl;
|
|
7
|
+
export function tpl<T extends ComputableSelector[]>(...args: [...T, string]): MemoSelector<string>;
|
|
8
|
+
export function tpl(...args: any[]): any {
|
|
9
|
+
if (args.length === 1)
|
|
10
|
+
return {
|
|
11
|
+
tpl: args[0],
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
let template = args[args.length - 1];
|
|
15
|
+
let formatter = StringTemplate.get(template);
|
|
16
|
+
let selectors = args.slice(0, -1);
|
|
17
|
+
return computable(...selectors, (...values: any[]) => formatter(values));
|
|
5
18
|
}
|