@jakobkg/shapes-ts 0.3.0 → 0.3.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.
package/README.md CHANGED
@@ -6,9 +6,11 @@ When describing the shape of your data in shapes-ts, you get the corresponding
6
6
  TS type and a validation utility for free!
7
7
 
8
8
  ```typescript
9
- const Role = Shapes.object({
10
- role: Shapes.string(),
11
- actor: Shapes.string(),
9
+ const CreditCategory = Shapes.enum(["director", "writer", "actor/actress"])
10
+
11
+ const Credits = Shapes.object({
12
+ category: CreditCategory,
13
+ name: Shapes.string(),
12
14
  });
13
15
 
14
16
  const Movie = Shapes.object({
@@ -17,14 +19,17 @@ const Movie = Shapes.object({
17
19
  (n) => n >= 0 && n <= 5,
18
20
  "rating must be in range 0.0-5.0",
19
21
  ),
20
- cast: Shapes.array(Role),
22
+ credits: Shapes.array(Credits),
21
23
  });
22
24
 
23
25
  // This infers as
24
26
  // {
25
27
  // title: string,
26
28
  // rating: number,
27
- // cast: { role: string, actor: string }[]
29
+ // credits: {
30
+ // category: "director" | "writer" | "actor/actress",
31
+ // name: string
32
+ // }[]
28
33
  // }
29
34
  type Movie = Shapes.Type<typeof Movie>;
30
35
 
@@ -55,6 +60,8 @@ for (const member of movieData.cast) {
55
60
 
56
61
  - object
57
62
  - array
63
+ - tuple
64
+ - enum
58
65
 
59
66
  ### Modified types
60
67
 
package/dist/shapes.d.ts CHANGED
@@ -45,6 +45,14 @@ export declare function string(): Shape<string>;
45
45
  */
46
46
  export declare function boolean(): Shape<boolean>;
47
47
  type Primitive = number | string | boolean | null | undefined;
48
+ /**
49
+ * Creates a shape representing a primitive literal type.
50
+ * Takes numbers, strings, booleans, null and undefined.
51
+ *
52
+ * The resulting shape is equivalent to the type you would
53
+ * get when doing something like `const one = 1 as const;`,
54
+ * where the type is just the literal `1`
55
+ */
48
56
  export declare function literal<T extends Primitive>(x: T): Shape<T>;
49
57
  /**
50
58
  * Creates an array shape, representing `Array<T>` (aka `T[]`).
@@ -73,11 +81,23 @@ export declare function array<T>(shape: Shape<T>): Shape<T[]>;
73
81
  export declare function object<T extends Record<string, Shape<any>>>(properties: T, options?: ObjectShapeOptions): ObjectShape<{
74
82
  [K in keyof T]: Type<T[K]>;
75
83
  }>;
84
+ /**
85
+ * Creates a shape representing a tuple type, with an optional rest
86
+ *
87
+ * Shapes.tuple([Shapes.number(), Shape.string()]) is equivalent to TS type [number, string]
88
+ * Shapes.tuple([Shapes.number()], Shapes.string()) is equivalent to TS type [number, ...string]
89
+ */
76
90
  export declare function tuple<Shapes extends Shape<any>[], Partial extends {
77
91
  [K in keyof Shapes]: Type<Shapes[K]>;
78
92
  }, Tuple extends RestShape extends Shape<infer Rest> ? [...Partial, ...Rest[]] : [...Partial], RestShape extends Shape<any> | undefined = undefined>(shapes: [...Shapes], rest?: RestShape): Shape<Tuple>;
79
93
  export { enumOf as enum };
80
- declare function enumOf<T extends (string | number)[]>(...values: T): Shape<T[number]>;
94
+ /**
95
+ * Creates a shape representing an enum type
96
+ * (Not an actual enum as made using the enum keyword)
97
+ *
98
+ * `Shapes.enum(["a", "b", "c"])` is equivalent to `"a" | "b" | "c"`
99
+ */
100
+ declare function enumOf<T extends (string | number)[]>(values: [...T]): Shape<T[number]>;
81
101
  /**
82
102
  * Creates a shape representing an optional type.
83
103
  * `Shapes.optional(T)` corresponds to `T | undefined`
@@ -1 +1 @@
1
- {"version":3,"file":"shapes.d.ts","sourceRoot":"","sources":["../src/shapes/shapes.ts"],"names":[],"mappings":"AAEA,QAAA,MAAM,UAAU,eAAW,CAAC;AAC5B,QAAA,MAAM,UAAU,eAAW,CAAC;AAC5B,QAAA,MAAM,sBAAsB,eAAW,CAAC;AAgCxC;;GAEG;AACH,MAAM,MAAM,IAAI,CAAC,CAAC,IAAI,CAAC,SAAS,KAAK,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;AAG3D,KAAK,cAAc,CAAC,CAAC,IAAI;IACvB,S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AAC,CAAC,MAAM,CAAC,CAAC,CAuB/E;AAID;;;GAGG;AACH,wBAAgB,QAAQ,CAAC,CAAC,EAAE,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,CAAC,GAAG,SAAS,CAAC,CAEjE;AAED;;;GAGG;AACH,wBAAgB,QAAQ,CAAC,CAAC,EAAE,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,CAAC,GAAG,IAAI,CAAC,CAE5D"}
package/dist/shapes.js CHANGED
@@ -178,6 +178,14 @@ function boolean() {
178
178
  },
179
179
  _a;
180
180
  }
181
+ /**
182
+ * Creates a shape representing a primitive literal type.
183
+ * Takes numbers, strings, booleans, null and undefined.
184
+ *
185
+ * The resulting shape is equivalent to the type you would
186
+ * get when doing something like `const one = 1 as const;`,
187
+ * where the type is just the literal `1`
188
+ */
181
189
  function literal(x) {
182
190
  var _a;
183
191
  function matchesLiteral(u) {
@@ -187,9 +195,9 @@ function literal(x) {
187
195
  typename: isUndefined(x) ? "undefined" : JSON.stringify(x)
188
196
  },
189
197
  _a[Predicates] = [],
190
- _a.check = function (input) {
191
- return matchesLiteral(input) &&
192
- this[Predicates].every(function (p) { return p.predicate(input); });
198
+ _a.check = function (x) {
199
+ return matchesLiteral(x) &&
200
+ this[Predicates].every(function (p) { return p.predicate(x); });
193
201
  },
194
202
  _a.where = function (predicate, description) {
195
203
  var _a;
@@ -213,12 +221,12 @@ function array(shape) {
213
221
  var typename = "Array<".concat(shape.typename, ">");
214
222
  return _a = {
215
223
  typename: typename,
216
- check: function (input) {
224
+ check: function (x) {
217
225
  function innerMatches(x) {
218
226
  return x.every(function (entry) { return shape.check(entry); });
219
227
  }
220
- return isArray(input) && innerMatches(input) &&
221
- this[Predicates].every(function (p) { return p.predicate(input); });
228
+ return isArray(x) && innerMatches(x) &&
229
+ this[Predicates].every(function (p) { return p.predicate(x); });
222
230
  }
223
231
  },
224
232
  _a[Predicates] = [],
@@ -262,7 +270,7 @@ function object(properties, options) {
262
270
  typename: "Object"
263
271
  },
264
272
  _a[AllowUnknownProperties] = allowUnknownProperties,
265
- _a.check = function (input) {
273
+ _a.check = function (x) {
266
274
  var _this = this;
267
275
  var matchesShape = function (input) {
268
276
  // Check that input is an object
@@ -294,8 +302,8 @@ function object(properties, options) {
294
302
  }
295
303
  return true;
296
304
  };
297
- return matchesShape(input) &&
298
- this[Predicates].every(function (p) { return p.predicate(input); });
305
+ return matchesShape(x) &&
306
+ this[Predicates].every(function (p) { return p.predicate(x); });
299
307
  },
300
308
  _a[Properties] = __assign(__assign({}, properties), options === null || options === void 0 ? void 0 : options.additionalPermittedProperties),
301
309
  _a[Predicates] = [],
@@ -311,6 +319,12 @@ function object(properties, options) {
311
319
  },
312
320
  _a;
313
321
  }
322
+ /**
323
+ * Creates a shape representing a tuple type, with an optional rest
324
+ *
325
+ * Shapes.tuple([Shapes.number(), Shape.string()]) is equivalent to TS type [number, string]
326
+ * Shapes.tuple([Shapes.number()], Shapes.string()) is equivalent to TS type [number, ...string]
327
+ */
314
328
  function tuple(shapes, rest) {
315
329
  var _a;
316
330
  var typename = "[".concat(shapes.map(function (s) { return s.typename; }).join(", "), "]");
@@ -321,15 +335,16 @@ function tuple(shapes, rest) {
321
335
  _a.check = function (x) {
322
336
  if (!isArray(x))
323
337
  return false;
324
- var xMightHaveRest = x.length > shapes.length;
325
- var haveRestShape = rest !== undefined;
326
- if (haveRestShape && xMightHaveRest) {
338
+ if (x.length > shapes.length) {
339
+ // Oversized x and no rest shape can't be valid
340
+ if (rest === undefined)
341
+ return false;
327
342
  for (var idx = shapes.length; idx < x.length; idx++) {
328
343
  if (!rest.check(x[idx]))
329
344
  return false;
330
345
  }
331
346
  }
332
- return shapes.every(function (shape, idx) { return shape.check(x[idx]); });
347
+ return shapes.every(function (shape, idx) { return shape.check(x[idx]); }) && this[Predicates].every(function (p) { return p.predicate(x); });
333
348
  },
334
349
  _a.where = function (predicate, description) {
335
350
  var _a;
@@ -343,18 +358,20 @@ function tuple(shapes, rest) {
343
358
  },
344
359
  _a;
345
360
  }
346
- function enumOf() {
361
+ /**
362
+ * Creates a shape representing an enum type
363
+ * (Not an actual enum as made using the enum keyword)
364
+ *
365
+ * `Shapes.enum(["a", "b", "c"])` is equivalent to `"a" | "b" | "c"`
366
+ */
367
+ function enumOf(values) {
347
368
  var _a;
348
- var values = [];
349
- for (var _i = 0; _i < arguments.length; _i++) {
350
- values[_i] = arguments[_i];
351
- }
352
369
  return _a = {
353
370
  typename: "".concat(values.map(function (v) { return JSON.stringify(v); }).join(" | "))
354
371
  },
355
372
  _a[Predicates] = [],
356
373
  _a.check = function (x) {
357
- return (isNumber(x) || isString(x)) && values.includes(x);
374
+ return (isNumber(x) || isString(x)) && values.includes(x) && this[Predicates].every(function (p) { return p.predicate(x); });
358
375
  },
359
376
  _a.where = function (predicate, description) {
360
377
  var _a;
package/package.json CHANGED
@@ -6,7 +6,7 @@
6
6
  },
7
7
  "homepage": "https://codeberg.org/jakobkg/shapes-ts#readme",
8
8
  "license": "BSD-3-Clause",
9
- "version": "0.3.0",
9
+ "version": "0.3.1",
10
10
  "private": false,
11
11
  "main": "dist/index.js",
12
12
  "scripts": {