@player-ui/asset-transform-plugin 0.7.4-next.4 → 0.7.5--canary.434.14868

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.
@@ -0,0 +1,445 @@
1
+ import { test, expect, describe } from "vitest";
2
+ import type { Node } from "@player-ui/player";
3
+ import { Parser } from "@player-ui/player";
4
+ import { composeBefore, propertiesToSkipTransform } from "..";
5
+
6
+ const parser = new Parser();
7
+
8
+ const actionAsset = {
9
+ id: "action",
10
+ type: "action",
11
+ exp: "{{count}} = {{count}} + 1",
12
+ label: {
13
+ asset: {
14
+ id: "action-label",
15
+ type: "text",
16
+ value: "Count: {{count}}",
17
+ },
18
+ },
19
+ };
20
+
21
+ const actionAssetNode: Node.Asset = parser.parseObject(
22
+ actionAsset,
23
+ ) as Node.Asset;
24
+
25
+ const actionAsset2 = {
26
+ id: "action",
27
+ type: "action",
28
+ exp: "{{count}} = {{count}} + 1",
29
+ label: {
30
+ asset: {
31
+ id: "action-label",
32
+ type: "text",
33
+ value: "Count: {{count}}",
34
+ },
35
+ },
36
+ plugins: {
37
+ stringResolver: {
38
+ propertiesToSkip: ["exp"],
39
+ },
40
+ },
41
+ };
42
+
43
+ const actionAsset2Node: Node.Asset = parser.parseObject(
44
+ actionAsset2,
45
+ ) as Node.Asset;
46
+
47
+ describe("propertiesToSkip", () => {
48
+ test("adds exp to the plugin stringResolver.propertiesToSkip", () => {
49
+ const before = composeBefore(propertiesToSkipTransform(["exp"]));
50
+
51
+ expect(before).toStrictEqual(
52
+ expect.objectContaining({
53
+ beforeResolve: expect.any(Function),
54
+ }),
55
+ );
56
+
57
+ expect(before.beforeResolve?.(actionAssetNode, {} as any, {} as any))
58
+ .toMatchInlineSnapshot(`
59
+ {
60
+ "children": [
61
+ {
62
+ "path": [
63
+ "label",
64
+ "asset",
65
+ ],
66
+ "value": {
67
+ "parent": {
68
+ "children": [Circular],
69
+ "type": "value",
70
+ "value": {
71
+ "exp": "{{count}} = {{count}} + 1",
72
+ "id": "action",
73
+ "type": "action",
74
+ },
75
+ },
76
+ "type": "asset",
77
+ "value": {
78
+ "id": "action-label",
79
+ "type": "text",
80
+ "value": "Count: {{count}}",
81
+ },
82
+ },
83
+ },
84
+ ],
85
+ "plugins": {
86
+ "stringResolver": {
87
+ "propertiesToSkip": [
88
+ "exp",
89
+ ],
90
+ },
91
+ },
92
+ "type": "value",
93
+ "value": {
94
+ "exp": "{{count}} = {{count}} + 1",
95
+ "id": "action",
96
+ "type": "action",
97
+ },
98
+ }
99
+ `);
100
+ });
101
+
102
+ test("Does not duplicate if propertiesToSkip already contains the value", () => {
103
+ const before = composeBefore(propertiesToSkipTransform(["exp"]));
104
+
105
+ expect(before).toStrictEqual(
106
+ expect.objectContaining({
107
+ beforeResolve: expect.any(Function),
108
+ }),
109
+ );
110
+
111
+ expect(before.beforeResolve?.(actionAsset2Node, {} as any, {} as any))
112
+ .toMatchInlineSnapshot(`
113
+ {
114
+ "children": [
115
+ {
116
+ "path": [
117
+ "label",
118
+ "asset",
119
+ ],
120
+ "value": {
121
+ "parent": {
122
+ "children": [Circular],
123
+ "type": "value",
124
+ "value": {
125
+ "exp": "{{count}} = {{count}} + 1",
126
+ "id": "action",
127
+ "type": "action",
128
+ },
129
+ },
130
+ "type": "asset",
131
+ "value": {
132
+ "id": "action-label",
133
+ "type": "text",
134
+ "value": "Count: {{count}}",
135
+ },
136
+ },
137
+ },
138
+ {
139
+ "path": [
140
+ "plugins",
141
+ "stringResolver",
142
+ "propertiesToSkip",
143
+ ],
144
+ "value": {
145
+ "override": true,
146
+ "parent": {
147
+ "children": [Circular],
148
+ "type": "value",
149
+ "value": {
150
+ "exp": "{{count}} = {{count}} + 1",
151
+ "id": "action",
152
+ "type": "action",
153
+ },
154
+ },
155
+ "type": "multi-node",
156
+ "values": [
157
+ {
158
+ "parent": [Circular],
159
+ "type": "value",
160
+ "value": "exp",
161
+ },
162
+ ],
163
+ },
164
+ },
165
+ ],
166
+ "plugins": {
167
+ "stringResolver": {
168
+ "propertiesToSkip": [
169
+ "exp",
170
+ ],
171
+ },
172
+ },
173
+ "type": "value",
174
+ "value": {
175
+ "exp": "{{count}} = {{count}} + 1",
176
+ "id": "action",
177
+ "type": "action",
178
+ },
179
+ }
180
+ `);
181
+ });
182
+
183
+ test("Does not add an empty array", () => {
184
+ const before = composeBefore(propertiesToSkipTransform([""]));
185
+
186
+ expect(before).toStrictEqual(
187
+ expect.objectContaining({
188
+ beforeResolve: expect.any(Function),
189
+ }),
190
+ );
191
+
192
+ expect(before.beforeResolve?.(actionAssetNode, {} as any, {} as any))
193
+ .toMatchInlineSnapshot(`
194
+ {
195
+ "children": [
196
+ {
197
+ "path": [
198
+ "label",
199
+ "asset",
200
+ ],
201
+ "value": {
202
+ "parent": [Circular],
203
+ "type": "asset",
204
+ "value": {
205
+ "id": "action-label",
206
+ "type": "text",
207
+ "value": "Count: {{count}}",
208
+ },
209
+ },
210
+ },
211
+ ],
212
+ "type": "value",
213
+ "value": {
214
+ "exp": "{{count}} = {{count}} + 1",
215
+ "id": "action",
216
+ "type": "action",
217
+ },
218
+ }
219
+ `);
220
+ });
221
+
222
+ test("Returns original Asset if the parameter passed in an empty array", () => {
223
+ const before = composeBefore(propertiesToSkipTransform([""]));
224
+
225
+ expect(before).toStrictEqual(
226
+ expect.objectContaining({
227
+ beforeResolve: expect.any(Function),
228
+ }),
229
+ );
230
+
231
+ expect(before.beforeResolve?.(actionAsset2Node, {} as any, {} as any))
232
+ .toMatchInlineSnapshot(`
233
+ {
234
+ "children": [
235
+ {
236
+ "path": [
237
+ "label",
238
+ "asset",
239
+ ],
240
+ "value": {
241
+ "parent": [Circular],
242
+ "type": "asset",
243
+ "value": {
244
+ "id": "action-label",
245
+ "type": "text",
246
+ "value": "Count: {{count}}",
247
+ },
248
+ },
249
+ },
250
+ {
251
+ "path": [
252
+ "plugins",
253
+ "stringResolver",
254
+ "propertiesToSkip",
255
+ ],
256
+ "value": {
257
+ "override": true,
258
+ "parent": [Circular],
259
+ "type": "multi-node",
260
+ "values": [
261
+ {
262
+ "parent": [Circular],
263
+ "type": "value",
264
+ "value": "exp",
265
+ },
266
+ ],
267
+ },
268
+ },
269
+ ],
270
+ "type": "value",
271
+ "value": {
272
+ "exp": "{{count}} = {{count}} + 1",
273
+ "id": "action",
274
+ "type": "action",
275
+ },
276
+ }
277
+ `);
278
+ });
279
+
280
+ test("Adds multiple values to propertiesToSkip, but skips empty strings", () => {
281
+ const before = composeBefore(
282
+ propertiesToSkipTransform(["exp", "", "", "label"]),
283
+ );
284
+
285
+ expect(before).toStrictEqual(
286
+ expect.objectContaining({
287
+ beforeResolve: expect.any(Function),
288
+ }),
289
+ );
290
+
291
+ expect(before.beforeResolve?.(actionAsset2Node, {} as any, {} as any))
292
+ .toMatchInlineSnapshot(`
293
+ {
294
+ "children": [
295
+ {
296
+ "path": [
297
+ "label",
298
+ "asset",
299
+ ],
300
+ "value": {
301
+ "parent": {
302
+ "children": [Circular],
303
+ "type": "value",
304
+ "value": {
305
+ "exp": "{{count}} = {{count}} + 1",
306
+ "id": "action",
307
+ "type": "action",
308
+ },
309
+ },
310
+ "type": "asset",
311
+ "value": {
312
+ "id": "action-label",
313
+ "type": "text",
314
+ "value": "Count: {{count}}",
315
+ },
316
+ },
317
+ },
318
+ {
319
+ "path": [
320
+ "plugins",
321
+ "stringResolver",
322
+ "propertiesToSkip",
323
+ ],
324
+ "value": {
325
+ "override": true,
326
+ "parent": {
327
+ "children": [Circular],
328
+ "type": "value",
329
+ "value": {
330
+ "exp": "{{count}} = {{count}} + 1",
331
+ "id": "action",
332
+ "type": "action",
333
+ },
334
+ },
335
+ "type": "multi-node",
336
+ "values": [
337
+ {
338
+ "parent": [Circular],
339
+ "type": "value",
340
+ "value": "exp",
341
+ },
342
+ ],
343
+ },
344
+ },
345
+ ],
346
+ "plugins": {
347
+ "stringResolver": {
348
+ "propertiesToSkip": [
349
+ "exp",
350
+ "label",
351
+ ],
352
+ },
353
+ },
354
+ "type": "value",
355
+ "value": {
356
+ "exp": "{{count}} = {{count}} + 1",
357
+ "id": "action",
358
+ "type": "action",
359
+ },
360
+ }
361
+ `);
362
+ });
363
+
364
+ test("Adds values to propertiesToSkip and retains original values", () => {
365
+ const before = composeBefore(propertiesToSkipTransform(["label", "test"]));
366
+
367
+ expect(before).toStrictEqual(
368
+ expect.objectContaining({
369
+ beforeResolve: expect.any(Function),
370
+ }),
371
+ );
372
+
373
+ expect(before.beforeResolve?.(actionAsset2Node, {} as any, {} as any))
374
+ .toMatchInlineSnapshot(`
375
+ {
376
+ "children": [
377
+ {
378
+ "path": [
379
+ "label",
380
+ "asset",
381
+ ],
382
+ "value": {
383
+ "parent": {
384
+ "children": [Circular],
385
+ "type": "value",
386
+ "value": {
387
+ "exp": "{{count}} = {{count}} + 1",
388
+ "id": "action",
389
+ "type": "action",
390
+ },
391
+ },
392
+ "type": "asset",
393
+ "value": {
394
+ "id": "action-label",
395
+ "type": "text",
396
+ "value": "Count: {{count}}",
397
+ },
398
+ },
399
+ },
400
+ {
401
+ "path": [
402
+ "plugins",
403
+ "stringResolver",
404
+ "propertiesToSkip",
405
+ ],
406
+ "value": {
407
+ "override": true,
408
+ "parent": {
409
+ "children": [Circular],
410
+ "type": "value",
411
+ "value": {
412
+ "exp": "{{count}} = {{count}} + 1",
413
+ "id": "action",
414
+ "type": "action",
415
+ },
416
+ },
417
+ "type": "multi-node",
418
+ "values": [
419
+ {
420
+ "parent": [Circular],
421
+ "type": "value",
422
+ "value": "exp",
423
+ },
424
+ ],
425
+ },
426
+ },
427
+ ],
428
+ "plugins": {
429
+ "stringResolver": {
430
+ "propertiesToSkip": [
431
+ "label",
432
+ "test",
433
+ ],
434
+ },
435
+ },
436
+ "type": "value",
437
+ "value": {
438
+ "exp": "{{count}} = {{count}} + 1",
439
+ "id": "action",
440
+ "type": "action",
441
+ },
442
+ }
443
+ `);
444
+ });
445
+ });
@@ -0,0 +1,170 @@
1
+ import { test, expect } from "vitest";
2
+ import type {
3
+ TransformFunction,
4
+ TransformFunctions,
5
+ BeforeTransformFunction,
6
+ Node,
7
+ } from "@player-ui/player";
8
+ import type { Asset } from "@player-ui/types";
9
+ import { compose, composeBefore } from "../utils";
10
+
11
+ interface TextAsset extends Asset<"text"> {
12
+ /**
13
+ *
14
+ */
15
+ value: string;
16
+ /**
17
+ *
18
+ */
19
+ metaData?: {
20
+ /**
21
+ *
22
+ */
23
+ sensitive?: boolean;
24
+ /**
25
+ *
26
+ */
27
+ size?: any;
28
+ };
29
+ }
30
+
31
+ const textAsset: TextAsset = {
32
+ id: "textAsset",
33
+ type: "text",
34
+ value: "Text",
35
+ };
36
+
37
+ const textNode: Node.Asset<TextAsset> = {
38
+ type: "asset" as any,
39
+ value: textAsset,
40
+ };
41
+
42
+ /**
43
+ *
44
+ */
45
+ const transform1: TransformFunction<TextAsset> = (asset) => asset;
46
+ /**
47
+ *
48
+ */
49
+ const transform2: TransformFunction<TextAsset> = (asset) => ({
50
+ ...asset,
51
+ metaData: { sensitive: true },
52
+ });
53
+ const transform3: TransformFunctions = {
54
+ resolve: (asset) => ({
55
+ ...asset,
56
+ metaData: { ...asset.metaData, size: "large" },
57
+ }),
58
+ };
59
+ const transform4: TransformFunctions = {
60
+ resolve: (asset) => asset,
61
+ beforeResolve: (node) => node,
62
+ };
63
+ /**
64
+ *
65
+ */
66
+ const beforeTransform: BeforeTransformFunction<TextAsset> = (node) => node;
67
+ /**
68
+ *
69
+ */
70
+ const beforeTransformWithChildren: BeforeTransformFunction<TextAsset> = (
71
+ node,
72
+ ) => ({
73
+ ...node,
74
+ children: [],
75
+ });
76
+
77
+ test("creates a transform function object with beforeResolve and resolve functions", () => {
78
+ const composedTransform = compose(transform1, composeBefore(beforeTransform));
79
+
80
+ expect(composedTransform).toStrictEqual(
81
+ expect.objectContaining({
82
+ beforeResolve: expect.any(Function),
83
+ resolve: expect.any(Function),
84
+ }),
85
+ );
86
+
87
+ const composedWithTransformFunctionsArg = compose(transform1, transform4);
88
+
89
+ expect(composedWithTransformFunctionsArg).toStrictEqual(
90
+ expect.objectContaining({
91
+ beforeResolve: expect.any(Function),
92
+ resolve: expect.any(Function),
93
+ }),
94
+ );
95
+ });
96
+
97
+ test("correctly compose the functions together", () => {
98
+ const composedTransform = compose(transform3, transform2, transform1);
99
+
100
+ expect(composedTransform.resolve?.(textAsset, {} as any, {} as any))
101
+ .toMatchInlineSnapshot(`
102
+ {
103
+ "id": "textAsset",
104
+ "metaData": {
105
+ "sensitive": true,
106
+ "size": "large",
107
+ },
108
+ "type": "text",
109
+ "value": "Text",
110
+ }
111
+ `);
112
+
113
+ // Since no deep merges, it overrides `metaData`
114
+ const composedWithOverrideTransform = compose(
115
+ transform2,
116
+ transform3,
117
+ transform1,
118
+ );
119
+ expect(
120
+ composedWithOverrideTransform.resolve?.(textAsset, {} as any, {} as any),
121
+ ).toMatchInlineSnapshot(`
122
+ {
123
+ "id": "textAsset",
124
+ "metaData": {
125
+ "sensitive": true,
126
+ },
127
+ "type": "text",
128
+ "value": "Text",
129
+ }
130
+ `);
131
+ });
132
+
133
+ test("composeBefore correctly composes any functions together", () => {
134
+ const before = composeBefore(beforeTransformWithChildren, beforeTransform);
135
+
136
+ expect(before).toStrictEqual(
137
+ expect.objectContaining({
138
+ beforeResolve: expect.any(Function),
139
+ }),
140
+ );
141
+
142
+ expect(before.beforeResolve?.(textNode, {} as any, {} as any))
143
+ .toMatchInlineSnapshot(`
144
+ {
145
+ "children": [],
146
+ "type": "asset",
147
+ "value": {
148
+ "id": "textAsset",
149
+ "type": "text",
150
+ "value": "Text",
151
+ },
152
+ }
153
+ `);
154
+ });
155
+
156
+ test("returns correctly with only one function", () => {
157
+ const composedTransform = compose(transform2);
158
+
159
+ expect(composedTransform.resolve?.(textAsset, {} as any, {} as any))
160
+ .toMatchInlineSnapshot(`
161
+ {
162
+ "id": "textAsset",
163
+ "metaData": {
164
+ "sensitive": true,
165
+ },
166
+ "type": "text",
167
+ "value": "Text",
168
+ }
169
+ `);
170
+ });
package/src/index.ts CHANGED
@@ -4,12 +4,12 @@ import type {
4
4
  TransformRegistry,
5
5
  TransformFunction,
6
6
  TransformFunctions,
7
- } from '@player-ui/player';
8
- import { Registry } from '@player-ui/partial-match-registry';
9
- import { compose } from './utils';
7
+ } from "@player-ui/player";
8
+ import { Registry } from "@player-ui/partial-match-registry";
9
+ import { compose } from "./utils";
10
10
 
11
- export * from './utils';
12
- export * from './propertiesToSkip';
11
+ export * from "./utils";
12
+ export * from "./propertiesToSkip";
13
13
 
14
14
  export type TransformType = TransformFunction<any> | TransformFunctions;
15
15
  export type TransformRegistryEntries = Array<[any, TransformType]>;
@@ -22,7 +22,7 @@ export type AssetTransformInit =
22
22
  * the resolve & beforeResolve functions.
23
23
  */
24
24
  function maybeCompose(maybeFn: any): TransformFunctions {
25
- if (typeof maybeFn === 'object') {
25
+ if (typeof maybeFn === "object") {
26
26
  return maybeFn;
27
27
  }
28
28
 
@@ -34,7 +34,7 @@ function maybeCompose(maybeFn: any): TransformFunctions {
34
34
  * registry shape.
35
35
  */
36
36
  function cleanupTransformRegistry(
37
- maybeRegistry: AssetTransformInit
37
+ maybeRegistry: AssetTransformInit,
38
38
  ): TransformRegistry {
39
39
  if (Array.isArray(maybeRegistry)) {
40
40
  const wrappedTransforms = maybeRegistry.map(([key, value]) => {
@@ -58,7 +58,7 @@ function cleanupTransformRegistry(
58
58
  * This allows users to embed stateful data into transforms.
59
59
  */
60
60
  export class AssetTransformPlugin implements PlayerPlugin {
61
- name = 'asset-transform';
61
+ name = "asset-transform";
62
62
  public readonly registry: TransformRegistry;
63
63
 
64
64
  constructor(transforms: AssetTransformInit) {
@@ -68,7 +68,7 @@ export class AssetTransformPlugin implements PlayerPlugin {
68
68
  apply(player: Player) {
69
69
  player.hooks.viewController.tap(this.name, (vc) => {
70
70
  this.registry.forEach(({ key, value }) =>
71
- vc.transformRegistry.set(key, maybeCompose(value))
71
+ vc.transformRegistry.set(key, maybeCompose(value)),
72
72
  );
73
73
  });
74
74
  }
@@ -1,4 +1,4 @@
1
- import type { BeforeTransformFunction } from '@player-ui/player';
1
+ import type { BeforeTransformFunction } from "@player-ui/player";
2
2
 
3
3
  /**
4
4
  * Passes a property to the plugins.stringResolver.propertiesToSkip array or creates it if it doesn't exist
@@ -6,14 +6,14 @@ import type { BeforeTransformFunction } from '@player-ui/player';
6
6
  * @param asset - Asset to apply the transform to
7
7
  */
8
8
  export const propertiesToSkipTransform = (
9
- parameters: string[]
9
+ parameters: string[],
10
10
  ): BeforeTransformFunction => {
11
11
  return (asset) => {
12
12
  const skipArray = asset.plugins?.stringResolver?.propertiesToSkip ?? [];
13
13
 
14
14
  /** makes sure there are no empty strings in the array */
15
15
  const parameterArray = parameters.filter((element) => {
16
- return element !== '';
16
+ return element !== "";
17
17
  });
18
18
 
19
19
  if (parameterArray.length === 0) {
package/src/utils.ts CHANGED
@@ -4,7 +4,7 @@ import type {
4
4
  BeforeTransformFunction,
5
5
  TransformFunction,
6
6
  TransformFunctions,
7
- } from '@player-ui/player';
7
+ } from "@player-ui/player";
8
8
 
9
9
  function composeTransforms(
10
10
  ...args: TransformFunction<any>[]
@@ -65,7 +65,7 @@ export function compose(
65
65
  const resolveFns: TransformFunction<any>[] = [];
66
66
 
67
67
  for (const arg of args) {
68
- if (typeof arg === 'function') {
68
+ if (typeof arg === "function") {
69
69
  resolveFns.push(arg);
70
70
  } else {
71
71
  if (arg?.resolve) {