creo 0.0.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/LICENSE +21 -0
- package/README.md +52 -0
- package/dist/index.cjs +54 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.js +51 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +51 -0
- package/dist/index.mjs.map +1 -0
- package/dist/index.umd.js +61 -0
- package/dist/index.umd.js.map +1 -0
- package/package.json +52 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Nik Mostovoy
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# @duorun/Maybe
|
|
2
|
+
|
|
3
|
+
TypeScript implementation maybe monad.
|
|
4
|
+
|
|
5
|
+
1. Easy-to-use
|
|
6
|
+
2. Small (267 Bytes)!
|
|
7
|
+
3. Typed API
|
|
8
|
+
|
|
9
|
+
## Install:
|
|
10
|
+
|
|
11
|
+
```shell
|
|
12
|
+
npm i @duorun/maybe --save
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Playground:
|
|
16
|
+
|
|
17
|
+
Sandbox: https://codesandbox.io/p/sandbox/nifty-lake-5wpq2y
|
|
18
|
+
|
|
19
|
+
## Usages
|
|
20
|
+
|
|
21
|
+
Value is defined:
|
|
22
|
+
|
|
23
|
+
```js
|
|
24
|
+
just("bar"); // Maybe<string>
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Value is empty:
|
|
28
|
+
|
|
29
|
+
```js
|
|
30
|
+
none(); // Maybe<never>
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Note: null / undefeind can be valid data:
|
|
34
|
+
|
|
35
|
+
```js
|
|
36
|
+
just(null); // Maybe<null>
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Check if value is defined:
|
|
40
|
+
|
|
41
|
+
```js
|
|
42
|
+
import { isNone } from "@duorun/maybe";
|
|
43
|
+
|
|
44
|
+
function test(maybe: Maybe<string>) {
|
|
45
|
+
if (isNone(maybe)) {
|
|
46
|
+
// maybe is None
|
|
47
|
+
} else {
|
|
48
|
+
// maybe is just
|
|
49
|
+
maybe.value; // string
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
```
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
var id = 0;
|
|
2
|
+
function _classPrivateFieldLooseKey(name) {
|
|
3
|
+
return "__private_" + id++ + "_" + name;
|
|
4
|
+
}
|
|
5
|
+
function _classPrivateFieldLooseBase(receiver, privateKey) {
|
|
6
|
+
if (!Object.prototype.hasOwnProperty.call(receiver, privateKey)) {
|
|
7
|
+
throw new TypeError("attempted to use private field on non-instance");
|
|
8
|
+
}
|
|
9
|
+
return receiver;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const JUST = true;
|
|
13
|
+
const NONE = false;
|
|
14
|
+
var _type = /*#__PURE__*/_classPrivateFieldLooseKey("type");
|
|
15
|
+
var _value = /*#__PURE__*/_classPrivateFieldLooseKey("value");
|
|
16
|
+
class CMaybe {
|
|
17
|
+
constructor(type, value) {
|
|
18
|
+
Object.defineProperty(this, _type, {
|
|
19
|
+
writable: true,
|
|
20
|
+
value: void 0
|
|
21
|
+
});
|
|
22
|
+
Object.defineProperty(this, _value, {
|
|
23
|
+
writable: true,
|
|
24
|
+
value: void 0
|
|
25
|
+
});
|
|
26
|
+
_classPrivateFieldLooseBase(this, _type)[_type] = type;
|
|
27
|
+
_classPrivateFieldLooseBase(this, _value)[_value] = value;
|
|
28
|
+
}
|
|
29
|
+
get value() {
|
|
30
|
+
return _classPrivateFieldLooseBase(this, _value)[_value];
|
|
31
|
+
}
|
|
32
|
+
isNone() {
|
|
33
|
+
return _classPrivateFieldLooseBase(this, _type)[_type] === NONE;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
function just(value) {
|
|
37
|
+
return new CMaybe(JUST, value);
|
|
38
|
+
}
|
|
39
|
+
function none() {
|
|
40
|
+
// @ts-expect-error We ignore here as maybe.value will never be accessible at this point
|
|
41
|
+
return new CMaybe(NONE, undefined);
|
|
42
|
+
}
|
|
43
|
+
function isNone(maybe) {
|
|
44
|
+
return maybe.isNone();
|
|
45
|
+
}
|
|
46
|
+
function isJust(maybe) {
|
|
47
|
+
return !maybe.isNone();
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
exports.isJust = isJust;
|
|
51
|
+
exports.isNone = isNone;
|
|
52
|
+
exports.just = just;
|
|
53
|
+
exports.none = none;
|
|
54
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../index.ts"],"sourcesContent":["const JUST = true as const;\ntype TJust = typeof JUST;\nconst NONE = false as const;\ntype TNone = typeof NONE;\ntype STATE = TJust | TNone;\n\nclass CMaybe<TValue, TState extends STATE = STATE> {\n #type: TState;\n #value: TState extends TJust ? TValue : undefined;\n constructor(type: TState, value: TState extends TJust ? TValue : undefined) {\n this.#type = type;\n this.#value = value;\n }\n\n public get value(): TState extends TJust ? TValue : undefined {\n return this.#value;\n }\n\n isNone(): this is None {\n return this.#type === NONE;\n }\n}\n\nexport type Just<T> = CMaybe<T, TJust>;\nexport type None<T = never> = CMaybe<T, TNone>;\n\nexport type Maybe<T> = Just<T> | None;\n\nexport function just<T>(value: T): Just<T> {\n return new CMaybe(JUST, value);\n}\n\nexport function none<T = never>(): None<T> {\n // @ts-expect-error We ignore here as maybe.value will never be accessible at this point\n return new CMaybe(NONE, undefined);\n}\n\nexport function isNone<T>(maybe: CMaybe<T>): maybe is None {\n return maybe.isNone();\n}\n\nexport function isJust<T>(maybe: CMaybe<T>): maybe is Just<T> {\n return !maybe.isNone();\n}\n"],"names":["JUST","NONE","_type","_classPrivateFieldLooseKey","_value","CMaybe","constructor","type","value","Object","defineProperty","writable","_classPrivateFieldLooseBase","isNone","just","none","undefined","maybe","isJust"],"mappings":";;;;;;;;;;;AAAA,MAAMA,IAAI,GAAG,IAAa,CAAA;AAE1B,MAAMC,IAAI,GAAG,KAAc,CAAA;AAAC,IAAAC,KAAA,gBAAAC,0BAAA,CAAA,MAAA,CAAA,CAAA;AAAA,IAAAC,MAAA,gBAAAD,0BAAA,CAAA,OAAA,CAAA,CAAA;AAI5B,MAAME,MAAM,CAAA;AAGVC,EAAAA,WAAYA,CAAAC,IAAY,EAAEC,KAAgD,EAAA;IAAAC,MAAA,CAAAC,cAAA,CAAA,IAAA,EAAAR,KAAA,EAAA;MAAAS,QAAA,EAAA,IAAA;MAAAH,KAAA,EAAA,KAAA,CAAA;AAAA,KAAA,CAAA,CAAA;IAAAC,MAAA,CAAAC,cAAA,CAAA,IAAA,EAAAN,MAAA,EAAA;MAAAO,QAAA,EAAA,IAAA;MAAAH,KAAA,EAAA,KAAA,CAAA;AAAA,KAAA,CAAA,CAAA;AACxEI,IAAAA,2BAAA,KAAI,EAAAV,KAAA,CAAAA,CAAAA,KAAA,IAASK,IAAI,CAAA;AACjBK,IAAAA,2BAAA,KAAI,EAAAR,MAAA,CAAAA,CAAAA,MAAA,IAAUI,KAAK,CAAA;AACrB,GAAA;EAEA,IAAWA,KAAKA,GAAA;AACd,IAAA,OAAAI,2BAAA,CAAO,IAAI,EAAAR,MAAA,EAAAA,MAAA,CAAA,CAAA;AACb,GAAA;AAEAS,EAAAA,MAAMA,GAAA;AACJ,IAAA,OAAOD,2BAAA,CAAI,IAAA,EAAAV,KAAA,CAAAA,CAAAA,KAAA,MAAWD,IAAI,CAAA;AAC5B,GAAA;AACD,CAAA;AAOK,SAAUa,IAAIA,CAAIN,KAAQ,EAAA;AAC9B,EAAA,OAAO,IAAIH,MAAM,CAACL,IAAI,EAAEQ,KAAK,CAAC,CAAA;AAChC,CAAA;SAEgBO,IAAIA,GAAA;AAClB;AACA,EAAA,OAAO,IAAIV,MAAM,CAACJ,IAAI,EAAEe,SAAS,CAAC,CAAA;AACpC,CAAA;AAEM,SAAUH,MAAMA,CAAII,KAAgB,EAAA;AACxC,EAAA,OAAOA,KAAK,CAACJ,MAAM,EAAE,CAAA;AACvB,CAAA;AAEM,SAAUK,MAAMA,CAAID,KAAgB,EAAA;AACxC,EAAA,OAAO,CAACA,KAAK,CAACJ,MAAM,EAAE,CAAA;AACxB;;;;;;;"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
declare const JUST: true;
|
|
2
|
+
type TJust = typeof JUST;
|
|
3
|
+
declare const NONE: false;
|
|
4
|
+
type TNone = typeof NONE;
|
|
5
|
+
type STATE = TJust | TNone;
|
|
6
|
+
declare class CMaybe<TValue, TState extends STATE = STATE> {
|
|
7
|
+
#private;
|
|
8
|
+
constructor(type: TState, value: TState extends TJust ? TValue : undefined);
|
|
9
|
+
get value(): TState extends TJust ? TValue : undefined;
|
|
10
|
+
isNone(): this is None;
|
|
11
|
+
}
|
|
12
|
+
export type Just<T> = CMaybe<T, TJust>;
|
|
13
|
+
export type None<T = never> = CMaybe<T, TNone>;
|
|
14
|
+
export type Maybe<T> = Just<T> | None;
|
|
15
|
+
export declare function just<T>(value: T): Just<T>;
|
|
16
|
+
export declare function none<T = never>(): None<T>;
|
|
17
|
+
export declare function isNone<T>(maybe: CMaybe<T>): maybe is None;
|
|
18
|
+
export declare function isJust<T>(maybe: CMaybe<T>): maybe is Just<T>;
|
|
19
|
+
export {};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
var id = 0;
|
|
2
|
+
function _classPrivateFieldLooseKey(name) {
|
|
3
|
+
return "__private_" + id++ + "_" + name;
|
|
4
|
+
}
|
|
5
|
+
function _classPrivateFieldLooseBase(receiver, privateKey) {
|
|
6
|
+
if (!Object.prototype.hasOwnProperty.call(receiver, privateKey)) {
|
|
7
|
+
throw new TypeError("attempted to use private field on non-instance");
|
|
8
|
+
}
|
|
9
|
+
return receiver;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const JUST = true;
|
|
13
|
+
const NONE = false;
|
|
14
|
+
var _type = /*#__PURE__*/_classPrivateFieldLooseKey("type");
|
|
15
|
+
var _value = /*#__PURE__*/_classPrivateFieldLooseKey("value");
|
|
16
|
+
class CMaybe {
|
|
17
|
+
constructor(type, value) {
|
|
18
|
+
Object.defineProperty(this, _type, {
|
|
19
|
+
writable: true,
|
|
20
|
+
value: void 0
|
|
21
|
+
});
|
|
22
|
+
Object.defineProperty(this, _value, {
|
|
23
|
+
writable: true,
|
|
24
|
+
value: void 0
|
|
25
|
+
});
|
|
26
|
+
_classPrivateFieldLooseBase(this, _type)[_type] = type;
|
|
27
|
+
_classPrivateFieldLooseBase(this, _value)[_value] = value;
|
|
28
|
+
}
|
|
29
|
+
get value() {
|
|
30
|
+
return _classPrivateFieldLooseBase(this, _value)[_value];
|
|
31
|
+
}
|
|
32
|
+
isNone() {
|
|
33
|
+
return _classPrivateFieldLooseBase(this, _type)[_type] === NONE;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
function just(value) {
|
|
37
|
+
return new CMaybe(JUST, value);
|
|
38
|
+
}
|
|
39
|
+
function none() {
|
|
40
|
+
// @ts-expect-error We ignore here as maybe.value will never be accessible at this point
|
|
41
|
+
return new CMaybe(NONE, undefined);
|
|
42
|
+
}
|
|
43
|
+
function isNone(maybe) {
|
|
44
|
+
return maybe.isNone();
|
|
45
|
+
}
|
|
46
|
+
function isJust(maybe) {
|
|
47
|
+
return !maybe.isNone();
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export { isJust, isNone, just, none };
|
|
51
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../index.ts"],"sourcesContent":["const JUST = true as const;\ntype TJust = typeof JUST;\nconst NONE = false as const;\ntype TNone = typeof NONE;\ntype STATE = TJust | TNone;\n\nclass CMaybe<TValue, TState extends STATE = STATE> {\n #type: TState;\n #value: TState extends TJust ? TValue : undefined;\n constructor(type: TState, value: TState extends TJust ? TValue : undefined) {\n this.#type = type;\n this.#value = value;\n }\n\n public get value(): TState extends TJust ? TValue : undefined {\n return this.#value;\n }\n\n isNone(): this is None {\n return this.#type === NONE;\n }\n}\n\nexport type Just<T> = CMaybe<T, TJust>;\nexport type None<T = never> = CMaybe<T, TNone>;\n\nexport type Maybe<T> = Just<T> | None;\n\nexport function just<T>(value: T): Just<T> {\n return new CMaybe(JUST, value);\n}\n\nexport function none<T = never>(): None<T> {\n // @ts-expect-error We ignore here as maybe.value will never be accessible at this point\n return new CMaybe(NONE, undefined);\n}\n\nexport function isNone<T>(maybe: CMaybe<T>): maybe is None {\n return maybe.isNone();\n}\n\nexport function isJust<T>(maybe: CMaybe<T>): maybe is Just<T> {\n return !maybe.isNone();\n}\n"],"names":["JUST","NONE","_type","_classPrivateFieldLooseKey","_value","CMaybe","constructor","type","value","Object","defineProperty","writable","_classPrivateFieldLooseBase","isNone","just","none","undefined","maybe","isJust"],"mappings":";;;;;;;;;;;AAAA,MAAMA,IAAI,GAAG,IAAa,CAAA;AAE1B,MAAMC,IAAI,GAAG,KAAc,CAAA;AAAC,IAAAC,KAAA,gBAAAC,0BAAA,CAAA,MAAA,CAAA,CAAA;AAAA,IAAAC,MAAA,gBAAAD,0BAAA,CAAA,OAAA,CAAA,CAAA;AAI5B,MAAME,MAAM,CAAA;AAGVC,EAAAA,WAAYA,CAAAC,IAAY,EAAEC,KAAgD,EAAA;IAAAC,MAAA,CAAAC,cAAA,CAAA,IAAA,EAAAR,KAAA,EAAA;MAAAS,QAAA,EAAA,IAAA;MAAAH,KAAA,EAAA,KAAA,CAAA;AAAA,KAAA,CAAA,CAAA;IAAAC,MAAA,CAAAC,cAAA,CAAA,IAAA,EAAAN,MAAA,EAAA;MAAAO,QAAA,EAAA,IAAA;MAAAH,KAAA,EAAA,KAAA,CAAA;AAAA,KAAA,CAAA,CAAA;AACxEI,IAAAA,2BAAA,KAAI,EAAAV,KAAA,CAAAA,CAAAA,KAAA,IAASK,IAAI,CAAA;AACjBK,IAAAA,2BAAA,KAAI,EAAAR,MAAA,CAAAA,CAAAA,MAAA,IAAUI,KAAK,CAAA;AACrB,GAAA;EAEA,IAAWA,KAAKA,GAAA;AACd,IAAA,OAAAI,2BAAA,CAAO,IAAI,EAAAR,MAAA,EAAAA,MAAA,CAAA,CAAA;AACb,GAAA;AAEAS,EAAAA,MAAMA,GAAA;AACJ,IAAA,OAAOD,2BAAA,CAAI,IAAA,EAAAV,KAAA,CAAAA,CAAAA,KAAA,MAAWD,IAAI,CAAA;AAC5B,GAAA;AACD,CAAA;AAOK,SAAUa,IAAIA,CAAIN,KAAQ,EAAA;AAC9B,EAAA,OAAO,IAAIH,MAAM,CAACL,IAAI,EAAEQ,KAAK,CAAC,CAAA;AAChC,CAAA;SAEgBO,IAAIA,GAAA;AAClB;AACA,EAAA,OAAO,IAAIV,MAAM,CAACJ,IAAI,EAAEe,SAAS,CAAC,CAAA;AACpC,CAAA;AAEM,SAAUH,MAAMA,CAAII,KAAgB,EAAA;AACxC,EAAA,OAAOA,KAAK,CAACJ,MAAM,EAAE,CAAA;AACvB,CAAA;AAEM,SAAUK,MAAMA,CAAID,KAAgB,EAAA;AACxC,EAAA,OAAO,CAACA,KAAK,CAACJ,MAAM,EAAE,CAAA;AACxB;;;;"}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
var id = 0;
|
|
2
|
+
function _classPrivateFieldLooseKey(name) {
|
|
3
|
+
return "__private_" + id++ + "_" + name;
|
|
4
|
+
}
|
|
5
|
+
function _classPrivateFieldLooseBase(receiver, privateKey) {
|
|
6
|
+
if (!Object.prototype.hasOwnProperty.call(receiver, privateKey)) {
|
|
7
|
+
throw new TypeError("attempted to use private field on non-instance");
|
|
8
|
+
}
|
|
9
|
+
return receiver;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const JUST = true;
|
|
13
|
+
const NONE = false;
|
|
14
|
+
var _type = /*#__PURE__*/_classPrivateFieldLooseKey("type");
|
|
15
|
+
var _value = /*#__PURE__*/_classPrivateFieldLooseKey("value");
|
|
16
|
+
class CMaybe {
|
|
17
|
+
constructor(type, value) {
|
|
18
|
+
Object.defineProperty(this, _type, {
|
|
19
|
+
writable: true,
|
|
20
|
+
value: void 0
|
|
21
|
+
});
|
|
22
|
+
Object.defineProperty(this, _value, {
|
|
23
|
+
writable: true,
|
|
24
|
+
value: void 0
|
|
25
|
+
});
|
|
26
|
+
_classPrivateFieldLooseBase(this, _type)[_type] = type;
|
|
27
|
+
_classPrivateFieldLooseBase(this, _value)[_value] = value;
|
|
28
|
+
}
|
|
29
|
+
get value() {
|
|
30
|
+
return _classPrivateFieldLooseBase(this, _value)[_value];
|
|
31
|
+
}
|
|
32
|
+
isNone() {
|
|
33
|
+
return _classPrivateFieldLooseBase(this, _type)[_type] === NONE;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
function just(value) {
|
|
37
|
+
return new CMaybe(JUST, value);
|
|
38
|
+
}
|
|
39
|
+
function none() {
|
|
40
|
+
// @ts-expect-error We ignore here as maybe.value will never be accessible at this point
|
|
41
|
+
return new CMaybe(NONE, undefined);
|
|
42
|
+
}
|
|
43
|
+
function isNone(maybe) {
|
|
44
|
+
return maybe.isNone();
|
|
45
|
+
}
|
|
46
|
+
function isJust(maybe) {
|
|
47
|
+
return !maybe.isNone();
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export { isJust, isNone, just, none };
|
|
51
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":["../index.ts"],"sourcesContent":["const JUST = true as const;\ntype TJust = typeof JUST;\nconst NONE = false as const;\ntype TNone = typeof NONE;\ntype STATE = TJust | TNone;\n\nclass CMaybe<TValue, TState extends STATE = STATE> {\n #type: TState;\n #value: TState extends TJust ? TValue : undefined;\n constructor(type: TState, value: TState extends TJust ? TValue : undefined) {\n this.#type = type;\n this.#value = value;\n }\n\n public get value(): TState extends TJust ? TValue : undefined {\n return this.#value;\n }\n\n isNone(): this is None {\n return this.#type === NONE;\n }\n}\n\nexport type Just<T> = CMaybe<T, TJust>;\nexport type None<T = never> = CMaybe<T, TNone>;\n\nexport type Maybe<T> = Just<T> | None;\n\nexport function just<T>(value: T): Just<T> {\n return new CMaybe(JUST, value);\n}\n\nexport function none<T = never>(): None<T> {\n // @ts-expect-error We ignore here as maybe.value will never be accessible at this point\n return new CMaybe(NONE, undefined);\n}\n\nexport function isNone<T>(maybe: CMaybe<T>): maybe is None {\n return maybe.isNone();\n}\n\nexport function isJust<T>(maybe: CMaybe<T>): maybe is Just<T> {\n return !maybe.isNone();\n}\n"],"names":["JUST","NONE","_type","_classPrivateFieldLooseKey","_value","CMaybe","constructor","type","value","Object","defineProperty","writable","_classPrivateFieldLooseBase","isNone","just","none","undefined","maybe","isJust"],"mappings":";;;;;;;;;;;AAAA,MAAMA,IAAI,GAAG,IAAa,CAAA;AAE1B,MAAMC,IAAI,GAAG,KAAc,CAAA;AAAC,IAAAC,KAAA,gBAAAC,0BAAA,CAAA,MAAA,CAAA,CAAA;AAAA,IAAAC,MAAA,gBAAAD,0BAAA,CAAA,OAAA,CAAA,CAAA;AAI5B,MAAME,MAAM,CAAA;AAGVC,EAAAA,WAAYA,CAAAC,IAAY,EAAEC,KAAgD,EAAA;IAAAC,MAAA,CAAAC,cAAA,CAAA,IAAA,EAAAR,KAAA,EAAA;MAAAS,QAAA,EAAA,IAAA;MAAAH,KAAA,EAAA,KAAA,CAAA;AAAA,KAAA,CAAA,CAAA;IAAAC,MAAA,CAAAC,cAAA,CAAA,IAAA,EAAAN,MAAA,EAAA;MAAAO,QAAA,EAAA,IAAA;MAAAH,KAAA,EAAA,KAAA,CAAA;AAAA,KAAA,CAAA,CAAA;AACxEI,IAAAA,2BAAA,KAAI,EAAAV,KAAA,CAAAA,CAAAA,KAAA,IAASK,IAAI,CAAA;AACjBK,IAAAA,2BAAA,KAAI,EAAAR,MAAA,CAAAA,CAAAA,MAAA,IAAUI,KAAK,CAAA;AACrB,GAAA;EAEA,IAAWA,KAAKA,GAAA;AACd,IAAA,OAAAI,2BAAA,CAAO,IAAI,EAAAR,MAAA,EAAAA,MAAA,CAAA,CAAA;AACb,GAAA;AAEAS,EAAAA,MAAMA,GAAA;AACJ,IAAA,OAAOD,2BAAA,CAAI,IAAA,EAAAV,KAAA,CAAAA,CAAAA,KAAA,MAAWD,IAAI,CAAA;AAC5B,GAAA;AACD,CAAA;AAOK,SAAUa,IAAIA,CAAIN,KAAQ,EAAA;AAC9B,EAAA,OAAO,IAAIH,MAAM,CAACL,IAAI,EAAEQ,KAAK,CAAC,CAAA;AAChC,CAAA;SAEgBO,IAAIA,GAAA;AAClB;AACA,EAAA,OAAO,IAAIV,MAAM,CAACJ,IAAI,EAAEe,SAAS,CAAC,CAAA;AACpC,CAAA;AAEM,SAAUH,MAAMA,CAAII,KAAgB,EAAA;AACxC,EAAA,OAAOA,KAAK,CAACJ,MAAM,EAAE,CAAA;AACvB,CAAA;AAEM,SAAUK,MAAMA,CAAID,KAAgB,EAAA;AACxC,EAAA,OAAO,CAACA,KAAK,CAACJ,MAAM,EAAE,CAAA;AACxB;;;;"}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
(function (global, factory) {
|
|
2
|
+
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
|
|
3
|
+
typeof define === 'function' && define.amd ? define(['exports'], factory) :
|
|
4
|
+
(global = global || self, factory(global["@duorun/maybe"] = {}));
|
|
5
|
+
})(this, (function (exports) {
|
|
6
|
+
var id = 0;
|
|
7
|
+
function _classPrivateFieldLooseKey(name) {
|
|
8
|
+
return "__private_" + id++ + "_" + name;
|
|
9
|
+
}
|
|
10
|
+
function _classPrivateFieldLooseBase(receiver, privateKey) {
|
|
11
|
+
if (!Object.prototype.hasOwnProperty.call(receiver, privateKey)) {
|
|
12
|
+
throw new TypeError("attempted to use private field on non-instance");
|
|
13
|
+
}
|
|
14
|
+
return receiver;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const JUST = true;
|
|
18
|
+
const NONE = false;
|
|
19
|
+
var _type = /*#__PURE__*/_classPrivateFieldLooseKey("type");
|
|
20
|
+
var _value = /*#__PURE__*/_classPrivateFieldLooseKey("value");
|
|
21
|
+
class CMaybe {
|
|
22
|
+
constructor(type, value) {
|
|
23
|
+
Object.defineProperty(this, _type, {
|
|
24
|
+
writable: true,
|
|
25
|
+
value: void 0
|
|
26
|
+
});
|
|
27
|
+
Object.defineProperty(this, _value, {
|
|
28
|
+
writable: true,
|
|
29
|
+
value: void 0
|
|
30
|
+
});
|
|
31
|
+
_classPrivateFieldLooseBase(this, _type)[_type] = type;
|
|
32
|
+
_classPrivateFieldLooseBase(this, _value)[_value] = value;
|
|
33
|
+
}
|
|
34
|
+
get value() {
|
|
35
|
+
return _classPrivateFieldLooseBase(this, _value)[_value];
|
|
36
|
+
}
|
|
37
|
+
isNone() {
|
|
38
|
+
return _classPrivateFieldLooseBase(this, _type)[_type] === NONE;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
function just(value) {
|
|
42
|
+
return new CMaybe(JUST, value);
|
|
43
|
+
}
|
|
44
|
+
function none() {
|
|
45
|
+
// @ts-expect-error We ignore here as maybe.value will never be accessible at this point
|
|
46
|
+
return new CMaybe(NONE, undefined);
|
|
47
|
+
}
|
|
48
|
+
function isNone(maybe) {
|
|
49
|
+
return maybe.isNone();
|
|
50
|
+
}
|
|
51
|
+
function isJust(maybe) {
|
|
52
|
+
return !maybe.isNone();
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
exports.isJust = isJust;
|
|
56
|
+
exports.isNone = isNone;
|
|
57
|
+
exports.just = just;
|
|
58
|
+
exports.none = none;
|
|
59
|
+
|
|
60
|
+
}));
|
|
61
|
+
//# sourceMappingURL=index.umd.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.umd.js","sources":["../index.ts"],"sourcesContent":["const JUST = true as const;\ntype TJust = typeof JUST;\nconst NONE = false as const;\ntype TNone = typeof NONE;\ntype STATE = TJust | TNone;\n\nclass CMaybe<TValue, TState extends STATE = STATE> {\n #type: TState;\n #value: TState extends TJust ? TValue : undefined;\n constructor(type: TState, value: TState extends TJust ? TValue : undefined) {\n this.#type = type;\n this.#value = value;\n }\n\n public get value(): TState extends TJust ? TValue : undefined {\n return this.#value;\n }\n\n isNone(): this is None {\n return this.#type === NONE;\n }\n}\n\nexport type Just<T> = CMaybe<T, TJust>;\nexport type None<T = never> = CMaybe<T, TNone>;\n\nexport type Maybe<T> = Just<T> | None;\n\nexport function just<T>(value: T): Just<T> {\n return new CMaybe(JUST, value);\n}\n\nexport function none<T = never>(): None<T> {\n // @ts-expect-error We ignore here as maybe.value will never be accessible at this point\n return new CMaybe(NONE, undefined);\n}\n\nexport function isNone<T>(maybe: CMaybe<T>): maybe is None {\n return maybe.isNone();\n}\n\nexport function isJust<T>(maybe: CMaybe<T>): maybe is Just<T> {\n return !maybe.isNone();\n}\n"],"names":["JUST","NONE","_type","_classPrivateFieldLooseKey","_value","CMaybe","constructor","type","value","Object","defineProperty","writable","_classPrivateFieldLooseBase","isNone","just","none","undefined","maybe","isJust"],"mappings":";;;;;;;;;;;;;;;;EAAA,MAAMA,IAAI,GAAG,IAAa,CAAA;EAE1B,MAAMC,IAAI,GAAG,KAAc,CAAA;EAAC,IAAAC,KAAA,gBAAAC,0BAAA,CAAA,MAAA,CAAA,CAAA;EAAA,IAAAC,MAAA,gBAAAD,0BAAA,CAAA,OAAA,CAAA,CAAA;EAI5B,MAAME,MAAM,CAAA;EAGVC,EAAAA,WAAYA,CAAAC,IAAY,EAAEC,KAAgD,EAAA;MAAAC,MAAA,CAAAC,cAAA,CAAA,IAAA,EAAAR,KAAA,EAAA;QAAAS,QAAA,EAAA,IAAA;QAAAH,KAAA,EAAA,KAAA,CAAA;EAAA,KAAA,CAAA,CAAA;MAAAC,MAAA,CAAAC,cAAA,CAAA,IAAA,EAAAN,MAAA,EAAA;QAAAO,QAAA,EAAA,IAAA;QAAAH,KAAA,EAAA,KAAA,CAAA;EAAA,KAAA,CAAA,CAAA;EACxEI,IAAAA,2BAAA,KAAI,EAAAV,KAAA,CAAAA,CAAAA,KAAA,IAASK,IAAI,CAAA;EACjBK,IAAAA,2BAAA,KAAI,EAAAR,MAAA,CAAAA,CAAAA,MAAA,IAAUI,KAAK,CAAA;EACrB,GAAA;IAEA,IAAWA,KAAKA,GAAA;EACd,IAAA,OAAAI,2BAAA,CAAO,IAAI,EAAAR,MAAA,EAAAA,MAAA,CAAA,CAAA;EACb,GAAA;EAEAS,EAAAA,MAAMA,GAAA;EACJ,IAAA,OAAOD,2BAAA,CAAI,IAAA,EAAAV,KAAA,CAAAA,CAAAA,KAAA,MAAWD,IAAI,CAAA;EAC5B,GAAA;EACD,CAAA;EAOK,SAAUa,IAAIA,CAAIN,KAAQ,EAAA;EAC9B,EAAA,OAAO,IAAIH,MAAM,CAACL,IAAI,EAAEQ,KAAK,CAAC,CAAA;EAChC,CAAA;WAEgBO,IAAIA,GAAA;EAClB;EACA,EAAA,OAAO,IAAIV,MAAM,CAACJ,IAAI,EAAEe,SAAS,CAAC,CAAA;EACpC,CAAA;EAEM,SAAUH,MAAMA,CAAII,KAAgB,EAAA;EACxC,EAAA,OAAOA,KAAK,CAACJ,MAAM,EAAE,CAAA;EACvB,CAAA;EAEM,SAAUK,MAAMA,CAAID,KAAgB,EAAA;EACxC,EAAA,OAAO,CAACA,KAAK,CAACJ,MAAM,EAAE,CAAA;EACxB;;;;;;;;;;;"}
|
package/package.json
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "creo",
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"description": "Simple and typed Maybe monad",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"source": "./index.ts",
|
|
7
|
+
"main": "dist/index.js",
|
|
8
|
+
"module": "dist/index.js",
|
|
9
|
+
"esmodule": "dist/index.mjs",
|
|
10
|
+
"umd:main": "dist/index.umd.js",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"module": "./dist/index.module.js",
|
|
15
|
+
"import": "./dist/index.mjs",
|
|
16
|
+
"require": "./dist/index.js"
|
|
17
|
+
},
|
|
18
|
+
"./package.json": "./package.json"
|
|
19
|
+
},
|
|
20
|
+
"sideEffects": false,
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"@size-limit/preset-small-lib": "^11.0.0",
|
|
23
|
+
"bun-types": "latest",
|
|
24
|
+
"size-limit": "^11.0.0"
|
|
25
|
+
},
|
|
26
|
+
"peerDependencies": {
|
|
27
|
+
"typescript": "^5.0.0"
|
|
28
|
+
},
|
|
29
|
+
"repository": {
|
|
30
|
+
"type": "git",
|
|
31
|
+
"url": "git+ssh://git@github.com/xnimorz/maybe.git"
|
|
32
|
+
},
|
|
33
|
+
"browserslist": ["last 2 years", "node >= 12"],
|
|
34
|
+
"author": "Nik (nik@xnim.me)",
|
|
35
|
+
"license": "MIT",
|
|
36
|
+
"files": ["dist"],
|
|
37
|
+
"bugs": {
|
|
38
|
+
"url": "https://github.com/xnimorz/maybe/issues"
|
|
39
|
+
},
|
|
40
|
+
"homepage": "https://github.com/xnimorz/maybe#readme",
|
|
41
|
+
"keywords": ["monad", "maybe", "ts-maybe"],
|
|
42
|
+
"scripts": {
|
|
43
|
+
"build": "microbundle build --entry ./index.ts --name @duorun/maybe --tsconfig tsconfig.json --compress false & bun run size-limit",
|
|
44
|
+
"prepublishOnly": "bun test & bun run build"
|
|
45
|
+
},
|
|
46
|
+
"size-limit": [
|
|
47
|
+
{
|
|
48
|
+
"path": "dist/index.js",
|
|
49
|
+
"limit": "270 B"
|
|
50
|
+
}
|
|
51
|
+
]
|
|
52
|
+
}
|