@amritk/helpers 0.13.2 → 0.13.3

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.
@@ -1,175 +1,158 @@
1
- /** Type guard to check if schema is not false */
2
- export const isSchemaObject = (schema) => {
3
- return typeof schema === 'object' && schema !== null && typeof schema !== 'boolean';
4
- };
5
- /** Type guard to check if schema has a type property */
6
- export const hasType = (schema) => {
7
- return isSchemaObject(schema) && 'type' in schema && typeof schema.type === 'string';
8
- };
9
- /** Type guard to check if schema is an object schema */
10
- export const isObjectSchema = (schema) => {
11
- return isSchemaObject(schema) && (('type' in schema && schema.type === 'object') || 'properties' in schema);
12
- };
13
- /** Type guard to check if schema has properties */
14
- export const hasProperties = (schema) => {
15
- return (isSchemaObject(schema) &&
16
- 'properties' in schema &&
17
- typeof schema.properties === 'object' &&
18
- schema.properties !== null);
19
- };
20
- /** Type guard to check if schema has enum */
21
- export const hasEnum = (schema) => {
22
- return isSchemaObject(schema) && 'enum' in schema && Array.isArray(schema.enum);
23
- };
24
- /** Type guard to check if schema has const */
25
- export const hasConst = (schema) => {
26
- return isSchemaObject(schema) && 'const' in schema;
27
- };
28
- /** Type guard to check if schema has pattern */
29
- export const hasPattern = (schema) => {
30
- return isSchemaObject(schema) && 'pattern' in schema && typeof schema.pattern === 'string';
31
- };
32
- /** Type guard to check if schema has format */
33
- export const hasFormat = (schema) => {
34
- return isSchemaObject(schema) && 'format' in schema && typeof schema.format === 'string';
35
- };
36
- /** Type guard to check if schema has default */
37
- export const hasDefault = (schema) => {
38
- return isSchemaObject(schema) && 'default' in schema;
39
- };
40
- /** Type guard to check if schema has examples */
41
- export const hasExamples = (schema) => {
42
- return isSchemaObject(schema) && 'examples' in schema && Array.isArray(schema.examples);
43
- };
44
- /** Type guard to check if schema has oneOf */
45
- export const hasOneOf = (schema) => {
46
- return isSchemaObject(schema) && 'oneOf' in schema && Array.isArray(schema.oneOf);
47
- };
48
- /** Type guard to check if schema has anyOf */
49
- export const hasAnyOf = (schema) => {
50
- return isSchemaObject(schema) && 'anyOf' in schema && Array.isArray(schema.anyOf);
51
- };
52
- /** Type guard to check if schema has allOf */
53
- export const hasAllOf = (schema) => {
54
- return isSchemaObject(schema) && 'allOf' in schema && Array.isArray(schema.allOf);
55
- };
56
- /** Type guard to check if schema has required */
57
- export const hasRequired = (schema) => {
58
- return isSchemaObject(schema) && 'required' in schema && Array.isArray(schema.required);
59
- };
60
- /** Type guard to check if schema has items (and it's not just boolean) */
61
- export const hasItems = (schema) => {
62
- return (isSchemaObject(schema) &&
63
- 'items' in schema &&
64
- typeof schema.items === 'object' &&
65
- schema.items !== null &&
66
- typeof schema.items !== 'boolean');
67
- };
68
- /** Type guard to check if schema has additionalProperties */
69
- export const hasAdditionalProperties = (schema) => {
70
- return isSchemaObject(schema) && 'additionalProperties' in schema;
71
- };
72
- /** Type guard to check if schema has minLength */
73
- export const hasMinLength = (schema) => {
74
- return isSchemaObject(schema) && 'minLength' in schema && typeof schema.minLength === 'number';
75
- };
76
- /** Type guard to check if schema has maxLength */
77
- export const hasMaxLength = (schema) => {
78
- return isSchemaObject(schema) && 'maxLength' in schema && typeof schema.maxLength === 'number';
79
- };
80
- /** Type guard to check if schema has minimum */
81
- export const hasMinimum = (schema) => {
82
- return isSchemaObject(schema) && 'minimum' in schema && typeof schema.minimum === 'number';
83
- };
84
- /** Type guard to check if schema has maximum */
85
- export const hasMaximum = (schema) => {
86
- return isSchemaObject(schema) && 'maximum' in schema && typeof schema.maximum === 'number';
87
- };
88
- /** Type guard to check if schema has exclusiveMinimum */
89
- export const hasExclusiveMinimum = (schema) => {
90
- return isSchemaObject(schema) && 'exclusiveMinimum' in schema && typeof schema.exclusiveMinimum === 'number';
91
- };
92
- /** Type guard to check if schema has exclusiveMaximum */
93
- export const hasExclusiveMaximum = (schema) => {
94
- return isSchemaObject(schema) && 'exclusiveMaximum' in schema && typeof schema.exclusiveMaximum === 'number';
95
- };
96
- /**
97
- * Draft-04 expressed a strict lower bound as a boolean `exclusiveMinimum: true`
98
- * paired with `minimum`, where draft-06+ uses a standalone numeric keyword. True
99
- * only for that legacy boolean form, so callers can tighten the `minimum` compare.
100
- */
101
- export const hasStrictExclusiveMinimum = (schema) => {
102
- if (!isSchemaObject(schema))
103
- return false;
104
- const flag = schema.exclusiveMinimum;
105
- return flag === true;
106
- };
107
- /** Draft-04 boolean `exclusiveMaximum: true` paired with `maximum`. See {@link hasStrictExclusiveMinimum}. */
108
- export const hasStrictExclusiveMaximum = (schema) => {
109
- if (!isSchemaObject(schema))
110
- return false;
111
- const flag = schema.exclusiveMaximum;
112
- return flag === true;
113
- };
114
- /** Type guard to check if schema has multipleOf */
115
- export const hasMultipleOf = (schema) => {
116
- return isSchemaObject(schema) && 'multipleOf' in schema && typeof schema.multipleOf === 'number';
117
- };
118
- /** Type guard to check if schema has minItems */
119
- export const hasMinItems = (schema) => {
120
- return isSchemaObject(schema) && 'minItems' in schema && typeof schema.minItems === 'number';
121
- };
122
- /** Type guard to check if schema has maxItems */
123
- export const hasMaxItems = (schema) => {
124
- return isSchemaObject(schema) && 'maxItems' in schema && typeof schema.maxItems === 'number';
125
- };
126
- /** Type guard to check if schema has uniqueItems */
127
- export const hasUniqueItems = (schema) => {
128
- return isSchemaObject(schema) && 'uniqueItems' in schema && typeof schema.uniqueItems === 'boolean';
129
- };
130
- /** Type guard to check if schema has minProperties */
131
- export const hasMinProperties = (schema) => {
132
- return isSchemaObject(schema) && 'minProperties' in schema && typeof schema.minProperties === 'number';
133
- };
134
- /** Type guard to check if schema has maxProperties */
135
- export const hasMaxProperties = (schema) => {
136
- return isSchemaObject(schema) && 'maxProperties' in schema && typeof schema.maxProperties === 'number';
137
- };
138
- /** Type guard to check if schema has dependentRequired (2020-12). */
139
- export const hasDependentRequired = (schema) => {
140
- return (isSchemaObject(schema) &&
141
- 'dependentRequired' in schema &&
142
- typeof schema.dependentRequired === 'object' &&
143
- schema.dependentRequired !== null);
144
- };
145
- /** Type guard to check if schema has a propertyNames subschema. */
146
- export const hasPropertyNames = (schema) => {
147
- return isSchemaObject(schema) && 'propertyNames' in schema;
148
- };
149
- /** Type guard to check if schema has dependentSchemas (2020-12). */
150
- export const hasDependentSchemas = (schema) => {
151
- return (isSchemaObject(schema) &&
152
- 'dependentSchemas' in schema &&
153
- typeof schema.dependentSchemas === 'object' &&
154
- schema.dependentSchemas !== null);
155
- };
156
- /** Type guard to check if schema has patternProperties. */
157
- export const hasPatternProperties = (schema) => {
158
- return (isSchemaObject(schema) &&
159
- 'patternProperties' in schema &&
160
- typeof schema.patternProperties === 'object' &&
161
- schema.patternProperties !== null);
162
- };
163
- /** Type guard to check if schema has a `contains` subschema (array). */
164
- export const hasContains = (schema) => {
165
- return isSchemaObject(schema) && 'contains' in schema;
166
- };
167
- /** Type guard to check if schema has a `not` subschema. */
168
- export const hasNot = (schema) => {
169
- return isSchemaObject(schema) && 'not' in schema;
170
- };
171
- /** Type guard to check if schema has an `if` subschema (with optional then/else). */
172
- export const hasIf = (schema) => {
173
- return isSchemaObject(schema) && 'if' in schema;
174
- };
175
- export { hasRef } from './has-ref.js';
1
+ const isSchemaObject = (schema) => {
2
+ return typeof schema === "object" && schema !== null && typeof schema !== "boolean";
3
+ };
4
+ const hasType = (schema) => {
5
+ return isSchemaObject(schema) && "type" in schema && typeof schema.type === "string";
6
+ };
7
+ const isObjectSchema = (schema) => {
8
+ return isSchemaObject(schema) && ("type" in schema && schema.type === "object" || "properties" in schema);
9
+ };
10
+ const hasProperties = (schema) => {
11
+ return isSchemaObject(schema) && "properties" in schema && typeof schema.properties === "object" && schema.properties !== null;
12
+ };
13
+ const hasEnum = (schema) => {
14
+ return isSchemaObject(schema) && "enum" in schema && Array.isArray(schema.enum);
15
+ };
16
+ const hasConst = (schema) => {
17
+ return isSchemaObject(schema) && "const" in schema;
18
+ };
19
+ const hasPattern = (schema) => {
20
+ return isSchemaObject(schema) && "pattern" in schema && typeof schema.pattern === "string";
21
+ };
22
+ const hasFormat = (schema) => {
23
+ return isSchemaObject(schema) && "format" in schema && typeof schema.format === "string";
24
+ };
25
+ const hasDefault = (schema) => {
26
+ return isSchemaObject(schema) && "default" in schema;
27
+ };
28
+ const hasExamples = (schema) => {
29
+ return isSchemaObject(schema) && "examples" in schema && Array.isArray(schema.examples);
30
+ };
31
+ const hasOneOf = (schema) => {
32
+ return isSchemaObject(schema) && "oneOf" in schema && Array.isArray(schema.oneOf);
33
+ };
34
+ const hasAnyOf = (schema) => {
35
+ return isSchemaObject(schema) && "anyOf" in schema && Array.isArray(schema.anyOf);
36
+ };
37
+ const hasAllOf = (schema) => {
38
+ return isSchemaObject(schema) && "allOf" in schema && Array.isArray(schema.allOf);
39
+ };
40
+ const hasRequired = (schema) => {
41
+ return isSchemaObject(schema) && "required" in schema && Array.isArray(schema.required);
42
+ };
43
+ const hasItems = (schema) => {
44
+ return isSchemaObject(schema) && "items" in schema && typeof schema.items === "object" && schema.items !== null && typeof schema.items !== "boolean";
45
+ };
46
+ const hasAdditionalProperties = (schema) => {
47
+ return isSchemaObject(schema) && "additionalProperties" in schema;
48
+ };
49
+ const hasMinLength = (schema) => {
50
+ return isSchemaObject(schema) && "minLength" in schema && typeof schema.minLength === "number";
51
+ };
52
+ const hasMaxLength = (schema) => {
53
+ return isSchemaObject(schema) && "maxLength" in schema && typeof schema.maxLength === "number";
54
+ };
55
+ const hasMinimum = (schema) => {
56
+ return isSchemaObject(schema) && "minimum" in schema && typeof schema.minimum === "number";
57
+ };
58
+ const hasMaximum = (schema) => {
59
+ return isSchemaObject(schema) && "maximum" in schema && typeof schema.maximum === "number";
60
+ };
61
+ const hasExclusiveMinimum = (schema) => {
62
+ return isSchemaObject(schema) && "exclusiveMinimum" in schema && typeof schema.exclusiveMinimum === "number";
63
+ };
64
+ const hasExclusiveMaximum = (schema) => {
65
+ return isSchemaObject(schema) && "exclusiveMaximum" in schema && typeof schema.exclusiveMaximum === "number";
66
+ };
67
+ const hasStrictExclusiveMinimum = (schema) => {
68
+ if (!isSchemaObject(schema))
69
+ return false;
70
+ const flag = schema.exclusiveMinimum;
71
+ return flag === true;
72
+ };
73
+ const hasStrictExclusiveMaximum = (schema) => {
74
+ if (!isSchemaObject(schema))
75
+ return false;
76
+ const flag = schema.exclusiveMaximum;
77
+ return flag === true;
78
+ };
79
+ const hasMultipleOf = (schema) => {
80
+ return isSchemaObject(schema) && "multipleOf" in schema && typeof schema.multipleOf === "number";
81
+ };
82
+ const hasMinItems = (schema) => {
83
+ return isSchemaObject(schema) && "minItems" in schema && typeof schema.minItems === "number";
84
+ };
85
+ const hasMaxItems = (schema) => {
86
+ return isSchemaObject(schema) && "maxItems" in schema && typeof schema.maxItems === "number";
87
+ };
88
+ const hasUniqueItems = (schema) => {
89
+ return isSchemaObject(schema) && "uniqueItems" in schema && typeof schema.uniqueItems === "boolean";
90
+ };
91
+ const hasMinProperties = (schema) => {
92
+ return isSchemaObject(schema) && "minProperties" in schema && typeof schema.minProperties === "number";
93
+ };
94
+ const hasMaxProperties = (schema) => {
95
+ return isSchemaObject(schema) && "maxProperties" in schema && typeof schema.maxProperties === "number";
96
+ };
97
+ const hasDependentRequired = (schema) => {
98
+ return isSchemaObject(schema) && "dependentRequired" in schema && typeof schema.dependentRequired === "object" && schema.dependentRequired !== null;
99
+ };
100
+ const hasPropertyNames = (schema) => {
101
+ return isSchemaObject(schema) && "propertyNames" in schema;
102
+ };
103
+ const hasDependentSchemas = (schema) => {
104
+ return isSchemaObject(schema) && "dependentSchemas" in schema && typeof schema.dependentSchemas === "object" && schema.dependentSchemas !== null;
105
+ };
106
+ const hasPatternProperties = (schema) => {
107
+ return isSchemaObject(schema) && "patternProperties" in schema && typeof schema.patternProperties === "object" && schema.patternProperties !== null;
108
+ };
109
+ const hasContains = (schema) => {
110
+ return isSchemaObject(schema) && "contains" in schema;
111
+ };
112
+ const hasNot = (schema) => {
113
+ return isSchemaObject(schema) && "not" in schema;
114
+ };
115
+ const hasIf = (schema) => {
116
+ return isSchemaObject(schema) && "if" in schema;
117
+ };
118
+ import { hasRef } from "./has-ref.js";
119
+ export {
120
+ hasAdditionalProperties,
121
+ hasAllOf,
122
+ hasAnyOf,
123
+ hasConst,
124
+ hasContains,
125
+ hasDefault,
126
+ hasDependentRequired,
127
+ hasDependentSchemas,
128
+ hasEnum,
129
+ hasExamples,
130
+ hasExclusiveMaximum,
131
+ hasExclusiveMinimum,
132
+ hasFormat,
133
+ hasIf,
134
+ hasItems,
135
+ hasMaxItems,
136
+ hasMaxLength,
137
+ hasMaxProperties,
138
+ hasMaximum,
139
+ hasMinItems,
140
+ hasMinLength,
141
+ hasMinProperties,
142
+ hasMinimum,
143
+ hasMultipleOf,
144
+ hasNot,
145
+ hasOneOf,
146
+ hasPattern,
147
+ hasPatternProperties,
148
+ hasProperties,
149
+ hasPropertyNames,
150
+ hasRef,
151
+ hasRequired,
152
+ hasStrictExclusiveMaximum,
153
+ hasStrictExclusiveMinimum,
154
+ hasType,
155
+ hasUniqueItems,
156
+ isObjectSchema,
157
+ isSchemaObject
158
+ };
@@ -1,40 +1,22 @@
1
- /**
2
- * Default key-count cutoff below which an unknown-key sweep inlines `!==`
3
- * comparisons instead of a hoisted `Set`. Chosen so typical schema objects stay
4
- * on the faster inline path while pathologically wide objects fall back to the
5
- * `Set`'s O(1) lookup.
6
- */
7
- export const INLINE_KEY_LIMIT = 16;
8
- /**
9
- * Builds the "is this an undeclared key" test used by `additionalProperties:
10
- * false` sweeps in the generated parsers and validators.
11
- *
12
- * For a small number of known keys V8 evaluates a chain of `!==` string
13
- * comparisons faster than `Set.has` (which has to hash the string), and the
14
- * inline form skips the per-module `Set` allocation — the same shape Ajv and
15
- * TypeBox compile to. Above `inlineLimit` the chain grows long enough that the
16
- * `Set`'s O(1) lookup wins, so a `Set` named `setName` is hoisted instead.
17
- *
18
- * @example
19
- * const check = unknownKeyCheck(['id', 'name'], '_knownKeys0')
20
- * check.declarations // []
21
- * check.isUnknown('_k') // '_k !== "id" && _k !== "name"'
22
- * check.isKnown('_k') // '_k === "id" || _k === "name"'
23
- */
24
- export const unknownKeyCheck = (knownKeys, setName, inlineLimit = INLINE_KEY_LIMIT) => {
25
- if (knownKeys.length === 0) {
26
- return { declarations: [], isUnknown: () => 'true', isKnown: () => 'false' };
27
- }
28
- if (knownKeys.length <= inlineLimit) {
29
- return {
30
- declarations: [],
31
- isUnknown: (keyVar) => knownKeys.map((key) => `${keyVar} !== ${JSON.stringify(key)}`).join(' && '),
32
- isKnown: (keyVar) => knownKeys.map((key) => `${keyVar} === ${JSON.stringify(key)}`).join(' || '),
33
- };
34
- }
1
+ const INLINE_KEY_LIMIT = 16;
2
+ const unknownKeyCheck = (knownKeys, setName, inlineLimit = INLINE_KEY_LIMIT) => {
3
+ if (knownKeys.length === 0) {
4
+ return { declarations: [], isUnknown: () => "true", isKnown: () => "false" };
5
+ }
6
+ if (knownKeys.length <= inlineLimit) {
35
7
  return {
36
- declarations: [`const ${setName} = new Set(${JSON.stringify(knownKeys)})`],
37
- isUnknown: (keyVar) => `!${setName}.has(${keyVar})`,
38
- isKnown: (keyVar) => `${setName}.has(${keyVar})`,
8
+ declarations: [],
9
+ isUnknown: (keyVar) => knownKeys.map((key) => `${keyVar} !== ${JSON.stringify(key)}`).join(" && "),
10
+ isKnown: (keyVar) => knownKeys.map((key) => `${keyVar} === ${JSON.stringify(key)}`).join(" || ")
39
11
  };
12
+ }
13
+ return {
14
+ declarations: [`const ${setName} = new Set(${JSON.stringify(knownKeys)})`],
15
+ isUnknown: (keyVar) => `!${setName}.has(${keyVar})`,
16
+ isKnown: (keyVar) => `${setName}.has(${keyVar})`
17
+ };
18
+ };
19
+ export {
20
+ INLINE_KEY_LIMIT,
21
+ unknownKeyCheck
40
22
  };