@player-ui/check-path-plugin 0.3.1-next.0 → 0.3.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.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { PlayerPlugin, Player } from '@player-ui/player';
1
+ import { PlayerPlugin, Player, Node } from '@player-ui/player';
2
2
  import { Asset } from '@player-ui/types';
3
3
 
4
4
  declare type QueryFunction = (asset: Asset) => boolean;
@@ -10,6 +10,7 @@ declare type Query = QueryFunction | string | object;
10
10
  declare class CheckPathPlugin implements PlayerPlugin {
11
11
  name: string;
12
12
  private viewInfo?;
13
+ readonly symbol: symbol;
13
14
  apply(player: Player): void;
14
15
  /**
15
16
  * Starts at the asset with the given id, and walks backwards _up_ the tree until it finds a match for the parent
@@ -46,11 +47,16 @@ declare class CheckPathPlugin implements PlayerPlugin {
46
47
  hasChildContext(id: string, query: Query | Array<Query>): boolean;
47
48
  /** Get the asset represented by id */
48
49
  getAsset(id: string): Asset | undefined;
50
+ /**
51
+ * Gets the value for an asset from an asset node
52
+ */
53
+ getAssetFromAssetNode(assetNode: Node.Asset | Node.View): Asset | undefined;
49
54
  /**
50
55
  * Get the path of the asset in the view upto
51
56
  * the asset that matches the query or to the view if no query is provided
52
57
  */
53
58
  getPath(id: string, query?: Query | Array<Query>): Array<string | number> | undefined;
59
+ private getResolvedValue;
54
60
  }
55
61
 
56
62
  export { CheckPathPlugin, Query, QueryFunction };
package/dist/index.esm.js CHANGED
@@ -2,6 +2,8 @@ import { NodeType } from '@player-ui/player';
2
2
  import { createObjectMatcher } from '@player-ui/partial-match-registry';
3
3
  import dlv from 'dlv';
4
4
 
5
+ const CheckPathPluginSymbol = Symbol.for("CheckPathPlugin");
6
+
5
7
  function createMatcher(match) {
6
8
  if (typeof match === "string" || typeof match === "number") {
7
9
  return createObjectMatcher({ type: match });
@@ -12,19 +14,19 @@ function createMatcher(match) {
12
14
  return createObjectMatcher(match);
13
15
  }
14
16
  function getParent(node, viewInfo) {
15
- var _a;
16
17
  let working = node;
17
18
  while (working.parent && working.parent.type !== NodeType.Asset && working.parent.type !== NodeType.View) {
18
19
  working = working.parent;
19
20
  }
20
21
  const { parent } = working;
21
22
  if (parent && (parent.type === NodeType.Asset || parent.type === NodeType.View)) {
22
- return (_a = viewInfo.resolver.getSourceNode(parent)) != null ? _a : parent;
23
+ return parent;
23
24
  }
24
25
  }
25
26
  class CheckPathPlugin {
26
27
  constructor() {
27
28
  this.name = "check-path";
29
+ this.symbol = CheckPathPluginSymbol;
28
30
  }
29
31
  apply(player) {
30
32
  player.hooks.viewController.tap(this.name, (viewController) => {
@@ -62,11 +64,10 @@ class CheckPathPlugin {
62
64
  if (!assetNode || !this.viewInfo) {
63
65
  return void 0;
64
66
  }
65
- let potentialMatch = getParent(assetNode, this.viewInfo);
67
+ let potentialMatch = getParent(assetNode);
66
68
  if (query === void 0) {
67
69
  if (potentialMatch) {
68
- const resolved = this.viewInfo.resolvedMap.get(potentialMatch);
69
- return resolved == null ? void 0 : resolved.value;
70
+ return this.getAssetFromAssetNode(potentialMatch);
70
71
  }
71
72
  return;
72
73
  }
@@ -78,19 +79,19 @@ class CheckPathPlugin {
78
79
  throw new Error("Recursion depth exceeded. Check for cycles in the AST graph");
79
80
  }
80
81
  const matcher = createMatcher(parentQuery);
81
- const resolved = this.viewInfo.resolvedMap.get(potentialMatch);
82
- if (resolved && matcher(resolved.value)) {
82
+ const resolved = this.getAssetFromAssetNode(potentialMatch);
83
+ if (resolved && matcher(resolved)) {
83
84
  if (queryArray.length === 0) {
84
- return resolved.value;
85
+ return resolved;
85
86
  }
86
87
  parentQuery = queryArray.shift();
87
88
  }
88
- potentialMatch = getParent(potentialMatch, this.viewInfo);
89
+ potentialMatch = getParent(potentialMatch);
89
90
  }
90
91
  return void 0;
91
92
  }
92
93
  getParentProp(id) {
93
- var _a, _b, _c, _d;
94
+ var _a, _b, _c;
94
95
  const assetNode = (_a = this.viewInfo) == null ? void 0 : _a.assetIdMap.get(id);
95
96
  if (!assetNode || !this.viewInfo) {
96
97
  return;
@@ -98,15 +99,15 @@ class CheckPathPlugin {
98
99
  let working = assetNode;
99
100
  let parent;
100
101
  while (working) {
101
- parent = (working == null ? void 0 : working.parent) && ((_b = this.viewInfo.resolvedMap.get(working.parent)) == null ? void 0 : _b.resolved);
102
+ parent = working == null ? void 0 : working.parent;
102
103
  if (parent && (parent.type === NodeType.Asset || parent.type === NodeType.View)) {
103
104
  break;
104
105
  }
105
106
  working = working == null ? void 0 : working.parent;
106
107
  }
107
108
  if (parent && "children" in parent) {
108
- const childProp = (_c = parent.children) == null ? void 0 : _c.find((child) => child.value === working);
109
- return (_d = childProp == null ? void 0 : childProp.path) == null ? void 0 : _d[0];
109
+ const childProp = (_b = parent.children) == null ? void 0 : _b.find((child) => child.value === working);
110
+ return (_c = childProp == null ? void 0 : childProp.path) == null ? void 0 : _c[0];
110
111
  }
111
112
  return void 0;
112
113
  }
@@ -122,28 +123,29 @@ class CheckPathPlugin {
122
123
  return Boolean(this.getParent(id, query));
123
124
  }
124
125
  findChildPath(node, query, includeSelfMatch = true) {
125
- var _a, _b, _c, _d;
126
+ var _a, _b;
126
127
  if (query.length === 0) {
127
128
  return true;
128
129
  }
129
130
  const [first, ...rest] = query;
130
131
  const matcher = createMatcher(first);
131
- if (node.type === NodeType.Asset || node.type === NodeType.View) {
132
- const resolved = (_a = this.viewInfo) == null ? void 0 : _a.resolvedMap.get(node);
133
- const includesSelf = (_b = includeSelfMatch && resolved && matcher(resolved.value)) != null ? _b : false;
132
+ if (node.type === NodeType.Asset || node.type === NodeType.View || node.type === NodeType.Applicability) {
133
+ const resolvedValue = this.getResolvedValue(node);
134
+ const includesSelf = (_a = includeSelfMatch && matcher(resolvedValue)) != null ? _a : false;
134
135
  const childQuery = includesSelf ? rest : query;
135
136
  if (childQuery.length === 0 && includesSelf) {
136
137
  return true;
137
138
  }
138
- if (childQuery.length && (!node.children || node.children.length === 0)) {
139
+ const children = node.type === NodeType.Applicability ? node.value.children : node.children;
140
+ if (childQuery.length && (!children || children.length === 0)) {
139
141
  return false;
140
142
  }
141
- if ((_c = node.children) == null ? void 0 : _c.some((childNode) => this.findChildPath(childNode.value, childQuery))) {
143
+ if (children == null ? void 0 : children.some((childNode) => this.findChildPath(childNode.value, childQuery))) {
142
144
  return true;
143
145
  }
144
146
  } else if (node.type === NodeType.MultiNode && node.values.some((childNode) => this.findChildPath(childNode, query))) {
145
147
  return true;
146
- } else if ("children" in node && ((_d = node.children) == null ? void 0 : _d.some((childNode) => this.findChildPath(childNode.value, query)))) {
148
+ } else if ("children" in node && ((_b = node.children) == null ? void 0 : _b.some((childNode) => this.findChildPath(childNode.value, query)))) {
147
149
  return true;
148
150
  }
149
151
  return false;
@@ -158,14 +160,18 @@ class CheckPathPlugin {
158
160
  return this.findChildPath(assetNode, queryArray, false);
159
161
  }
160
162
  getAsset(id) {
161
- var _a, _b, _c;
163
+ var _a;
162
164
  const assetNode = (_a = this.viewInfo) == null ? void 0 : _a.assetIdMap.get(id);
163
165
  if (!assetNode)
164
166
  return;
167
+ return this.getAssetFromAssetNode(assetNode);
168
+ }
169
+ getAssetFromAssetNode(assetNode) {
170
+ var _a, _b;
165
171
  const sourceNode = this.getSourceAssetNode(assetNode);
166
172
  if (!sourceNode)
167
173
  return;
168
- return (_c = (_b = this.viewInfo) == null ? void 0 : _b.resolvedMap.get(sourceNode)) == null ? void 0 : _c.value;
174
+ return (_b = (_a = this.viewInfo) == null ? void 0 : _a.resolvedMap.get(sourceNode)) == null ? void 0 : _b.value;
169
175
  }
170
176
  getPath(id, query) {
171
177
  var _a, _b;
@@ -185,34 +191,35 @@ class CheckPathPlugin {
185
191
  return (_a2 = parent.children) == null ? void 0 : _a2.find((n) => n.value === working);
186
192
  };
187
193
  while (working !== void 0) {
188
- const parent = (working == null ? void 0 : working.parent) && this.viewInfo.resolvedMap.get(working.parent);
189
- const parentNode = parent == null ? void 0 : parent.resolved;
190
- if (parentNode) {
191
- if (parentNode.type === NodeType.MultiNode) {
192
- const index = parentNode.values.indexOf(working);
194
+ const { parent } = working;
195
+ if (parent) {
196
+ if (parent.type === NodeType.MultiNode) {
197
+ const index = parent.values.indexOf(working);
193
198
  if (index !== -1) {
194
- const actualIndex = index - parentNode.values.slice(0, index).reduce((undefCount, next) => {
195
- var _a2, _b2;
196
- return ((_b2 = (_a2 = this.viewInfo) == null ? void 0 : _a2.resolvedMap.get(next)) == null ? void 0 : _b2.value) === void 0 ? undefCount + 1 : undefCount;
197
- }, 0);
199
+ const actualIndex = index - parent.values.slice(0, index).reduce((undefCount, next) => this.getResolvedValue(next) === void 0 ? undefCount + 1 : undefCount, 0);
198
200
  path = [actualIndex, ...path];
199
201
  }
200
- } else if ("children" in parentNode) {
201
- const childProp = findWorkingChild(parentNode);
202
+ } else if ("children" in parent) {
203
+ const childProp = findWorkingChild(parent);
202
204
  path = [...(_b = childProp == null ? void 0 : childProp.path) != null ? _b : [], ...path];
203
205
  }
204
- }
205
- if (parentQuery) {
206
- const matcher = createMatcher(parentQuery);
207
- if (matcher(parent == null ? void 0 : parent.value)) {
208
- parentQuery = queryArray.shift();
209
- if (!parentQuery)
210
- return path;
206
+ if (parentQuery) {
207
+ const matcher = createMatcher(parentQuery);
208
+ if (matcher(this.getResolvedValue(parent))) {
209
+ parentQuery = queryArray.shift();
210
+ if (!parentQuery)
211
+ return path;
212
+ }
211
213
  }
212
214
  }
213
215
  working = working.parent;
214
216
  }
215
- return queryArray.length === 0 ? path : void 0;
217
+ return parentQuery ? void 0 : path;
218
+ }
219
+ getResolvedValue(node) {
220
+ var _a, _b;
221
+ const sourceNode = this.getSourceAssetNode(node);
222
+ return (_b = (_a = this.viewInfo) == null ? void 0 : _a.resolvedMap.get(sourceNode != null ? sourceNode : node)) == null ? void 0 : _b.value;
216
223
  }
217
224
  }
218
225
 
package/package.json CHANGED
@@ -1,17 +1,17 @@
1
1
  {
2
2
  "name": "@player-ui/check-path-plugin",
3
- "version": "0.3.1-next.0",
3
+ "version": "0.3.1",
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.1-next.0"
9
+ "@player-ui/player": "0.3.1"
10
10
  },
11
11
  "dependencies": {
12
- "@player-ui/partial-match-registry": "0.3.1-next.0",
12
+ "@player-ui/partial-match-registry": "0.3.1",
13
13
  "dlv": "^1.1.3",
14
- "tapable-ts": "^0.1.0",
14
+ "tapable-ts": "^0.2.3",
15
15
  "@babel/runtime": "7.15.4"
16
16
  },
17
17
  "main": "dist/index.cjs.js",
@@ -55,6 +55,15 @@
55
55
  {
56
56
  "name": "Kelly Harrop",
57
57
  "url": "https://github.com/kharrop"
58
+ },
59
+ {
60
+ "name": "Alejandro Fimbres",
61
+ "url": "https://github.com/lexfm"
62
+ },
63
+ {
64
+ "name": "Rafael Campos",
65
+ "url": "https://github.com/rafbcampos"
58
66
  }
59
- ]
67
+ ],
68
+ "bundle": "./dist/check-path-plugin.prod.js"
60
69
  }
package/src/index.ts CHANGED
@@ -3,6 +3,7 @@ import type { Player, PlayerPlugin, Node, Resolver } from '@player-ui/player';
3
3
  import type { Asset } from '@player-ui/types';
4
4
  import { createObjectMatcher } from '@player-ui/partial-match-registry';
5
5
  import dlv from 'dlv';
6
+ import { CheckPathPluginSymbol } from './symbols';
6
7
 
7
8
  export type QueryFunction = (asset: Asset) => boolean;
8
9
  export type Query = QueryFunction | string | object;
@@ -51,7 +52,7 @@ interface ViewInfo {
51
52
  */
52
53
  function getParent(
53
54
  node: Node.Node,
54
- viewInfo: ViewInfo
55
+ viewInfo?: ViewInfo
55
56
  ): Node.ViewOrAsset | undefined {
56
57
  let working = node;
57
58
 
@@ -69,8 +70,7 @@ function getParent(
69
70
  parent &&
70
71
  (parent.type === NodeType.Asset || parent.type === NodeType.View)
71
72
  ) {
72
- return (viewInfo.resolver.getSourceNode(parent) ??
73
- parent) as Node.ViewOrAsset;
73
+ return parent;
74
74
  }
75
75
  }
76
76
 
@@ -81,6 +81,7 @@ function getParent(
81
81
  export class CheckPathPlugin implements PlayerPlugin {
82
82
  name = 'check-path';
83
83
  private viewInfo?: ViewInfo;
84
+ public readonly symbol = CheckPathPluginSymbol;
84
85
 
85
86
  apply(player: Player) {
86
87
  player.hooks.viewController.tap(this.name, (viewController) => {
@@ -135,14 +136,12 @@ export class CheckPathPlugin implements PlayerPlugin {
135
136
  return undefined;
136
137
  }
137
138
 
138
- let potentialMatch = getParent(assetNode, this.viewInfo);
139
+ let potentialMatch = getParent(assetNode);
139
140
 
140
141
  // Handle the case of an empty query (just get the immediate parent)
141
142
  if (query === undefined) {
142
143
  if (potentialMatch) {
143
- const resolved = this.viewInfo.resolvedMap.get(potentialMatch);
144
-
145
- return resolved?.value;
144
+ return this.getAssetFromAssetNode(potentialMatch);
146
145
  }
147
146
 
148
147
  return;
@@ -162,18 +161,18 @@ export class CheckPathPlugin implements PlayerPlugin {
162
161
  }
163
162
 
164
163
  const matcher = createMatcher(parentQuery);
165
- const resolved = this.viewInfo.resolvedMap.get(potentialMatch);
164
+ const resolved = this.getAssetFromAssetNode(potentialMatch);
166
165
 
167
- if (resolved && matcher(resolved.value)) {
166
+ if (resolved && matcher(resolved)) {
168
167
  // This is the last match.
169
168
  if (queryArray.length === 0) {
170
- return resolved.value;
169
+ return resolved;
171
170
  }
172
171
 
173
172
  parentQuery = queryArray.shift();
174
173
  }
175
174
 
176
- potentialMatch = getParent(potentialMatch, this.viewInfo);
175
+ potentialMatch = getParent(potentialMatch);
177
176
  }
178
177
 
179
178
  return undefined;
@@ -196,9 +195,7 @@ export class CheckPathPlugin implements PlayerPlugin {
196
195
  let parent;
197
196
 
198
197
  while (working) {
199
- parent =
200
- working?.parent &&
201
- this.viewInfo.resolvedMap.get(working.parent)?.resolved;
198
+ parent = working?.parent;
202
199
 
203
200
  if (
204
201
  parent &&
@@ -254,22 +251,30 @@ export class CheckPathPlugin implements PlayerPlugin {
254
251
  const [first, ...rest] = query;
255
252
  const matcher = createMatcher(first);
256
253
 
257
- if (node.type === NodeType.Asset || node.type === NodeType.View) {
258
- const resolved = this.viewInfo?.resolvedMap.get(node);
254
+ if (
255
+ node.type === NodeType.Asset ||
256
+ node.type === NodeType.View ||
257
+ node.type === NodeType.Applicability
258
+ ) {
259
+ const resolvedValue = this.getResolvedValue(node);
259
260
  const includesSelf =
260
- (includeSelfMatch && resolved && matcher(resolved.value)) ?? false;
261
+ (includeSelfMatch && matcher(resolvedValue)) ?? false;
261
262
  const childQuery = includesSelf ? rest : query;
262
263
 
263
264
  if (childQuery.length === 0 && includesSelf) {
264
265
  return true;
265
266
  }
266
267
 
267
- if (childQuery.length && (!node.children || node.children.length === 0)) {
268
+ const children =
269
+ node.type === NodeType.Applicability
270
+ ? (node.value as Node.ViewOrAsset).children
271
+ : node.children;
272
+ if (childQuery.length && (!children || children.length === 0)) {
268
273
  return false;
269
274
  }
270
275
 
271
276
  if (
272
- node.children?.some((childNode) =>
277
+ children?.some((childNode) =>
273
278
  this.findChildPath(childNode.value, childQuery)
274
279
  )
275
280
  ) {
@@ -314,6 +319,15 @@ export class CheckPathPlugin implements PlayerPlugin {
314
319
  const assetNode = this.viewInfo?.assetIdMap.get(id);
315
320
  if (!assetNode) return;
316
321
 
322
+ return this.getAssetFromAssetNode(assetNode);
323
+ }
324
+
325
+ /**
326
+ * Gets the value for an asset from an asset node
327
+ */
328
+ public getAssetFromAssetNode(
329
+ assetNode: Node.Asset | Node.View
330
+ ): Asset | undefined {
317
331
  const sourceNode = this.getSourceAssetNode(assetNode);
318
332
  if (!sourceNode) return;
319
333
 
@@ -352,23 +366,20 @@ export class CheckPathPlugin implements PlayerPlugin {
352
366
  };
353
367
 
354
368
  while (working !== undefined) {
355
- const parent =
356
- working?.parent && this.viewInfo.resolvedMap.get(working.parent);
369
+ const { parent } = working;
357
370
 
358
- const parentNode = parent?.resolved;
359
-
360
- if (parentNode) {
361
- if (parentNode.type === NodeType.MultiNode) {
362
- const index = parentNode.values.indexOf(working);
371
+ if (parent) {
372
+ if (parent.type === NodeType.MultiNode) {
373
+ const index = parent.values.indexOf(working);
363
374
 
364
375
  if (index !== -1) {
365
376
  const actualIndex =
366
377
  index -
367
- parentNode.values
378
+ parent.values
368
379
  .slice(0, index)
369
380
  .reduce(
370
381
  (undefCount, next) =>
371
- this.viewInfo?.resolvedMap.get(next)?.value === undefined
382
+ this.getResolvedValue(next) === undefined
372
383
  ? undefCount + 1
373
384
  : undefCount,
374
385
  0
@@ -376,18 +387,17 @@ export class CheckPathPlugin implements PlayerPlugin {
376
387
 
377
388
  path = [actualIndex, ...path];
378
389
  }
379
- } else if ('children' in parentNode) {
380
- const childProp = findWorkingChild(parentNode);
390
+ } else if ('children' in parent) {
391
+ const childProp = findWorkingChild(parent);
381
392
  path = [...(childProp?.path ?? []), ...path];
382
393
  }
383
- }
384
-
385
- if (parentQuery) {
386
- const matcher = createMatcher(parentQuery);
387
394
 
388
- if (matcher(parent?.value)) {
389
- parentQuery = queryArray.shift();
390
- if (!parentQuery) return path;
395
+ if (parentQuery) {
396
+ const matcher = createMatcher(parentQuery);
397
+ if (matcher(this.getResolvedValue(parent))) {
398
+ parentQuery = queryArray.shift();
399
+ if (!parentQuery) return path;
400
+ }
391
401
  }
392
402
  }
393
403
 
@@ -396,6 +406,11 @@ export class CheckPathPlugin implements PlayerPlugin {
396
406
 
397
407
  /* if at the end all queries haven't been consumed,
398
408
  it means we couldn't find a path till the matching query */
399
- return queryArray.length === 0 ? path : undefined;
409
+ return parentQuery ? undefined : path;
410
+ }
411
+
412
+ private getResolvedValue(node: Node.Node) {
413
+ const sourceNode = this.getSourceAssetNode(node);
414
+ return this.viewInfo?.resolvedMap.get(sourceNode ?? node)?.value;
400
415
  }
401
416
  }
package/src/symbols.ts ADDED
@@ -0,0 +1 @@
1
+ export const CheckPathPluginSymbol = Symbol.for('CheckPathPlugin');