lucid-extension-sdk 0.0.356 → 0.0.358

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/core/guards.d.ts CHANGED
@@ -7,4 +7,4 @@ export type DestructureGuardedTypeObj<Obj extends {
7
7
  }>;
8
8
  export type Validator<TO extends FROM, FROM = unknown> = (p1: FROM) => p1 is TO;
9
9
  export type ValidatorWithTracking<TO extends FROM, FROM = unknown> = (p1: FROM, p2?: Map<number, unknown[]>, p3?: number) => p1 is TO;
10
- export type Pruner = (p1: unknown, p2?: Map<number, unknown[]>, p3?: number) => unknown;
10
+ export type Pruner = (p1: unknown, p2?: Map<number, unknown[]>, p3?: number, key?: string | undefined) => unknown;
@@ -1,8 +1,8 @@
1
1
  import { Pruner } from '../guards';
2
2
  /**
3
3
  * Creates a pruner function that will remove object fields that are invalid.
4
- * If a sub-pruner is provided, then the object values themselves will be pruned rather
5
- * than set to undefined.
4
+ * If a sub-pruner is provided for a field, then the object field values will be pruned.
5
+ * If no pruner is provided for field, nothing happens to that field.
6
6
  *
7
7
  * @returns A pruner function that takes data and invalid fields. If data is not an
8
8
  * object, it will return the data as is instead of pruning.
@@ -19,3 +19,4 @@ export declare function objectPruner(subPruners: {
19
19
  * array, it will return the data as is instead of pruning.
20
20
  */
21
21
  export declare function arrayPruner(subPruner?: Pruner | undefined): Pruner;
22
+ export declare function pruneObjectField(): Pruner;
@@ -1,11 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.arrayPruner = exports.objectPruner = void 0;
3
+ exports.pruneObjectField = exports.arrayPruner = exports.objectPruner = void 0;
4
4
  const checks_1 = require("../checks");
5
5
  /**
6
6
  * Creates a pruner function that will remove object fields that are invalid.
7
- * If a sub-pruner is provided, then the object values themselves will be pruned rather
8
- * than set to undefined.
7
+ * If a sub-pruner is provided for a field, then the object field values will be pruned.
8
+ * If no pruner is provided for field, nothing happens to that field.
9
9
  *
10
10
  * @returns A pruner function that takes data and invalid fields. If data is not an
11
11
  * object, it will return the data as is instead of pruning.
@@ -15,20 +15,11 @@ function objectPruner(subPruners) {
15
15
  if ((0, checks_1.isArray)(data) || !(0, checks_1.isObjectUnsafe)(data)) {
16
16
  return data;
17
17
  }
18
- if (Object.keys(subPruners).length > 0) {
19
- Object.entries(subPruners).forEach(([key, pruner]) => {
20
- data[key] = pruner(data[key], invalidFields, level + 1);
21
- });
22
- }
23
- else {
24
- Object.keys(data).forEach((key) => {
25
- var _a;
26
- if ((_a = invalidFields === null || invalidFields === void 0 ? void 0 : invalidFields.get(level)) === null || _a === void 0 ? void 0 : _a.includes(key)) {
27
- data[key] = undefined;
28
- }
29
- });
30
- }
31
- return data;
18
+ const dataToBePruned = Object.assign({}, data);
19
+ Object.entries(subPruners).forEach(([key, pruner]) => {
20
+ dataToBePruned[key] = pruner(dataToBePruned[key], invalidFields, level + 1, key);
21
+ });
22
+ return dataToBePruned;
32
23
  };
33
24
  }
34
25
  exports.objectPruner = objectPruner;
@@ -46,7 +37,19 @@ function arrayPruner(subPruner) {
46
37
  return data;
47
38
  }
48
39
  if (subPruner) {
49
- return data.map((item) => subPruner(item, invalidElements, level + 1));
40
+ return data.reduce((acc, item) => {
41
+ var _a;
42
+ const prunedItem = subPruner(item, invalidElements, level + 1);
43
+ /* Here we are relying on the fact that the pruner will return the same object reference, if it is not
44
+ pruned, but will return a new object reference, if it is pruned. This is a bit of a hack but
45
+ TODO EI 50 we're overhauling how this works in future
46
+ */
47
+ if (prunedItem === item && ((_a = invalidElements === null || invalidElements === void 0 ? void 0 : invalidElements.get(level)) === null || _a === void 0 ? void 0 : _a.includes(item))) {
48
+ return acc;
49
+ }
50
+ acc.push(prunedItem);
51
+ return acc;
52
+ }, []);
50
53
  }
51
54
  else {
52
55
  return data.filter((item) => { var _a; return !((_a = invalidElements === null || invalidElements === void 0 ? void 0 : invalidElements.get(level)) === null || _a === void 0 ? void 0 : _a.includes(item)); });
@@ -54,3 +57,24 @@ function arrayPruner(subPruner) {
54
57
  };
55
58
  }
56
59
  exports.arrayPruner = arrayPruner;
60
+ /*
61
+ * Creates a pruner function to be passed in with the object pruner that will remove the field if it is invalid.
62
+ */
63
+ function pruneObjectField() {
64
+ return (data, invalidFields, level = 0, key = undefined) => {
65
+ var _a;
66
+ // When this is called as a part of objectPruner, level will be atleast 1
67
+ if ((0, checks_1.isArray)(data) || (0, checks_1.isObjectUnsafe)(data) || level <= 0) {
68
+ return data;
69
+ }
70
+ const invalidFieldAtCurrentLevel = (_a = invalidFields === null || invalidFields === void 0 ? void 0 : invalidFields.get(level - 1)) === null || _a === void 0 ? void 0 : _a.filter(checks_1.isArray);
71
+ if (!(0, checks_1.isDef)(invalidFieldAtCurrentLevel)) {
72
+ return data;
73
+ }
74
+ if (invalidFieldAtCurrentLevel.some((value) => value[0] === key && value[1] === data)) {
75
+ return undefined;
76
+ }
77
+ return data;
78
+ };
79
+ }
80
+ exports.pruneObjectField = pruneObjectField;
@@ -1,5 +1,5 @@
1
1
  import { isNumber, isString } from '../checks';
2
- import { DestructureGuardedTypeObj, GuardToType, Validator } from '../guards';
2
+ import { DestructureGuardedTypeObj, GuardToType, Validator, ValidatorWithTracking } from '../guards';
3
3
  /*********************************************************************************
4
4
  * Validator generators: These functions construct new composite validators
5
5
  *from elemental validators.
@@ -104,6 +104,11 @@ export declare function strictObjectValidator<T extends {
104
104
  export declare function recordValidator<K extends string, V>(keyList: K[], valueValidator: Validator<V>): (x: unknown) => x is Record<K, V>;
105
105
  export declare function typedRecordValidator<K extends string | number | symbol, V>(keyValidator: Validator<K>, valueValidator: Validator<V>): (x: unknown) => x is Record<K, V>;
106
106
  export declare function objectOfValidator<T>(subValidator: Validator<T>): (x: unknown) => x is Record<any, T>;
107
+ /**
108
+ * This validator functions the same as {@link objectOfValidator}, with the option of passing in a map
109
+ * which will track all of the fields that were found to be invalid.
110
+ */
111
+ export declare function objectOfValidatorWithInvalidFieldTracking<T>(subValidator: ValidatorWithTracking<T>): (subject: unknown, invalidFields?: Map<number, unknown[]>, level?: number) => subject is Record<any, T>;
107
112
  /**
108
113
  * Create a validator which allows the target to be either null or satisfy the
109
114
  * sub-validator.
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.asAssertion = exports.validatorWithMessage = exports.minLengthValidator = exports.maxLengthValidator = exports.isDate = exports.isPositiveNumber = exports.isSize = exports.isPanelSize = exports.isBoundingBox = exports.isPointLike = exports.isOpacity = exports.isFalse = exports.isTrue = exports.isFlag = exports.isRestrictions = exports.isStringOrNegativeOne = exports.isBooleanOrEmptyString = exports.isNumberOrEmptyString = exports.isSet = exports.propertyValidator = exports.exclude = exports.both = exports.either = exports.isNullOption = exports.nullableOption = exports.option = exports.nullableValidatorWithInvalidElementTracking = exports.nullable = exports.objectOfValidator = exports.typedRecordValidator = exports.recordValidator = exports.strictObjectValidator = exports.partialObjectValidator = exports.objectValidatorWithInvalidFieldTracking = exports.objectValidator = exports.mapValidator = exports.someValidator = exports.someValue = exports.tupleValidator = exports.arrayValidatorWithInvalidElementTracking = exports.arrayValidator = exports.rangeValidator = exports.enumValidator = exports.stringEnumValidator = void 0;
3
+ exports.asAssertion = exports.validatorWithMessage = exports.minLengthValidator = exports.maxLengthValidator = exports.isDate = exports.isPositiveNumber = exports.isSize = exports.isPanelSize = exports.isBoundingBox = exports.isPointLike = exports.isOpacity = exports.isFalse = exports.isTrue = exports.isFlag = exports.isRestrictions = exports.isStringOrNegativeOne = exports.isBooleanOrEmptyString = exports.isNumberOrEmptyString = exports.isSet = exports.propertyValidator = exports.exclude = exports.both = exports.either = exports.isNullOption = exports.nullableOption = exports.option = exports.nullableValidatorWithInvalidElementTracking = exports.nullable = exports.objectOfValidatorWithInvalidFieldTracking = exports.objectOfValidator = exports.typedRecordValidator = exports.recordValidator = exports.strictObjectValidator = exports.partialObjectValidator = exports.objectValidatorWithInvalidFieldTracking = exports.objectValidator = exports.mapValidator = exports.someValidator = exports.someValue = exports.tupleValidator = exports.arrayValidatorWithInvalidElementTracking = exports.arrayValidator = exports.rangeValidator = exports.enumValidator = exports.stringEnumValidator = void 0;
4
4
  const checks_1 = require("../checks");
5
5
  /*********************************************************************************
6
6
  * Validator generators: These functions construct new composite validators
@@ -70,6 +70,7 @@ exports.arrayValidator = arrayValidator;
70
70
  */
71
71
  function arrayValidatorWithInvalidElementTracking(subValidator) {
72
72
  return (subject, invalidElements, level = 0) => {
73
+ var _a;
73
74
  if (!(0, checks_1.isArray)(subject)) {
74
75
  return false;
75
76
  }
@@ -81,7 +82,10 @@ function arrayValidatorWithInvalidElementTracking(subValidator) {
81
82
  valid = false;
82
83
  }
83
84
  });
84
- invalidElements === null || invalidElements === void 0 ? void 0 : invalidElements.set(level, invalidList);
85
+ if ((0, checks_1.isMap)(invalidElements)) {
86
+ const prevInvalidList = (_a = invalidElements.get(level)) !== null && _a !== void 0 ? _a : [];
87
+ invalidElements.set(level, prevInvalidList.concat(invalidList));
88
+ }
85
89
  return valid;
86
90
  };
87
91
  }
@@ -173,6 +177,7 @@ exports.objectValidator = objectValidator;
173
177
  */
174
178
  function objectValidatorWithInvalidFieldTracking(validatorStructure) {
175
179
  return (subject, invalidFields, level = 0) => {
180
+ var _a;
176
181
  if ((0, checks_1.isArray)(subject) || !(0, checks_1.isObjectUnsafe)(subject)) {
177
182
  return false;
178
183
  }
@@ -186,7 +191,8 @@ function objectValidatorWithInvalidFieldTracking(validatorStructure) {
186
191
  }
187
192
  });
188
193
  if ((0, checks_1.isMap)(invalidFields)) {
189
- invalidFields.set(level, invalidList);
194
+ const prevInvalidList = (_a = invalidFields.get(level)) !== null && _a !== void 0 ? _a : [];
195
+ invalidFields.set(level, prevInvalidList.concat(invalidList));
190
196
  }
191
197
  return valid;
192
198
  }
@@ -257,6 +263,32 @@ function objectOfValidator(subValidator) {
257
263
  };
258
264
  }
259
265
  exports.objectOfValidator = objectOfValidator;
266
+ /**
267
+ * This validator functions the same as {@link objectOfValidator}, with the option of passing in a map
268
+ * which will track all of the fields that were found to be invalid.
269
+ */
270
+ function objectOfValidatorWithInvalidFieldTracking(subValidator) {
271
+ return (subject, invalidFields, level = 0) => {
272
+ var _a;
273
+ if ((0, checks_1.isArray)(subject) || !(0, checks_1.isObjectUnsafe)(subject)) {
274
+ return false;
275
+ }
276
+ let valid = true;
277
+ const invalidList = [];
278
+ Object.entries(subject).forEach(([key, val]) => {
279
+ if (!subValidator(val, invalidFields, level + 1)) {
280
+ invalidList.push([key, val]);
281
+ valid = false;
282
+ }
283
+ });
284
+ if ((0, checks_1.isMap)(invalidFields)) {
285
+ const prevInvalidList = (_a = invalidFields.get(level)) !== null && _a !== void 0 ? _a : [];
286
+ invalidFields.set(level, prevInvalidList.concat(invalidList));
287
+ }
288
+ return valid;
289
+ };
290
+ }
291
+ exports.objectOfValidatorWithInvalidFieldTracking = objectOfValidatorWithInvalidFieldTracking;
260
292
  /**
261
293
  * Create a validator which allows the target to be either null or satisfy the
262
294
  * sub-validator.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lucid-extension-sdk",
3
- "version": "0.0.356",
3
+ "version": "0.0.358",
4
4
  "description": "Utility classes for writing Lucid Software editor extensions",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",