5etools-utils 0.8.5 → 0.8.7

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/lib/Api.js CHANGED
@@ -5,7 +5,7 @@ import {BrewIndexGenerator} from "./BrewIndexGenerator.js";
5
5
  import {BrewCleaner} from "./BrewCleaner.js";
6
6
  import {BrewTimestamper} from "./BrewTimestamper.js";
7
7
  import {BrewTester} from "./BrewTester.js";
8
- import {getCleanJson} from "./UtilClean.js";
8
+ import {getCleanJson, getCleanString} from "./UtilClean.js";
9
9
  import {DataTester, DataTesterBase, BraceCheck, EscapeCharacterCheck} from "./TestData.js";
10
10
  import {ObjectWalker} from "./ObjectWalker.js";
11
11
 
@@ -18,6 +18,7 @@ export {
18
18
  BrewTimestamper,
19
19
  BrewTester,
20
20
  getCleanJson,
21
+ getCleanString,
21
22
  DataTester,
22
23
  DataTesterBase,
23
24
  BraceCheck,
@@ -9,11 +9,12 @@ class ObjectWalker {
9
9
  lastKey,
10
10
  },
11
11
  ) {
12
- if (!primitiveHandlers[to]) return;
12
+ if (!primitiveHandlers[to]) return obj;
13
13
 
14
- primitiveHandlers[to] instanceof Array
15
- ? primitiveHandlers[to].forEach(ph => ph(obj, {filePath, lastType, lastKey}))
16
- : primitiveHandlers[to](obj, {filePath, lastType, lastKey});
14
+ if (!(primitiveHandlers[to] instanceof Array)) return primitiveHandlers[to](obj, {filePath, lastType, lastKey});
15
+
16
+ return primitiveHandlers[to]
17
+ .reduce((accum, ph) => ph(accum, {filePath, lastType, lastKey}), obj);
17
18
  }
18
19
 
19
20
  static walk (
@@ -23,6 +24,7 @@ class ObjectWalker {
23
24
  primitiveHandlers,
24
25
  lastType,
25
26
  lastKey,
27
+ isModify = false,
26
28
  },
27
29
  ) {
28
30
  const to = typeof obj;
@@ -31,48 +33,50 @@ class ObjectWalker {
31
33
  const nxtOpts = {...opts};
32
34
 
33
35
  if (obj === null) {
34
- this._runHandlers({
36
+ const objMod = this._runHandlers({
35
37
  ...nxtOpts,
36
38
  to: "null",
37
39
  });
38
40
 
41
+ if (isModify) return objMod;
39
42
  return obj;
40
43
  }
41
44
 
42
45
  switch (to) {
43
- case undefined:
46
+ case "undefined":
44
47
  case "boolean":
45
48
  case "number":
46
49
  case "string": {
47
- this._runHandlers({
50
+ const objMod = this._runHandlers({
48
51
  ...nxtOpts,
49
52
  to,
50
53
  });
51
54
 
52
- return obj;
55
+ return isModify ? objMod : obj;
53
56
  }
54
57
 
55
58
  case "object": {
56
59
  if (obj instanceof Array) {
57
- this._runHandlers({
60
+ const objMod = this._runHandlers({
58
61
  ...nxtOpts,
59
62
  to: "array",
60
63
  });
61
64
 
62
- return obj.map(it => this.walk({obj: it, filePath, primitiveHandlers, lastType, lastKey}));
65
+ return (isModify ? objMod : obj)
66
+ .map(it => this.walk({obj: it, filePath, primitiveHandlers, lastType, lastKey, isModify}));
63
67
  }
64
68
 
65
- this._runHandlers({
69
+ const objMod = this._runHandlers({
66
70
  ...nxtOpts,
67
71
  to: "object",
68
72
  });
69
73
 
70
- Object.keys(obj)
71
- .forEach(k => {
72
- obj[k] = this.walk({obj: obj[k], filePath, primitiveHandlers, lastType, lastKey: k});
74
+ Object.entries(isModify ? objMod : obj)
75
+ .forEach(([k, v]) => {
76
+ (isModify ? objMod : obj)[k] = this.walk({obj: v, filePath, primitiveHandlers, lastType, lastKey: k, isModify});
73
77
  });
74
78
 
75
- return obj;
79
+ return isModify ? objMod : obj;
76
80
  }
77
81
 
78
82
  default: throw new Error(`Unhandled type "${to}"`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "5etools-utils",
3
- "version": "0.8.5",
3
+ "version": "0.8.7",
4
4
  "description": "Shared utilities for the 5etools ecosystem.",
5
5
  "type": "module",
6
6
  "main": "lib/Api.js",