creo 0.0.2 → 0.0.4-dev

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.
Files changed (54) hide show
  1. package/.env.development +1 -0
  2. package/.github/workflows/main.yml +24 -0
  3. package/README.md +1 -52
  4. package/TODOS.md +2 -0
  5. package/index.html +13 -0
  6. package/index.ts +1 -0
  7. package/package.json +12 -44
  8. package/src/DOM/Context.ts +36 -0
  9. package/src/DOM/DomEngine.ts +106 -0
  10. package/src/DOM/IRenderCycle.ts +9 -0
  11. package/src/DOM/Key.ts +1 -0
  12. package/src/DOM/Node.ts +472 -0
  13. package/src/DOM/Registry.ts +53 -0
  14. package/src/creo.ts +134 -0
  15. package/src/data-structures/assert/assert.ts +12 -0
  16. package/src/data-structures/indexed-map/IndexedMap.ts +281 -0
  17. package/src/data-structures/linked-map/LinkedMap.spec.ts +67 -0
  18. package/src/data-structures/linked-map/LinkedMap.ts +198 -0
  19. package/src/data-structures/list/List.spec.ts +181 -0
  20. package/src/data-structures/list/List.ts +195 -0
  21. package/src/data-structures/maybe/Maybe.ts +25 -0
  22. package/src/data-structures/null/null.ts +3 -0
  23. package/src/data-structures/record/IsRecordLike.spec.ts +29 -0
  24. package/src/data-structures/record/IsRecordLike.ts +3 -0
  25. package/src/data-structures/record/Record.spec.ts +240 -0
  26. package/src/data-structures/record/Record.ts +145 -0
  27. package/src/data-structures/shalllowEqual/shallowEqual.ts +26 -0
  28. package/src/data-structures/simpleKey/simpleKey.ts +8 -0
  29. package/src/data-structures/wildcard/wildcard.ts +1 -0
  30. package/src/examples/SimpleTodoList/SimpleTodoList.ts +53 -0
  31. package/src/examples/simple.ts +0 -0
  32. package/src/globals.d.ts +1 -0
  33. package/src/main.ts +24 -0
  34. package/src/style.css +41 -0
  35. package/src/ui/html/Block.ts +10 -0
  36. package/src/ui/html/Button.ts +12 -0
  37. package/src/ui/html/HStack.ts +10 -0
  38. package/src/ui/html/Inline.ts +12 -0
  39. package/src/ui/html/List.ts +10 -0
  40. package/src/ui/html/Text.ts +9 -0
  41. package/src/ui/html/VStack.ts +11 -0
  42. package/src/vite-env.d.ts +1 -0
  43. package/tsconfig.json +23 -0
  44. package/vite.config.js +10 -0
  45. package/LICENSE +0 -21
  46. package/dist/index.cjs +0 -54
  47. package/dist/index.cjs.map +0 -1
  48. package/dist/index.d.ts +0 -19
  49. package/dist/index.js +0 -51
  50. package/dist/index.js.map +0 -1
  51. package/dist/index.mjs +0 -51
  52. package/dist/index.mjs.map +0 -1
  53. package/dist/index.umd.js +0 -61
  54. package/dist/index.umd.js.map +0 -1
@@ -0,0 +1,145 @@
1
+ /**
2
+ * Ideas:
3
+ * [x] didUpdate support
4
+ * [x] Proxy proxifies all children as well
5
+ * [x] Support nested updates + nested listeners (e.g. only part of the object)
6
+ * [x] Cache records
7
+ * [ ] Keep track on updates, until there are no users on the old state
8
+ * [ ] Support symbol iterator
9
+ * [ ] Add js dispose tracker to automatically close listeners
10
+ * [ ] Allow to "stop propagate" changes if needed (or allow catching changes only on top level)
11
+ */
12
+
13
+ import { Maybe } from "../maybe/Maybe";
14
+ import { Wildcard } from "../wildcard/wildcard";
15
+ import { isRecordLike } from "./IsRecordLike";
16
+
17
+ // #region Record Type
18
+ const ParentRecord = Symbol("parent-record");
19
+ // const example: RecordOf<{foo: 'bar'}> = {
20
+ // foo: 'bar',
21
+ // [ParentRecord]: null // Root record
22
+ // }
23
+ export type RecordOf<T extends object> = T & {
24
+ [ParentRecord]: Maybe<WeakRef<RecordOf<Wildcard>>>;
25
+ };
26
+ type RecordDidChangeListener<T extends object> = (record: RecordOf<T>) => void;
27
+
28
+ export function isRecord<T extends object>(value: T): value is RecordOf<T> {
29
+ return ParentRecord in value;
30
+ }
31
+
32
+ // #region Weak map for listenets
33
+ const didUpdateMap: WeakMap<
34
+ RecordOf<Wildcard>,
35
+ Set<RecordDidChangeListener<Wildcard>>
36
+ > = new WeakMap();
37
+
38
+ // #region Update notifier
39
+ const scheduledUpdatesNotifiers: Set<RecordOf<Wildcard>> = new Set();
40
+ let shouldScheduleMicrotask = true;
41
+ function queuedNotifier() {
42
+ function iterate(record: RecordOf<Wildcard>) {
43
+ const listeners = didUpdateMap.get(record);
44
+ listeners?.forEach((listener) => {
45
+ listener(record);
46
+ });
47
+ const maybeParent: Maybe<RecordOf<Wildcard>> = record[ParentRecord];
48
+ if (maybeParent) {
49
+ iterate(maybeParent);
50
+ }
51
+ }
52
+ shouldScheduleMicrotask = true;
53
+ scheduledUpdatesNotifiers.forEach(iterate);
54
+ }
55
+ function recordDidUpdate<T extends object>(record: RecordOf<T>) {
56
+ scheduledUpdatesNotifiers.add(record);
57
+ shouldScheduleMicrotask && queueMicrotask(queuedNotifier);
58
+ shouldScheduleMicrotask = false;
59
+ }
60
+
61
+ type InternalOnly = never;
62
+
63
+ // #region Record creation, fields wrapper
64
+ function creoRecord<TNode extends object, T extends object>(
65
+ parent: Maybe<RecordOf<TNode>>,
66
+ value: T,
67
+ ): RecordOf<T> {
68
+ const parentWeakRef = parent != null ? new WeakRef(parent) : null;
69
+
70
+ type CacheField<K extends keyof T> = T[K] extends object
71
+ ? Maybe<RecordOf<T[K]>>
72
+ : never;
73
+ type Cache = { [K in keyof T]: CacheField<K> };
74
+ const cache: Cache = {} as Cache;
75
+ const record: RecordOf<T> = new Proxy(value, {
76
+ // @ts-ignore we override `has` to improve typing
77
+ has<K extends keyof T>(target: T, property: K): boolean {
78
+ if (property === ParentRecord) {
79
+ return true;
80
+ }
81
+ return property in target;
82
+ },
83
+ // @ts-ignore we override `get` to improve typing
84
+ get<K extends keyof T>(target: T, property: K): T[K] {
85
+ const val = target[property];
86
+
87
+ if (property === ParentRecord) {
88
+ // Only for internal use
89
+ return parentWeakRef?.deref() as InternalOnly;
90
+ }
91
+ // If the value is cached, return the cached record:
92
+ if (cache[property] != null) {
93
+ return cache[property] as T[K];
94
+ }
95
+ // No cached value:
96
+ if (isRecordLike(val)) {
97
+ // Object / Array, etc.
98
+ // we proxify all nested objects / arrays to ensure correct behaviour
99
+ const childRecord = creoRecord(record, val);
100
+ cache[property] = childRecord as CacheField<K>;
101
+ return childRecord;
102
+ }
103
+
104
+ // Primitive value:
105
+ return val;
106
+ },
107
+ set<K, TNewValue>(target: T, property: K, newValue: TNewValue) {
108
+ // property is actually the keyof K, but TS defines Proxy differently:
109
+ const prop: keyof T = property as keyof T;
110
+ const value: T[typeof prop] = newValue as T[typeof prop];
111
+
112
+ target[prop] = value;
113
+ if (cache[prop] != null) {
114
+ cache[prop] = null as Cache[keyof Cache];
115
+ }
116
+ recordDidUpdate(record);
117
+ return true;
118
+ },
119
+ }) as RecordOf<T>;
120
+ didUpdateMap.set(record, new Set());
121
+ return record;
122
+ }
123
+
124
+ // #region Public API
125
+ export function record<TNode extends object>(value: TNode): RecordOf<TNode> {
126
+ if (isRecord(value)) {
127
+ return value;
128
+ }
129
+ return creoRecord(null, value);
130
+ }
131
+
132
+ export function onDidUpdate<T extends object>(
133
+ record: RecordOf<T>,
134
+ listener: (record: RecordOf<T>) => void,
135
+ ): () => void {
136
+ const listeners = didUpdateMap.get(record);
137
+ if (!listeners) {
138
+ // Safe-guard: Essentialy this path cannot happen
139
+ throw new TypeError(`Record ${record} was created without listener`);
140
+ }
141
+ listeners.add(listener);
142
+ return function unsubscribe() {
143
+ listeners.delete(listener);
144
+ };
145
+ }
@@ -0,0 +1,26 @@
1
+ import { Wildcard } from "../wildcard/wildcard";
2
+
3
+ export function shallowEqual(a: Wildcard, b: Wildcard): boolean {
4
+ if (a === b) return true;
5
+
6
+ if (
7
+ typeof a !== "object" ||
8
+ a === null ||
9
+ typeof b !== "object" ||
10
+ b === null
11
+ ) {
12
+ return false;
13
+ }
14
+
15
+ const keysA = Object.keys(a);
16
+ if (keysA.length !== Object.keys(b).length) return false;
17
+
18
+ for (let i = 0; i < keysA.length; i++) {
19
+ const key = keysA[i];
20
+ if (!Object.is(a[key], b[key])) {
21
+ return false;
22
+ }
23
+ }
24
+
25
+ return true;
26
+ }
@@ -0,0 +1,8 @@
1
+ let i = 0;
2
+
3
+ export function generateNextKey(childrenSize: number): string {
4
+ if (i === Number.MAX_SAFE_INTEGER) {
5
+ i = 0;
6
+ }
7
+ return `c:${i++}:${childrenSize}`;
8
+ }
@@ -0,0 +1 @@
1
+ export type Wildcard = any;
@@ -0,0 +1,53 @@
1
+ import { Block, Button, creo, Inline, Text } from "../../creo";
2
+ import { Maybe } from "../../data-structures/maybe/Maybe";
3
+ import { _ } from "../../data-structures/null/null";
4
+
5
+ type Todo = { text: string };
6
+
7
+ export const SimpleTodoList = creo<{ text: string; todos: Array<Todo> }>(
8
+ (c) => {
9
+ const todos: Array<Todo> = c.tracked(c.p.todos);
10
+ let button: () => Maybe<HTMLElement>;
11
+ return {
12
+ didMount() {
13
+ console.warn("did mount");
14
+ console.log(button());
15
+ button()?.addEventListener("click", () => {
16
+ todos.push({ text: `Task #${todos.length}` });
17
+ });
18
+ // button?.extension.getButton().addEventListener("click", () => {
19
+ // todos.push({ text: `New Todo: ${counter++}` });
20
+ // });
21
+ },
22
+ render() {
23
+ console.log("rendering todo list");
24
+ Inline(_, () => {
25
+ Text(c.p.text);
26
+ });
27
+ Block(_, () => {
28
+ Text("Hello inside container");
29
+ Block(_, () => {
30
+ TodoList({ todos });
31
+ });
32
+ });
33
+ button = Button(_, () => Text("Add todo"));
34
+ },
35
+ };
36
+ },
37
+ );
38
+
39
+ export const TodoList = creo<{ todos: Array<Todo> }>((c) => {
40
+ const todos: Array<Todo> = c.tracked(c.p.todos);
41
+ return {
42
+ render() {
43
+ console.log("rendering todos");
44
+ todos.map((todo) => {
45
+ console.log(todo);
46
+ Block({ class: "todo" }, () => {
47
+ console.log(`Entity: ${todo.text}`);
48
+ Text(`Entity: ${todo.text}`);
49
+ });
50
+ });
51
+ },
52
+ };
53
+ });
File without changes
@@ -0,0 +1 @@
1
+ declare const __DEV__: boolean;
package/src/main.ts ADDED
@@ -0,0 +1,24 @@
1
+ import { record } from "./data-structures/record/Record";
2
+ import { DomEngine } from "./DOM/DomEngine";
3
+ import { SimpleTodoList } from "./examples/SimpleTodoList/SimpleTodoList";
4
+ import "./style.css";
5
+
6
+ const todoList = record([
7
+ { text: "First" },
8
+ { text: "Second" },
9
+ { text: "Third" },
10
+ ]);
11
+
12
+ const engine = new DomEngine(document.querySelector("#app") as HTMLElement);
13
+ engine.render(() => {
14
+ SimpleTodoList({
15
+ text: "Hello world",
16
+ todos: todoList,
17
+ });
18
+ });
19
+
20
+ todoList.push({ text: "New item" });
21
+
22
+ setTimeout(() => {
23
+ todoList[2].text = "123";
24
+ }, 1000);
package/src/style.css ADDED
@@ -0,0 +1,41 @@
1
+ :root {
2
+ font-family: Inter, system-ui, Avenir, Helvetica, Arial, sans-serif;
3
+ line-height: 1.5;
4
+ font-weight: 400;
5
+
6
+ color-scheme: light dark;
7
+ color: rgba(255, 255, 255, 0.87);
8
+ background-color: #242424;
9
+
10
+ font-synthesis: none;
11
+ text-rendering: optimizeLegibility;
12
+ -webkit-font-smoothing: antialiased;
13
+ -moz-osx-font-smoothing: grayscale;
14
+ }
15
+
16
+ a {
17
+ font-weight: 500;
18
+ color: #646cff;
19
+ text-decoration: inherit;
20
+ }
21
+ a:hover {
22
+ color: #535bf2;
23
+ }
24
+
25
+ body {
26
+ margin: 0;
27
+ display: flex;
28
+ place-items: center;
29
+ min-width: 320px;
30
+ min-height: 100vh;
31
+ }
32
+
33
+ h1 {
34
+ font-size: 3.2em;
35
+ line-height: 1.1;
36
+ }
37
+
38
+ .todo {
39
+ margin: 10px;
40
+ border: 1px solid blue;
41
+ }
@@ -0,0 +1,10 @@
1
+ import { creo, ui } from "../../creo";
2
+
3
+ export const Block = creo<{
4
+ // TODO replace with nice UI params
5
+ [key: string]: string;
6
+ }>((c) => ({
7
+ render() {
8
+ ui("div", c.p, c.slot);
9
+ },
10
+ }));
@@ -0,0 +1,12 @@
1
+ import { creo, ui } from "../../creo";
2
+
3
+ export const Button = creo<{
4
+ // TODO replace with nice UI params
5
+ [key: string]: string;
6
+ }>((c) => {
7
+ return {
8
+ render() {
9
+ ui("button", c.p, c.slot);
10
+ },
11
+ };
12
+ });
@@ -0,0 +1,10 @@
1
+ import { creo, ui } from "../../creo";
2
+
3
+ export const HStack = creo<{
4
+ // TODO replace with nice UI params
5
+ [key: string]: string;
6
+ }>((c) => ({
7
+ render() {
8
+ ui("div", c.p, c.slot);
9
+ },
10
+ }));
@@ -0,0 +1,12 @@
1
+ import { creo, ui } from "../../creo";
2
+
3
+ export const Inline = creo<{
4
+ // TODO replace with nice UI params
5
+ [key: string]: string;
6
+ }>((c) => {
7
+ return {
8
+ render() {
9
+ ui("span", c.p, c.slot);
10
+ },
11
+ };
12
+ });
@@ -0,0 +1,10 @@
1
+ import { creo, ui } from "../../creo";
2
+
3
+ export const List = creo<{
4
+ // TODO replace with nice UI params
5
+ [key: string]: string;
6
+ }>((c) => ({
7
+ render() {
8
+ ui("div", c.p, c.slot);
9
+ },
10
+ }));
@@ -0,0 +1,9 @@
1
+ import { creo, ui } from "../../creo";
2
+
3
+ export const Text = creo<string>((c) => {
4
+ return {
5
+ render() {
6
+ ui("text", c.p);
7
+ },
8
+ };
9
+ });
@@ -0,0 +1,11 @@
1
+ import { creo, ui } from "../../creo";
2
+
3
+ export const VStack = creo<{
4
+ // TODO replace with nice UI params
5
+ [key: string]: string;
6
+ }>((c) => ({
7
+ render() {
8
+ ui("div", c.p, c.slot);
9
+ },
10
+ // TODO allow `with` field to enable the `slot`
11
+ }));
@@ -0,0 +1 @@
1
+ /// <reference types="vite/client" />
package/tsconfig.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2020",
4
+ "useDefineForClassFields": true,
5
+ "module": "ESNext",
6
+ "lib": ["ESNext", "ES2020", "DOM", "DOM.Iterable"],
7
+ "skipLibCheck": true,
8
+ "experimentalDecorators": true,
9
+
10
+ /* Bundler mode */
11
+ "moduleResolution": "bundler",
12
+ "allowImportingTsExtensions": true,
13
+ "resolveJsonModule": true,
14
+ "isolatedModules": true,
15
+ "noEmit": true,
16
+
17
+ "strict": true,
18
+ "noUnusedLocals": true,
19
+ "noUnusedParameters": true,
20
+ "noFallthroughCasesInSwitch": true
21
+ },
22
+ "include": ["src"]
23
+ }
package/vite.config.js ADDED
@@ -0,0 +1,10 @@
1
+ import { defineConfig, loadEnv } from "vite";
2
+
3
+ export default defineConfig(({ mode }) => {
4
+ const env = loadEnv(mode, process.cwd(), "");
5
+ return {
6
+ define: {
7
+ __DEV__: env.__DEV__,
8
+ },
9
+ };
10
+ });
package/LICENSE DELETED
@@ -1,21 +0,0 @@
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/dist/index.cjs DELETED
@@ -1,54 +0,0 @@
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
@@ -1 +0,0 @@
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 DELETED
@@ -1,19 +0,0 @@
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 DELETED
@@ -1,51 +0,0 @@
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
package/dist/index.js.map DELETED
@@ -1 +0,0 @@
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 DELETED
@@ -1,51 +0,0 @@
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
@@ -1 +0,0 @@
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;;;;"}