semantic-typescript 0.2.7 → 0.2.9
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/hook.d.ts +2 -0
- package/dist/hook.js +36 -1
- package/dist/optional.d.ts +2 -0
- package/dist/optional.js +11 -0
- package/dist/utility.d.ts +6 -0
- package/package.json +1 -1
package/dist/hook.d.ts
CHANGED
|
@@ -1,2 +1,4 @@
|
|
|
1
|
+
import { type BiConsumer, type DeepPropertyKey, type DeepPropertyValue } from "./utility";
|
|
1
2
|
export declare let useCompare: <T>(t1: T, t2: T) => number;
|
|
2
3
|
export declare let useRandom: <T = number | bigint>(index: T) => T;
|
|
4
|
+
export declare let useTraverse: <T extends object>(t: T, callback: BiConsumer<DeepPropertyKey<T>, DeepPropertyValue<T>>) => void;
|
package/dist/hook.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { isFunction, isNumber, isPrimitive } from "./guard";
|
|
1
|
+
import { isFunction, isIterable, isNumber, isObject, isPrimitive } from "./guard";
|
|
2
|
+
import { validate } from "./utility";
|
|
2
3
|
export let useCompare = (t1, t2) => {
|
|
3
4
|
if (t1 === t2 || Object.is(t1, t2)) {
|
|
4
5
|
return 0;
|
|
@@ -62,3 +63,37 @@ export let useRandom = (index) => {
|
|
|
62
63
|
}
|
|
63
64
|
throw new TypeError("Invalid input type");
|
|
64
65
|
};
|
|
66
|
+
export let useTraverse = (t, callback) => {
|
|
67
|
+
if (isObject(t)) {
|
|
68
|
+
let seen = new WeakSet();
|
|
69
|
+
let traverse = (target) => {
|
|
70
|
+
if (!seen.has(target)) {
|
|
71
|
+
seen.add(target);
|
|
72
|
+
let properties = Reflect.ownKeys(target);
|
|
73
|
+
for (let property of properties) {
|
|
74
|
+
let value = target[property];
|
|
75
|
+
if (validate(value)) {
|
|
76
|
+
if (isObject(value)) {
|
|
77
|
+
if (isIterable(value)) {
|
|
78
|
+
for (let item of value) {
|
|
79
|
+
if (validate(item)) {
|
|
80
|
+
if (isObject(item)) {
|
|
81
|
+
traverse(item);
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
callback(property, item);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
else {
|
|
91
|
+
callback(property, value);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
traverse(t);
|
|
98
|
+
}
|
|
99
|
+
};
|
package/dist/optional.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { Semantic } from "./semantic";
|
|
1
2
|
import { type Consumer, type Functional, type MaybeInvalid, type Predicate, type Runnable } from "./utility";
|
|
2
3
|
export declare class Optional<T> {
|
|
3
4
|
protected value: MaybeInvalid<T>;
|
|
@@ -11,6 +12,7 @@ export declare class Optional<T> {
|
|
|
11
12
|
isEmpty(): boolean;
|
|
12
13
|
isPresent(): boolean;
|
|
13
14
|
map<R>(mapper: Functional<T, R>): Optional<R>;
|
|
15
|
+
semantic(): Semantic<T>;
|
|
14
16
|
static empty<T>(): Optional<T>;
|
|
15
17
|
static of<T>(value: MaybeInvalid<T>): Optional<T>;
|
|
16
18
|
static ofNullable<T>(value?: MaybeInvalid<T>): Optional<T>;
|
package/dist/optional.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { empty, generate } from "./factory";
|
|
1
2
|
import { isFunction } from "./guard";
|
|
2
3
|
import { OptionalSymbol } from "./symbol";
|
|
3
4
|
import { invalidate, validate } from "./utility";
|
|
@@ -46,6 +47,16 @@ export class Optional {
|
|
|
46
47
|
}
|
|
47
48
|
return new Optional(null);
|
|
48
49
|
}
|
|
50
|
+
semantic() {
|
|
51
|
+
if (this.isPresent()) {
|
|
52
|
+
return generate(() => {
|
|
53
|
+
return this.value;
|
|
54
|
+
}, () => {
|
|
55
|
+
return this.isEmpty();
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
return empty();
|
|
59
|
+
}
|
|
49
60
|
static empty() {
|
|
50
61
|
return new Optional(null);
|
|
51
62
|
}
|
package/dist/utility.d.ts
CHANGED
|
@@ -6,6 +6,12 @@ export declare let invalidate: <T>(t: MaybeInvalid<T>) => t is (null | undefined
|
|
|
6
6
|
export type Primitive = string | number | boolean | symbol | bigint | Function | ((...args: any[]) => any);
|
|
7
7
|
export type MaybePrimitive<T> = T | Primitive;
|
|
8
8
|
export type AsyncFunction = (...args: any[]) => Promise<unknown>;
|
|
9
|
+
export type DeepPropertyKey<T extends object> = {
|
|
10
|
+
[K in keyof T]: T[K] extends object ? DeepPropertyKey<T[K]> : K;
|
|
11
|
+
};
|
|
12
|
+
export type DeepPropertyValue<T extends object> = {
|
|
13
|
+
[K in keyof T]: T[K] extends object ? DeepPropertyValue<T[K]> : T[K];
|
|
14
|
+
};
|
|
9
15
|
export interface Runnable {
|
|
10
16
|
(): void;
|
|
11
17
|
}
|
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.2.
|
|
9
|
+
"version": "0.2.9",
|
|
10
10
|
"type": "module",
|
|
11
11
|
"readme": "readme.md",
|
|
12
12
|
"main": "dist/index.js",
|