j-templates 8.0.23 → 8.0.24

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.
@@ -34,16 +34,16 @@ function FlattenValue(root, value, keyFunc) {
34
34
  * @param path - Dot-separated path to the value (empty string returns source)
35
35
  * @returns The value at the specified path
36
36
  */
37
- function GetPathValue(source, path) {
37
+ function GetPathValue(source, path, keyFunc) {
38
38
  if (path === "")
39
39
  return [source, true];
40
40
  const parts = path.split(".");
41
41
  let curr = source;
42
42
  let x = 0;
43
43
  for (; curr && x < parts.length - 1; x++)
44
- curr = curr[parts[x]];
44
+ curr = ResolveKeyed(source, curr[parts[x]], keyFunc);
45
45
  if (curr && Object.hasOwn(curr, parts[x])) {
46
- curr = curr[parts[x]];
46
+ curr = ResolveKeyed(source, curr[parts[x]], keyFunc);
47
47
  x++;
48
48
  }
49
49
  else
@@ -224,7 +224,7 @@ function RunProjections(source, keyFunc, projections, memo) {
224
224
  let y = 0;
225
225
  for (; allPathsExist && y < reads.length && !reads[y].endsWith("*"); y++) {
226
226
  const read = reads[y];
227
- const [value, found] = GetPathValue(source, read);
227
+ const [value, found] = GetPathValue(source, read, keyFunc);
228
228
  allPathsExist = found;
229
229
  keys.push(read);
230
230
  values.push((found && CreateSnapshot(source, keyFunc, value)) || value);
@@ -233,6 +233,7 @@ function RunProjections(source, keyFunc, projections, memo) {
233
233
  const prefix = reads[y].slice(0, reads[y].length - 1);
234
234
  const matchedKeys = Object.keys(source).filter(key => key.startsWith(prefix));
235
235
  for (let z = 0; z < matchedKeys.length; z++) {
236
+ // No keyFunc passed because dot paths are not supported for wildcard paths
236
237
  const [value, found] = GetPathValue(source, matchedKeys[z]);
237
238
  allPathsExist = allPathsExist && found;
238
239
  keys.push(matchedKeys[z]);
@@ -253,6 +254,12 @@ function RunProjections(source, keyFunc, projections, memo) {
253
254
  }
254
255
  return diffResult;
255
256
  }
257
+ function ResolveKeyed(source, value, keyFunc) {
258
+ if (keyFunc === undefined || JsonType(value) === 'value')
259
+ return value;
260
+ const key = keyFunc(value);
261
+ return (key && source[key]) ?? value;
262
+ }
256
263
  /**
257
264
  * Resolves a path to its keyed root object if applicable.
258
265
  * Searches up the path to find an object with a key from keyFunc.
@@ -402,7 +409,7 @@ export class DiffTree {
402
409
  * @returns The value at the specified path
403
410
  */
404
411
  GetPath(path) {
405
- const [value] = GetPathValue(this.rootState, path);
412
+ const [value] = GetPathValue(this.rootState, path, this.keyFunc);
406
413
  return value;
407
414
  }
408
415
  /**
@@ -411,7 +418,7 @@ export class DiffTree {
411
418
  * @param path - Dot-separated path to the value
412
419
  */
413
420
  Snapshot(path) {
414
- const [value, found] = GetPathValue(this.rootState, path);
421
+ const [value, found] = GetPathValue(this.rootState, path, this.keyFunc);
415
422
  if (!found)
416
423
  throw `Unable to get snapshot for path ${path}`;
417
424
  return CreateSnapshot(this.rootState, this.keyFunc, value);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "j-templates",
3
- "version": "8.0.23",
3
+ "version": "8.0.24",
4
4
  "description": "j-templates",
5
5
  "license": "MIT",
6
6
  "repository": "https://github.com/TypesInCode/jTemplates",