@based/schema 5.0.0-alpha.9 → 5.0.1

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.
Files changed (106) hide show
  1. package/dist/def/addEdges.js +26 -3
  2. package/dist/def/createEmptyDef.d.ts +13 -9
  3. package/dist/def/createEmptyDef.js +7 -3
  4. package/dist/def/defaultMap.js +28 -24
  5. package/dist/def/fillEmptyMain.d.ts +2 -2
  6. package/dist/def/fillEmptyMain.js +13 -4
  7. package/dist/def/index.d.ts +0 -1
  8. package/dist/def/index.js +0 -1
  9. package/dist/def/makeSeparateSort.js +6 -6
  10. package/dist/def/makeSeparateTextSort.js +12 -12
  11. package/dist/def/refSet.d.ts +7 -0
  12. package/dist/def/refSet.js +25 -0
  13. package/dist/def/selvaBuffer.js +77 -28
  14. package/dist/def/typeDef.d.ts +9 -3
  15. package/dist/def/typeDef.js +136 -59
  16. package/dist/def/typeIndexes.d.ts +40 -0
  17. package/dist/def/typeIndexes.js +50 -0
  18. package/dist/def/types.d.ts +36 -57
  19. package/dist/def/types.js +22 -28
  20. package/dist/def/utils.d.ts +5 -3
  21. package/dist/def/utils.js +44 -2
  22. package/dist/def/validation.d.ts +2 -0
  23. package/dist/def/validation.js +89 -41
  24. package/dist/index.d.ts +3 -1
  25. package/dist/index.js +3 -1
  26. package/dist/infer.d.ts +82 -0
  27. package/dist/infer.js +5 -0
  28. package/dist/lang.d.ts +3 -1
  29. package/dist/lang.js +6 -0
  30. package/dist/parse/assert.d.ts +4 -0
  31. package/dist/parse/assert.js +19 -2
  32. package/dist/parse/index.d.ts +2 -0
  33. package/dist/parse/index.js +61 -4
  34. package/dist/parse/props.d.ts +1 -0
  35. package/dist/parse/props.js +171 -54
  36. package/dist/parse/semver/_constants.d.ts +12 -0
  37. package/dist/parse/semver/_constants.js +21 -0
  38. package/dist/parse/semver/_shared.d.ts +44 -0
  39. package/dist/parse/semver/_shared.js +165 -0
  40. package/dist/parse/semver/_test_comparator_set.d.ts +3 -0
  41. package/dist/parse/semver/_test_comparator_set.js +60 -0
  42. package/dist/parse/semver/can_parse.d.ts +17 -0
  43. package/dist/parse/semver/can_parse.js +28 -0
  44. package/dist/parse/semver/compare.d.ts +28 -0
  45. package/dist/parse/semver/compare.js +36 -0
  46. package/dist/parse/semver/difference.d.ts +27 -0
  47. package/dist/parse/semver/difference.js +41 -0
  48. package/dist/parse/semver/equals.d.ts +24 -0
  49. package/dist/parse/semver/equals.js +28 -0
  50. package/dist/parse/semver/format.d.ts +22 -0
  51. package/dist/parse/semver/format.js +33 -0
  52. package/dist/parse/semver/format_range.d.ts +18 -0
  53. package/dist/parse/semver/format_range.js +29 -0
  54. package/dist/parse/semver/greater_or_equal.d.ts +25 -0
  55. package/dist/parse/semver/greater_or_equal.js +27 -0
  56. package/dist/parse/semver/greater_than.d.ts +25 -0
  57. package/dist/parse/semver/greater_than.js +29 -0
  58. package/dist/parse/semver/greater_than_range.d.ts +23 -0
  59. package/dist/parse/semver/greater_than_range.js +59 -0
  60. package/dist/parse/semver/increment.d.ts +51 -0
  61. package/dist/parse/semver/increment.js +175 -0
  62. package/dist/parse/semver/is_range.d.ts +23 -0
  63. package/dist/parse/semver/is_range.js +39 -0
  64. package/dist/parse/semver/is_semver.d.ts +34 -0
  65. package/dist/parse/semver/is_semver.js +54 -0
  66. package/dist/parse/semver/less_or_equal.d.ts +25 -0
  67. package/dist/parse/semver/less_or_equal.js +27 -0
  68. package/dist/parse/semver/less_than.d.ts +25 -0
  69. package/dist/parse/semver/less_than.js +27 -0
  70. package/dist/parse/semver/less_than_range.d.ts +23 -0
  71. package/dist/parse/semver/less_than_range.js +59 -0
  72. package/dist/parse/semver/max_satisfying.d.ts +22 -0
  73. package/dist/parse/semver/max_satisfying.js +31 -0
  74. package/dist/parse/semver/min_satisfying.d.ts +22 -0
  75. package/dist/parse/semver/min_satisfying.js +31 -0
  76. package/dist/parse/semver/mod.d.ts +298 -0
  77. package/dist/parse/semver/mod.js +301 -0
  78. package/dist/parse/semver/not_equals.d.ts +24 -0
  79. package/dist/parse/semver/not_equals.js +26 -0
  80. package/dist/parse/semver/parse.d.ts +25 -0
  81. package/dist/parse/semver/parse.js +43 -0
  82. package/dist/parse/semver/parse_range.d.ts +27 -0
  83. package/dist/parse/semver/parse_range.js +361 -0
  84. package/dist/parse/semver/range_intersects.d.ts +25 -0
  85. package/dist/parse/semver/range_intersects.js +83 -0
  86. package/dist/parse/semver/satisfies.d.ts +24 -0
  87. package/dist/parse/semver/satisfies.js +28 -0
  88. package/dist/parse/semver/try_parse.d.ts +21 -0
  89. package/dist/parse/semver/try_parse.js +28 -0
  90. package/dist/parse/semver/try_parse_range.d.ts +23 -0
  91. package/dist/parse/semver/try_parse_range.js +34 -0
  92. package/dist/parse/semver/types.d.ts +58 -0
  93. package/dist/parse/semver/types.js +4 -0
  94. package/dist/serialize.d.ts +14 -0
  95. package/dist/serialize.js +543 -0
  96. package/dist/types.d.ts +74 -20
  97. package/dist/types.js +3 -1
  98. package/package.json +5 -4
  99. package/dist/def/getPropLen.d.ts +0 -3
  100. package/dist/def/getPropLen.js +0 -23
  101. package/dist/def/makePacked.d.ts +0 -3
  102. package/dist/def/makePacked.js +0 -50
  103. package/dist/def/readFromPacked.d.ts +0 -3
  104. package/dist/def/readFromPacked.js +0 -140
  105. package/dist/mermaid.d.ts +0 -3
  106. package/dist/mermaid.js +0 -24
@@ -1,7 +1,12 @@
1
+ import { convertToTimestamp } from '@based/utils';
2
+ import { NUMBER, TYPE_INDEX_MAP } from '../def/types.js';
3
+ import { VALIDATION_MAP } from '../def/validation.js';
1
4
  import { stringFormats, dateDisplays, numberDisplays, } from '../types.js';
2
- import { expectBoolean, expectFloat32Array, expectFunction, expectNumber, expectObject, expectString, } from './assert.js';
3
- import { EXPECTED_ARR, EXPECTED_DATE, EXPECTED_OBJ, EXPECTED_PRIMITIVE, EXPECTED_VALUE_IN_ENUM, INVALID_VALUE, MIN_MAX, MISSING_TYPE, OUT_OF_RANGE, TEXT_REQUIRES_LOCALES, TYPE_MISMATCH, UNKNOWN_PROP, NOT_ALLOWED_IN_ITEMS, } from './errors.js';
5
+ import { expectBoolean, expectFunction, expectNumber, expectObject, expectString, } from './assert.js';
6
+ import { EXPECTED_ARR, EXPECTED_OBJ, EXPECTED_PRIMITIVE, INVALID_VALUE, MIN_MAX, MISSING_TYPE, TEXT_REQUIRES_LOCALES, TYPE_MISMATCH, UNKNOWN_PROP, NOT_ALLOWED_IN_ITEMS, } from './errors.js';
4
7
  import { getPropType } from './utils.js';
8
+ import { DEFAULT_MAP } from '../def/defaultMap.js';
9
+ import { parseMinMaxStep } from '../def/utils.js';
5
10
  let stringFormatsSet;
6
11
  let numberDisplaysSet;
7
12
  let dateDisplaysSet;
@@ -38,16 +43,29 @@ const shared = {
38
43
  throw Error(TYPE_MISMATCH);
39
44
  }
40
45
  },
41
- title(val) { },
42
- description(val) { },
43
- readOnly(val) { },
44
- examples(val) { },
46
+ title(val) {
47
+ expectString(val);
48
+ },
49
+ description(val) {
50
+ expectString(val);
51
+ },
52
+ readOnly(val) {
53
+ expectBoolean(val);
54
+ },
55
+ examples(val) {
56
+ expectString(val);
57
+ },
58
+ validation(val) {
59
+ expectFunction(val);
60
+ },
45
61
  };
46
62
  function propParser(required, optional, allowShorthand) {
47
63
  return (prop, ctx) => {
48
64
  if (typeof prop === 'string') {
49
65
  // allow string
50
66
  if (allowShorthand === 0) {
67
+ // @ts-ignore
68
+ required?.type?.(prop, { type: prop }, ctx);
51
69
  return;
52
70
  }
53
71
  throw Error(EXPECTED_OBJ);
@@ -91,9 +109,67 @@ function propParser(required, optional, allowShorthand) {
91
109
  };
92
110
  }
93
111
  const p = {};
112
+ export const isDefault = (val, prop, ctx) => {
113
+ let typeIndex;
114
+ typeIndex = TYPE_INDEX_MAP[prop.type];
115
+ if ('enum' in prop) {
116
+ typeIndex = TYPE_INDEX_MAP['enum'];
117
+ }
118
+ if (prop.type === 'timestamp') {
119
+ val = convertToTimestamp(val);
120
+ }
121
+ const validation = prop.validation || VALIDATION_MAP[typeIndex];
122
+ const propDef = {
123
+ typeIndex,
124
+ __isPropDef: true,
125
+ start: 0,
126
+ path: [],
127
+ prop: 0,
128
+ len: 0,
129
+ separate: false,
130
+ enum: prop.enum,
131
+ validation,
132
+ default: DEFAULT_MAP[typeIndex],
133
+ step: parseMinMaxStep((prop.step ?? typeIndex === NUMBER) ? 0 : 1),
134
+ max: parseMinMaxStep(prop.max),
135
+ min: parseMinMaxStep(prop.min),
136
+ };
137
+ if (prop.type === 'text') {
138
+ if (typeof val === 'object') {
139
+ for (const key in val) {
140
+ if (!ctx.schema.locales || !(key in ctx.schema.locales)) {
141
+ throw new Error(`Incorrect default for type "text" lang "${key}"`);
142
+ }
143
+ if (!validation(val[key], propDef)) {
144
+ throw new Error(`Incorrect default for type "text" lang "${key}"`);
145
+ }
146
+ }
147
+ }
148
+ else {
149
+ if (!validation(val, propDef)) {
150
+ throw new Error(`Incorrect default for type "text"`);
151
+ }
152
+ val = {};
153
+ for (const key in ctx.schema.locales) {
154
+ val[key] = ctx.schema.locales[key];
155
+ }
156
+ }
157
+ return val;
158
+ }
159
+ if (!validation(val, propDef)) {
160
+ throw new Error(`Incorrect default for type "${prop.type ?? 'enum'}"`);
161
+ }
162
+ if ('enum' in prop) {
163
+ if (val === undefined) {
164
+ return 0;
165
+ }
166
+ return prop.enum.findIndex((v) => v === val) + 1;
167
+ }
168
+ return val;
169
+ };
94
170
  p.boolean = propParser(STUB, {
95
- default(val) {
96
- expectBoolean(val);
171
+ default(val, prop, ctx) {
172
+ return isDefault(val, prop, ctx);
97
173
  },
98
174
  }, 0);
99
175
  p.vector = propParser({
@@ -101,8 +177,49 @@ p.vector = propParser({
101
177
  expectNumber(val);
102
178
  },
103
179
  }, {
104
- default(val) {
105
- expectFloat32Array(val);
180
+ default(val, prop, ctx) {
181
+ return isDefault(val, prop, ctx);
182
+ },
183
+ baseType(val, prop, ctx) {
184
+ if (![
185
+ 'number',
186
+ 'int8',
187
+ ,
188
+ 'uint8',
189
+ 'int16',
190
+ 'uint16',
191
+ 'int32',
192
+ 'uint32',
193
+ 'float32',
194
+ 'float64',
195
+ ].includes(val)) {
196
+ throw Error(INVALID_VALUE);
197
+ }
198
+ },
199
+ }, 0);
200
+ p.colvec = propParser({
201
+ size(val) {
202
+ expectNumber(val);
203
+ },
204
+ }, {
205
+ default(val, prop, ctx) {
206
+ return isDefault(val, prop, ctx);
207
+ },
208
+ baseType(val, prop, ctx) {
209
+ if (![
210
+ 'number',
211
+ 'int8',
212
+ ,
213
+ 'uint8',
214
+ 'int16',
215
+ 'uint16',
216
+ 'int32',
217
+ 'uint32',
218
+ 'float32',
219
+ 'float64',
220
+ ].includes(val)) {
221
+ throw Error(INVALID_VALUE);
222
+ }
106
223
  },
107
224
  }, 0);
108
225
  p.enum = propParser({
@@ -120,10 +237,8 @@ p.enum = propParser({
120
237
  }
121
238
  },
122
239
  }, {
123
- default(val, prop) {
124
- if (!prop.enum.includes(val)) {
125
- throw Error(EXPECTED_VALUE_IN_ENUM);
126
- }
240
+ default(val, prop, ctx) {
241
+ return isDefault(val, prop, ctx);
127
242
  },
128
243
  }, 1);
129
244
  const numberOpts = {
@@ -146,18 +261,8 @@ const numberOpts = {
146
261
  throw Error(INVALID_VALUE);
147
262
  }
148
263
  },
149
- default(val, prop) {
150
- expectNumber(val);
151
- if (val > prop.max || val < prop.min) {
152
- throw Error(OUT_OF_RANGE);
153
- }
154
- if (prop.step !== 'any') {
155
- const min = typeof prop.min !== 'number' || prop.min === Infinity ? 0 : prop.min;
156
- const v = val - min;
157
- if (~~(v / prop.step) * prop.step !== v) {
158
- throw Error(INVALID_VALUE);
159
- }
160
- }
264
+ default(val, prop, ctx) {
265
+ return isDefault(val, prop, ctx);
161
266
  },
162
267
  };
163
268
  p.number = propParser(STUB, numberOpts, 0);
@@ -168,14 +273,10 @@ p.uint16 = propParser(STUB, numberOpts, 0);
168
273
  p.int32 = propParser(STUB, numberOpts, 0);
169
274
  p.uint32 = propParser(STUB, numberOpts, 0);
170
275
  p.object = propParser({
171
- props(val, prop, ctx) {
276
+ props(val, _prop, ctx) {
172
277
  ctx.parseProps(val, ctx.type);
173
278
  },
174
- }, {
175
- default(val) {
176
- console.warn('TODO object default value');
177
- },
178
- });
279
+ }, {});
179
280
  p.set = propParser({
180
281
  items(items, prop, ctx) {
181
282
  expectObject(items);
@@ -218,16 +319,13 @@ p.references = propParser({
218
319
  }
219
320
  },
220
321
  }, {
221
- default(val, prop) {
222
- console.warn('TODO SET DEFAULT VALUE');
223
- // if (typeof val === 'object') {
224
- // throwErr(ERRORS.EXPECTED_PRIMITIVE, prop, 'default')
225
- // }
322
+ default(val, prop, ctx) {
323
+ return isDefault(val, prop, ctx);
226
324
  },
227
325
  });
228
326
  const binaryOpts = {
229
- default(val) {
230
- expectString(val);
327
+ default(val, prop, ctx) {
328
+ return isDefault(val, prop, ctx);
231
329
  },
232
330
  format(val) {
233
331
  expectString(val);
@@ -270,9 +368,12 @@ p.text = propParser({
270
368
  throw Error(TEXT_REQUIRES_LOCALES);
271
369
  },
272
370
  }, {
371
+ compression(val) {
372
+ return val;
373
+ },
273
374
  format: binaryOpts.format,
274
- default(val, prop) {
275
- console.warn('MAKE DEFAULT VALUE FOR TEXT');
375
+ default(val, prop, ctx) {
376
+ return isDefault(val, prop, ctx);
276
377
  },
277
378
  }, 0);
278
379
  p.timestamp = propParser(STUB, {
@@ -281,11 +382,27 @@ p.timestamp = propParser(STUB, {
281
382
  dateDisplaysSet ??= new Set(dateDisplays);
282
383
  dateDisplaysSet.has(val);
283
384
  },
284
- default(val) {
285
- if (typeof val !== 'number' && !(val instanceof Date)) {
286
- throw Error(EXPECTED_DATE);
385
+ min(val) {
386
+ if (typeof val !== 'string' && typeof val !== 'number') {
387
+ throw Error(INVALID_VALUE);
287
388
  }
288
389
  },
390
+ max(val) {
391
+ if (typeof val !== 'string' && typeof val !== 'number') {
392
+ throw Error(INVALID_VALUE);
393
+ }
394
+ },
395
+ step(val) {
396
+ if (typeof val !== 'string' && typeof val !== 'number') {
397
+ throw Error(INVALID_VALUE);
398
+ }
399
+ if (typeof val === 'string' && val.includes('now')) {
400
+ throw Error(INVALID_VALUE);
401
+ }
402
+ },
403
+ default(val, prop, ctx) {
404
+ return isDefault(val, prop, ctx);
405
+ },
289
406
  on(val) {
290
407
  if (val !== 'create' && val !== 'update') {
291
408
  throw Error(INVALID_VALUE);
@@ -298,7 +415,7 @@ p.reference = propParser({
298
415
  throw Error(MISSING_TYPE);
299
416
  }
300
417
  },
301
- prop(propKey, prop, { schema, type, inQuery, path }) {
418
+ prop(propKey, prop, { schema, type, inQuery, path, lvl }) {
302
419
  const propAllowed = type && !inQuery;
303
420
  if (propAllowed) {
304
421
  expectString(propKey);
@@ -316,7 +433,7 @@ p.reference = propParser({
316
433
  if (create) {
317
434
  const ref = path[1];
318
435
  let prop = '';
319
- for (let i = 3; i < path.length - 1; i += 2) {
436
+ for (let i = 3; i < lvl; i += 2) {
320
437
  prop += prop ? `.${path[i]}` : path[i];
321
438
  }
322
439
  targetProp.readOnly = true;
@@ -349,8 +466,8 @@ p.reference = propParser({
349
466
  },
350
467
  }, {
351
468
  mime: binaryOpts.mime,
352
- default(val) {
353
- expectString(val);
469
+ default(val, prop, ctx) {
470
+ return isDefault(val, prop, ctx);
354
471
  },
355
472
  edge(val, prop, ctx, key) {
356
473
  const edgeAllowed = ctx.type && !ctx.inQuery;
@@ -378,19 +495,19 @@ p.reference = propParser({
378
495
  },
379
496
  });
380
497
  p.alias = propParser(STUB, {
381
- default(val) {
382
- expectString(val);
498
+ default(val, prop, ctx) {
499
+ return isDefault(val, prop, ctx);
383
500
  },
384
501
  format: binaryOpts.format,
385
502
  }, 0);
386
503
  p.cardinality = propParser(STUB, {
387
- default(val) {
388
- expectNumber(val);
504
+ default(val, prop, ctx) {
505
+ return isDefault(val, prop, ctx);
389
506
  },
390
507
  }, 0);
391
508
  p.json = propParser(STUB, {
392
- default(val) {
393
- expectObject(val);
509
+ default(val, prop, ctx) {
510
+ return isDefault(val, prop, ctx);
394
511
  },
395
512
  }, 0);
396
513
  export default p;
@@ -0,0 +1,12 @@
1
+ import type { Comparator, SemVer } from './types.js';
2
+ /**
3
+ * ANY is a sentinel value used by some range calculations. It is not a valid
4
+ * SemVer object and should not be used directly.
5
+ */
6
+ export declare const ANY: SemVer;
7
+ /**
8
+ * A comparator which will span all valid semantic versions
9
+ */
10
+ export declare const ALL: Comparator;
11
+ export declare const OPERATORS: readonly [any, "=", "!=", ">", ">=", "<", "<="];
12
+ //# sourceMappingURL=_constants.d.ts.map
@@ -0,0 +1,21 @@
1
+ // Copyright 2018-2025 the Deno authors. MIT license.
2
+ /**
3
+ * ANY is a sentinel value used by some range calculations. It is not a valid
4
+ * SemVer object and should not be used directly.
5
+ */
6
+ export const ANY = {
7
+ major: Number.NaN,
8
+ minor: Number.NaN,
9
+ patch: Number.NaN,
10
+ prerelease: [],
11
+ build: [],
12
+ };
13
+ /**
14
+ * A comparator which will span all valid semantic versions
15
+ */
16
+ export const ALL = {
17
+ operator: undefined,
18
+ ...ANY,
19
+ };
20
+ export const OPERATORS = [undefined, '=', '!=', '>', '>=', '<', '<='];
21
+ //# sourceMappingURL=_constants.js.map
@@ -0,0 +1,44 @@
1
+ import type { Comparator } from './types.js';
2
+ export declare function compareNumber(a: number, b: number): 1 | 0 | -1;
3
+ export declare function checkIdentifier(v1?: ReadonlyArray<string | number>, v2?: ReadonlyArray<string | number>): 1 | 0 | -1;
4
+ export declare function compareIdentifier(v1?: ReadonlyArray<string | number>, v2?: ReadonlyArray<string | number>): 1 | 0 | -1;
5
+ export declare const FULL_REGEXP: RegExp;
6
+ /**
7
+ * A version that can contain wildcards.
8
+ * @example "x", "1.x", "1.x.x", "1.2.x", "*", "1.*", "1.*.*", "1.2.*"
9
+ */
10
+ export declare const XRANGE = "[v=\\s]*(?<major>0|[1-9]\\d*|x|X|\\*)(?:\\.(?<minor>0|[1-9]\\d*|x|X|\\*)(?:\\.(?<patch>0|[1-9]\\d*|x|X|\\*)(?:-(?<prerelease>(?:0|[1-9]\\d*|\\d*[a-zA-Z-][a-zA-Z0-9-]*)(?:\\.(?:0|[1-9]\\d*|\\d*[a-zA-Z-][a-zA-Z0-9-]*))*))?(?:\\+(?<buildmetadata>[0-9A-Za-z-]+(?:\\.[0-9A-Za-z-]+)*))?)?)?";
11
+ /**
12
+ * An operator (`~`, `~>`, `^`, `=`, `<`, `<=`, `>`, or `>=`), followed by an x-range.
13
+ * @example "~1.x.x", "^1.2.*", ">=1.2.3"
14
+ */
15
+ export declare const OPERATOR_XRANGE_REGEXP: RegExp;
16
+ /**
17
+ * An empty string or a comparator (`=`, `<`, `<=`, `>`, or `>=`), followed by a version.
18
+ * @example ">1.2.3"
19
+ */
20
+ export declare const COMPARATOR_REGEXP: RegExp;
21
+ /**
22
+ * Returns true if the value is a valid SemVer number.
23
+ *
24
+ * Must be a number. Must not be NaN. Can be positive or negative infinity.
25
+ * Can be between 0 and MAX_SAFE_INTEGER.
26
+ * @param value The value to check
27
+ * @returns True if its a valid semver number
28
+ */
29
+ export declare function isValidNumber(value: unknown): value is number;
30
+ export declare const MAX_LENGTH = 256;
31
+ /**
32
+ * Returns true if the value is a valid semver pre-release or build identifier.
33
+ *
34
+ * Must be a string. Must be between 1 and 256 characters long. Must match
35
+ * the regular expression /[0-9A-Za-z-]+/.
36
+ * @param value The value to check
37
+ * @returns True if the value is a valid semver string.
38
+ */
39
+ export declare function isValidString(value: unknown): value is string;
40
+ export declare function parsePrerelease(prerelease: string): (string | number)[];
41
+ export declare function parseBuild(buildmetadata: string): string[];
42
+ export declare function parseNumber(input: string, errorMessage: string): number;
43
+ export declare function isWildcardComparator(c: Comparator): boolean;
44
+ //# sourceMappingURL=_shared.d.ts.map
@@ -0,0 +1,165 @@
1
+ // Copyright 2018-2025 the Deno authors. MIT license.
2
+ export function compareNumber(a, b) {
3
+ if (isNaN(a) || isNaN(b)) {
4
+ throw new Error('Cannot compare against non-numbers');
5
+ }
6
+ return a === b ? 0 : a < b ? -1 : 1;
7
+ }
8
+ export function checkIdentifier(v1 = [], v2 = []) {
9
+ // NOT having a prerelease is > having one
10
+ // But NOT having a build is < having one
11
+ if (v1.length && !v2.length)
12
+ return -1;
13
+ if (!v1.length && v2.length)
14
+ return 1;
15
+ return 0;
16
+ }
17
+ export function compareIdentifier(v1 = [], v2 = []) {
18
+ const length = Math.max(v1.length, v2.length);
19
+ for (let i = 0; i < length; i++) {
20
+ const a = v1[i];
21
+ const b = v2[i];
22
+ // same length is equal
23
+ if (a === undefined && b === undefined)
24
+ return 0;
25
+ // longer > shorter
26
+ if (b === undefined)
27
+ return 1;
28
+ // shorter < longer
29
+ if (a === undefined)
30
+ return -1;
31
+ // string > number
32
+ if (typeof a === 'string' && typeof b === 'number')
33
+ return 1;
34
+ // number < string
35
+ if (typeof a === 'number' && typeof b === 'string')
36
+ return -1;
37
+ if (a < b)
38
+ return -1;
39
+ if (a > b)
40
+ return 1;
41
+ // If they're equal, continue comparing segments.
42
+ }
43
+ return 0;
44
+ }
45
+ /**
46
+ * A single `0`, or a non-zero digit followed by zero or more digits.
47
+ */
48
+ const NUMERIC_IDENTIFIER = '0|[1-9]\\d*';
49
+ /**
50
+ * Zero or more digits, followed by a letter or hyphen, and then zero or more letters, digits, or hyphens.
51
+ */
52
+ const NON_NUMERIC_IDENTIFIER = '\\d*[a-zA-Z-][a-zA-Z0-9-]*';
53
+ /**
54
+ * Three dot-separated numeric identifiers.
55
+ */
56
+ const VERSION_CORE = `(?<major>${NUMERIC_IDENTIFIER})\\.(?<minor>${NUMERIC_IDENTIFIER})\\.(?<patch>${NUMERIC_IDENTIFIER})`;
57
+ /**
58
+ * A numeric identifier, or a non-numeric identifier.
59
+ */
60
+ const PRERELEASE_IDENTIFIER = `(?:${NUMERIC_IDENTIFIER}|${NON_NUMERIC_IDENTIFIER})`;
61
+ /**
62
+ * A hyphen, followed by one or more dot-separated pre-release version identifiers.
63
+ * @example "-pre.release"
64
+ */
65
+ const PRERELEASE = `(?:-(?<prerelease>${PRERELEASE_IDENTIFIER}(?:\\.${PRERELEASE_IDENTIFIER})*))`;
66
+ /**
67
+ * Any combination of digits, letters, or hyphens.
68
+ */
69
+ const BUILD_IDENTIFIER = '[0-9A-Za-z-]+';
70
+ /**
71
+ * A plus sign, followed by one or more period-separated build metadata identifiers.
72
+ * @example "+build.meta"
73
+ */
74
+ const BUILD = `(?:\\+(?<buildmetadata>${BUILD_IDENTIFIER}(?:\\.${BUILD_IDENTIFIER})*))`;
75
+ /**
76
+ * A version, followed optionally by a pre-release version and build metadata.
77
+ */
78
+ const FULL_VERSION = `v?${VERSION_CORE}${PRERELEASE}?${BUILD}?`;
79
+ export const FULL_REGEXP = new RegExp(`^${FULL_VERSION}$`);
80
+ /**
81
+ * A comparator.
82
+ * @example `=`, `<`, `<=`, `>`, `>=`
83
+ */
84
+ const COMPARATOR = '(?:<|>)?=?';
85
+ /**
86
+ * A wildcard identifier.
87
+ * @example "x", "X", "*"
88
+ */
89
+ const WILDCARD_IDENTIFIER = `x|X|\\*`;
90
+ const XRANGE_IDENTIFIER = `${NUMERIC_IDENTIFIER}|${WILDCARD_IDENTIFIER}`;
91
+ /**
92
+ * A version that can contain wildcards.
93
+ * @example "x", "1.x", "1.x.x", "1.2.x", "*", "1.*", "1.*.*", "1.2.*"
94
+ */
95
+ export const XRANGE = `[v=\\s]*(?<major>${XRANGE_IDENTIFIER})(?:\\.(?<minor>${XRANGE_IDENTIFIER})(?:\\.(?<patch>${XRANGE_IDENTIFIER})${PRERELEASE}?${BUILD}?)?)?`;
96
+ /**
97
+ * An operator (`~`, `~>`, `^`, `=`, `<`, `<=`, `>`, or `>=`), followed by an x-range.
98
+ * @example "~1.x.x", "^1.2.*", ">=1.2.3"
99
+ */
100
+ export const OPERATOR_XRANGE_REGEXP = new RegExp(`^(?<operator>~>?|\\^|${COMPARATOR})\\s*${XRANGE}$`);
101
+ /**
102
+ * An empty string or a comparator (`=`, `<`, `<=`, `>`, or `>=`), followed by a version.
103
+ * @example ">1.2.3"
104
+ */
105
+ export const COMPARATOR_REGEXP = new RegExp(`^(?<operator>${COMPARATOR})\\s*(${FULL_VERSION})$|^$`);
106
+ /**
107
+ * Returns true if the value is a valid SemVer number.
108
+ *
109
+ * Must be a number. Must not be NaN. Can be positive or negative infinity.
110
+ * Can be between 0 and MAX_SAFE_INTEGER.
111
+ * @param value The value to check
112
+ * @returns True if its a valid semver number
113
+ */
114
+ export function isValidNumber(value) {
115
+ return (typeof value === 'number' &&
116
+ !Number.isNaN(value) &&
117
+ (!Number.isFinite(value) ||
118
+ (0 <= value && value <= Number.MAX_SAFE_INTEGER)));
119
+ }
120
+ export const MAX_LENGTH = 256;
121
+ /**
122
+ * Returns true if the value is a valid semver pre-release or build identifier.
123
+ *
124
+ * Must be a string. Must be between 1 and 256 characters long. Must match
125
+ * the regular expression /[0-9A-Za-z-]+/.
126
+ * @param value The value to check
127
+ * @returns True if the value is a valid semver string.
128
+ */
129
+ export function isValidString(value) {
130
+ return (typeof value === 'string' &&
131
+ value.length > 0 &&
132
+ value.length <= MAX_LENGTH &&
133
+ /[0-9A-Za-z-]+/.test(value));
134
+ }
135
+ const NUMERIC_IDENTIFIER_REGEXP = new RegExp(`^${NUMERIC_IDENTIFIER}$`);
136
+ export function parsePrerelease(prerelease) {
137
+ return prerelease
138
+ .split('.')
139
+ .filter(Boolean)
140
+ .map((id) => {
141
+ if (NUMERIC_IDENTIFIER_REGEXP.test(id)) {
142
+ const number = Number(id);
143
+ if (isValidNumber(number))
144
+ return number;
145
+ }
146
+ return id;
147
+ });
148
+ }
149
+ export function parseBuild(buildmetadata) {
150
+ return buildmetadata.split('.').filter(Boolean);
151
+ }
152
+ export function parseNumber(input, errorMessage) {
153
+ const number = Number(input);
154
+ if (!isValidNumber(number))
155
+ throw new TypeError(errorMessage);
156
+ return number;
157
+ }
158
+ export function isWildcardComparator(c) {
159
+ return (Number.isNaN(c.major) &&
160
+ Number.isNaN(c.minor) &&
161
+ Number.isNaN(c.patch) &&
162
+ (c.prerelease === undefined || c.prerelease.length === 0) &&
163
+ (c.build === undefined || c.build.length === 0));
164
+ }
165
+ //# sourceMappingURL=_shared.js.map
@@ -0,0 +1,3 @@
1
+ import type { Comparator, SemVer } from './types.js';
2
+ export declare function testComparatorSet(version: SemVer, set: Comparator[]): boolean;
3
+ //# sourceMappingURL=_test_comparator_set.d.ts.map
@@ -0,0 +1,60 @@
1
+ // Copyright 2018-2025 the Deno authors. MIT license.
2
+ import { isWildcardComparator } from './_shared.js';
3
+ import { compare } from './compare.js';
4
+ function testComparator(version, comparator) {
5
+ if (isWildcardComparator(comparator)) {
6
+ return true;
7
+ }
8
+ const cmp = compare(version, comparator);
9
+ switch (comparator.operator) {
10
+ case '=':
11
+ case undefined: {
12
+ return cmp === 0;
13
+ }
14
+ case '!=': {
15
+ return cmp !== 0;
16
+ }
17
+ case '>': {
18
+ return cmp > 0;
19
+ }
20
+ case '<': {
21
+ return cmp < 0;
22
+ }
23
+ case '>=': {
24
+ return cmp >= 0;
25
+ }
26
+ case '<=': {
27
+ return cmp <= 0;
28
+ }
29
+ }
30
+ }
31
+ export function testComparatorSet(version, set) {
32
+ for (const comparator of set) {
33
+ if (!testComparator(version, comparator)) {
34
+ return false;
35
+ }
36
+ }
37
+ if (version.prerelease && version.prerelease.length > 0) {
38
+ // Find the comparator that is allowed to have prereleases
39
+ // For example, ^1.2.3-pr.1 desugars to >=1.2.3-pr.1 <2.0.0
40
+ // That should allow `1.2.3-pr.2` to pass.
41
+ // However, `1.2.4-alpha.notready` should NOT be allowed,
42
+ // even though it's within the range set by the comparators.
43
+ for (const comparator of set) {
44
+ if (isWildcardComparator(comparator)) {
45
+ continue;
46
+ }
47
+ const { major, minor, patch, prerelease } = comparator;
48
+ if (prerelease && prerelease.length > 0) {
49
+ if (version.major === major &&
50
+ version.minor === minor &&
51
+ version.patch === patch) {
52
+ return true;
53
+ }
54
+ }
55
+ }
56
+ return false;
57
+ }
58
+ return true;
59
+ }
60
+ //# sourceMappingURL=_test_comparator_set.js.map
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Returns true if the string can be parsed as SemVer.
3
+ *
4
+ * @example Usage
5
+ * ```ts
6
+ * import { canParse } from "@std/semver/can-parse";
7
+ * import { assert, assertFalse } from "@std/assert";
8
+ *
9
+ * assert(canParse("1.2.3"));
10
+ * assertFalse(canParse("invalid"));
11
+ * ```
12
+ *
13
+ * @param value The version string to check
14
+ * @returns `true` if the string can be parsed as SemVer, `false` otherwise
15
+ */
16
+ export declare function canParse(value: string): boolean;
17
+ //# sourceMappingURL=can_parse.d.ts.map