@player-ui/common-expressions-plugin 0.0.1-next.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.
- package/dist/index.cjs.js +148 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.esm.js +144 -0
- package/package.json +19 -0
- package/src/expressions/index.ts +208 -0
- package/src/index.ts +16 -0
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var expressionPlugin = require('@player-ui/expression-plugin');
|
|
6
|
+
var expressions = require('@player-ui/expressions');
|
|
7
|
+
var utils = require('@player-ui/utils');
|
|
8
|
+
|
|
9
|
+
function ifString(fn) {
|
|
10
|
+
return (arg) => {
|
|
11
|
+
if (typeof arg === "string") {
|
|
12
|
+
return fn(arg);
|
|
13
|
+
}
|
|
14
|
+
return arg;
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
const size = expressions.withoutContext((val) => {
|
|
18
|
+
if (typeof val === "string") {
|
|
19
|
+
return val.length;
|
|
20
|
+
}
|
|
21
|
+
if (typeof val === "object" && val !== null) {
|
|
22
|
+
return Object.keys(val).length;
|
|
23
|
+
}
|
|
24
|
+
return 0;
|
|
25
|
+
});
|
|
26
|
+
const length = size;
|
|
27
|
+
const isEmpty = (ctx, val) => {
|
|
28
|
+
if (val === void 0 || val === null) {
|
|
29
|
+
return true;
|
|
30
|
+
}
|
|
31
|
+
if (typeof val === "object" || typeof val === "string") {
|
|
32
|
+
return size(ctx, val) === 0;
|
|
33
|
+
}
|
|
34
|
+
return false;
|
|
35
|
+
};
|
|
36
|
+
const isNotEmpty = (ctx, val) => {
|
|
37
|
+
return !isEmpty(ctx, val);
|
|
38
|
+
};
|
|
39
|
+
const concat = expressions.withoutContext((...args) => {
|
|
40
|
+
if (args.every((v) => Array.isArray(v))) {
|
|
41
|
+
const arrayArgs = args;
|
|
42
|
+
return arrayArgs.reduce((merged, next) => [...merged, ...next]);
|
|
43
|
+
}
|
|
44
|
+
return args.reduce((merged, next) => merged + (next != null ? next : ""), "");
|
|
45
|
+
});
|
|
46
|
+
const trim = expressions.withoutContext(ifString((str) => str.trim()));
|
|
47
|
+
const upperCase = expressions.withoutContext(ifString((str) => str.toUpperCase()));
|
|
48
|
+
const lowerCase = expressions.withoutContext(ifString((str) => str.toLowerCase()));
|
|
49
|
+
const replace = expressions.withoutContext((str, pattern, replacement = "") => {
|
|
50
|
+
if (typeof str === "string" && typeof pattern === "string" && typeof replacement === "string") {
|
|
51
|
+
const replacementRegex = new RegExp(pattern, "g");
|
|
52
|
+
return str.replace(replacementRegex, replacement);
|
|
53
|
+
}
|
|
54
|
+
return str;
|
|
55
|
+
});
|
|
56
|
+
const titleCase = expressions.withoutContext(ifString((str) => str.split(" ").map((word) => word[0].toUpperCase() + word.slice(1)).join(" ")));
|
|
57
|
+
const sentenceCase = expressions.withoutContext(ifString((str) => str.replace(/\b[a-zA-Z]/, (word) => word.toUpperCase())));
|
|
58
|
+
const number = expressions.withoutContext(utils.toNum);
|
|
59
|
+
const round = expressions.withoutContext((num) => {
|
|
60
|
+
var _a;
|
|
61
|
+
return Math.round((_a = utils.toNum(num, true)) != null ? _a : 0);
|
|
62
|
+
});
|
|
63
|
+
const floor = expressions.withoutContext((num) => {
|
|
64
|
+
var _a;
|
|
65
|
+
return Math.floor((_a = utils.toNum(num, true)) != null ? _a : 0);
|
|
66
|
+
});
|
|
67
|
+
const ceil = expressions.withoutContext((num) => {
|
|
68
|
+
var _a;
|
|
69
|
+
return Math.ceil((_a = utils.toNum(num, true)) != null ? _a : 0);
|
|
70
|
+
});
|
|
71
|
+
const sum = expressions.withoutContext((...args) => {
|
|
72
|
+
return args.reduce((s, next) => {
|
|
73
|
+
var _a;
|
|
74
|
+
return s + ((_a = utils.toNum(next)) != null ? _a : 0);
|
|
75
|
+
}, 0);
|
|
76
|
+
});
|
|
77
|
+
const findPropertyIndex = (context, bindingOrModel, propToCheck, valueToCheck) => {
|
|
78
|
+
const searchArray = Array.isArray(bindingOrModel) ? bindingOrModel : context.model.get(bindingOrModel);
|
|
79
|
+
if (!Array.isArray(searchArray)) {
|
|
80
|
+
return -1;
|
|
81
|
+
}
|
|
82
|
+
return searchArray.findIndex((value) => {
|
|
83
|
+
const propVal = typeof value === "object" && propToCheck !== void 0 ? value[propToCheck] : value;
|
|
84
|
+
return valueToCheck === propVal;
|
|
85
|
+
});
|
|
86
|
+
};
|
|
87
|
+
const findProperty = (context, bindingOrModel, propToCheck, valueToCheck, propToReturn, defaultValue) => {
|
|
88
|
+
var _a;
|
|
89
|
+
const searchArray = Array.isArray(bindingOrModel) ? bindingOrModel : context.model.get(bindingOrModel);
|
|
90
|
+
if (!Array.isArray(searchArray)) {
|
|
91
|
+
return defaultValue;
|
|
92
|
+
}
|
|
93
|
+
const foundValue = searchArray.find((value) => {
|
|
94
|
+
const propVal = typeof value === "object" && propToCheck !== void 0 ? value[propToCheck] : value;
|
|
95
|
+
return valueToCheck === propVal;
|
|
96
|
+
});
|
|
97
|
+
if (foundValue === void 0) {
|
|
98
|
+
return defaultValue;
|
|
99
|
+
}
|
|
100
|
+
if (typeof foundValue === "object" && propToReturn) {
|
|
101
|
+
return (_a = foundValue[propToReturn]) != null ? _a : defaultValue;
|
|
102
|
+
}
|
|
103
|
+
return foundValue;
|
|
104
|
+
};
|
|
105
|
+
const containsAny = expressions.withoutContext((str, keywords) => {
|
|
106
|
+
if (!(typeof str === "string") || !(typeof keywords === "string" || Array.isArray(keywords))) {
|
|
107
|
+
return false;
|
|
108
|
+
}
|
|
109
|
+
if (Array.isArray(keywords)) {
|
|
110
|
+
return keywords.some((keyword) => str.indexOf(keyword) > -1);
|
|
111
|
+
}
|
|
112
|
+
return str.indexOf(keywords) > -1;
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
var Expressions = /*#__PURE__*/Object.freeze({
|
|
116
|
+
__proto__: null,
|
|
117
|
+
size: size,
|
|
118
|
+
length: length,
|
|
119
|
+
isEmpty: isEmpty,
|
|
120
|
+
isNotEmpty: isNotEmpty,
|
|
121
|
+
concat: concat,
|
|
122
|
+
trim: trim,
|
|
123
|
+
upperCase: upperCase,
|
|
124
|
+
lowerCase: lowerCase,
|
|
125
|
+
replace: replace,
|
|
126
|
+
titleCase: titleCase,
|
|
127
|
+
sentenceCase: sentenceCase,
|
|
128
|
+
number: number,
|
|
129
|
+
round: round,
|
|
130
|
+
floor: floor,
|
|
131
|
+
ceil: ceil,
|
|
132
|
+
sum: sum,
|
|
133
|
+
findPropertyIndex: findPropertyIndex,
|
|
134
|
+
findProperty: findProperty,
|
|
135
|
+
containsAny: containsAny
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
class CommonExpressionsPlugin {
|
|
139
|
+
constructor() {
|
|
140
|
+
this.name = "CommonExpressions";
|
|
141
|
+
}
|
|
142
|
+
apply(player) {
|
|
143
|
+
player.registerPlugin(new expressionPlugin.ExpressionPlugin(new Map(Object.entries(Expressions))));
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
exports.CommonExpressionsPlugin = CommonExpressionsPlugin;
|
|
148
|
+
//# sourceMappingURL=index.cjs.js.map
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { PlayerPlugin, Player } from '@player-ui/player';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Exposes a lot of expressions to Player.
|
|
5
|
+
*/
|
|
6
|
+
declare class CommonExpressionsPlugin implements PlayerPlugin {
|
|
7
|
+
name: string;
|
|
8
|
+
apply(player: Player): void;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export { CommonExpressionsPlugin };
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import { ExpressionPlugin } from '@player-ui/expression-plugin';
|
|
2
|
+
import { withoutContext } from '@player-ui/expressions';
|
|
3
|
+
import { toNum } from '@player-ui/utils';
|
|
4
|
+
|
|
5
|
+
function ifString(fn) {
|
|
6
|
+
return (arg) => {
|
|
7
|
+
if (typeof arg === "string") {
|
|
8
|
+
return fn(arg);
|
|
9
|
+
}
|
|
10
|
+
return arg;
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
const size = withoutContext((val) => {
|
|
14
|
+
if (typeof val === "string") {
|
|
15
|
+
return val.length;
|
|
16
|
+
}
|
|
17
|
+
if (typeof val === "object" && val !== null) {
|
|
18
|
+
return Object.keys(val).length;
|
|
19
|
+
}
|
|
20
|
+
return 0;
|
|
21
|
+
});
|
|
22
|
+
const length = size;
|
|
23
|
+
const isEmpty = (ctx, val) => {
|
|
24
|
+
if (val === void 0 || val === null) {
|
|
25
|
+
return true;
|
|
26
|
+
}
|
|
27
|
+
if (typeof val === "object" || typeof val === "string") {
|
|
28
|
+
return size(ctx, val) === 0;
|
|
29
|
+
}
|
|
30
|
+
return false;
|
|
31
|
+
};
|
|
32
|
+
const isNotEmpty = (ctx, val) => {
|
|
33
|
+
return !isEmpty(ctx, val);
|
|
34
|
+
};
|
|
35
|
+
const concat = withoutContext((...args) => {
|
|
36
|
+
if (args.every((v) => Array.isArray(v))) {
|
|
37
|
+
const arrayArgs = args;
|
|
38
|
+
return arrayArgs.reduce((merged, next) => [...merged, ...next]);
|
|
39
|
+
}
|
|
40
|
+
return args.reduce((merged, next) => merged + (next != null ? next : ""), "");
|
|
41
|
+
});
|
|
42
|
+
const trim = withoutContext(ifString((str) => str.trim()));
|
|
43
|
+
const upperCase = withoutContext(ifString((str) => str.toUpperCase()));
|
|
44
|
+
const lowerCase = withoutContext(ifString((str) => str.toLowerCase()));
|
|
45
|
+
const replace = withoutContext((str, pattern, replacement = "") => {
|
|
46
|
+
if (typeof str === "string" && typeof pattern === "string" && typeof replacement === "string") {
|
|
47
|
+
const replacementRegex = new RegExp(pattern, "g");
|
|
48
|
+
return str.replace(replacementRegex, replacement);
|
|
49
|
+
}
|
|
50
|
+
return str;
|
|
51
|
+
});
|
|
52
|
+
const titleCase = withoutContext(ifString((str) => str.split(" ").map((word) => word[0].toUpperCase() + word.slice(1)).join(" ")));
|
|
53
|
+
const sentenceCase = withoutContext(ifString((str) => str.replace(/\b[a-zA-Z]/, (word) => word.toUpperCase())));
|
|
54
|
+
const number = withoutContext(toNum);
|
|
55
|
+
const round = withoutContext((num) => {
|
|
56
|
+
var _a;
|
|
57
|
+
return Math.round((_a = toNum(num, true)) != null ? _a : 0);
|
|
58
|
+
});
|
|
59
|
+
const floor = withoutContext((num) => {
|
|
60
|
+
var _a;
|
|
61
|
+
return Math.floor((_a = toNum(num, true)) != null ? _a : 0);
|
|
62
|
+
});
|
|
63
|
+
const ceil = withoutContext((num) => {
|
|
64
|
+
var _a;
|
|
65
|
+
return Math.ceil((_a = toNum(num, true)) != null ? _a : 0);
|
|
66
|
+
});
|
|
67
|
+
const sum = withoutContext((...args) => {
|
|
68
|
+
return args.reduce((s, next) => {
|
|
69
|
+
var _a;
|
|
70
|
+
return s + ((_a = toNum(next)) != null ? _a : 0);
|
|
71
|
+
}, 0);
|
|
72
|
+
});
|
|
73
|
+
const findPropertyIndex = (context, bindingOrModel, propToCheck, valueToCheck) => {
|
|
74
|
+
const searchArray = Array.isArray(bindingOrModel) ? bindingOrModel : context.model.get(bindingOrModel);
|
|
75
|
+
if (!Array.isArray(searchArray)) {
|
|
76
|
+
return -1;
|
|
77
|
+
}
|
|
78
|
+
return searchArray.findIndex((value) => {
|
|
79
|
+
const propVal = typeof value === "object" && propToCheck !== void 0 ? value[propToCheck] : value;
|
|
80
|
+
return valueToCheck === propVal;
|
|
81
|
+
});
|
|
82
|
+
};
|
|
83
|
+
const findProperty = (context, bindingOrModel, propToCheck, valueToCheck, propToReturn, defaultValue) => {
|
|
84
|
+
var _a;
|
|
85
|
+
const searchArray = Array.isArray(bindingOrModel) ? bindingOrModel : context.model.get(bindingOrModel);
|
|
86
|
+
if (!Array.isArray(searchArray)) {
|
|
87
|
+
return defaultValue;
|
|
88
|
+
}
|
|
89
|
+
const foundValue = searchArray.find((value) => {
|
|
90
|
+
const propVal = typeof value === "object" && propToCheck !== void 0 ? value[propToCheck] : value;
|
|
91
|
+
return valueToCheck === propVal;
|
|
92
|
+
});
|
|
93
|
+
if (foundValue === void 0) {
|
|
94
|
+
return defaultValue;
|
|
95
|
+
}
|
|
96
|
+
if (typeof foundValue === "object" && propToReturn) {
|
|
97
|
+
return (_a = foundValue[propToReturn]) != null ? _a : defaultValue;
|
|
98
|
+
}
|
|
99
|
+
return foundValue;
|
|
100
|
+
};
|
|
101
|
+
const containsAny = withoutContext((str, keywords) => {
|
|
102
|
+
if (!(typeof str === "string") || !(typeof keywords === "string" || Array.isArray(keywords))) {
|
|
103
|
+
return false;
|
|
104
|
+
}
|
|
105
|
+
if (Array.isArray(keywords)) {
|
|
106
|
+
return keywords.some((keyword) => str.indexOf(keyword) > -1);
|
|
107
|
+
}
|
|
108
|
+
return str.indexOf(keywords) > -1;
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
var Expressions = /*#__PURE__*/Object.freeze({
|
|
112
|
+
__proto__: null,
|
|
113
|
+
size: size,
|
|
114
|
+
length: length,
|
|
115
|
+
isEmpty: isEmpty,
|
|
116
|
+
isNotEmpty: isNotEmpty,
|
|
117
|
+
concat: concat,
|
|
118
|
+
trim: trim,
|
|
119
|
+
upperCase: upperCase,
|
|
120
|
+
lowerCase: lowerCase,
|
|
121
|
+
replace: replace,
|
|
122
|
+
titleCase: titleCase,
|
|
123
|
+
sentenceCase: sentenceCase,
|
|
124
|
+
number: number,
|
|
125
|
+
round: round,
|
|
126
|
+
floor: floor,
|
|
127
|
+
ceil: ceil,
|
|
128
|
+
sum: sum,
|
|
129
|
+
findPropertyIndex: findPropertyIndex,
|
|
130
|
+
findProperty: findProperty,
|
|
131
|
+
containsAny: containsAny
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
class CommonExpressionsPlugin {
|
|
135
|
+
constructor() {
|
|
136
|
+
this.name = "CommonExpressions";
|
|
137
|
+
}
|
|
138
|
+
apply(player) {
|
|
139
|
+
player.registerPlugin(new ExpressionPlugin(new Map(Object.entries(Expressions))));
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export { CommonExpressionsPlugin };
|
|
144
|
+
//# sourceMappingURL=index.esm.js.map
|
package/package.json
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@player-ui/common-expressions-plugin",
|
|
3
|
+
"version": "0.0.1-next.1",
|
|
4
|
+
"private": false,
|
|
5
|
+
"publishConfig": {
|
|
6
|
+
"registry": "https://registry.npmjs.org"
|
|
7
|
+
},
|
|
8
|
+
"peerDependencies": {
|
|
9
|
+
"@player-ui/binding-grammar": "0.0.1-next.1"
|
|
10
|
+
},
|
|
11
|
+
"dependencies": {
|
|
12
|
+
"@player-ui/utils": "0.0.1-next.1",
|
|
13
|
+
"@player-ui/binding-grammar": "0.0.1-next.1",
|
|
14
|
+
"@babel/runtime": "7.15.4"
|
|
15
|
+
},
|
|
16
|
+
"main": "dist/index.cjs.js",
|
|
17
|
+
"module": "dist/index.esm.js",
|
|
18
|
+
"typings": "dist/index.d.ts"
|
|
19
|
+
}
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ExpressionHandler,
|
|
3
|
+
ExpressionContext,
|
|
4
|
+
} from '@player-ui/expressions';
|
|
5
|
+
import { withoutContext } from '@player-ui/expressions';
|
|
6
|
+
import { toNum } from '@player-ui/utils';
|
|
7
|
+
import type { Binding } from '@player-ui/types';
|
|
8
|
+
|
|
9
|
+
/** Returns a function that executes the given function only if the first argument is a string */
|
|
10
|
+
function ifString(fn: (arg: string) => unknown) {
|
|
11
|
+
return (arg: unknown) => {
|
|
12
|
+
if (typeof arg === 'string') {
|
|
13
|
+
return fn(arg);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
return arg;
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** Generic Types */
|
|
21
|
+
|
|
22
|
+
export const size = withoutContext((val: unknown): number => {
|
|
23
|
+
if (typeof val === 'string') {
|
|
24
|
+
return val.length;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
if (typeof val === 'object' && val !== null) {
|
|
28
|
+
return Object.keys(val).length;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return 0;
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
export const length = size;
|
|
35
|
+
|
|
36
|
+
/** Checks to see if the given value is empty */
|
|
37
|
+
export const isEmpty: ExpressionHandler<[unknown], boolean> = (ctx, val) => {
|
|
38
|
+
if (val === undefined || val === null) {
|
|
39
|
+
return true;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (typeof val === 'object' || typeof val === 'string') {
|
|
43
|
+
return size(ctx, val) === 0;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return false;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
/** Checks to see if the given value is not empty */
|
|
50
|
+
export const isNotEmpty: ExpressionHandler<[unknown], boolean> = (ctx, val) => {
|
|
51
|
+
return !isEmpty(ctx, val);
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
export const concat = withoutContext((...args: Array<unknown>) => {
|
|
55
|
+
if (args.every((v) => Array.isArray(v))) {
|
|
56
|
+
const arrayArgs = args as Array<Array<unknown>>;
|
|
57
|
+
|
|
58
|
+
return arrayArgs.reduce((merged, next) => [...merged, ...next]);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return args.reduce((merged: any, next) => merged + (next ?? ''), '');
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
/** String Types */
|
|
65
|
+
|
|
66
|
+
export const trim = withoutContext(ifString((str) => str.trim()));
|
|
67
|
+
export const upperCase = withoutContext(ifString((str) => str.toUpperCase()));
|
|
68
|
+
export const lowerCase = withoutContext(ifString((str) => str.toLowerCase()));
|
|
69
|
+
export const replace = withoutContext(
|
|
70
|
+
(str: unknown, pattern: unknown, replacement: unknown = '') => {
|
|
71
|
+
if (
|
|
72
|
+
typeof str === 'string' &&
|
|
73
|
+
typeof pattern === 'string' &&
|
|
74
|
+
typeof replacement === 'string'
|
|
75
|
+
) {
|
|
76
|
+
const replacementRegex = new RegExp(pattern, 'g');
|
|
77
|
+
|
|
78
|
+
return str.replace(replacementRegex, replacement);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return str;
|
|
82
|
+
}
|
|
83
|
+
);
|
|
84
|
+
export const titleCase = withoutContext(
|
|
85
|
+
ifString((str) =>
|
|
86
|
+
str
|
|
87
|
+
.split(' ')
|
|
88
|
+
.map((word) => word[0].toUpperCase() + word.slice(1))
|
|
89
|
+
.join(' ')
|
|
90
|
+
)
|
|
91
|
+
);
|
|
92
|
+
|
|
93
|
+
export const sentenceCase = withoutContext(
|
|
94
|
+
ifString((str) => str.replace(/\b[a-zA-Z]/, (word) => word.toUpperCase()))
|
|
95
|
+
);
|
|
96
|
+
|
|
97
|
+
/** Math Types */
|
|
98
|
+
|
|
99
|
+
export const number = withoutContext(toNum);
|
|
100
|
+
|
|
101
|
+
export const round = withoutContext<[number | string], number>((num) =>
|
|
102
|
+
Math.round(toNum(num, true) ?? 0)
|
|
103
|
+
);
|
|
104
|
+
|
|
105
|
+
export const floor = withoutContext<[number | string], number>((num) =>
|
|
106
|
+
Math.floor(toNum(num, true) ?? 0)
|
|
107
|
+
);
|
|
108
|
+
|
|
109
|
+
export const ceil = withoutContext<[number | string], number>((num) =>
|
|
110
|
+
Math.ceil(toNum(num, true) ?? 0)
|
|
111
|
+
);
|
|
112
|
+
|
|
113
|
+
export const sum = withoutContext<Array<number | string>, number>((...args) => {
|
|
114
|
+
return args.reduce<number>((s, next) => s + (toNum(next) ?? 0), 0);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
/** Array Operations */
|
|
118
|
+
|
|
119
|
+
/** Finds the property in an array of objects */
|
|
120
|
+
export const findPropertyIndex: ExpressionHandler<
|
|
121
|
+
[Array<any> | Binding, string | undefined, any],
|
|
122
|
+
number
|
|
123
|
+
> = <T = unknown>(
|
|
124
|
+
context: ExpressionContext,
|
|
125
|
+
bindingOrModel: Binding | Array<Record<string, T>>,
|
|
126
|
+
propToCheck: string | undefined,
|
|
127
|
+
valueToCheck: T
|
|
128
|
+
) => {
|
|
129
|
+
const searchArray: Array<Record<string, T>> = Array.isArray(bindingOrModel)
|
|
130
|
+
? bindingOrModel
|
|
131
|
+
: context.model.get(bindingOrModel);
|
|
132
|
+
|
|
133
|
+
if (!Array.isArray(searchArray)) {
|
|
134
|
+
return -1;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
return searchArray.findIndex((value) => {
|
|
138
|
+
const propVal =
|
|
139
|
+
typeof value === 'object' && propToCheck !== undefined
|
|
140
|
+
? value[propToCheck]
|
|
141
|
+
: value;
|
|
142
|
+
|
|
143
|
+
return valueToCheck === propVal;
|
|
144
|
+
});
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
/** Searches an array for an object matching the criteria. Returns the target prop from that object */
|
|
148
|
+
export const findProperty: ExpressionHandler<
|
|
149
|
+
[Array<any> | Binding, string | undefined, any, string | undefined, any],
|
|
150
|
+
any
|
|
151
|
+
> = <T = unknown>(
|
|
152
|
+
context: ExpressionContext,
|
|
153
|
+
bindingOrModel: Binding | Array<Record<string, T>>,
|
|
154
|
+
propToCheck: string | undefined,
|
|
155
|
+
valueToCheck: T,
|
|
156
|
+
propToReturn?: string,
|
|
157
|
+
defaultValue?: T
|
|
158
|
+
) => {
|
|
159
|
+
const searchArray: Array<Record<string, T>> = Array.isArray(bindingOrModel)
|
|
160
|
+
? bindingOrModel
|
|
161
|
+
: context.model.get(bindingOrModel);
|
|
162
|
+
|
|
163
|
+
if (!Array.isArray(searchArray)) {
|
|
164
|
+
return defaultValue;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const foundValue = searchArray.find((value) => {
|
|
168
|
+
const propVal =
|
|
169
|
+
typeof value === 'object' && propToCheck !== undefined
|
|
170
|
+
? value[propToCheck]
|
|
171
|
+
: value;
|
|
172
|
+
|
|
173
|
+
return valueToCheck === propVal;
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
if (foundValue === undefined) {
|
|
177
|
+
return defaultValue;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
if (typeof foundValue === 'object' && propToReturn) {
|
|
181
|
+
return foundValue[propToReturn] ?? defaultValue;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
return foundValue;
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
/*
|
|
188
|
+
* Checks if a given string contains any keywords present in the given array
|
|
189
|
+
* @param str: The string to search in
|
|
190
|
+
* @param searchStrs: A keyword(s) to search for
|
|
191
|
+
# @returns boolean: Return true if any of the keywords exist in the given string
|
|
192
|
+
*/
|
|
193
|
+
export const containsAny = withoutContext<[string, string[] | string], boolean>(
|
|
194
|
+
(str, keywords) => {
|
|
195
|
+
if (
|
|
196
|
+
!(typeof str === 'string') ||
|
|
197
|
+
!(typeof keywords === 'string' || Array.isArray(keywords))
|
|
198
|
+
) {
|
|
199
|
+
return false;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
if (Array.isArray(keywords)) {
|
|
203
|
+
return keywords.some((keyword) => str.indexOf(keyword) > -1);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
return str.indexOf(keywords) > -1;
|
|
207
|
+
}
|
|
208
|
+
);
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Player, PlayerPlugin } from '@player-ui/player';
|
|
2
|
+
import { ExpressionPlugin } from '@player-ui/expression-plugin';
|
|
3
|
+
import * as Expressions from './expressions';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Exposes a lot of expressions to Player.
|
|
7
|
+
*/
|
|
8
|
+
export class CommonExpressionsPlugin implements PlayerPlugin {
|
|
9
|
+
name = 'CommonExpressions';
|
|
10
|
+
|
|
11
|
+
apply(player: Player) {
|
|
12
|
+
player.registerPlugin(
|
|
13
|
+
new ExpressionPlugin(new Map(Object.entries(Expressions)))
|
|
14
|
+
);
|
|
15
|
+
}
|
|
16
|
+
}
|