@jakobkg/shapes-ts 0.3.1 → 0.3.2

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,7 +6,7 @@ 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 CreditCategory = Shapes.enum(["director", "writer", "actor/actress"])
9
+ const CreditCategory = Shapes.enum(["director", "writer", "actor/actress"]);
10
10
 
11
11
  const Credits = Shapes.object({
12
12
  category: CreditCategory,
@@ -41,8 +41,8 @@ if (!Movie.check(movieData)) {
41
41
 
42
42
  // Past this point, movieData is typed as Movie
43
43
 
44
- for (const member of movieData.cast) {
45
- console.log(`${member.actor} as ${member.role}`);
44
+ for (const credit of movieData.credits) {
45
+ console.log(`${credit.name}: ${credit.category}`);
46
46
  }
47
47
  ```
48
48
 
package/dist/shapes.d.ts CHANGED
@@ -7,11 +7,13 @@ declare const AllowUnknownProperties: unique symbol;
7
7
  export type Type<T> = T extends Shape<infer U> ? U : never;
8
8
  type ShapePredicate<T> = {
9
9
  predicate: (x: T) => boolean;
10
- description: string | undefined;
10
+ description?: string | undefined;
11
11
  };
12
12
  interface Shape<T> {
13
13
  readonly typename: string;
14
14
  readonly [Predicates]: ShapePredicate<T>[];
15
+ readonly [Properties]?: Record<string, Shape<T>>;
16
+ readonly [AllowUnknownProperties]?: boolean;
15
17
  check(x: unknown): x is T;
16
18
  /**
17
19
  * Adds a custom validation predicate to the shape
@@ -19,15 +21,11 @@ interface Shape<T> {
19
21
  * @param description Optional description of the constraint for error messages
20
22
  * @returns A new shape with the additional validation
21
23
  */
22
- where(predicate: (x: T) => boolean, description: string | undefined): Shape<T>;
24
+ where(predicate: (x: T) => boolean, description?: string | undefined): Shape<T>;
23
25
  and<U>(other: Shape<U>): Shape<T & U>;
24
26
  or<U>(other: Shape<U>): Shape<T | U>;
25
27
  }
26
- interface ObjectShape<T = Record<string, unknown>> extends Shape<T> {
27
- readonly [Properties]: Record<string, Shape<T>>;
28
- readonly [AllowUnknownProperties]?: boolean;
29
- }
30
- interface ObjectShapeOptions {
28
+ interface ObjectOptions {
31
29
  readonly allowUnknownProperties?: boolean;
32
30
  readonly additionalPermittedProperties?: Record<string, Shape<Record<string, unknown>>>;
33
31
  }
@@ -78,7 +76,10 @@ export declare function array<T>(shape: Shape<T>): Shape<T[]>;
78
76
  * });
79
77
  * ```
80
78
  */
81
- export declare function object<T extends Record<string, Shape<any>>>(properties: T, options?: ObjectShapeOptions): ObjectShape<{
79
+ export declare function object<T extends Record<string, Shape<any>>>(typename: string, properties: T, options?: ObjectOptions | undefined): Shape<{
80
+ [K in keyof T]: Type<T[K]>;
81
+ }>;
82
+ export declare function object<T extends Record<string, Shape<any>>>(properties: T, options?: ObjectOptions | undefined): Shape<{
82
83
  [K in keyof T]: Type<T[K]>;
83
84
  }>;
84
85
  /**
@@ -87,12 +88,12 @@ export declare function object<T extends Record<string, Shape<any>>>(properties:
87
88
  * Shapes.tuple([Shapes.number(), Shape.string()]) is equivalent to TS type [number, string]
88
89
  * Shapes.tuple([Shapes.number()], Shapes.string()) is equivalent to TS type [number, ...string]
89
90
  */
90
- export declare function tuple<Shapes extends Shape<any>[], Partial extends {
91
+ export declare function tuple<Shapes extends Shape<any>[], Elements extends {
91
92
  [K in keyof Shapes]: Type<Shapes[K]>;
92
- }, Tuple extends RestShape extends Shape<infer Rest> ? [...Partial, ...Rest[]] : [...Partial], RestShape extends Shape<any> | undefined = undefined>(shapes: [...Shapes], rest?: RestShape): Shape<Tuple>;
93
+ }, Tuple extends RestShape extends Shape<infer Rest> ? [...Elements, ...Rest[]] : [...Elements], RestShape extends Shape<any> | undefined = undefined>(shapes: [...Shapes], rest?: RestShape): Shape<Tuple>;
93
94
  export { enumOf as enum };
94
95
  /**
95
- * Creates a shape representing an enum type
96
+ * Creates a shape representing an enum type.
96
97
  * (Not an actual enum as made using the enum keyword)
97
98
  *
98
99
  * `Shap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KAAK,CAEnB,MAAM,SAAS,KAAK,CAAC,GAAG,CAAC,EAAE,EAC3B,QAAQ,SAAS;KAAG,CAAC,IAAI,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;CAAE,EACzD,KAAK,SAAS,SAAS,SAAS,KAAK,CAAC,MAAM,IAAI,CAAC,GAAG,CAAC,GAAG,QAAQ,EAAE,GAAG,IAAI,EAAE,CAAC,GACxE,CAAC,GAAG,QAAQ,CAAC,EAEjB,SAAS,SAAS,KAAK,CAAC,GAAG,CAAC,GAAG,SAAS,GAAG,SAAS,EACpD,MAAM,EAAE,CAAC,GAAG,MAAM,CAAC,EAAE,IAAI,CAAC,EAAE,SAAS,GAAG,KAAK,CAAC,KAAK,CAAC,CAoBrD;AAED,OAAO,EAAE,MAAM,IAAI,IAAI,EAAE,CAAC;AAC1B;;;;;GAKG;AACH,iBAAS,MAAM,CAAC,CAAC,SAAS,CAAC,MAAM,GAAG,MAAM,CAAC,EAAE,EAC3C,MAAM,EAAE,CAAC,GAAG,CAAC,CAAC,GACb,KAAK,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CASlB;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
@@ -56,46 +56,34 @@ function isBoolean(input) {
56
56
  function isUndefined(input) {
57
57
  return input === undefined;
58
58
  }
59
- function makeUnion(left, right) {
60
- var _a;
61
- return _a = {
62
- typename: "".concat(left.typename, " | ").concat(right.typename)
63
- },
64
- _a[Predicates] = [],
65
- _a.where = function (predicate, description) {
66
- var _a;
67
- return __assign(__assign({}, this), (_a = {}, _a[Predicates] = __spreadArray(__spreadArray([], this[Predicates], true), [{ predicate: predicate, description: description }], false), _a));
68
- },
69
- _a.check = function (x) {
70
- return (left.check(x) || right.check(x)) &&
71
- this[Predicates].every(function (p) { return p.predicate(x); });
72
- },
73
- _a.and = function (other) {
74
- return makeIntersection(this, other);
75
- },
76
- _a.or = function (other) {
77
- return makeUnion(this, other);
78
- },
79
- _a;
59
+ /// HELLA UTILS
60
+ function tryPredicates(self, input) {
61
+ return self[Predicates].every(function (p) {
62
+ try {
63
+ return p.predicate(input);
64
+ }
65
+ catch (_) {
66
+ return false;
67
+ }
68
+ });
80
69
  }
81
- function makeIntersection(left, right) {
70
+ /// PRIMITIVE SHAPE FACTORIES
71
+ /**
72
+ * Creates a base object that implements the most basic shared logic for all shapes
73
+ *
74
+ * `.typename` and `.check(x)` on the returned object are stubs and should be overwritten
75
+ */
76
+ function base() {
82
77
  var _a;
83
78
  return _a = {
84
- typename: "".concat(left.typename, " & ").concat(right.typename)
79
+ typename: "",
80
+ check: function (_) { return false; }
85
81
  },
86
82
  _a[Predicates] = [],
87
83
  _a.where = function (predicate, description) {
88
84
  var _a;
89
85
  return __assign(__assign({}, this), (_a = {}, _a[Predicates] = __spreadArray(__spreadArray([], this[Predicates], true), [{ predicate: predicate, description: description }], false), _a));
90
86
  },
91
- _a.check = function (x) {
92
- var _a, _b;
93
- // Trickery: combine permitted properties of left and right
94
- var tolerantLeft = __assign(__assign({}, left), (_a = {}, _a[Properties] = __assign(__assign({}, right[Properties]), left[Properties]), _a));
95
- var tolerantRight = __assign(__assign({}, right), (_b = {}, _b[Properties] = __assign(__assign({}, left[Properties]), right[Properties]), _b));
96
- return tolerantLeft.check(x) && tolerantRight.check(x) &&
97
- this[Predicates].every(function (p) { return p.predicate(x); });
98
- },
99
87
  _a.and = function (other) {
100
88
  return makeIntersection(this, other);
101
89
  },
@@ -104,79 +92,29 @@ function makeIntersection(left, right) {
104
92
  },
105
93
  _a;
106
94
  }
107
- /// PRIMITIVE SHAPE FACTORIES
95
+ function makePrimitive(name, guard) {
96
+ return __assign(__assign({}, base()), { typename: name, check: function (x) {
97
+ return guard(x) && tryPredicates(this, x);
98
+ } });
99
+ }
108
100
  /**
109
101
  * Creates a shape representing a JS `number`.
110
102
  * Does not support BigInt.
111
103
  */
112
104
  function number() {
113
- var _a;
114
- return _a = {
115
- typename: "number"
116
- },
117
- _a[Predicates] = [],
118
- _a.check = function (x) {
119
- return isNumber(x) && this[Predicates].every(function (p) { return p.predicate(x); });
120
- },
121
- _a.where = function (predicate, description) {
122
- var _a;
123
- return __assign(__assign({}, this), (_a = {}, _a[Predicates] = __spreadArray(__spreadArray([], this[Predicates], true), [{ predicate: predicate, description: description }], false), _a));
124
- },
125
- _a.and = function (other) {
126
- return makeIntersection(this, other);
127
- },
128
- _a.or = function (other) {
129
- return makeUnion(this, other);
130
- },
131
- _a;
105
+ return makePrimitive("number", isNumber);
132
106
  }
133
107
  /**
134
108
  * Creates a shape representing a JS `string`.
135
109
  */
136
110
  function string() {
137
- var _a;
138
- return _a = {
139
- typename: "string",
140
- check: function (x) {
141
- return isString(x) && this[Predicates].every(function (p) { return p.predicate(x); });
142
- }
143
- },
144
- _a[Predicates] = [],
145
- _a.where = function (predicate, description) {
146
- var _a;
147
- return __assign(__assign({}, this), (_a = {}, _a[Predicates] = __spreadArray(__spreadArray([], this[Predicates], true), [{ predicate: predicate, description: description }], false), _a));
148
- },
149
- _a.and = function (other) {
150
- return makeIntersection(this, other);
151
- },
152
- _a.or = function (other) {
153
- return makeUnion(this, other);
154
- },
155
- _a;
111
+ return makePrimitive("string", isString);
156
112
  }
157
113
  /**
158
114
  * Creates a shape representing a JS `boolean`.
159
115
  */
160
116
  function boolean() {
161
- var _a;
162
- return _a = {
163
- typename: "boolean",
164
- check: function (x) {
165
- return isBoolean(x) && this[Predicates].every(function (p) { return p.predicate(x); });
166
- }
167
- },
168
- _a[Predicates] = [],
169
- _a.where = function (predicate, description) {
170
- var _a;
171
- return __assign(__assign({}, this), (_a = {}, _a[Predicates] = __spreadArray(__spreadArray([], this[Predicates], true), [{ predicate: predicate, description: description }], false), _a));
172
- },
173
- _a.and = function (other) {
174
- return makeIntersection(this, other);
175
- },
176
- _a.or = function (other) {
177
- return makeUnion(this, other);
178
- },
179
- _a;
117
+ return makePrimitive("boolean", isBoolean);
180
118
  }
181
119
  /**
182
120
  * Creates a shape representing a primitive literal type.
@@ -187,137 +125,92 @@ function boolean() {
187
125
  * where the type is just the literal `1`
188
126
  */
189
127
  function literal(x) {
190
- var _a;
191
128
  function matchesLiteral(u) {
192
129
  return u === x;
193
130
  }
194
- return _a = {
195
- typename: isUndefined(x) ? "undefined" : JSON.stringify(x)
196
- },
197
- _a[Predicates] = [],
198
- _a.check = function (x) {
199
- return matchesLiteral(x) &&
200
- this[Predicates].every(function (p) { return p.predicate(x); });
201
- },
202
- _a.where = function (predicate, description) {
203
- var _a;
204
- return __assign(__assign({}, this), (_a = {}, _a[Predicates] = __spreadArray(__spreadArray([], this[Predicates], true), [{ predicate: predicate, description: description }], false), _a));
205
- },
206
- _a.and = function (other) {
207
- return makeIntersection(this, other);
208
- },
209
- _a.or = function (other) {
210
- return makeUnion(this, other);
211
- },
212
- _a;
131
+ return __assign(__assign({}, base()), { typename: isUndefined(x) ? "undefined" : JSON.stringify(x), check: function (x) {
132
+ return matchesLiteral(x) && tryPredicates(this, x);
133
+ } });
213
134
  }
214
135
  /// COMPLEX SHAPE FACTORIES
136
+ function makeUnion(left, right) {
137
+ return __assign(__assign({}, base()), { typename: "".concat(left.typename, " | ").concat(right.typename), check: function (x) {
138
+ return (left.check(x) || right.check(x)) && tryPredicates(this, x);
139
+ } });
140
+ }
141
+ function makeIntersection(left, right) {
142
+ var _a, _b, _c;
143
+ // Trickery: combine permitted properties of left and right shape
144
+ var tolerantLeft = __assign(__assign({}, left), (_a = {}, _a[AllowUnknownProperties] = true, _a));
145
+ var tolerantRight = __assign(__assign({}, right), (_b = {}, _b[AllowUnknownProperties] = true, _b));
146
+ return __assign(__assign({}, base()), (_c = { typename: "".concat(left.typename, " & ").concat(right.typename) }, _c[Properties] = __assign(__assign({}, left[Properties]), right[Properties]), _c.check = function (x) {
147
+ return tolerantLeft.check(x) && tolerantRight.check(x) && tryPredicates(this, x);
148
+ }, _c));
149
+ }
215
150
  /**
216
151
  * Creates an array shape, representing `Array<T>` (aka `T[]`).
217
152
  * Takes the shape representing `T` as a parameter.
218
153
  */
219
154
  function array(shape) {
220
- var _a;
221
- var typename = "Array<".concat(shape.typename, ">");
222
- return _a = {
223
- typename: typename,
224
- check: function (x) {
225
- function innerMatches(x) {
226
- return x.every(function (entry) { return shape.check(entry); });
227
- }
228
- return isArray(x) && innerMatches(x) &&
229
- this[Predicates].every(function (p) { return p.predicate(x); });
230
- }
231
- },
232
- _a[Predicates] = [],
233
- _a.where = function (predicate, description) {
234
- var _a;
235
- return __assign(__assign({}, this), (_a = {}, _a[Predicates] = __spreadArray(__spreadArray([], this[Predicates], true), [{ predicate: predicate, description: description }], false), _a));
236
- },
237
- _a.and = function (other) {
238
- return makeIntersection(this, other);
239
- },
240
- _a.or = function (other) {
241
- return makeUnion(this, other);
242
- },
243
- _a;
155
+ function innerMatches(x) {
156
+ return x.every(function (entry) { return shape.check(entry); });
157
+ }
158
+ return __assign(__assign({}, base()), { typename: "Array<".concat(shape.typename, ">"), check: function (x) {
159
+ return isArray(x) && innerMatches(x) && tryPredicates(this, x);
160
+ } });
244
161
  }
245
- /**
246
- * Creates an object shape.
247
- *
248
- * @example
249
- * ```typescript
250
- * const Group = Shapes.object({
251
- * id: Shapes.number(),
252
- * name: Shapes.string(),
253
- * });
254
- *
255
- * const User = Shapes.object({
256
- * id: Shapes.number(),
257
- * name: Shapes.string(),
258
- * groups: Shapes.nullable(
259
- * Shapes.array(Group)
260
- * )
261
- * });
262
- * ```
263
- */
264
162
  // deno-lint-ignore no-explicit-any
265
- function object(properties, options) {
163
+ function object(nameOrProperties, propertiesOrOptions, maybeOptions) {
266
164
  var _a;
267
165
  var _b;
166
+ var typename;
167
+ var properties;
168
+ var options;
169
+ if (typeof nameOrProperties === "string") {
170
+ typename = nameOrProperties;
171
+ properties = propertiesOrOptions;
172
+ options = maybeOptions;
173
+ }
174
+ else {
175
+ properties = nameOrProperties;
176
+ options = propertiesOrOptions;
177
+ }
268
178
  var allowUnknownProperties = (_b = options === null || options === void 0 ? void 0 : options.allowUnknownProperties) !== null && _b !== void 0 ? _b : false;
269
- return _a = {
270
- typename: "Object"
271
- },
272
- _a[AllowUnknownProperties] = allowUnknownProperties,
273
- _a.check = function (x) {
274
- var _this = this;
275
- var matchesShape = function (input) {
276
- // Check that input is an object
277
- if (!isObject(input)) {
179
+ function matchesShape(self, input) {
180
+ // Check that input is an object
181
+ if (!isObject(input)) {
182
+ return false;
183
+ }
184
+ if (self[Properties] === undefined)
185
+ return false;
186
+ // Check for unknown properties
187
+ if (!self[AllowUnknownProperties]) {
188
+ for (var inputProperty in input) {
189
+ if (!(inputProperty in self[Properties])) {
278
190
  return false;
279
191
  }
280
- // Check for unknown properties
281
- if (!_this[AllowUnknownProperties]) {
282
- for (var inputProperty in input) {
283
- if (!(inputProperty in _this[Properties])) {
284
- return false;
285
- }
286
- }
287
- }
288
- // Check required properties and their types
289
- for (var property in _this[Properties]) {
290
- var field = input[property];
291
- var propertyShape = _this[Properties][property];
292
- if (isUndefined(propertyShape))
293
- return false;
294
- // Property presence check
295
- if (!(property in input) && !propertyShape.check(undefined)) {
296
- return false;
297
- }
298
- // Property type check
299
- if (!propertyShape.check(field)) {
300
- return false;
301
- }
302
- }
303
- return true;
304
- };
305
- return matchesShape(x) &&
306
- this[Predicates].every(function (p) { return p.predicate(x); });
307
- },
308
- _a[Properties] = __assign(__assign({}, properties), options === null || options === void 0 ? void 0 : options.additionalPermittedProperties),
309
- _a[Predicates] = [],
310
- _a.where = function (predicate, description) {
311
- var _a;
312
- return __assign(__assign({}, this), (_a = {}, _a[Predicates] = __spreadArray(__spreadArray([], this[Predicates], true), [{ predicate: predicate, description: description }], false), _a));
313
- },
314
- _a.and = function (other) {
315
- return makeIntersection(this, other);
316
- },
317
- _a.or = function (other) {
318
- return makeUnion(this, other);
319
- },
320
- _a;
192
+ }
193
+ }
194
+ // Check required properties and their types
195
+ for (var property in self[Properties]) {
196
+ var field = input[property];
197
+ var propertyShape = self[Properties][property];
198
+ if (isUndefined(propertyShape))
199
+ return false;
200
+ // Property presence check
201
+ if (!(property in input) && !propertyShape.check(undefined)) {
202
+ return false;
203
+ }
204
+ // Property type check
205
+ if (!propertyShape.check(field)) {
206
+ return false;
207
+ }
208
+ }
209
+ return true;
210
+ }
211
+ return __assign(__assign({}, base()), (_a = { typename: typename !== null && typename !== void 0 ? typename : "object" }, _a[AllowUnknownProperties] = allowUnknownProperties, _a.check = function (x) {
212
+ return matchesShape(this, x) && tryPredicates(this, x);
213
+ }, _a[Properties] = __assign(__assign({}, properties), options === null || options === void 0 ? void 0 : options.additionalPermittedProperties), _a));
321
214
  }
322
215
  /**
323
216
  * Creates a shape representing a tuple type, with an optional rest
@@ -326,17 +219,11 @@ function object(properties, options) {
326
219
  * Shapes.tuple([Shapes.number()], Shapes.string()) is equivalent to TS type [number, ...string]
327
220
  */
328
221
  function tuple(shapes, rest) {
329
- var _a;
330
- var typename = "[".concat(shapes.map(function (s) { return s.typename; }).join(", "), "]");
331
- return _a = {
332
- typename: typename
333
- },
334
- _a[Predicates] = [],
335
- _a.check = function (x) {
222
+ return __assign(__assign({}, base()), { typename: "[".concat(shapes.map(function (s) { return s.typename; }).join(", ")).concat(rest === undefined ? "" : ", ...".concat(rest.typename, "[]"), "]"), check: function (x) {
336
223
  if (!isArray(x))
337
224
  return false;
338
225
  if (x.length > shapes.length) {
339
- // Oversized x and no rest shape can't be valid
226
+ // Oversized x and no rest shape can't be valid
340
227
  if (rest === undefined)
341
228
  return false;
342
229
  for (var idx = shapes.length; idx < x.length; idx++) {
@@ -344,46 +231,19 @@ function tuple(shapes, rest) {
344
231
  return false;
345
232
  }
346
233
  }
347
- return shapes.every(function (shape, idx) { return shape.check(x[idx]); }) && this[Predicates].every(function (p) { return p.predicate(x); });
348
- },
349
- _a.where = function (predicate, description) {
350
- var _a;
351
- return __assign(__assign({}, this), (_a = {}, _a[Predicates] = __spreadArray(__spreadArray([], this[Predicates], true), [{ predicate: predicate, description: description }], false), _a));
352
- },
353
- _a.and = function (other) {
354
- return makeIntersection(this, other);
355
- },
356
- _a.or = function (other) {
357
- return makeUnion(this, other);
358
- },
359
- _a;
234
+ return shapes.every(function (shape, idx) { return shape.check(x[idx]); }) && tryPredicates(this, x);
235
+ } });
360
236
  }
361
237
  /**
362
- * Creates a shape representing an enum type
238
+ * Creates a shape representing an enum type.
363
239
  * (Not an actual enum as made using the enum keyword)
364
240
  *
365
241
  * `Shapes.enum(["a", "b", "c"])` is equivalent to `"a" | "b" | "c"`
366
242
  */
367
243
  function enumOf(values) {
368
- var _a;
369
- return _a = {
370
- typename: "".concat(values.map(function (v) { return JSON.stringify(v); }).join(" | "))
371
- },
372
- _a[Predicates] = [],
373
- _a.check = function (x) {
374
- return (isNumber(x) || isString(x)) && values.includes(x) && this[Predicates].every(function (p) { return p.predicate(x); });
375
- },
376
- _a.where = function (predicate, description) {
377
- var _a;
378
- return __assign(__assign({}, this), (_a = {}, _a[Predicates] = __spreadArray(__spreadArray([], this[Predicates], true), [{ predicate: predicate, description: description }], false), _a));
379
- },
380
- _a.and = function (other) {
381
- return makeIntersection(this, other);
382
- },
383
- _a.or = function (other) {
384
- return makeUnion(this, other);
385
- },
386
- _a;
244
+ return __assign(__assign({}, base()), { typename: "".concat(values.map(function (v) { return JSON.stringify(v); }).join(" | ")), check: function (x) {
245
+ return (isNumber(x) || isString(x)) && values.includes(x) && tryPredicates(this, x);
246
+ } });
387
247
  }
388
248
  /// TYPE MODIFIERS
389
249
  /**
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.1",
9
+ "version": "0.3.2",
10
10
  "private": false,
11
11
  "main": "dist/index.js",
12
12
  "scripts": {