@player-ui/common-expressions-plugin 0.3.0-next.2 → 0.3.0-next.4

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 CHANGED
@@ -3,8 +3,21 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var expressionPlugin = require('@player-ui/expression-plugin');
6
- var expressions = require('@player-ui/expressions');
7
- var utils = require('@player-ui/utils');
6
+ var player = require('@player-ui/player');
7
+
8
+ function toNum(val, coerceTo0) {
9
+ if (typeof val === "number") {
10
+ return val;
11
+ }
12
+ if (typeof val === "string" && val.length > 0) {
13
+ let newVal = val.trim();
14
+ newVal = newVal.replace(/,/g, "");
15
+ newVal = newVal.replace(/[¥£$€]/, "");
16
+ const nVal = Number(newVal);
17
+ return newVal.match(/^0[xbo]/i) || isNaN(nVal) ? void 0 : nVal;
18
+ }
19
+ return coerceTo0 ? 0 : void 0;
20
+ }
8
21
 
9
22
  function ifString(fn) {
10
23
  return (arg) => {
@@ -14,7 +27,7 @@ function ifString(fn) {
14
27
  return arg;
15
28
  };
16
29
  }
17
- const size = expressions.withoutContext((val) => {
30
+ const size = player.withoutContext((val) => {
18
31
  if (typeof val === "string") {
19
32
  return val.length;
20
33
  }
@@ -36,42 +49,42 @@ const isEmpty = (ctx, val) => {
36
49
  const isNotEmpty = (ctx, val) => {
37
50
  return !isEmpty(ctx, val);
38
51
  };
39
- const concat = expressions.withoutContext((...args) => {
52
+ const concat = player.withoutContext((...args) => {
40
53
  if (args.every((v) => Array.isArray(v))) {
41
54
  const arrayArgs = args;
42
55
  return arrayArgs.reduce((merged, next) => [...merged, ...next]);
43
56
  }
44
57
  return args.reduce((merged, next) => merged + (next != null ? next : ""), "");
45
58
  });
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 = "") => {
59
+ const trim = player.withoutContext(ifString((str) => str.trim()));
60
+ const upperCase = player.withoutContext(ifString((str) => str.toUpperCase()));
61
+ const lowerCase = player.withoutContext(ifString((str) => str.toLowerCase()));
62
+ const replace = player.withoutContext((str, pattern, replacement = "") => {
50
63
  if (typeof str === "string" && typeof pattern === "string" && typeof replacement === "string") {
51
64
  const replacementRegex = new RegExp(pattern, "g");
52
65
  return str.replace(replacementRegex, replacement);
53
66
  }
54
67
  return str;
55
68
  });
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) => {
69
+ const titleCase = player.withoutContext(ifString((str) => str.split(" ").map((word) => word[0].toUpperCase() + word.slice(1)).join(" ")));
70
+ const sentenceCase = player.withoutContext(ifString((str) => str.replace(/\b[a-zA-Z]/, (word) => word.toUpperCase())));
71
+ const number = player.withoutContext(toNum);
72
+ const round = player.withoutContext((num) => {
60
73
  var _a;
61
- return Math.round((_a = utils.toNum(num, true)) != null ? _a : 0);
74
+ return Math.round((_a = toNum(num, true)) != null ? _a : 0);
62
75
  });
63
- const floor = expressions.withoutContext((num) => {
76
+ const floor = player.withoutContext((num) => {
64
77
  var _a;
65
- return Math.floor((_a = utils.toNum(num, true)) != null ? _a : 0);
78
+ return Math.floor((_a = toNum(num, true)) != null ? _a : 0);
66
79
  });
67
- const ceil = expressions.withoutContext((num) => {
80
+ const ceil = player.withoutContext((num) => {
68
81
  var _a;
69
- return Math.ceil((_a = utils.toNum(num, true)) != null ? _a : 0);
82
+ return Math.ceil((_a = toNum(num, true)) != null ? _a : 0);
70
83
  });
71
- const sum = expressions.withoutContext((...args) => {
84
+ const sum = player.withoutContext((...args) => {
72
85
  return args.reduce((s, next) => {
73
86
  var _a;
74
- return s + ((_a = utils.toNum(next)) != null ? _a : 0);
87
+ return s + ((_a = toNum(next)) != null ? _a : 0);
75
88
  }, 0);
76
89
  });
77
90
  const findPropertyIndex = (context, bindingOrModel, propToCheck, valueToCheck) => {
@@ -102,7 +115,7 @@ const findProperty = (context, bindingOrModel, propToCheck, valueToCheck, propTo
102
115
  }
103
116
  return foundValue;
104
117
  };
105
- const containsAny = expressions.withoutContext((str, keywords) => {
118
+ const containsAny = player.withoutContext((str, keywords) => {
106
119
  if (!(typeof str === "string") || !(typeof keywords === "string" || Array.isArray(keywords))) {
107
120
  return false;
108
121
  }
package/dist/index.esm.js CHANGED
@@ -1,6 +1,19 @@
1
1
  import { ExpressionPlugin } from '@player-ui/expression-plugin';
2
- import { withoutContext } from '@player-ui/expressions';
3
- import { toNum } from '@player-ui/utils';
2
+ import { withoutContext } from '@player-ui/player';
3
+
4
+ function toNum(val, coerceTo0) {
5
+ if (typeof val === "number") {
6
+ return val;
7
+ }
8
+ if (typeof val === "string" && val.length > 0) {
9
+ let newVal = val.trim();
10
+ newVal = newVal.replace(/,/g, "");
11
+ newVal = newVal.replace(/[¥£$€]/, "");
12
+ const nVal = Number(newVal);
13
+ return newVal.match(/^0[xbo]/i) || isNaN(nVal) ? void 0 : nVal;
14
+ }
15
+ return coerceTo0 ? 0 : void 0;
16
+ }
4
17
 
5
18
  function ifString(fn) {
6
19
  return (arg) => {
package/package.json CHANGED
@@ -1,16 +1,15 @@
1
1
  {
2
2
  "name": "@player-ui/common-expressions-plugin",
3
- "version": "0.3.0-next.2",
3
+ "version": "0.3.0-next.4",
4
4
  "private": false,
5
5
  "publishConfig": {
6
6
  "registry": "https://registry.npmjs.org"
7
7
  },
8
8
  "peerDependencies": {
9
- "@player-ui/player": "0.3.0-next.2"
9
+ "@player-ui/player": "0.3.0-next.4"
10
10
  },
11
11
  "dependencies": {
12
- "@player-ui/utils": "0.3.0-next.2",
13
- "@player-ui/expression-plugin": "0.3.0-next.2",
12
+ "@player-ui/expression-plugin": "0.3.0-next.4",
14
13
  "@babel/runtime": "7.15.4"
15
14
  },
16
15
  "main": "dist/index.cjs.js",
@@ -2,9 +2,9 @@ import type {
2
2
  ExpressionHandler,
3
3
  ExpressionContext,
4
4
  } from '@player-ui/expressions';
5
- import { withoutContext } from '@player-ui/expressions';
6
- import { toNum } from '@player-ui/utils';
5
+ import { withoutContext } from '@player-ui/player';
7
6
  import type { Binding } from '@player-ui/types';
7
+ import { toNum } from './toNum';
8
8
 
9
9
  /** Returns a function that executes the given function only if the first argument is a string */
10
10
  function ifString(fn: (arg: string) => unknown) {
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Convert a string that might contain formatting (such as commas and a currency symbol) to a number
3
+ */
4
+ export function toNum(val: unknown, coerceTo0?: boolean): number | undefined {
5
+ if (typeof val === 'number') {
6
+ return val;
7
+ }
8
+
9
+ if (typeof val === 'string' && val.length > 0) {
10
+ // Trim whitespace
11
+ let newVal = val.trim();
12
+ // Remove all commas
13
+ newVal = newVal.replace(/,/g, '');
14
+ // Remove up to 1 commonly-used currency symbol
15
+ newVal = newVal.replace(/[¥£$€]/, '');
16
+ const nVal = Number(newVal);
17
+
18
+ // ignore hex, binary, octal, and values that don't parse
19
+ return newVal.match(/^0[xbo]/i) || isNaN(nVal) ? undefined : nVal;
20
+ }
21
+
22
+ return coerceTo0 ? 0 : undefined;
23
+ }