semantic-typescript 0.3.8 → 0.5.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/tree.js ADDED
@@ -0,0 +1,257 @@
1
+ import { isFunction, isRedBlackNode } from './guard';
2
+ import { useCompare } from './hook';
3
+ import { BinaryNode, RedBlackNode } from './node';
4
+ import { Optional } from './optional';
5
+ import { BinaryTreeSymbol, TreeSymbol } from './symbol';
6
+ import { validate } from './utility';
7
+ export class AbstractTree {
8
+ Tree = TreeSymbol;
9
+ internal = 0n;
10
+ constructor() {
11
+ }
12
+ isEmpty() {
13
+ return this.root().isEmpty();
14
+ }
15
+ size() {
16
+ return this.internal;
17
+ }
18
+ contains(value) {
19
+ return this.find(value).isPresent();
20
+ }
21
+ toArray() {
22
+ return Array.from(this.preorder());
23
+ }
24
+ toSet() {
25
+ return new Set(this.preorder());
26
+ }
27
+ height() {
28
+ let root = this.root();
29
+ if (root.isPresent()) {
30
+ return root.get().height();
31
+ }
32
+ return 0n;
33
+ }
34
+ width() {
35
+ let root = this.root();
36
+ if (root.isPresent()) {
37
+ return root.get().width();
38
+ }
39
+ return 0n;
40
+ }
41
+ depth() {
42
+ let root = this.root();
43
+ if (root.isPresent()) {
44
+ return root.get().depth();
45
+ }
46
+ return 0n;
47
+ }
48
+ level() {
49
+ let root = this.root();
50
+ if (root.isPresent()) {
51
+ return root.get().level();
52
+ }
53
+ return 0n;
54
+ }
55
+ }
56
+ ;
57
+ export class BinaryTree extends AbstractTree {
58
+ BinaryTree = BinaryTreeSymbol;
59
+ first;
60
+ constructor(root = Optional.empty()) {
61
+ super();
62
+ this.first = root;
63
+ }
64
+ root() {
65
+ return this.first;
66
+ }
67
+ find(value) {
68
+ let root = this.root();
69
+ if (root.isPresent()) {
70
+ for (let node of root.get().preorder()) {
71
+ if (node.getElement() === value || Object.is(node.getElement(), value)) {
72
+ return Optional.of(node);
73
+ }
74
+ }
75
+ }
76
+ return Optional.empty();
77
+ }
78
+ *preorder() {
79
+ let root = this.root();
80
+ if (root.isPresent()) {
81
+ for (let node of root.get().preorder()) {
82
+ let element = node.getElement();
83
+ if (validate(element) && element.isPresent()) {
84
+ yield element.get();
85
+ }
86
+ }
87
+ }
88
+ }
89
+ *inorder() {
90
+ let root = this.root();
91
+ if (root.isPresent()) {
92
+ for (let node of root.get().inorder()) {
93
+ let element = node.getElement();
94
+ if (validate(element) && element.isPresent()) {
95
+ yield element.get();
96
+ }
97
+ }
98
+ }
99
+ }
100
+ *postorder() {
101
+ let root = this.root();
102
+ if (root.isPresent()) {
103
+ for (let node of root.get().postorder()) {
104
+ let element = node.getElement();
105
+ if (validate(element) && element.isPresent()) {
106
+ yield element.get();
107
+ }
108
+ }
109
+ }
110
+ }
111
+ isBalanced() {
112
+ let root = this.root();
113
+ if (root.isPresent()) {
114
+ return root.get().isBanlanced();
115
+ }
116
+ return false;
117
+ }
118
+ isComplete() {
119
+ let root = this.root();
120
+ if (root.isPresent()) {
121
+ return root.get().isComplete();
122
+ }
123
+ return false;
124
+ }
125
+ isFull() {
126
+ let root = this.root();
127
+ if (root.isPresent()) {
128
+ return root.get().isFull();
129
+ }
130
+ return false;
131
+ }
132
+ isPerfect() {
133
+ let root = this.root();
134
+ if (root.isPresent()) {
135
+ return root.get().isPerfect();
136
+ }
137
+ return false;
138
+ }
139
+ *[Symbol.iterator]() {
140
+ yield* this.preorder();
141
+ }
142
+ }
143
+ ;
144
+ export class RedBlackTree extends BinaryTree {
145
+ RedBlackTree = Symbol("RedBlackTree");
146
+ comparator;
147
+ constructor(argument1, argument2) {
148
+ if (isFunction(argument1)) {
149
+ super();
150
+ this.comparator = argument1;
151
+ }
152
+ else if (isRedBlackNode(argument1)) {
153
+ super(argument1);
154
+ if (isFunction(argument2)) {
155
+ this.comparator = argument2;
156
+ }
157
+ else {
158
+ this.comparator = useCompare;
159
+ }
160
+ }
161
+ else {
162
+ throw new TypeError("Invalid arguments.");
163
+ }
164
+ }
165
+ add(value) {
166
+ let node = new RedBlackNode(value);
167
+ if (this.first.isEmpty()) {
168
+ this.first = Optional.of(node);
169
+ }
170
+ else {
171
+ this.first.get().compareAndLink(node);
172
+ }
173
+ this.internal++;
174
+ }
175
+ remove(value) {
176
+ let node = this.find(value);
177
+ if (node.isPresent()) {
178
+ node.get().detach();
179
+ node.get().fix();
180
+ this.internal--;
181
+ }
182
+ }
183
+ *breadth() {
184
+ let root = this.root();
185
+ if (root.isPresent()) {
186
+ let queue = [root.get()];
187
+ while (queue.length > 0) {
188
+ let node = queue.shift();
189
+ let element = node.getElement();
190
+ if (validate(element) && element.isPresent()) {
191
+ yield element.get();
192
+ }
193
+ if (validate(node.getLeft()) && node.getRight().isPresent()) {
194
+ queue.push(node.getLeft().get());
195
+ }
196
+ if (validate(node.getLeft()) && node.getLeft().isPresent()) {
197
+ queue.push(node.getLeft().get());
198
+ }
199
+ }
200
+ }
201
+ }
202
+ sub(node) {
203
+ return new RedBlackTree(Optional.of(node));
204
+ }
205
+ prune(node) {
206
+ let tree = new RedBlackTree();
207
+ let queue = [node];
208
+ while (queue.length > 0) {
209
+ let node = queue.shift();
210
+ let element = node.getElement();
211
+ if (validate(element) && element.isPresent()) {
212
+ tree.add(element.get());
213
+ }
214
+ if (validate(node.getLeft()) && node.getLeft().isPresent()) {
215
+ queue.push(node.getLeft().get());
216
+ }
217
+ if (validate(node.getRight()) && node.getRight().isPresent()) {
218
+ queue.push(node.getRight().get());
219
+ }
220
+ return tree;
221
+ }
222
+ return tree;
223
+ }
224
+ merge(other) {
225
+ let tree = new RedBlackTree();
226
+ let queue = [this.root().get()];
227
+ while (queue.length > 0) {
228
+ let node = queue.shift();
229
+ let element = node.getElement();
230
+ if (validate(element) && element.isPresent()) {
231
+ tree.add(element.get());
232
+ }
233
+ if (validate(node.getLeft()) && node.getLeft().isPresent()) {
234
+ queue.push(node.getLeft().get());
235
+ }
236
+ if (validate(node.getRight()) && node.getRight().isPresent()) {
237
+ queue.push(node.getRight().get());
238
+ }
239
+ }
240
+ queue = [other.root().get()];
241
+ while (queue.length > 0) {
242
+ let node = queue.shift();
243
+ let element = node.getElement();
244
+ if (validate(element) && element.isPresent()) {
245
+ tree.add(element.get());
246
+ }
247
+ if (validate(node.getLeft()) && node.getLeft().isPresent()) {
248
+ queue.push(node.getLeft().get());
249
+ }
250
+ if (validate(node.getRight()) && node.getRight().isPresent()) {
251
+ queue.push(node.getRight().get());
252
+ }
253
+ }
254
+ return tree;
255
+ }
256
+ }
257
+ ;
package/dist/utility.d.ts CHANGED
@@ -1,9 +1,13 @@
1
1
  export type Invalid<T> = T extends null | undefined ? T : never;
2
2
  export type Valid<T> = T extends null | undefined ? never : T;
3
3
  export type MaybeInvalid<T> = T | null | undefined;
4
+ export type MaybeUndefined<T> = T | undefined;
5
+ export type MaybeNull<T> = T | null;
4
6
  export declare let validate: <T>(t: MaybeInvalid<T>) => t is T;
5
7
  export declare let invalidate: <T>(t: MaybeInvalid<T>) => t is (null | undefined);
6
- export type Primitive = string | number | boolean | symbol | bigint | Function | ((...args: any[]) => any);
8
+ export type Type = "undefined" | "null" | "boolean" | "number" | "bigint" | "symbol" | "string" | "function" | "object";
9
+ export declare let typeOf: <T>(t: T) => Type;
10
+ export type Primitive = null | undefined | string | number | boolean | symbol | bigint | Function | ((...args: any[]) => any);
7
11
  export type MaybePrimitive<T> = T | Primitive;
8
12
  export type AsyncFunction = (...args: any[]) => Promise<unknown>;
9
13
  export type DeepPropertyKey<T extends object> = {
@@ -12,6 +16,9 @@ export type DeepPropertyKey<T extends object> = {
12
16
  export type DeepPropertyValue<T extends object> = {
13
17
  [K in keyof T]: T[K] extends object ? DeepPropertyValue<T[K]> : T[K];
14
18
  };
19
+ export interface Constructor<T> {
20
+ new (...args: any[]): T;
21
+ }
15
22
  export interface Runnable {
16
23
  (): void;
17
24
  }
package/dist/utility.js CHANGED
@@ -4,6 +4,15 @@ export let validate = (t) => {
4
4
  export let invalidate = (t) => {
5
5
  return t === null || t === (void 0);
6
6
  };
7
+ export let typeOf = (t) => {
8
+ if (typeof t === "object") {
9
+ if (t === null) {
10
+ return "null";
11
+ }
12
+ return "object";
13
+ }
14
+ return typeof t;
15
+ };
7
16
  ;
8
17
  ;
9
18
  ;
package/dist/window.js CHANGED
@@ -7,11 +7,13 @@ export class WindowCollectable extends OrderedCollectable {
7
7
  WindowCollectable = WindowCollectableSymbol;
8
8
  constructor(parameter, comparator = useCompare) {
9
9
  super(parameter, comparator);
10
- Object.defineProperty(this, "WindowCollectable", {
11
- value: WindowCollectableSymbol,
12
- enumerable: false,
13
- writable: false,
14
- configurable: false
10
+ Object.defineProperties(this, {
11
+ "WindowCollectable": {
12
+ value: WindowCollectableSymbol,
13
+ writable: false,
14
+ enumerable: false,
15
+ configurable: false
16
+ }
15
17
  });
16
18
  Object.freeze(this);
17
19
  }
package/package.json CHANGED
@@ -6,7 +6,7 @@
6
6
  "url": "https://github.com/eloyhere"
7
7
  },
8
8
  "description": "A modern type-safe stream processing library inspired by JavaScript Generator, Java Stream, and MySQL Index. Supports lazy evaluation, async streams, statistics, and IO-like operations.",
9
- "version": "0.3.8",
9
+ "version": "0.5.0",
10
10
  "type": "module",
11
11
  "readme": "readme.md",
12
12
  "main": "dist/index.js",
@@ -52,10 +52,6 @@
52
52
  },
53
53
  "scripts": {
54
54
  "build": "tsc",
55
- "dev": "vite",
56
55
  "prepublishOnly": "npm run build"
57
- },
58
- "devDependencies": {
59
- "typescript": "~5.9.3"
60
56
  }
61
57
  }