functionalscript 0.12.3 → 0.12.4

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "functionalscript",
3
- "version": "0.12.3",
3
+ "version": "0.12.4",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "**/*.js",
@@ -62,4 +62,6 @@ export declare const record: MakeType1<'record'>;
62
62
  export type Or<T extends readonly Type[]> = () => readonly ['or', ...T];
63
63
  /** Constructs a schema that validates a value matching any of the given schemas. */
64
64
  export declare const or: <T extends readonly Type[]>(...types: T) => Or<T>;
65
+ /** Constructs a schema that validates a value matching `T` or `undefined`. */
66
+ export declare const option: <T extends Type>(t: T) => Or<readonly [T, undefined]>;
65
67
  export {};
@@ -21,3 +21,5 @@ export const array = type1('array');
21
21
  export const record = type1('record');
22
22
  /** Constructs a schema that validates a value matching any of the given schemas. */
23
23
  export const or = (...types) => () => ['or', ...types];
24
+ /** Constructs a schema that validates a value matching `T` or `undefined`. */
25
+ export const option = (t) => or(t, undefined);
@@ -82,6 +82,10 @@ declare const _default: {
82
82
  error: () => void;
83
83
  };
84
84
  };
85
+ option: {
86
+ ok: () => void;
87
+ error: () => void;
88
+ };
85
89
  recursive: {
86
90
  arrayOfArrays: () => void;
87
91
  recordOfRecords: () => void;
@@ -1,5 +1,5 @@
1
1
  import { validate } from "./module.f.js";
2
- import { boolean, number, string, bigint, unknown, array, record, or } from "../module.f.js";
2
+ import { boolean, number, string, bigint, unknown, array, record, or, option } from "../module.f.js";
3
3
  const assertOk = ([k]) => { if (k !== 'ok') {
4
4
  throw 'expected ok';
5
5
  } };
@@ -195,6 +195,18 @@ export default {
195
195
  },
196
196
  },
197
197
  },
198
+ option: {
199
+ ok: () => {
200
+ const t = option(number);
201
+ assertOk(validate(t)(42));
202
+ assertOk(validate(t)(undefined));
203
+ },
204
+ error: () => {
205
+ const t = option(number);
206
+ assertError(validate(t)(null));
207
+ assertError(validate(t)('42'));
208
+ },
209
+ },
198
210
  recursive: {
199
211
  arrayOfArrays: () => {
200
212
  // self-referential schema: an array whose elements are also arrays of the same type