t0n 0.1.11 → 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 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.11",
4
+ "version": "0.1.13",
5
5
  "type": "module",
6
6
  "main": "./src/index.ts",
7
7
  "files": ["src"],
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/index.ts CHANGED
@@ -6,5 +6,7 @@ export * from './enum'
6
6
  export * from './envir'
7
7
  export * from './import'
8
8
  export * from './lenght'
9
+ export * from './lru-cache'
9
10
  export * from './timestamp'
10
11
  export * from './toint'
12
+ export * from './useragent'
@@ -0,0 +1,111 @@
1
+ class Node<K, V> {
2
+ constructor(
3
+ public key: K,
4
+ public value: V,
5
+ public prev?: Node<K, V>,
6
+ public next?: Node<K, V>
7
+ ) {}
8
+ }
9
+
10
+ export class LRUCache<K, V> {
11
+ #maxSize!: number
12
+ #map = new Map<K, Node<K, V>>()
13
+
14
+ #head?: Node<K, V>
15
+ #tail?: Node<K, V>
16
+
17
+ constructor(maxSize = 100) {
18
+ this.#maxSize = maxSize
19
+ }
20
+
21
+ #moveToFront(node: Node<K, V>) {
22
+ if (node === this.#head) return
23
+
24
+ this.#removeNode(node)
25
+ this.#addFront(node)
26
+ }
27
+
28
+ #addFront(node: Node<K, V>) {
29
+ node.prev = undefined
30
+ node.next = this.#head
31
+
32
+ if (this.#head) this.#head.prev = node
33
+ this.#head = node
34
+
35
+ if (!this.#tail) this.#tail = node
36
+ }
37
+
38
+ #removeNode(node: Node<K, V>) {
39
+ if (node.prev) node.prev.next = node.next
40
+ if (node.next) node.next.prev = node.prev
41
+
42
+ if (node === this.#head) this.#head = node.next
43
+ if (node === this.#tail) this.#tail = node.prev
44
+ }
45
+
46
+ #evict() {
47
+ if (!this.#tail) return
48
+
49
+ const node = this.#tail
50
+ this.#removeNode(node)
51
+ this.#map.delete(node.key)
52
+ }
53
+
54
+ getOrSet(key: K, factory: () => V): V {
55
+ let val = this.get(key)
56
+ if (val !== undefined) return val
57
+
58
+ val = factory()
59
+ this.set(key, val)
60
+ return val
61
+ }
62
+
63
+ get(key: K): V | undefined {
64
+ const node = this.#map.get(key)
65
+ if (!node) return
66
+
67
+ this.#moveToFront(node)
68
+ return node.value
69
+ }
70
+
71
+ peek(key: K): V | undefined {
72
+ return this.#map.get(key)?.value
73
+ }
74
+
75
+ set(key: K, value: V) {
76
+ let node = this.#map.get(key)
77
+
78
+ if (node) {
79
+ node.value = value
80
+ this.#moveToFront(node)
81
+ return
82
+ }
83
+
84
+ node = new Node(key, value)
85
+ this.#map.set(key, node)
86
+ this.#addFront(node)
87
+
88
+ if (this.#map.size > this.#maxSize) {
89
+ this.#evict()
90
+ }
91
+ }
92
+
93
+ delete(key: K) {
94
+ const node = this.#map.get(key)
95
+ if (!node) return false
96
+
97
+ this.#removeNode(node)
98
+ this.#map.delete(key)
99
+ return true
100
+ }
101
+
102
+ clear() {
103
+ this.#map.clear()
104
+ this.#head = undefined
105
+ this.#tail = undefined
106
+ }
107
+
108
+ get size() {
109
+ return this.#map.size
110
+ }
111
+ }
@@ -0,0 +1,214 @@
1
+ import { LRUCache } from './lru-cache'
2
+
3
+ export type OsType = 'desktop' | 'tablet' | 'mobile' | 'tv' | 'bot' | 'unknown'
4
+
5
+ const UNKNOWN = 'unknown'
6
+
7
+ export type IUAResult = {
8
+ browser: {
9
+ name: string,
10
+ version: string,
11
+ }
12
+ os: {
13
+ name: string,
14
+ type: OsType,
15
+ }
16
+ raw?: string | null,
17
+ }
18
+
19
+ type IBrowserRule = {
20
+ name: string,
21
+ pattern: RegExp,
22
+ }
23
+
24
+ type IOsRule = {
25
+ name: string,
26
+ pattern: RegExp,
27
+ type: OsType,
28
+ }
29
+
30
+ const BROWSER_RULES: readonly IBrowserRule[] = [
31
+
32
+ // Bots / Crawlers
33
+ { name: 'Googlebot', pattern: /Googlebot\/([\d.]+)/i },
34
+ { name: 'Googlebot-Image', pattern: /Googlebot-Image/i },
35
+ { name: 'Googlebot-News', pattern: /Googlebot-News/i },
36
+ { name: 'GoogleOther', pattern: /GoogleOther/i },
37
+
38
+ { name: 'Bingbot', pattern: /bingbot\/([\d.]+)/i },
39
+ { name: 'YandexBot', pattern: /YandexBot\/([\d.]+)/i },
40
+ { name: 'DuckDuckBot', pattern: /DuckDuckBot\/([\d.]+)/i },
41
+ { name: 'Baiduspider', pattern: /Baiduspider\/([\d.]+)/i },
42
+ { name: 'Slurp', pattern: /Slurp/i },
43
+
44
+ { name: 'Applebot', pattern: /Applebot\/([\d.]+)/i },
45
+ { name: 'SemrushBot', pattern: /SemrushBot/i },
46
+ { name: 'AhrefsBot', pattern: /AhrefsBot/i },
47
+ { name: 'MJ12bot', pattern: /MJ12bot/i },
48
+ { name: 'DotBot', pattern: /DotBot/i },
49
+ { name: 'PetalBot', pattern: /PetalBot/i },
50
+
51
+ // AI Crawlers
52
+ { name: 'GPTBot', pattern: /GPTBot/i },
53
+ { name: 'ChatGPT-User', pattern: /ChatGPT-User/i },
54
+ { name: 'CCBot', pattern: /CCBot/i },
55
+ { name: 'PerplexityBot', pattern: /PerplexityBot/i },
56
+ { name: 'ClaudeBot', pattern: /ClaudeBot/i },
57
+ { name: 'Amazonbot', pattern: /Amazonbot/i },
58
+
59
+ // In-app browsers
60
+ { name: 'Instagram', pattern: /Instagram/i },
61
+ { name: 'Facebook', pattern: /FBAN|FBAV/i },
62
+ { name: 'TikTok', pattern: /TikTok/i },
63
+ { name: 'LinkedIn', pattern: /LinkedInApp/i },
64
+ { name: 'Discord', pattern: /Discord/i },
65
+ { name: 'Telegram', pattern: /Telegram/i },
66
+ { name: 'Snapchat', pattern: /Snapchat/i },
67
+ { name: 'Line', pattern: /Line\/([\d.]+)/i },
68
+
69
+ // Chromium family
70
+ { name: 'Arc', pattern: /Arc\/([\d.]+)/i },
71
+ { name: 'Arc Search', pattern: /ArcSearch/i },
72
+
73
+ { name: 'Edge', pattern: /Edg\/([\d.]+)/i },
74
+ { name: 'Edge (Legacy)', pattern: /Edge\/([\d.]+)/i },
75
+
76
+ { name: 'Opera', pattern: /OPR\/([\d.]+)/i },
77
+ { name: 'Opera', pattern: /Opera\/([\d.]+)/i },
78
+
79
+ { name: 'Brave', pattern: /Brave\/([\d.]+)/i },
80
+ { name: 'Vivaldi', pattern: /Vivaldi\/([\d.]+)/i },
81
+ { name: 'Thorium', pattern: /Thorium\/([\d.]+)/i },
82
+
83
+ { name: 'YaBrowser', pattern: /YaBrowser\/([\d.]+)/i },
84
+ { name: 'CocCoc', pattern: /coc_coc_browser\/([\d.]+)/i },
85
+
86
+ { name: 'Maxthon', pattern: /Maxthon\/([\d.]+)/i },
87
+ { name: 'Comodo Dragon', pattern: /Dragon\/([\d.]+)/i },
88
+
89
+ { name: 'Whale', pattern: /Whale\/([\d.]+)/i },
90
+ { name: 'Puffin', pattern: /Puffin\/([\d.]+)/i },
91
+
92
+ // Mobile browsers
93
+ { name: 'Samsung Browser', pattern: /SamsungBrowser\/([\d.]+)/i },
94
+ { name: 'Huawei Browser', pattern: /HuaweiBrowser\/([\d.]+)/i },
95
+ { name: 'Miui Browser', pattern: /MiuiBrowser\/([\d.]+)/i },
96
+
97
+ { name: 'UCBrowser', pattern: /UCBrowser\/([\d.]+)/i },
98
+ { name: 'QQBrowser', pattern: /QQBrowser\/([\d.]+)/i },
99
+ { name: 'Sogou', pattern: /SogouMobileBrowser/i },
100
+
101
+ { name: 'DuckDuckGo', pattern: /DuckDuckGo\/([\d.]+)/i },
102
+
103
+ // Firefox family
104
+ { name: 'Firefox', pattern: /Firefox\/([\d.]+)/i },
105
+ { name: 'Waterfox', pattern: /Waterfox\/([\d.]+)/i },
106
+ { name: 'Pale Moon', pattern: /PaleMoon\/([\d.]+)/i },
107
+ { name: 'SeaMonkey', pattern: /SeaMonkey\/([\d.]+)/i },
108
+
109
+ // Chrome family
110
+ { name: 'Chrome', pattern: /CriOS\/([\d.]+)/i },
111
+ { name: 'Chrome', pattern: /Chrome\/([\d.]+)/i },
112
+ { name: 'Chromium', pattern: /Chromium\/([\d.]+)/i },
113
+
114
+ // Safari
115
+ { name: 'Safari', pattern: /Version\/([\d.]+).*Safari/i },
116
+
117
+ // Legacy
118
+ { name: 'MSIE', pattern: /MSIE ([\d.]+)/i },
119
+ { name: 'IE', pattern: /Trident\/.*rv:([\d.]+)/i },
120
+ ]
121
+
122
+ const OS_RULES: readonly IOsRule[] = [
123
+ { name: 'Bot', pattern: /bot|crawl|spider|slurp|crawler/i, type: 'bot' },
124
+
125
+ // TVs
126
+ { name: 'Android TV', pattern: /Android TV/i, type: 'tv' },
127
+ { name: 'Tizen', pattern: /Tizen/i, type: 'tv' },
128
+ { name: 'WebOS', pattern: /webOS/i, type: 'tv' },
129
+ { name: 'SmartTV', pattern: /SmartTV|SMART-TV/i, type: 'tv' },
130
+
131
+ { name: 'PlayStation', pattern: /PlayStation/i, type: 'tv' },
132
+ { name: 'Xbox', pattern: /Xbox/i, type: 'tv' },
133
+ { name: 'Nintendo', pattern: /Nintendo/i, type: 'tv' },
134
+
135
+ // Mobile OS
136
+ { name: 'HarmonyOS', pattern: /HarmonyOS/i, type: 'mobile' },
137
+ { name: 'KaiOS', pattern: /KaiOS/i, type: 'mobile' },
138
+
139
+ { name: 'FireOS', pattern: /KF[A-Z]{2,}/i, type: 'tablet' },
140
+
141
+ { name: 'iPadOS', pattern: /iPad/i, type: 'tablet' },
142
+ { name: 'Android', pattern: /Android(?!.*Mobile)/i, type: 'tablet' },
143
+
144
+ { name: 'iOS', pattern: /iPhone|iPod/i, type: 'mobile' },
145
+ { name: 'Android', pattern: /Android.*Mobile/i, type: 'mobile' },
146
+
147
+ { name: 'Windows Phone', pattern: /Windows Phone|IEMobile/i, type: 'mobile' },
148
+
149
+ { name: 'BlackBerry', pattern: /BlackBerry|BB10/i, type: 'mobile' },
150
+
151
+ // Desktop
152
+ { name: 'Windows', pattern: /Windows NT/i, type: 'desktop' },
153
+ { name: 'macOS', pattern: /Macintosh|Mac OS X/i, type: 'desktop' },
154
+ { name: 'ChromeOS', pattern: /CrOS/i, type: 'desktop' },
155
+
156
+ { name: 'Ubuntu', pattern: /Ubuntu/i, type: 'desktop' },
157
+ { name: 'Debian', pattern: /Debian/i, type: 'desktop' },
158
+ { name: 'Fedora', pattern: /Fedora/i, type: 'desktop' },
159
+ { name: 'Arch', pattern: /Arch Linux/i, type: 'desktop' },
160
+
161
+ { name: 'Linux', pattern: /Linux/i, type: 'desktop' }
162
+ ]
163
+
164
+ const cache = new LRUCache<string, IUAResult>(512)
165
+
166
+ export const parseUA = (ua?: string | null): IUAResult => {
167
+ if (!ua) {
168
+ return {
169
+ browser: { name: UNKNOWN, version: '' },
170
+ os: { name: UNKNOWN, type: UNKNOWN },
171
+ raw: ua,
172
+ }
173
+ }
174
+
175
+ const cached = cache.get(ua)
176
+ if (cached) return cached
177
+
178
+ let browserName = UNKNOWN
179
+ let browserVersion = ''
180
+
181
+ for (const rule of BROWSER_RULES) {
182
+ const match = rule.pattern.exec(ua)
183
+ if (match) {
184
+ browserName = rule.name
185
+ browserVersion = match[1] ?? ''
186
+ break
187
+ }
188
+ }
189
+
190
+ let osName = UNKNOWN
191
+ let osType: OsType = UNKNOWN
192
+
193
+ for (const rule of OS_RULES) {
194
+ if (rule.pattern.test(ua)) {
195
+ osName = rule.name
196
+ osType = rule.type
197
+ break
198
+ }
199
+ }
200
+
201
+ const result: IUAResult = {
202
+ browser: { name: browserName, version: browserVersion },
203
+ os: { name: osName, type: osType },
204
+ raw: ua,
205
+ }
206
+
207
+ cache.set(ua, result)
208
+ return result
209
+ }
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'