t0n 0.1.16 → 0.1.18

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.16",
4
+ "version": "0.1.18",
5
5
  "type": "module",
6
6
  "main": "./src/index.ts",
7
7
  "files": ["src"],
package/src/import.ts CHANGED
@@ -1,5 +1,6 @@
1
- export async function IMPORT(path: string, name: string): Promise<unknown>
2
- export async function IMPORT(path: string, ...name: string[]): Promise<Record<string, unknown>> {
1
+ export async function IMPORT<T extends unknown>(path: string): Promise<T>
2
+ export async function IMPORT<T extends unknown>(path: string, name: string): Promise<T>
3
+ export async function IMPORT<T extends unknown>(path: string, ...name: string[]): Promise<Record<string, T>> {
3
4
  if (process.platform === 'win32' && !path.startsWith('file://'))
4
5
  path = 'file://' + path
5
6
 
package/src/index.ts CHANGED
@@ -5,6 +5,7 @@ export * from './date'
5
5
  export * from './enum'
6
6
  export * from './envir'
7
7
  export * from './import'
8
+ export * from './json'
8
9
  export * from './lenght'
9
10
  export * from './lru-cache'
10
11
  export * from './obj'
package/src/json.ts ADDED
@@ -0,0 +1,25 @@
1
+ const IDENTIFIER_RE = /^[$_\p{ID_Start}][$_\u200C\u200D\p{ID_Continue}]*$/u
2
+
3
+ export function JSJSON(value: unknown): string {
4
+ if (value === null) return 'null'
5
+ if (value === undefined) return 'undefined'
6
+
7
+ const type = typeof value
8
+
9
+ if (type === 'string') return JSON.stringify(value)
10
+ if (type === 'number' || type === 'boolean') return String(value)
11
+ if (type === 'bigint') return `${value}n`
12
+ if (type === 'function') return value.toString()
13
+
14
+ if (Array.isArray(value))
15
+ return `[${value.map(JSJSON).join(',')}]`
16
+
17
+ if (type === 'object') {
18
+ const entries = Object.entries(value as Record<string, unknown>)
19
+ .map(([key, val]) => `${IDENTIFIER_RE.test(key) ? key : JSON.stringify(key)}:${JSJSON(val)}`)
20
+
21
+ return `{${entries.join(',')}}`
22
+ }
23
+
24
+ return 'undefined'
25
+ }