lucid-extension-sdk 0.0.340 → 0.0.341

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/checks.d.ts CHANGED
@@ -117,6 +117,13 @@ export declare function isArray(val: unknown): val is unknown[];
117
117
  * @return Whether variable is an array of the given type.
118
118
  */
119
119
  export declare function isTypedArray<T>(typeGuard: (a: unknown) => a is T): (val: unknown) => val is T[];
120
+ /**
121
+ * Returns true if the specified value is a map.
122
+ *
123
+ * @param val Variable to test.
124
+ * @return Whether variable is a map.
125
+ */
126
+ export declare function isMap(val: unknown): val is Map<unknown, unknown>;
120
127
  export type Tuple<T, N extends number> = N extends N ? (number extends N ? T[] : TupleOfHelper<T, N, []>) : never;
121
128
  type TupleOfHelper<T, N extends number, R extends unknown[]> = R['length'] extends N ? R : TupleOfHelper<T, N, [T, ...R]>;
122
129
  export declare function isExactLength<T, N extends number>(arr: readonly T[], exactLength: N): arr is Tuple<T, N>;
package/core/checks.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isInstanceOf = exports.isLiteral = exports.isPromise = exports.isUnknown = exports.isAny = exports.isEmptyOrNullishObject = exports.isPair = exports.isAtLeastLength = exports.isExactLength = exports.isTypedArray = exports.isArray = exports.isRecord = exports.isObjectUnsafer = exports.isObjectUnsafe = exports.isObject = exports.isFunction = exports.isInfinite = exports.isInt = exports.isNumber = exports.isBoolean = exports.isString = exports.isNullish = exports.isDefAndNotNull = exports.isVoid = exports.isUndefined = exports.isNull = exports.isDef = void 0;
3
+ exports.isInstanceOf = exports.isLiteral = exports.isPromise = exports.isUnknown = exports.isAny = exports.isEmptyOrNullishObject = exports.isPair = exports.isAtLeastLength = exports.isExactLength = exports.isMap = exports.isTypedArray = exports.isArray = exports.isRecord = exports.isObjectUnsafer = exports.isObjectUnsafe = exports.isObject = exports.isFunction = exports.isInfinite = exports.isInt = exports.isNumber = exports.isBoolean = exports.isString = exports.isNullish = exports.isDefAndNotNull = exports.isVoid = exports.isUndefined = exports.isNull = exports.isDef = void 0;
4
4
  /**
5
5
  * Returns true if the specified value is not undefined.
6
6
  *
@@ -177,6 +177,16 @@ function isTypedArray(typeGuard) {
177
177
  };
178
178
  }
179
179
  exports.isTypedArray = isTypedArray;
180
+ /**
181
+ * Returns true if the specified value is a map.
182
+ *
183
+ * @param val Variable to test.
184
+ * @return Whether variable is a map.
185
+ */
186
+ function isMap(val) {
187
+ return val instanceof Map;
188
+ }
189
+ exports.isMap = isMap;
180
190
  function isExactLength(arr, exactLength) {
181
191
  return arr.length === exactLength;
182
192
  }
package/core/guards.d.ts CHANGED
@@ -6,4 +6,5 @@ export type DestructureGuardedTypeObj<Obj extends {
6
6
  [key in keyof Obj]: GuardToType<Obj[key]>;
7
7
  }>;
8
8
  export type Validator<TO extends FROM, FROM = unknown> = (p1: FROM) => p1 is TO;
9
- export type ValidatorWithList<TO extends FROM, FROM = unknown> = (p1: FROM, p2: unknown[]) => p1 is TO;
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;
@@ -0,0 +1,21 @@
1
+ import { Pruner } from '../guards';
2
+ /**
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.
6
+ *
7
+ * @returns A pruner function that takes data and invalid fields. If data is not an
8
+ * object, it will return the data as is instead of pruning.
9
+ */
10
+ export declare function objectPruner(subPruners: {
11
+ [key: string]: Pruner;
12
+ }): Pruner;
13
+ /**
14
+ * Creates a pruner function that will remove array elements that are invalid.
15
+ * If a sub-pruner is provided, then the elements themselves will be pruned rather
16
+ * than removed from the list.
17
+ *
18
+ * @returns A pruner function that takes data and invalid fields. If data is not an
19
+ * array, it will return the data as is instead of pruning.
20
+ */
21
+ export declare function arrayPruner(subPruner?: Pruner | undefined): Pruner;
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.arrayPruner = exports.objectPruner = void 0;
4
+ const checks_1 = require("../checks");
5
+ /**
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.
9
+ *
10
+ * @returns A pruner function that takes data and invalid fields. If data is not an
11
+ * object, it will return the data as is instead of pruning.
12
+ */
13
+ function objectPruner(subPruners) {
14
+ return (data, invalidFields, level = 0) => {
15
+ if ((0, checks_1.isArray)(data) || !(0, checks_1.isObjectUnsafe)(data)) {
16
+ return data;
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;
32
+ };
33
+ }
34
+ exports.objectPruner = objectPruner;
35
+ /**
36
+ * Creates a pruner function that will remove array elements that are invalid.
37
+ * If a sub-pruner is provided, then the elements themselves will be pruned rather
38
+ * than removed from the list.
39
+ *
40
+ * @returns A pruner function that takes data and invalid fields. If data is not an
41
+ * array, it will return the data as is instead of pruning.
42
+ */
43
+ function arrayPruner(subPruner) {
44
+ return (data, invalidElements, level = 0) => {
45
+ if (!(0, checks_1.isArray)(data)) {
46
+ return data;
47
+ }
48
+ if (subPruner) {
49
+ return data.map((item) => subPruner(item, invalidElements, level + 1));
50
+ }
51
+ else {
52
+ 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)); });
53
+ }
54
+ };
55
+ }
56
+ exports.arrayPruner = arrayPruner;
@@ -29,11 +29,10 @@ export declare function rangeValidator(min: number, max: number): (x: unknown) =
29
29
  */
30
30
  export declare function arrayValidator<T>(subValidator: (p1: unknown) => p1 is T): (p1: unknown) => p1 is T[];
31
31
  /**
32
- * valid if at least one item in the array is valid.
33
- * @param subValidator
34
- * @returns
32
+ * This validator functions the same as {@link arrayValidator}, with the option of passing in a map
33
+ * which will track all of the elements that were found to be invalid.
35
34
  */
36
- export declare function arrayValidatorWithList<T>(subValidator: (p1: unknown, invalidItems?: unknown[]) => p1 is T): (p1: unknown, invalidItems?: unknown[]) => p1 is T[];
35
+ export declare function arrayValidatorWithInvalidElementTracking<T>(subValidator: (p1: unknown, invalidElements?: Map<number, unknown[]>, level?: number) => p1 is T): (subject: unknown, invalidElements?: Map<number, unknown[]>, level?: number) => subject is T[];
37
36
  /**
38
37
  * Creates a validator for a fixed width array where each entry
39
38
  * in the array can have a separate validator
@@ -76,12 +75,12 @@ export declare function objectValidator<T extends {
76
75
  [key: string]: (p1: unknown) => p1 is unknown;
77
76
  }>(validatorStructure: T): (subject: unknown) => subject is DestructureGuardedTypeObj<T>;
78
77
  /**
79
- * This validator functions the same as {@link objectValidator}, with the option of passing in a list
80
- * which will return all of the fields that were found to be invalid.
78
+ * This validator functions the same as {@link objectValidator}, with the option of passing in a map
79
+ * which will track all of the fields that were found to be invalid.
81
80
  */
82
- export declare function objectValidatorWithList<T extends {
83
- [key: string]: (p1: unknown, invalidFields?: unknown[]) => p1 is unknown;
84
- }>(validatorStructure: T): (subject: unknown, invalidFields?: unknown[]) => subject is DestructureGuardedTypeObj<T>;
81
+ export declare function objectValidatorWithInvalidFieldTracking<T extends {
82
+ [key: string]: (p1: unknown, invalidFields?: Map<number, unknown[]>, level?: number) => p1 is unknown;
83
+ }>(validatorStructure: T): (subject: unknown, invalidFields?: Map<number, unknown[]>, level?: number) => subject is DestructureGuardedTypeObj<T>;
85
84
  /**
86
85
  * Creates a validator which tests if the target is an object
87
86
  * and if the structure of the object matches the structure of the passed-in
@@ -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.nullable = exports.objectOfValidator = exports.typedRecordValidator = exports.recordValidator = exports.strictObjectValidator = exports.partialObjectValidator = exports.objectValidatorWithList = exports.objectValidator = exports.mapValidator = exports.someValidator = exports.someValue = exports.tupleValidator = exports.arrayValidatorWithList = 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.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;
4
4
  const checks_1 = require("../checks");
5
5
  /*********************************************************************************
6
6
  * Validator generators: These functions construct new composite validators
@@ -65,19 +65,27 @@ function arrayValidator(subValidator) {
65
65
  }
66
66
  exports.arrayValidator = arrayValidator;
67
67
  /**
68
- * valid if at least one item in the array is valid.
69
- * @param subValidator
70
- * @returns
68
+ * This validator functions the same as {@link arrayValidator}, with the option of passing in a map
69
+ * which will track all of the elements that were found to be invalid.
71
70
  */
72
- function arrayValidatorWithList(subValidator) {
73
- return (x, invalidItems) => {
74
- if (!(0, checks_1.isArray)(x)) {
71
+ function arrayValidatorWithInvalidElementTracking(subValidator) {
72
+ return (subject, invalidElements, level = 0) => {
73
+ if (!(0, checks_1.isArray)(subject)) {
75
74
  return false;
76
75
  }
77
- return x.some((item) => subValidator(item, invalidItems));
76
+ let valid = true;
77
+ const invalidList = [];
78
+ subject.forEach((item) => {
79
+ if (!subValidator(item, invalidElements, level + 1)) {
80
+ invalidList.push(item);
81
+ valid = false;
82
+ }
83
+ });
84
+ invalidElements === null || invalidElements === void 0 ? void 0 : invalidElements.set(level, invalidList);
85
+ return valid;
78
86
  };
79
87
  }
80
- exports.arrayValidatorWithList = arrayValidatorWithList;
88
+ exports.arrayValidatorWithInvalidElementTracking = arrayValidatorWithInvalidElementTracking;
81
89
  /**
82
90
  * Creates a validator for a fixed width array where each entry
83
91
  * in the array can have a separate validator
@@ -160,29 +168,31 @@ function objectValidator(validatorStructure) {
160
168
  }
161
169
  exports.objectValidator = objectValidator;
162
170
  /**
163
- * This validator functions the same as {@link objectValidator}, with the option of passing in a list
164
- * which will return all of the fields that were found to be invalid.
171
+ * This validator functions the same as {@link objectValidator}, with the option of passing in a map
172
+ * which will track all of the fields that were found to be invalid.
165
173
  */
166
- function objectValidatorWithList(validatorStructure) {
167
- return (subject, invalidFields) => {
174
+ function objectValidatorWithInvalidFieldTracking(validatorStructure) {
175
+ return (subject, invalidFields, level = 0) => {
168
176
  if ((0, checks_1.isArray)(subject) || !(0, checks_1.isObjectUnsafe)(subject)) {
169
177
  return false;
170
178
  }
171
179
  else {
172
180
  let valid = true;
181
+ const invalidList = [];
173
182
  Object.entries(validatorStructure).forEach(([key, validator]) => {
174
- if (!validator(subject[key], invalidFields)) {
175
- if (invalidFields && Array.isArray(invalidFields)) {
176
- invalidFields.push(key);
177
- }
183
+ if (!validator(subject[key], invalidFields, level + 1)) {
184
+ invalidList.push([key, subject[key]]);
178
185
  valid = false;
179
186
  }
180
187
  });
188
+ if ((0, checks_1.isMap)(invalidFields)) {
189
+ invalidFields.set(level, invalidList);
190
+ }
181
191
  return valid;
182
192
  }
183
193
  };
184
194
  }
185
- exports.objectValidatorWithList = objectValidatorWithList;
195
+ exports.objectValidatorWithInvalidFieldTracking = objectValidatorWithInvalidFieldTracking;
186
196
  /**
187
197
  * Creates a validator which tests if the target is an object
188
198
  * and if the structure of the object matches the structure of the passed-in
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lucid-extension-sdk",
3
- "version": "0.0.340",
3
+ "version": "0.0.341",
4
4
  "description": "Utility classes for writing Lucid Software editor extensions",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",