semantic-typescript 0.4.1 → 0.5.3

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/guard.js CHANGED
@@ -1,4 +1,4 @@
1
- import { BigIntStatisticsSymbol, CollectableSymbol, CollectorsSymbol, HashableSymbol, NumericStatisticsSymbol, OrderedCollectableSymbol, SemanticMapSymbol, SemanticSymbol, StatisticsSymbol, UnorderedCollectableSymbol, WindowCollectableSymbol } from "./symbol";
1
+ import { BigIntStatisticsSymbol, CollectableSymbol, CollectorsSymbol, NumericStatisticsSymbol, OptionalSymbol, OrderedCollectableSymbol, SemanticSymbol, StatisticsSymbol, UnorderedCollectableSymbol, WindowCollectableSymbol } from "./symbol";
2
2
  export let isBoolean = (target) => {
3
3
  return typeof target === "boolean";
4
4
  };
@@ -89,51 +89,52 @@ export let isBigIntStatistics = (target) => {
89
89
  }
90
90
  return false;
91
91
  };
92
- export let isSemanticMap = (target) => {
92
+ export let isOptional = (target) => {
93
93
  if (isObject(target)) {
94
- return Reflect.get(target, "SemanticMap") === SemanticMapSymbol;
94
+ return Reflect.get(target, "Optional") === OptionalSymbol;
95
95
  }
96
96
  return false;
97
97
  };
98
- export let isHashable = (target) => {
98
+ export let isPromise = (target) => {
99
99
  if (isObject(target)) {
100
- return Reflect.get(target, HashableSymbol) === HashableSymbol;
100
+ return isFunction(Reflect.get(target, "then")) && isFunction(Reflect.get(target, "catch"));
101
101
  }
102
102
  return false;
103
103
  };
104
- export let isHashMap = (target) => {
105
- if (isObject(target)) {
106
- return Reflect.get(target, "HashMap") === SemanticMapSymbol;
104
+ export let isAsyncFunction = (target) => {
105
+ if (isFunction(target)) {
106
+ return Reflect.get(target, Symbol.toStringTag) === "AsyncFunction" && target.constructor.name === "AsyncFunction";
107
107
  }
108
108
  return false;
109
109
  };
110
- export let isHashSet = (target) => {
110
+ export let isGeneratorFunction = (target) => {
111
111
  if (isObject(target)) {
112
- return false;
112
+ return isFunction(target) && Reflect.get(target, "constructor").name === "GeneratorFunction";
113
113
  }
114
114
  return false;
115
115
  };
116
- export let isPromise = (target) => {
116
+ export let isAsyncGeneratorFunction = (target) => {
117
117
  if (isObject(target)) {
118
- return isFunction(Reflect.get(target, "then")) && isFunction(Reflect.get(target, "catch"));
118
+ return isFunction(target) && Reflect.get(target, "constructor").name === "AsyncGeneratorFunction";
119
119
  }
120
120
  return false;
121
121
  };
122
- export let isAsyncFunction = (target) => {
123
- if (isFunction(target)) {
124
- return Reflect.get(target, Symbol.toStringTag) === "AsyncFunction" && target.constructor.name === "AsyncFunction";
122
+ export let isWindow = (target) => {
123
+ if (isObject(target) && isObject(Reflect.get(target, "window"))) {
124
+ return Object.prototype.toString.call(Reflect.get(target, "window")) === "[object Window]";
125
125
  }
126
126
  return false;
127
127
  };
128
- export let isGeneratorFunction = (target) => {
129
- if (isObject(target)) {
130
- return isFunction(target) && Reflect.get(target, "constructor").name === "GeneratorFunction";
128
+ export let isDocument = (target) => {
129
+ if (isObject(target) && isObject(Reflect.get(target, "document"))) {
130
+ return Object.prototype.toString.call(Reflect.get(target, "document")) === "[object HTMLDocument]";
131
131
  }
132
132
  return false;
133
133
  };
134
- export let isAsyncGeneratorFunction = (target) => {
134
+ export let isHTMLElemet = (target) => {
135
135
  if (isObject(target)) {
136
- return isFunction(target) && Reflect.get(target, "constructor").name === "AsyncGeneratorFunction";
136
+ let regex = /^\[object HTML\w+Element\]$/;
137
+ return regex.test(Object.prototype.toString.call(target));
137
138
  }
138
139
  return false;
139
140
  };
package/dist/hash.d.ts CHANGED
@@ -1,6 +1,4 @@
1
1
  import { type Type } from "./utility";
2
- export interface Hashable {
3
- }
4
2
  export declare let maskOf: (type: Type) => bigint;
5
3
  interface Handler<T> {
6
4
  (value: T): bigint;
@@ -9,6 +7,7 @@ export declare let register: <T>(type: Type, handler: Handler<T>) => void;
9
7
  export declare let unregister: (type: Type) => void;
10
8
  interface UseHash {
11
9
  <T = unknown>(target: T): bigint;
10
+ (...value: Array<unknown>): bigint;
12
11
  }
13
12
  export declare let useHash: UseHash;
14
13
  export {};
package/dist/hash.js CHANGED
@@ -1,8 +1,6 @@
1
- import { isBigInt, isBoolean, isFunction, isHashable, isNumber, isObject, isString, isSymbol } from "./guard";
1
+ import { isBigInt, isBoolean, isFunction, isNumber, isObject, isString, isSymbol } from "./guard";
2
2
  import { useTraverse } from "./hook";
3
- import { HashableSymbol } from "./symbol";
4
3
  import { typeOf } from "./utility";
5
- ;
6
4
  const masks = new Map();
7
5
  masks.set("undefined", BigInt(masks.size));
8
6
  masks.set("null", BigInt(masks.size));
@@ -14,7 +12,6 @@ masks.set("string", BigInt(masks.size));
14
12
  masks.set("function", BigInt(masks.size));
15
13
  masks.set("object", BigInt(masks.size));
16
14
  Object.freeze(masks);
17
- const internal = Object.freeze(Array.from(masks.keys()));
18
15
  export let maskOf = (type) => {
19
16
  return masks.get(type) || masks.get("object") || 0n;
20
17
  };
@@ -27,7 +24,8 @@ export let register = (type, handler) => {
27
24
  }
28
25
  };
29
26
  export let unregister = (type) => {
30
- if (isString(type) && handlers.has(type) && !internal.includes(type)) {
27
+ let whiteList = ["undefined", "null", "boolean", "number", "bigint", "symbol", "string", "function", "object"];
28
+ if (isString(type) && handlers.has(type) && !whiteList.includes(type)) {
31
29
  handlers.delete(type);
32
30
  }
33
31
  };
@@ -196,16 +194,12 @@ register("object", (value) => {
196
194
  return 0n;
197
195
  });
198
196
  ;
199
- export let useHash = (target) => {
197
+ export let useHash = (...value) => {
198
+ let target = value.length === 1 ? value[0] : value;
200
199
  let type = typeOf(target);
201
- if (type === "object" && isHashable(target)) {
202
- if (complex.has(target)) {
203
- return complex.get(target) || 0n;
204
- }
205
- let hash = Reflect.get(target, HashableSymbol)();
206
- complex.set(target, hash);
207
- return hash;
200
+ let handler = handlers.get(type);
201
+ if (handler) {
202
+ return handler(target);
208
203
  }
209
- let handler = handlers.get(type) || (() => 0n);
210
- return handler(target);
204
+ return 0n;
211
205
  };
package/dist/hook.d.ts CHANGED
@@ -1,7 +1,11 @@
1
1
  import { type DeepPropertyKey, type DeepPropertyValue } from "./utility";
2
2
  import type { Comparator, Generator } from "./utility";
3
3
  export declare let useCompare: <T>(t1: T, t2: T) => number;
4
- export declare let Useandom: <T = number | bigint>(index: T) => T;
4
+ interface UseRandom {
5
+ <N extends number | bigint>(start: N): N extends number ? number : (N extends bigint ? bigint : never);
6
+ <N extends number | bigint>(start: N, end: N): N;
7
+ }
8
+ export declare let useRandom: UseRandom;
5
9
  export type UseTraverseKey<T extends object> = DeepPropertyKey<T> & (symbol | string | number);
6
10
  export type UseTraverseValue<T extends object> = DeepPropertyValue<T>;
7
11
  export type UseTraversePath<T extends object> = Array<UseTraverseKey<T> & (symbol | string | number)>;
package/dist/hook.js CHANGED
@@ -44,28 +44,81 @@ export let useCompare = (t1, t2) => {
44
44
  }
45
45
  throw new TypeError("Cannot compare values of different types.");
46
46
  };
47
- export let Useandom = (index) => {
48
- if (isNumber(index)) {
49
- let x = Number(index);
50
- let phi = (1 + Math.sqrt(5)) / 2;
51
- let vanDerCorput = (base, n) => {
52
- let result = 0;
53
- let f = 1 / base;
54
- let i = n;
55
- while (i > 0) {
56
- result += (i % base) * f;
57
- i = Math.floor(i / base);
58
- f = f / base;
47
+ ;
48
+ export let useRandom = (start, end) => {
49
+ let getRandomBits = () => {
50
+ let random = Math.random();
51
+ let full = random * 0x10000000000000;
52
+ let high = Math.floor(full / 0x100000000);
53
+ let low = Math.floor(full) & 0xFFFFFFFF;
54
+ return [high, low];
55
+ };
56
+ let getRandomBigInt = () => {
57
+ let [h1, l1] = getRandomBits();
58
+ let [h2, l2] = getRandomBits();
59
+ let [h3, l3] = getRandomBits();
60
+ return ((BigInt(h1) << 84n) | (BigInt(l1) << 52n) | (BigInt(h2) << 32n) | (BigInt(l2) << 3n) | (BigInt(h3) << 116n)) | (BigInt(l3) << 80n);
61
+ };
62
+ if (isNumber(start)) {
63
+ let seed = Math.random();
64
+ if (isNumber(end)) {
65
+ let minimum = Math.min(start, end);
66
+ let maximum = Math.max(start, end);
67
+ let range = maximum - minimum;
68
+ return (minimum + seed * range);
69
+ }
70
+ else {
71
+ return Math.exp(-seed);
72
+ }
73
+ }
74
+ if (isBigInt(start)) {
75
+ let randomBigInt = getRandomBigInt();
76
+ if (isBigInt(end)) {
77
+ let minimum = start < end ? start : end;
78
+ let maximum = start < end ? end : start;
79
+ let range = maximum - minimum;
80
+ if (range === 0n) {
81
+ return minimum;
82
+ }
83
+ let mask = range;
84
+ mask |= mask >> 1n;
85
+ mask |= mask >> 2n;
86
+ mask |= mask >> 4n;
87
+ mask |= mask >> 8n;
88
+ mask |= mask >> 16n;
89
+ mask |= mask >> 32n;
90
+ mask |= mask >> 64n;
91
+ let result;
92
+ do {
93
+ let raw = randomBigInt & mask;
94
+ result = raw;
95
+ result ^= result >> 17n;
96
+ result *= 0xed5ad4bbn;
97
+ result ^= result >> 11n;
98
+ result *= 0xac4c1b51n;
99
+ result ^= result >> 15n;
100
+ result *= 0x31848babn;
101
+ result ^= result >> 14n;
102
+ } while (result >= range);
103
+ return (minimum + result);
104
+ }
105
+ else {
106
+ let seed = Math.random();
107
+ let expValue = Math.exp(-seed);
108
+ let normalized = expValue / Math.E;
109
+ let [high, low] = getRandomBits();
110
+ let adjusted = BigInt(Math.floor(normalized * 0xFFFFFFFF)) << 32n;
111
+ let result = (adjusted | BigInt(low)) ^ BigInt(high);
112
+ if (start <= 0n) {
113
+ return 0n;
114
+ }
115
+ if (result >= start) {
116
+ result = result % start;
59
117
  }
60
118
  return result;
61
- };
62
- let h = vanDerCorput(2, x) + vanDerCorput(3, x);
63
- let golden = (x * phi) % 1;
64
- let lcg = (1103515245 * x + 12345) % 2147483648;
65
- let mixed = (h * 0.5 + golden * 0.3 + lcg / 2147483648 * 0.2);
66
- return (mixed * 1000000);
119
+ }
67
120
  }
68
- throw new TypeError("Invalid input type");
121
+ throw new TypeError("Invalid arguments.");
69
122
  };
70
123
  ;
71
124
  ;
package/dist/index.d.ts CHANGED
@@ -1,14 +1,8 @@
1
- export * from "./collectable";
2
1
  export * from "./collector";
3
2
  export * from "./factory";
4
3
  export * from "./guard";
5
- export * from "./hash";
6
4
  export * from "./hook";
7
- export * from "./map";
8
5
  export * from "./optional";
9
6
  export * from "./semantic";
10
- export * from "./set";
11
7
  export * from "./symbol";
12
- export * from "./statistics";
13
8
  export * from "./utility";
14
- export * from "./window";
package/dist/index.js CHANGED
@@ -1,14 +1,8 @@
1
- export * from "./collectable";
2
1
  export * from "./collector";
3
2
  export * from "./factory";
4
3
  export * from "./guard";
5
- export * from "./hash";
6
4
  export * from "./hook";
7
- export * from "./map";
8
5
  export * from "./optional";
9
6
  export * from "./semantic";
10
- export * from "./set";
11
7
  export * from "./symbol";
12
- export * from "./statistics";
13
8
  export * from "./utility";
14
- export * from "./window";
package/dist/main.d.ts ADDED
@@ -0,0 +1 @@
1
+ export {};
package/dist/main.js ADDED
@@ -0,0 +1,4 @@
1
+ import { useWindow } from 'semantic-typescript';
2
+ useWindow("click").toEvent().forEach((event, index) => {
3
+ console.log(event.type + `第${index}次触发`);
4
+ });
package/dist/map.d.ts CHANGED
@@ -70,3 +70,7 @@ export declare class HashMap<K, V> extends AbstractSemanticMap<K, V> {
70
70
  set(key: K, value: V): this;
71
71
  values(): IterableIterator<V>;
72
72
  }
73
+ export declare class TreeMap<K, V> {
74
+ key: K;
75
+ value: V;
76
+ }
package/dist/map.js CHANGED
@@ -83,9 +83,6 @@ export class AbstractSemanticMap {
83
83
  [Symbol.toStringTag] = "SemanticMap";
84
84
  }
85
85
  ;
86
- Object.freeze(AbstractSemanticMap);
87
- Object.freeze(AbstractSemanticMap.prototype);
88
- Object.freeze(Object.getPrototypeOf(AbstractSemanticMap));
89
86
  export class HashMap extends AbstractSemanticMap {
90
87
  buckets = new Map();
91
88
  threashold;
@@ -242,6 +239,7 @@ export class HashMap extends AbstractSemanticMap {
242
239
  }
243
240
  }
244
241
  ;
245
- Object.freeze(HashMap);
246
- Object.freeze(HashMap.prototype);
247
- Object.freeze(Object.getPrototypeOf(HashMap));
242
+ export class TreeMap {
243
+ key = (void 0);
244
+ value = (void 0);
245
+ }
package/dist/node.d.ts ADDED
@@ -0,0 +1,182 @@
1
+ import { Optional } from "./optional";
2
+ import { type Comparator } from "./utility";
3
+ export interface Node<E, N extends Node<E, N>> extends Iterable<N> {
4
+ ancestors(): Iterable<N>;
5
+ children(): Iterable<N>;
6
+ descendants(): Iterable<N>;
7
+ siblings(): Iterable<N>;
8
+ parent(): Optional<N>;
9
+ root(): N;
10
+ previousSibling(): Optional<N>;
11
+ nextSibling(): Optional<N>;
12
+ preorder(): Generator<N>;
13
+ inorder(): Generator<N>;
14
+ postorder(): Generator<N>;
15
+ breadthfirst(): Generator<N>;
16
+ depth(): bigint;
17
+ height(): bigint;
18
+ width(): bigint;
19
+ level(): bigint;
20
+ isLeaf(): boolean;
21
+ isRoot(): boolean;
22
+ [Symbol.iterator](): Iterator<N>;
23
+ }
24
+ export declare abstract class AbstractNode<E, N extends Node<E, N>> implements Node<E, N> {
25
+ protected readonly Node: symbol;
26
+ constructor();
27
+ ancestors(): Iterable<N>;
28
+ abstract children(): Iterable<N>;
29
+ descendants(): Iterable<N>;
30
+ root(): N;
31
+ siblings(): Iterable<N>;
32
+ abstract parent(): Optional<N>;
33
+ previousSibling(): Optional<N>;
34
+ nextSibling(): Optional<N>;
35
+ preorder(): Generator<N>;
36
+ inorder(): Generator<N>;
37
+ postorder(): Generator<N>;
38
+ breadthfirst(): Generator<N>;
39
+ [Symbol.iterator](): Iterator<N>;
40
+ depth(): bigint;
41
+ height(): bigint;
42
+ width(): bigint;
43
+ level(): bigint;
44
+ isLeaf(): boolean;
45
+ isRoot(): boolean;
46
+ }
47
+ export declare class LinearNode<E> extends AbstractNode<E, LinearNode<E>> {
48
+ protected previous: Optional<LinearNode<E>>;
49
+ protected next: Optional<LinearNode<E>>;
50
+ protected element: Optional<E>;
51
+ protected readonly LinearNode: symbol;
52
+ constructor(element: E);
53
+ constructor(element: E, previous: Optional<LinearNode<E>>, next: Optional<LinearNode<E>>);
54
+ ancestors(): Iterable<LinearNode<E>>;
55
+ descendants(): Iterable<LinearNode<E>>;
56
+ root(): LinearNode<E>;
57
+ siblings(): Iterable<LinearNode<E>>;
58
+ children(): Iterable<LinearNode<E>>;
59
+ parent(): Optional<LinearNode<E>>;
60
+ [Symbol.iterator](): Iterator<LinearNode<E>>;
61
+ getElement(): Optional<E>;
62
+ setElement(element: E): void;
63
+ getPrevious(): Optional<LinearNode<E>>;
64
+ setPrevious(previous: LinearNode<E>): void;
65
+ setPrevious(previous: Optional<LinearNode<E>>): void;
66
+ getNext(): Optional<LinearNode<E>>;
67
+ setNext(next: LinearNode<E>): void;
68
+ setNext(next: Optional<LinearNode<E>>): void;
69
+ linkPrevious(previous: LinearNode<E>): void;
70
+ unlinkPrevious(): void;
71
+ linkNext(next: LinearNode<E>): void;
72
+ unlinkNext(): void;
73
+ }
74
+ export declare abstract class BinaryNode<E, N extends BinaryNode<E, N>> extends AbstractNode<E, N> {
75
+ BinaryNode: symbol;
76
+ protected ancestor: Optional<N>;
77
+ protected left: Optional<N>;
78
+ protected right: Optional<N>;
79
+ protected element: Optional<E>;
80
+ constructor(element: E);
81
+ constructor(element: E, ancestor: Optional<N>, left: Optional<N>, right: Optional<N>);
82
+ ancestors(): Iterable<N>;
83
+ compare(other: N): bigint;
84
+ compare(other: N, comparator: Comparator<E>): bigint;
85
+ descendants(): Iterable<N>;
86
+ abstract identity(): N;
87
+ root(): N;
88
+ siblings(): Iterable<N>;
89
+ children(): Iterable<N>;
90
+ parent(): Optional<N>;
91
+ [Symbol.iterator](): Iterator<N>;
92
+ preorder(): Generator<N>;
93
+ inorder(): Generator<N>;
94
+ postorder(): Generator<N>;
95
+ breadthfirst(): Generator<N>;
96
+ getElement(): Optional<E>;
97
+ setElement(element: E): void;
98
+ getLeft(): Optional<N>;
99
+ setLeft(left: N): void;
100
+ setLeft(left: Optional<N>): void;
101
+ getRight(): Optional<N>;
102
+ setRight(right: N): void;
103
+ setRight(right: Optional<N>): void;
104
+ getAncestor(): Optional<N>;
105
+ setAncestor(ancestor: N): void;
106
+ setAncestor(ancestor: Optional<N>): void;
107
+ linkLeft(left: N): void;
108
+ unlinkLeft(): Optional<N>;
109
+ linkRight(right: N): void;
110
+ unlinkRight(): Optional<N>;
111
+ isLeftChild(): boolean;
112
+ isRightChild(): boolean;
113
+ detach(): void;
114
+ isBanlanced(): boolean;
115
+ isFull(): boolean;
116
+ isComplete(): boolean;
117
+ isPerfect(): boolean;
118
+ invert(): void;
119
+ invert(deep: boolean): void;
120
+ }
121
+ type Color = "RED" | "BLACK";
122
+ export declare class RedBlackNode<E> extends BinaryNode<E, RedBlackNode<E>> {
123
+ protected readonly RedBlackNode: symbol;
124
+ protected color: Color;
125
+ constructor(element: E);
126
+ constructor(element: E, color: Color);
127
+ constructor(element: E, color: Color, ancestor: Optional<RedBlackNode<E>>, left: Optional<RedBlackNode<E>>, right: Optional<RedBlackNode<E>>);
128
+ identity(): RedBlackNode<E>;
129
+ getElement(): Optional<E>;
130
+ setElement(element: E): void;
131
+ getLeft(): Optional<RedBlackNode<E>>;
132
+ getRight(): Optional<RedBlackNode<E>>;
133
+ getColor(): Color;
134
+ setColor(color: Color): void;
135
+ isRed(): boolean;
136
+ isBlack(): boolean;
137
+ turnRed(): void;
138
+ turnBlack(): void;
139
+ toggle(): void;
140
+ compareAndLink(other: RedBlackNode<E>): void;
141
+ compareAndLink(other: RedBlackNode<E>, comparator: Comparator<E>): void;
142
+ rotate(): void;
143
+ rotateLeft(): RedBlackNode<E>;
144
+ rotateRight(): RedBlackNode<E>;
145
+ fix(): void;
146
+ uncle(): Optional<RedBlackNode<E>>;
147
+ findMinimum(): RedBlackNode<E>;
148
+ findMaximum(): RedBlackNode<E>;
149
+ }
150
+ export declare class AverageLevelNode<E> extends BinaryNode<E, AverageLevelNode<E>> {
151
+ constructor(element: E);
152
+ constructor(element: E, ancestor: Optional<AverageLevelNode<E>>, left: Optional<AverageLevelNode<E>>, right: Optional<AverageLevelNode<E>>);
153
+ [Symbol.iterator](): Iterator<AverageLevelNode<E>>;
154
+ identity(): AverageLevelNode<E>;
155
+ getElement(): Optional<E>;
156
+ setElement(element: E): void;
157
+ getLeft(): Optional<AverageLevelNode<E>>;
158
+ getRight(): Optional<AverageLevelNode<E>>;
159
+ }
160
+ export declare class BinarySearchNode<E> extends AbstractNode<E, BinarySearchNode<E>> {
161
+ protected left: Optional<BinarySearchNode<E>>;
162
+ protected right: Optional<BinarySearchNode<E>>;
163
+ protected element: Optional<E>;
164
+ constructor(element: E);
165
+ constructor(element: E, left: Optional<BinarySearchNode<E>>, right: Optional<BinarySearchNode<E>>);
166
+ ancestors(): Iterable<BinarySearchNode<E>>;
167
+ descendants(): Iterable<BinarySearchNode<E>>;
168
+ root(): BinarySearchNode<E>;
169
+ siblings(): Iterable<BinarySearchNode<E>>;
170
+ children(): Iterable<BinarySearchNode<E>>;
171
+ parent(): Optional<BinarySearchNode<E>>;
172
+ [Symbol.iterator](): Iterator<BinarySearchNode<E>>;
173
+ inorder(): Generator<BinarySearchNode<E>>;
174
+ preorder(): Generator<BinarySearchNode<E>>;
175
+ postorder(): Generator<BinarySearchNode<E>>;
176
+ breadthfirst(): Generator<BinarySearchNode<E>>;
177
+ getElement(): Optional<E>;
178
+ setElement(element: E): void;
179
+ getLeft(): Optional<BinarySearchNode<E>>;
180
+ getRight(): Optional<BinarySearchNode<E>>;
181
+ }
182
+ export {};