t0n 0.1.12 → 0.1.13
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/databag.ts +112 -34
- package/src/enum.ts +1 -1
- package/src/useragent.ts +4 -4
package/package.json
CHANGED
package/src/databag.ts
CHANGED
|
@@ -1,20 +1,51 @@
|
|
|
1
1
|
export interface Baggable<T = any> {
|
|
2
|
-
all(): Record<string, T
|
|
3
|
-
keys(): string[]
|
|
4
|
-
has(key: string): boolean
|
|
5
|
-
add(data: Record<string, T>): void
|
|
6
|
-
replace(data: Record<string, T>): void
|
|
7
|
-
get(key: string, defaultValue?: T): T
|
|
8
|
-
remove(key: string): void
|
|
9
|
-
set(key: string, value: T): void
|
|
10
|
-
clear(): void
|
|
11
|
-
values(): T[]
|
|
12
|
-
toArray(): [string, T][]
|
|
13
|
-
toJson(options?: number): string
|
|
2
|
+
all(): Record<string, T>
|
|
3
|
+
keys(): string[]
|
|
4
|
+
has(key: string): boolean
|
|
5
|
+
add(data: Record<string, T>): void
|
|
6
|
+
replace(data: Record<string, T>): void
|
|
7
|
+
get(key: string, defaultValue?: T): T
|
|
8
|
+
remove(key: string): void
|
|
9
|
+
set(key: string, value: T): void
|
|
10
|
+
clear(): void
|
|
11
|
+
values(): T[]
|
|
12
|
+
toArray(): [string, T][]
|
|
13
|
+
toJson(options?: number): string
|
|
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)]
|
|
14
45
|
}
|
|
15
46
|
|
|
16
47
|
export class DataBag<T = any> implements Baggable<T> {
|
|
17
|
-
protected data: Record<string, T>
|
|
48
|
+
protected data: Record<string, T>
|
|
18
49
|
|
|
19
50
|
constructor(data: Record<string, T> = {}) {
|
|
20
51
|
this.data = { ...data }
|
|
@@ -25,41 +56,80 @@ export class DataBag<T = any> implements Baggable<T> {
|
|
|
25
56
|
}
|
|
26
57
|
|
|
27
58
|
has(key: string): boolean {
|
|
28
|
-
|
|
29
|
-
|
|
59
|
+
if (!key.includes('.'))
|
|
60
|
+
return key in this.data
|
|
30
61
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
}
|
|
62
|
+
const resolved = walk(this.data, key)
|
|
63
|
+
if (!resolved) return false
|
|
34
64
|
|
|
35
|
-
|
|
36
|
-
|
|
65
|
+
const [obj, prop] = resolved
|
|
66
|
+
return prop in obj
|
|
37
67
|
}
|
|
38
68
|
|
|
39
69
|
get(key: string, defaultValue?: T): T {
|
|
40
|
-
|
|
70
|
+
if (!key.includes('.')) {
|
|
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
|
+
|
|
81
|
+
return (value !== undefined ? value : defaultValue) as T
|
|
41
82
|
}
|
|
42
83
|
|
|
43
|
-
set(key: string, value: T)
|
|
44
|
-
if (
|
|
45
|
-
|
|
84
|
+
set(key: string, value: T) {
|
|
85
|
+
if (!key.includes('.')) {
|
|
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]
|
|
46
101
|
|
|
47
|
-
|
|
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
|
|
48
108
|
}
|
|
49
109
|
|
|
50
|
-
remove(key: string)
|
|
51
|
-
|
|
110
|
+
remove(key: string) {
|
|
111
|
+
if (!key.includes('.')) {
|
|
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]
|
|
52
121
|
}
|
|
53
122
|
|
|
54
|
-
|
|
55
|
-
this.data
|
|
123
|
+
add(data: Record<string, T> = {}) {
|
|
124
|
+
Object.assign(this.data, data)
|
|
56
125
|
}
|
|
57
126
|
|
|
58
|
-
|
|
59
|
-
|
|
127
|
+
replace(data: Record<string, T> = {}) {
|
|
128
|
+
this.data = { ...data }
|
|
60
129
|
}
|
|
61
|
-
|
|
62
|
-
|
|
130
|
+
|
|
131
|
+
clear() {
|
|
132
|
+
this.data = {}
|
|
63
133
|
}
|
|
64
134
|
|
|
65
135
|
keys(): string[] {
|
|
@@ -83,7 +153,15 @@ export class DataBag<T = any> implements Baggable<T> {
|
|
|
83
153
|
}
|
|
84
154
|
|
|
85
155
|
toJson(options: number = 0): string {
|
|
86
|
-
return JSON.stringify(this.
|
|
156
|
+
return JSON.stringify(this.data, null, options)
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
get length(): number {
|
|
160
|
+
return Object.keys(this.data).length
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
get size(): number {
|
|
164
|
+
return this.length
|
|
87
165
|
}
|
|
88
166
|
|
|
89
167
|
[Symbol.iterator](): IterableIterator<[string, T]> {
|
package/src/enum.ts
CHANGED
|
@@ -30,7 +30,7 @@ export function Enum<T extends string[] | Record<string, string | number>>(defin
|
|
|
30
30
|
Object.entries(enumObj).forEach(([key, value]) => {
|
|
31
31
|
keyToValueMap.set(key, value)
|
|
32
32
|
valueToKeyMap.set(String(value), key)
|
|
33
|
-
if (typeof value
|
|
33
|
+
if (typeof value === 'number') valueToKeyMap.set(value.toString(), key)
|
|
34
34
|
})
|
|
35
35
|
|
|
36
36
|
const GeneratedEnum = class {
|
package/src/useragent.ts
CHANGED
|
@@ -208,7 +208,7 @@ export const parseUA = (ua?: string | null): IUAResult => {
|
|
|
208
208
|
return result
|
|
209
209
|
}
|
|
210
210
|
|
|
211
|
-
export const isBot = (ua: string) => parseUA(ua).os.type
|
|
212
|
-
export const isMobile = (ua: string) => parseUA(ua).os.type
|
|
213
|
-
export const isTablet = (ua: string) => parseUA(ua).os.type
|
|
214
|
-
export const isDesktop = (ua: string) => parseUA(ua).os.type
|
|
211
|
+
export const isBot = (ua: string) => parseUA(ua).os.type === 'bot'
|
|
212
|
+
export const isMobile = (ua: string) => parseUA(ua).os.type === 'mobile'
|
|
213
|
+
export const isTablet = (ua: string) => parseUA(ua).os.type === 'tablet'
|
|
214
|
+
export const isDesktop = (ua: string) => parseUA(ua).os.type === 'desktop'
|