t0n 0.1.14 → 0.1.16
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/package.json +1 -1
- package/src/color.ts +2 -0
- package/src/databag.ts +29 -107
- package/src/import.ts +18 -2
- package/src/index.ts +1 -0
- package/src/obj.ts +102 -0
package/package.json
CHANGED
package/src/color.ts
CHANGED
|
@@ -16,6 +16,8 @@
|
|
|
16
16
|
//
|
|
17
17
|
// https://github.com/alexeyraspopov/picocolors/commit/6f0a4638348ed20633d623ee973f9c9a96f65104
|
|
18
18
|
|
|
19
|
+
// TODO: https://github.com/delucis/piccolore/tree/main/src
|
|
20
|
+
|
|
19
21
|
import { getColorEnabledAsync } from 'hono/utils/color'
|
|
20
22
|
|
|
21
23
|
export const enabled = await getColorEnabledAsync()
|
package/src/databag.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import * as Obj from './obj'
|
|
2
|
+
|
|
1
3
|
export interface Baggable<T = any> {
|
|
2
4
|
all(): Record<string, T>
|
|
3
5
|
keys(): string[]
|
|
@@ -10,158 +12,78 @@ export interface Baggable<T = any> {
|
|
|
10
12
|
clear(): void
|
|
11
13
|
values(): T[]
|
|
12
14
|
toArray(): [string, T][]
|
|
13
|
-
toJson(
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
function walk(
|
|
17
|
-
root: Record<string, any>,
|
|
18
|
-
path: string,
|
|
19
|
-
create = false
|
|
20
|
-
): [Record<string, any>, string] | null {
|
|
21
|
-
let obj = root
|
|
22
|
-
let start = 0
|
|
23
|
-
|
|
24
|
-
for (let i = 0; i < path.length; i++) {
|
|
25
|
-
if (path.charCodeAt(i) === 46) { // '.'
|
|
26
|
-
const key = path.slice(start, i)
|
|
27
|
-
let next = obj[key]
|
|
28
|
-
|
|
29
|
-
if (next === undefined) {
|
|
30
|
-
if (!create) return null
|
|
31
|
-
next = obj[key] = {}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
if (typeof next !== 'object' || next === null) {
|
|
35
|
-
if (!create) return null
|
|
36
|
-
next = obj[key] = {}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
obj = next
|
|
40
|
-
start = i + 1
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
return [obj, path.slice(start)]
|
|
15
|
+
toJson(replacer?: (string | number)[] | null, space?: string | number): string
|
|
45
16
|
}
|
|
46
17
|
|
|
47
18
|
export class DataBag<T = any> implements Baggable<T> {
|
|
48
|
-
|
|
19
|
+
#data: Record<string, T>
|
|
49
20
|
|
|
50
21
|
constructor(data: Record<string, T> = {}) {
|
|
51
|
-
this
|
|
22
|
+
this.#data = { ...data }
|
|
52
23
|
}
|
|
53
24
|
|
|
54
25
|
all(): Record<string, T> {
|
|
55
|
-
return { ...this
|
|
26
|
+
return { ...this.#data }
|
|
56
27
|
}
|
|
57
28
|
|
|
58
29
|
has(key: string): boolean {
|
|
59
|
-
|
|
60
|
-
return key in this.data
|
|
61
|
-
|
|
62
|
-
const resolved = walk(this.data, key)
|
|
63
|
-
if (!resolved) return false
|
|
64
|
-
|
|
65
|
-
const [obj, prop] = resolved
|
|
66
|
-
return prop in obj
|
|
30
|
+
return Obj.has(this.#data, key)
|
|
67
31
|
}
|
|
68
32
|
|
|
69
33
|
get(key: string, defaultValue?: T): T {
|
|
70
|
-
|
|
71
|
-
const v = this.data[key]
|
|
72
|
-
return (v !== undefined ? v : defaultValue) as T
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
const resolved = walk(this.data, key)
|
|
76
|
-
if (!resolved) return defaultValue as T
|
|
77
|
-
|
|
78
|
-
const [obj, prop] = resolved
|
|
79
|
-
const value = obj[prop]
|
|
80
|
-
|
|
34
|
+
const value = Obj.get(this.#data, key)
|
|
81
35
|
return (value !== undefined ? value : defaultValue) as T
|
|
82
36
|
}
|
|
83
37
|
|
|
84
38
|
set(key: string, value: T) {
|
|
85
|
-
|
|
86
|
-
const current = this.data[key]
|
|
87
|
-
|
|
88
|
-
if (current !== undefined && typeof current !== typeof value)
|
|
89
|
-
throw new TypeError(
|
|
90
|
-
`Type mismatch for key '${key}'. Expected ${typeof current}, got ${typeof value}`
|
|
91
|
-
)
|
|
92
|
-
|
|
93
|
-
this.data[key] = value
|
|
94
|
-
return
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
const resolved = walk(this.data, key, true)!
|
|
98
|
-
const [obj, prop] = resolved
|
|
99
|
-
|
|
100
|
-
const current = obj[prop]
|
|
101
|
-
|
|
102
|
-
if (current !== undefined && typeof current !== typeof value)
|
|
103
|
-
throw new TypeError(
|
|
104
|
-
`Type mismatch for key '${key}'. Expected ${typeof current}, got ${typeof value}`
|
|
105
|
-
)
|
|
106
|
-
|
|
107
|
-
obj[prop] = value
|
|
39
|
+
Obj.set(this.#data, key, value)
|
|
108
40
|
}
|
|
109
41
|
|
|
110
42
|
remove(key: string) {
|
|
111
|
-
|
|
112
|
-
delete this.data[key]
|
|
113
|
-
return
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
const resolved = walk(this.data, key)
|
|
117
|
-
if (!resolved) return
|
|
118
|
-
|
|
119
|
-
const [obj, prop] = resolved
|
|
120
|
-
delete obj[prop]
|
|
43
|
+
Obj.remove(this.#data, key)
|
|
121
44
|
}
|
|
122
45
|
|
|
123
46
|
add(data: Record<string, T> = {}) {
|
|
124
|
-
Object.assign(this
|
|
47
|
+
Object.assign(this.#data, data)
|
|
125
48
|
}
|
|
126
49
|
|
|
127
50
|
replace(data: Record<string, T> = {}) {
|
|
128
|
-
this
|
|
51
|
+
this.#data = { ...data }
|
|
129
52
|
}
|
|
130
53
|
|
|
131
54
|
clear() {
|
|
132
|
-
this
|
|
55
|
+
this.#data = {}
|
|
133
56
|
}
|
|
134
57
|
|
|
135
|
-
|
|
136
|
-
return Object.keys(this
|
|
58
|
+
get length() {
|
|
59
|
+
return Object.keys(this.#data).length
|
|
137
60
|
}
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
return Object.values(this.data)
|
|
61
|
+
get size() {
|
|
62
|
+
return this.length
|
|
141
63
|
}
|
|
142
64
|
|
|
143
|
-
|
|
144
|
-
return Object.
|
|
65
|
+
keys() {
|
|
66
|
+
return Object.keys(this.#data)
|
|
145
67
|
}
|
|
146
68
|
|
|
147
|
-
|
|
148
|
-
return
|
|
69
|
+
values(): T[] {
|
|
70
|
+
return Object.values(this.#data)
|
|
149
71
|
}
|
|
150
72
|
|
|
151
|
-
|
|
152
|
-
return
|
|
73
|
+
entries() {
|
|
74
|
+
return Object.entries(this.#data)
|
|
153
75
|
}
|
|
154
76
|
|
|
155
|
-
|
|
156
|
-
return
|
|
77
|
+
toArray() {
|
|
78
|
+
return this.entries()
|
|
157
79
|
}
|
|
158
80
|
|
|
159
|
-
|
|
160
|
-
return
|
|
81
|
+
jsonSerialize() {
|
|
82
|
+
return this.all()
|
|
161
83
|
}
|
|
162
84
|
|
|
163
|
-
|
|
164
|
-
return this
|
|
85
|
+
toJson(replacer: (string | number)[] | null | undefined = null, space: string | number = 0) {
|
|
86
|
+
return JSON.stringify(this.#data, replacer, space)
|
|
165
87
|
}
|
|
166
88
|
|
|
167
89
|
[Symbol.iterator](): IterableIterator<[string, T]> {
|
package/src/import.ts
CHANGED
|
@@ -1,6 +1,22 @@
|
|
|
1
|
-
export async function IMPORT(path: string)
|
|
1
|
+
export async function IMPORT(path: string, name: string): Promise<unknown>
|
|
2
|
+
export async function IMPORT(path: string, ...name: string[]): Promise<Record<string, unknown>> {
|
|
2
3
|
if (process.platform === 'win32' && !path.startsWith('file://'))
|
|
3
4
|
path = 'file://' + path
|
|
4
5
|
|
|
5
|
-
|
|
6
|
+
const source = path +'?v='+ Date.now()
|
|
7
|
+
|
|
8
|
+
if (name.length > 0) {
|
|
9
|
+
const module = await import(source)
|
|
10
|
+
const result: Record<string, any> = {}
|
|
11
|
+
for (const n of name) {
|
|
12
|
+
if (n in module)
|
|
13
|
+
result[n] = module[n]
|
|
14
|
+
else
|
|
15
|
+
throw new Error(`Module ${path} does not export ${n}`)
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return name.length === 1 ? result[name[0]] : result
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return import(source)
|
|
6
22
|
}
|
package/src/index.ts
CHANGED
package/src/obj.ts
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
type AnyObject = Record<string, any>
|
|
2
|
+
|
|
3
|
+
const PATH_CACHE: Record<string, string[]> = {}
|
|
4
|
+
let cacheSize = 0
|
|
5
|
+
const CACHE_LIMIT = 1000
|
|
6
|
+
|
|
7
|
+
function parse(key: string): string[] {
|
|
8
|
+
let cached = PATH_CACHE[key]
|
|
9
|
+
if (cached) return cached
|
|
10
|
+
|
|
11
|
+
const segments = key.split('.')
|
|
12
|
+
|
|
13
|
+
if (cacheSize < CACHE_LIMIT) {
|
|
14
|
+
PATH_CACHE[key] = segments
|
|
15
|
+
cacheSize++
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return segments
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function isPath(key: string): boolean {
|
|
22
|
+
return key.indexOf('.') !== -1
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function get<T = unknown>(obj: AnyObject, key: string): T | undefined {
|
|
26
|
+
if (key.indexOf('.') === -1) return obj[key] as T | undefined
|
|
27
|
+
|
|
28
|
+
const segments = parse(key)
|
|
29
|
+
let current: any = obj
|
|
30
|
+
|
|
31
|
+
for (let i = 0, l = segments.length; i < l; i++) {
|
|
32
|
+
if (current == null) return undefined
|
|
33
|
+
current = current[segments[i]]
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return current
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function set(obj: AnyObject, key: string, value: unknown): void {
|
|
40
|
+
if (key.indexOf('.') === -1) {
|
|
41
|
+
obj[key] = value
|
|
42
|
+
return
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const segments = parse(key)
|
|
46
|
+
let current: any = obj
|
|
47
|
+
const last = segments.length - 1
|
|
48
|
+
|
|
49
|
+
for (let i = 0; i < last; i++) {
|
|
50
|
+
const k = segments[i]
|
|
51
|
+
let next = current[k]
|
|
52
|
+
|
|
53
|
+
if (next == null || typeof next !== 'object') {
|
|
54
|
+
next = {}
|
|
55
|
+
current[k] = next
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
current = next
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
current[segments[last]] = value
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function remove(obj: AnyObject, key: string): void {
|
|
65
|
+
if (key.indexOf('.') === -1) {
|
|
66
|
+
delete obj[key]
|
|
67
|
+
return
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const segments = parse(key)
|
|
71
|
+
let current: any = obj
|
|
72
|
+
const last = segments.length - 1
|
|
73
|
+
|
|
74
|
+
for (let i = 0; i < last; i++) {
|
|
75
|
+
if (current == null) return
|
|
76
|
+
current = current[segments[i]]
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (current != null) delete current[segments[last]]
|
|
80
|
+
}
|
|
81
|
+
const hasOwnProperty = Object.prototype.hasOwnProperty
|
|
82
|
+
export function has<T = unknown>(
|
|
83
|
+
obj: AnyObject,
|
|
84
|
+
key: string,
|
|
85
|
+
): obj is AnyObject & Record<string, T> {
|
|
86
|
+
if (key.indexOf('.') === -1)
|
|
87
|
+
return hasOwnProperty.call(obj, key)
|
|
88
|
+
|
|
89
|
+
const segments = parse(key)
|
|
90
|
+
let current: any = obj
|
|
91
|
+
|
|
92
|
+
for (let i = 0, l = segments.length; i < l; i++) {
|
|
93
|
+
if (current == null) return false
|
|
94
|
+
|
|
95
|
+
if (!hasOwnProperty.call(current, segments[i]))
|
|
96
|
+
return false
|
|
97
|
+
|
|
98
|
+
current = current[segments[i]]
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return true
|
|
102
|
+
}
|