t0n 0.1.12 → 0.1.14

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "t0n",
3
3
  "description": "Collection of elegant typescript resources for web artisans",
4
- "version": "0.1.12",
4
+ "version": "0.1.14",
5
5
  "type": "module",
6
6
  "main": "./src/index.ts",
7
7
  "files": ["src"],
@@ -15,7 +15,7 @@
15
15
  "-prepublishOnly": "bun run build"
16
16
  },
17
17
  "dependencies": {
18
- "hono": "^4.11.7"
18
+ "hono": "^4.12.8"
19
19
  },
20
20
  "devDependencies": {
21
21
  "bun-types": "^1.3.8",
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
- return key in this.data
29
- }
59
+ if (!key.includes('.'))
60
+ return key in this.data
30
61
 
31
- add(data: Record<string, T> = {}): void {
32
- this.data = { ...this.data, ...data }
33
- }
62
+ const resolved = walk(this.data, key)
63
+ if (!resolved) return false
34
64
 
35
- replace(data: Record<string, T> = {}): void {
36
- this.data = { ...data }
65
+ const [obj, prop] = resolved
66
+ return prop in obj
37
67
  }
38
68
 
39
69
  get(key: string, defaultValue?: T): T {
40
- return this.has(key) ? this.data[key] : defaultValue as T
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): void {
44
- if (this.has(key) && typeof value != typeof this.data[key])
45
- throw new TypeError(`Type mismatch for key ${key}. Expected ${typeof this.data[key]}, got ${typeof value}`)
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
- this.data[key] = value
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): void {
51
- delete this.data[key]
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
- clear(): void {
55
- this.data = {}
123
+ add(data: Record<string, T> = {}) {
124
+ Object.assign(this.data, data)
56
125
  }
57
126
 
58
- get length(): number {
59
- return this.keys().length
127
+ replace(data: Record<string, T> = {}) {
128
+ this.data = { ...data }
60
129
  }
61
- get size(): number {
62
- return this.length
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.all(), null, options)
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 == 'number') valueToKeyMap.set(value.toString(), key)
33
+ if (typeof value === 'number') valueToKeyMap.set(value.toString(), key)
34
34
  })
35
35
 
36
36
  const GeneratedEnum = class {
package/src/import.ts CHANGED
@@ -1,3 +1,6 @@
1
1
  export async function IMPORT(path: string) {
2
- return import(path +'?v='+ Date.now())
2
+ if (process.platform === 'win32' && !path.startsWith('file://'))
3
+ path = 'file://' + path
4
+
5
+ return import(path + '?v=' + Date.now())
3
6
  }
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 == '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'
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'