@onrails/maybe 0.2.5 → 0.3.0

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/dist/maybe.d.ts CHANGED
@@ -11,12 +11,37 @@
11
11
  * }
12
12
  * ```
13
13
  */
14
- export type Maybe<T> = {
14
+ export type Maybe<T> = MaybeData<T> & MaybeMethods<T>;
15
+ /**
16
+ * Plain-data shape of a {@link Maybe}: what `JSON.parse`, `structuredClone`
17
+ * and `postMessage` hand back. It has no methods; lift it with `Maybe.from`.
18
+ */
19
+ export type MaybeData<T> = {
15
20
  readonly _tag: "Some";
16
21
  readonly value: T;
17
22
  } | {
18
23
  readonly _tag: "None";
19
24
  };
25
+ /**
26
+ * Methods every live {@link Maybe} carries on its (non-enumerable) prototype.
27
+ * Core semantics, mirroring `ResultMethods`: `tap` does not swallow throws.
28
+ */
29
+ export interface MaybeMethods<T> {
30
+ /** Transform the `Some` value; `None` passes through. */
31
+ map<U>(fn: (value: T) => U): Maybe<U>;
32
+ /** Canonical bind; short-circuits on `None`. */
33
+ flatMap<U>(fn: (value: T) => Maybe<U>): Maybe<U>;
34
+ /** Alias of {@link MaybeMethods.flatMap}. */
35
+ andThen<U>(fn: (value: T) => Maybe<U>): Maybe<U>;
36
+ /** Fold both branches into one value. */
37
+ match<U>(onSome: (value: T) => U, onNone: () => U): U;
38
+ /** The `Some` value, or `defaultValue` on `None`. */
39
+ unwrapOr<U>(defaultValue: U): T | U;
40
+ /** Observe the `Some` value, pass the maybe through. */
41
+ tap(fn: (value: T) => void): Maybe<T>;
42
+ /** Observe `None`, pass the maybe through. */
43
+ tapNone(fn: () => void): Maybe<T>;
44
+ }
20
45
  /** The `Some` branch of a {@link Maybe} — a present value tagged `"Some"`. */
21
46
  export type Some<T> = Extract<Maybe<T>, {
22
47
  _tag: "Some";
@@ -54,6 +79,22 @@ export declare const of: typeof some;
54
79
  * ```
55
80
  */
56
81
  export declare const none: <T = never>() => Maybe<T>;
82
+ /**
83
+ * Value-side companion of the {@link Maybe} type. `from` lifts plain data back
84
+ * into a live `Maybe` at every deserialization sink (`JSON.parse`,
85
+ * `structuredClone`, `postMessage`, Redux rehydration). It trusts the shape,
86
+ * but an unknown `_tag` is a defect and throws a `TypeError` rather than
87
+ * silently becoming `None`.
88
+ *
89
+ * @example
90
+ * ```ts
91
+ * const live = Maybe.from<number>(JSON.parse(JSON.stringify(some(1))));
92
+ * live.map((n) => n + 1);
93
+ * ```
94
+ */
95
+ export declare const Maybe: {
96
+ readonly from: <T>(data: MaybeData<T>) => Maybe<T>;
97
+ };
57
98
  /**
58
99
  * Type-narrowing predicate: returns `true` when the value is `Some`.
59
100
  *
@@ -153,8 +194,8 @@ export declare const match: {
153
194
  <T, U>(onSome: (value: T) => U, onNone: () => U): (maybe: Maybe<T>) => U;
154
195
  };
155
196
  export declare const unwrapOr: {
156
- <T>(maybe: Maybe<T>, defaultValue: T): T;
157
- <T>(defaultValue: T): (maybe: Maybe<T>) => T;
197
+ <T, U>(maybe: Maybe<T>, defaultValue: U): T | U;
198
+ <U>(defaultValue: U): <T>(maybe: Maybe<T>) => T | U;
158
199
  };
159
200
  /**
160
201
  * Unwraps the `Some` value or throws if the value is `None`.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onrails/maybe",
3
- "version": "0.2.5",
3
+ "version": "0.3.0",
4
4
  "description": "Tagged Maybe for expected absence — pure tagged unions, Result interop, tree-shakeable",
5
5
  "license": "MIT",
6
6
  "author": "Alan R. Soares",
@@ -74,7 +74,7 @@
74
74
  }
75
75
  },
76
76
  "peerDependencies": {
77
- "@onrails/result": "^0.4.0"
77
+ "@onrails/result": "^0.4.2"
78
78
  },
79
79
  "peerDependenciesMeta": {
80
80
  "@onrails/result": {
@@ -90,7 +90,7 @@
90
90
  "prepublishOnly": "bun run build"
91
91
  },
92
92
  "devDependencies": {
93
- "@onrails/result": "0.4.0",
93
+ "@onrails/result": "0.4.2",
94
94
  "fast-check": "^4.9.0",
95
95
  "ts-expect": "^1.3.0",
96
96
  "tsup": "^8.5.1"