5etools-utils 0.12.18 → 0.12.20

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.
@@ -96,6 +96,23 @@ class _BrewIndexMeta extends _BrewIndex {
96
96
  }
97
97
  }
98
98
 
99
+ class _BrewIndexAdventureBookIds extends _BrewIndex {
100
+ static _FILE_PATH = "_generated/index-adventure-book-ids.json";
101
+ static _DISPLAY_NAME = "ids";
102
+
103
+ addToIndex (fileInfo) {
104
+ ["adventure", "book"]
105
+ .forEach(prop => {
106
+ (fileInfo.contents[prop] || [])
107
+ .forEach(({name, source, id}) => {
108
+ if (!name || !source || !id) return;
109
+ // Lowercase the key, as we expect this to come from a hash
110
+ this._index[id.toLowerCase()] = {name, source, id};
111
+ });
112
+ });
113
+ }
114
+ }
115
+
99
116
  class BrewIndexGenerator {
100
117
  static _DIR_TO_PRIMARY_PROP = {
101
118
  "creature": [
@@ -147,6 +164,7 @@ class BrewIndexGenerator {
147
164
  new _BrewIndexProps(),
148
165
  new _BrewIndexSources(),
149
166
  new _BrewIndexMeta(),
167
+ new _BrewIndexAdventureBookIds(),
150
168
  ];
151
169
 
152
170
  Um.info(`INDEX`, `Indexing...`);
package/lib/TestJson.js CHANGED
@@ -3,16 +3,14 @@ import * as path from "path";
3
3
  import * as url from "url";
4
4
  import {Worker} from "worker_threads";
5
5
 
6
- import Ajv2020 from "ajv/dist/2020.js";
7
- import addFormats from "ajv-formats";
8
6
  import * as jsonSourceMap from "json-source-map";
9
- import NP from "number-precision";
10
7
 
11
8
  import * as Uf from "./UtilFs.js";
12
9
  import Um from "./UtilMisc.js";
13
10
  import {WorkerList, Deferred} from "./WorkerList.js";
14
11
  import {ObjectWalker} from "./ObjectWalker.js";
15
12
  import {UrlUtil} from "./UrlUtil.js";
13
+ import {UtilAjv} from "./UtilAjv.js";
16
14
 
17
15
  const __dirname = url.fileURLToPath(new URL(".", import.meta.url));
18
16
 
@@ -28,8 +26,6 @@ const MODES = {
28
26
  };
29
27
 
30
28
  class JsonTester {
31
- static _RE_DATE = /^\d\d\d\d-\d\d-\d\d$/;
32
-
33
29
  constructor (
34
30
  {
35
31
  fnGetSchemaId,
@@ -53,41 +49,7 @@ class JsonTester {
53
49
 
54
50
  this._fnGetSchemaId = fnGetSchemaId;
55
51
 
56
- // region Set up validator
57
- this._ajv = new Ajv2020({
58
- allowUnionTypes: true,
59
- });
60
- addFormats(this._ajv);
61
-
62
- this._ajv.addKeyword({
63
- keyword: "version",
64
- validate: false,
65
- });
66
-
67
- this._ajv.addFormat(
68
- "date",
69
- {
70
- validate: str => JsonTester._RE_DATE.test(str),
71
- },
72
- );
73
- // endregion
74
-
75
- // region Patch AJV `"multipleOf"`
76
- // See: https://github.com/ajv-validator/ajv/issues/652#issuecomment-944202626
77
- this._ajv.removeKeyword("multipleOf");
78
-
79
- this._ajv.addKeyword({
80
- keyword: "multipleOf",
81
- type: "number",
82
- compile (schema) {
83
- return (data) => Number.isInteger(NP.divide(data, schema));
84
- },
85
- errors: false,
86
- metaSchema: {
87
- type: "number",
88
- },
89
- });
90
- // endregion
52
+ this._ajv = UtilAjv.getValidator();
91
53
 
92
54
  this._pLoadSchema = null;
93
55
  }
package/lib/UtilAjv.js ADDED
@@ -0,0 +1,47 @@
1
+ import Ajv2020 from "ajv/dist/2020.js";
2
+ import addFormats from "ajv-formats";
3
+ import NP from "number-precision";
4
+
5
+ export class UtilAjv {
6
+ static _RE_DATE = /^\d\d\d\d-\d\d-\d\d$/;
7
+
8
+ static getValidator () {
9
+ // region Set up validator
10
+ const ajv = new Ajv2020({
11
+ allowUnionTypes: true,
12
+ });
13
+ addFormats(ajv);
14
+
15
+ ajv.addKeyword({
16
+ keyword: "version",
17
+ validate: false,
18
+ });
19
+
20
+ ajv.addFormat(
21
+ "date",
22
+ {
23
+ validate: str => this._RE_DATE.test(str),
24
+ },
25
+ );
26
+ // endregion
27
+
28
+ // region Patch AJV `"multipleOf"`
29
+ // See: https://github.com/ajv-validator/ajv/issues/652#issuecomment-944202626
30
+ ajv.removeKeyword("multipleOf");
31
+
32
+ ajv.addKeyword({
33
+ keyword: "multipleOf",
34
+ type: "number",
35
+ compile (schema) {
36
+ return (data) => Number.isInteger(NP.divide(data, schema));
37
+ },
38
+ errors: false,
39
+ metaSchema: {
40
+ type: "number",
41
+ },
42
+ });
43
+ // endregion
44
+
45
+ return ajv;
46
+ }
47
+ }
package/lib/UtilSource.js CHANGED
@@ -1,6 +1,9 @@
1
1
  import url from "url";
2
2
  import path from "path";
3
+
3
4
  import {readJsonSync} from "./UtilFs.js";
5
+ import {UtilAjv} from "./UtilAjv.js";
6
+ import * as Uf from "./UtilFs.js";
4
7
 
5
8
  const __dirname = url.fileURLToPath(new URL(".", import.meta.url));
6
9
 
@@ -16,4 +19,35 @@ export class UtilSource {
16
19
 
17
20
  return this._SOURCES.has(source?.toLowerCase());
18
21
  }
22
+
23
+ /* -------------------------------------------- */
24
+
25
+ static _HOMEBREW_SOURCE_VALIDATOR = null;
26
+
27
+ static isValidHomebrewSorce (source) {
28
+ if (!this._HOMEBREW_SOURCE_VALIDATOR) {
29
+ this._HOMEBREW_SOURCE_VALIDATOR = UtilAjv.getValidator();
30
+
31
+ [
32
+ "util.json",
33
+ "sources-homebrew-legacy.json",
34
+ "sources-5etools.json",
35
+ ]
36
+ .forEach(fname => {
37
+ this._HOMEBREW_SOURCE_VALIDATOR.addSchema(
38
+ Uf.readJsonSync(path.join(path.join(__dirname, "..", "schema", "brew", fname))),
39
+ fname,
40
+ );
41
+ });
42
+
43
+ this._HOMEBREW_SOURCE_VALIDATOR.addSchema(
44
+ {
45
+ "$ref": "util.json#/$defs/sourceJson",
46
+ },
47
+ "homebrewSource",
48
+ );
49
+ }
50
+
51
+ return this._HOMEBREW_SOURCE_VALIDATOR.validate("homebrewSource", source);
52
+ }
19
53
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "5etools-utils",
3
- "version": "0.12.18",
3
+ "version": "0.12.20",
4
4
  "description": "Shared utilities for the 5etools ecosystem.",
5
5
  "type": "module",
6
6
  "main": "lib/Api.js",