t0n 0.1.10 → 0.1.11
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 +12 -11
- package/src/attempt.ts +61 -0
- package/src/base64.ts +57 -0
- package/src/color.ts +102 -0
- package/src/databag.ts +92 -0
- package/src/date.ts +14 -0
- package/src/enum.ts +75 -0
- package/src/envir.ts +45 -0
- package/src/import.ts +3 -0
- package/src/index.ts +10 -0
- package/src/lenght.ts +32 -0
- package/src/timestamp.ts +42 -0
- package/src/toint.ts +10 -0
- package/dist/index.d.ts +0 -109
- package/dist/index.js +0 -2
package/package.json
CHANGED
|
@@ -1,31 +1,32 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "t0n",
|
|
3
3
|
"description": "Collection of elegant typescript resources for web artisans",
|
|
4
|
-
"version": "0.1.
|
|
4
|
+
"version": "0.1.11",
|
|
5
5
|
"type": "module",
|
|
6
|
-
"main": "
|
|
7
|
-
"
|
|
6
|
+
"main": "./src/index.ts",
|
|
7
|
+
"files": ["src"],
|
|
8
8
|
"exports": {
|
|
9
|
-
".":
|
|
10
|
-
|
|
11
|
-
"types": "./dist/index.d.ts"
|
|
12
|
-
}
|
|
9
|
+
".": "./src/index.ts",
|
|
10
|
+
"./color": "./src/color.ts"
|
|
13
11
|
},
|
|
14
12
|
"scripts": {
|
|
15
13
|
"bench": "tsx tests/benchmark/*.bench.ts",
|
|
16
14
|
"build": "tsup src/index.ts --dts --format esm",
|
|
17
|
-
"prepublishOnly": "bun run build"
|
|
15
|
+
"-prepublishOnly": "bun run build"
|
|
16
|
+
},
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"hono": "^4.11.7"
|
|
18
19
|
},
|
|
19
20
|
"devDependencies": {
|
|
20
|
-
"bun-types": "^1.
|
|
21
|
+
"bun-types": "^1.3.8",
|
|
21
22
|
"tsup": "^8.5.0",
|
|
22
23
|
"tsx": "^4.19.4",
|
|
23
|
-
"typescript": "^5.
|
|
24
|
+
"typescript": "^5.9.3"
|
|
24
25
|
},
|
|
26
|
+
"packageManager": "bun@1.3.10",
|
|
25
27
|
"engines": {
|
|
26
28
|
"node": ">=18.0.0"
|
|
27
29
|
},
|
|
28
|
-
"files": ["dist"],
|
|
29
30
|
"publishConfig": {
|
|
30
31
|
"registry": "https://registry.npmjs.org"
|
|
31
32
|
},
|
package/src/attempt.ts
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
export type VALUE<T = any> = T | null | undefined | Symbol
|
|
2
|
+
|
|
3
|
+
export class Attempt<T = any> {
|
|
4
|
+
static readonly UNDEFINED = Symbol('UNDEFINED')
|
|
5
|
+
|
|
6
|
+
static resolve<T>(callback: () => T, defaultValue?: T): Attempt<T> {
|
|
7
|
+
let value: VALUE<T> = Attempt.UNDEFINED
|
|
8
|
+
try { value = callback() } catch (e) {}
|
|
9
|
+
return new Attempt<T>(value, defaultValue)
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
static defined(value: any): boolean {
|
|
13
|
+
return !this.undefined(value) && !this.isUnset(value)
|
|
14
|
+
}
|
|
15
|
+
static undefined(value: any): boolean {
|
|
16
|
+
return value === Attempt.UNDEFINED || this.isUnset(value)
|
|
17
|
+
}
|
|
18
|
+
static isUnset(value: any): boolean {
|
|
19
|
+
return value === null || value === undefined
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
private value: VALUE<T>
|
|
23
|
+
private _default?: T
|
|
24
|
+
|
|
25
|
+
constructor(
|
|
26
|
+
value: VALUE<T>,
|
|
27
|
+
defaultValue?: T
|
|
28
|
+
) {
|
|
29
|
+
this.value = value
|
|
30
|
+
this._default = defaultValue
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
or(callback: () => T): Attempt<T> {
|
|
34
|
+
return Attempt.defined(this.value) ? this : Attempt.resolve(callback, this._default)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
default(defaultValue: T): this {
|
|
38
|
+
this._default = defaultValue
|
|
39
|
+
return this
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
tap(callback?: (value: T | null | undefined) => void): this {
|
|
43
|
+
const value = this.get()
|
|
44
|
+
callback?.(value)
|
|
45
|
+
return this
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
get(): T {
|
|
49
|
+
return Attempt.undefined(this.value)
|
|
50
|
+
? this._default as T
|
|
51
|
+
: this.value as T
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
getOrThrow(error?: Error): T {
|
|
55
|
+
const value = this.get()
|
|
56
|
+
if (Attempt.undefined(value))
|
|
57
|
+
throw error ?? new Error('Value is null or undefined')
|
|
58
|
+
|
|
59
|
+
return value
|
|
60
|
+
}
|
|
61
|
+
}
|
package/src/base64.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
export class Base64 {
|
|
2
|
+
static encode(data: any): string {
|
|
3
|
+
const type = typeof data
|
|
4
|
+
data = this.toString(data, type)
|
|
5
|
+
|
|
6
|
+
const length = data?.byteLength || data.length
|
|
7
|
+
return length < 1 ? '' : Buffer.from(data).toString('base64')
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
static decode(data: string): string {
|
|
11
|
+
if (typeof data !== 'string')
|
|
12
|
+
throw new TypeError('Expected input to be a string')
|
|
13
|
+
|
|
14
|
+
if (!/^[A-Za-z0-9+/]*={0,2}$/.test(data))
|
|
15
|
+
throw new Error('Invalid Base64 string')
|
|
16
|
+
|
|
17
|
+
try {
|
|
18
|
+
return Buffer.from(data, 'base64').toString('utf8')
|
|
19
|
+
} catch (e) {
|
|
20
|
+
throw new Error('Invalid base64 characters')
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// https://www.npmjs.com/package/crypt?activeTab=code
|
|
25
|
+
// https://github.com/puleos/object-hash/blob/master/index.js
|
|
26
|
+
// https://github.com/shuding/stable-hash/blob/main/src/index.ts
|
|
27
|
+
static toString(data: any, type?: string): string | Uint8Array | ArrayBuffer {
|
|
28
|
+
type ??= typeof data
|
|
29
|
+
if (Buffer.isBuffer(data)) return data
|
|
30
|
+
|
|
31
|
+
if (type === 'string') return data
|
|
32
|
+
// if (type === 'string' || Buffer.isBuffer(data)) return data
|
|
33
|
+
|
|
34
|
+
if (Array.isArray(data) || ['function', 'symbol'].includes(type))
|
|
35
|
+
return data.toString()
|
|
36
|
+
else if (data === null || ['number', 'bigint', 'boolean', 'undefined'].includes(type))
|
|
37
|
+
return String(data)
|
|
38
|
+
else if (type === 'object' && !(data instanceof Uint8Array) && !(data instanceof ArrayBuffer))
|
|
39
|
+
return JSON.stringify(data)
|
|
40
|
+
|
|
41
|
+
return data
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export class UrlSafeBase64 {
|
|
46
|
+
static encode(data: any): string {
|
|
47
|
+
return Base64.encode(data)?.replace(/=/g, '').replace(/\+/g, '-').replace(/\//g, '_') || ''
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
static decode(data: string): string {
|
|
51
|
+
const remainder = data.length % 4
|
|
52
|
+
if (remainder)
|
|
53
|
+
data += '='.repeat(4 - remainder)
|
|
54
|
+
|
|
55
|
+
return Base64.decode(data.replace(/-/g, '+').replace(/_/g, '/'))
|
|
56
|
+
}
|
|
57
|
+
}
|
package/src/color.ts
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
// ISC License
|
|
2
|
+
|
|
3
|
+
// Copyright (c) 2021 Alexey Raspopov, Kostiantyn Denysov, Anton Verinov
|
|
4
|
+
|
|
5
|
+
// Permission to use, copy, modify, and/or distribute this software for any
|
|
6
|
+
// purpose with or without fee is hereby granted, provided that the above
|
|
7
|
+
// copyright notice and this permission notice appear in all copies.
|
|
8
|
+
|
|
9
|
+
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
10
|
+
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
11
|
+
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
12
|
+
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
13
|
+
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
14
|
+
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
15
|
+
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
16
|
+
//
|
|
17
|
+
// https://github.com/alexeyraspopov/picocolors/commit/6f0a4638348ed20633d623ee973f9c9a96f65104
|
|
18
|
+
|
|
19
|
+
import { getColorEnabledAsync } from 'hono/utils/color'
|
|
20
|
+
|
|
21
|
+
export const enabled = await getColorEnabledAsync()
|
|
22
|
+
export const isColorSupported = enabled
|
|
23
|
+
|
|
24
|
+
// const { env, stdout } = globalThis?.process ?? {}
|
|
25
|
+
// const enabled =
|
|
26
|
+
// env &&
|
|
27
|
+
// !env.NO_COLOR &&
|
|
28
|
+
// (env.FORCE_COLOR || (stdout?.isTTY && !env.CI && env.TERM !== 'dumb'))
|
|
29
|
+
|
|
30
|
+
const replaceClose = (
|
|
31
|
+
str: string,
|
|
32
|
+
close: string,
|
|
33
|
+
replace: string,
|
|
34
|
+
index: number
|
|
35
|
+
): string => {
|
|
36
|
+
const start = str.substring(0, index) + replace
|
|
37
|
+
const end = str.substring(index + close.length)
|
|
38
|
+
const nextIndex = end.indexOf(close)
|
|
39
|
+
return ~nextIndex
|
|
40
|
+
? start + replaceClose(end, close, replace, nextIndex)
|
|
41
|
+
: start + end
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const formatter = (open: string, close: string, replace = open) => {
|
|
45
|
+
if (!enabled) return String
|
|
46
|
+
return (input: string) => {
|
|
47
|
+
const string = '' + input
|
|
48
|
+
const index = string.indexOf(close, open.length)
|
|
49
|
+
return ~index
|
|
50
|
+
? open + replaceClose(string, close, replace, index) + close
|
|
51
|
+
: open + string + close
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export const reset = enabled ? (s: string) => `\x1b[0m${s}\x1b[0m` : String
|
|
56
|
+
export const bold = formatter('\x1b[1m', '\x1b[22m', '\x1b[22m\x1b[1m')
|
|
57
|
+
export const dim = formatter('\x1b[2m', '\x1b[22m', '\x1b[22m\x1b[2m')
|
|
58
|
+
export const italic = formatter('\x1b[3m', '\x1b[23m')
|
|
59
|
+
export const underline = formatter('\x1b[4m', '\x1b[24m')
|
|
60
|
+
export const inverse = formatter('\x1b[7m', '\x1b[27m')
|
|
61
|
+
export const hidden = formatter('\x1b[8m', '\x1b[28m')
|
|
62
|
+
export const strikethrough = formatter('\x1b[9m', '\x1b[29m')
|
|
63
|
+
|
|
64
|
+
const endText = '\x1b[39m'
|
|
65
|
+
export const black = formatter('\x1b[30m', endText)
|
|
66
|
+
export const red = formatter('\x1b[31m', endText)
|
|
67
|
+
export const green = formatter('\x1b[32m', endText)
|
|
68
|
+
export const yellow = formatter('\x1b[33m', endText)
|
|
69
|
+
export const blue = formatter('\x1b[34m', endText)
|
|
70
|
+
export const magenta = formatter('\x1b[35m', endText)
|
|
71
|
+
export const purple = formatter('\x1b[38;2;173;127;168m', endText)
|
|
72
|
+
export const cyan = formatter('\x1b[36m', endText)
|
|
73
|
+
export const white = formatter('\x1b[37m', endText)
|
|
74
|
+
export const gray = formatter('\x1b[90m', endText)
|
|
75
|
+
|
|
76
|
+
const endBg = '\x1b[49m'
|
|
77
|
+
export const bgBlack = formatter('\x1b[40m', endBg)
|
|
78
|
+
export const bgRed = formatter('\x1b[41m', endBg)
|
|
79
|
+
export const bgGreen = formatter('\x1b[42m', endBg)
|
|
80
|
+
export const bgYellow = formatter('\x1b[43m', endBg)
|
|
81
|
+
export const bgBlue = formatter('\x1b[44m', endBg)
|
|
82
|
+
export const bgMagenta = formatter('\x1b[45m', endBg)
|
|
83
|
+
export const bgCyan = formatter('\x1b[46m', endBg)
|
|
84
|
+
export const bgWhite = formatter('\x1b[47m', endBg)
|
|
85
|
+
|
|
86
|
+
export const blackBright = formatter("\x1b[90m", endText)
|
|
87
|
+
export const redBright = formatter("\x1b[91m", endText)
|
|
88
|
+
export const greenBright = formatter("\x1b[92m", endText)
|
|
89
|
+
export const yellowBright = formatter("\x1b[93m", endText)
|
|
90
|
+
export const blueBright = formatter("\x1b[94m", endText)
|
|
91
|
+
export const magentaBright = formatter("\x1b[95m", endText)
|
|
92
|
+
export const cyanBright = formatter("\x1b[96m", endText)
|
|
93
|
+
export const whiteBright = formatter("\x1b[97m", endText)
|
|
94
|
+
|
|
95
|
+
export const bgBlackBright = formatter("\x1b[100m", endBg)
|
|
96
|
+
export const bgRedBright = formatter("\x1b[101m", endBg)
|
|
97
|
+
export const bgGreenBright = formatter("\x1b[102m", endBg)
|
|
98
|
+
export const bgYellowBright = formatter("\x1b[103m", endBg)
|
|
99
|
+
export const bgBlueBright = formatter("\x1b[104m", endBg)
|
|
100
|
+
export const bgMagentaBright = formatter("\x1b[105m", endBg)
|
|
101
|
+
export const bgCyanBright = formatter("\x1b[106m", endBg)
|
|
102
|
+
export const bgWhiteBright = formatter("\x1b[107m", endBg)
|
package/src/databag.ts
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
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,
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export class DataBag<T = any> implements Baggable<T> {
|
|
17
|
+
protected data: Record<string, T> = {}
|
|
18
|
+
|
|
19
|
+
constructor(data: Record<string, T> = {}) {
|
|
20
|
+
this.data = { ...data }
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
all(): Record<string, T> {
|
|
24
|
+
return { ...this.data }
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
has(key: string): boolean {
|
|
28
|
+
return key in this.data
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
add(data: Record<string, T> = {}): void {
|
|
32
|
+
this.data = { ...this.data, ...data }
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
replace(data: Record<string, T> = {}): void {
|
|
36
|
+
this.data = { ...data }
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
get(key: string, defaultValue?: T): T {
|
|
40
|
+
return this.has(key) ? this.data[key] : defaultValue as T
|
|
41
|
+
}
|
|
42
|
+
|
|
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}`)
|
|
46
|
+
|
|
47
|
+
this.data[key] = value
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
remove(key: string): void {
|
|
51
|
+
delete this.data[key]
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
clear(): void {
|
|
55
|
+
this.data = {}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
get length(): number {
|
|
59
|
+
return this.keys().length
|
|
60
|
+
}
|
|
61
|
+
get size(): number {
|
|
62
|
+
return this.length
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
keys(): string[] {
|
|
66
|
+
return Object.keys(this.data)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
values(): T[] {
|
|
70
|
+
return Object.values(this.data)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
entries(): [string, T][] {
|
|
74
|
+
return Object.entries(this.data)
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
toArray(): [string, T][] {
|
|
78
|
+
return this.entries()
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
jsonSerialize(): Record<string, T> {
|
|
82
|
+
return this.all()
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
toJson(options: number = 0): string {
|
|
86
|
+
return JSON.stringify(this.all(), null, options)
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
[Symbol.iterator](): IterableIterator<[string, T]> {
|
|
90
|
+
return this.entries()[Symbol.iterator]()
|
|
91
|
+
}
|
|
92
|
+
}
|
package/src/date.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
const getFullYear = Date.prototype.getFullYear
|
|
2
|
+
const getMonth = Date.prototype.getMonth
|
|
3
|
+
const getDate = Date.prototype.getDate
|
|
4
|
+
const getHours = Date.prototype.getHours
|
|
5
|
+
const getMinutes = Date.prototype.getMinutes
|
|
6
|
+
const getSeconds = Date.prototype.getSeconds
|
|
7
|
+
const pad = (n: number) => n > 9 ? n : '0' + n
|
|
8
|
+
|
|
9
|
+
export class Datte {
|
|
10
|
+
static dateTime(date: Date = new Date()) {
|
|
11
|
+
return getFullYear.call(date) + '-' + pad(getMonth.call(date) + 1) + '-' + pad(getDate.call(date))
|
|
12
|
+
+ ' ' + pad(getHours.call(date)) + ':' + pad(getMinutes.call(date)) + ':' + pad(getSeconds.call(date))
|
|
13
|
+
}
|
|
14
|
+
}
|
package/src/enum.ts
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
export type EnumType<T> = T extends string[]
|
|
2
|
+
? { [K in T[number]]: K }
|
|
3
|
+
: T extends Record<string, string | number>
|
|
4
|
+
? T
|
|
5
|
+
: never
|
|
6
|
+
|
|
7
|
+
export type EnumValue<T> = T extends string[]
|
|
8
|
+
? T[number]
|
|
9
|
+
: T extends Record<string, infer V>
|
|
10
|
+
? V
|
|
11
|
+
: never
|
|
12
|
+
|
|
13
|
+
export type EnumStatic<T> = {
|
|
14
|
+
has(search: string | number): boolean
|
|
15
|
+
key(search: string | number): string | null
|
|
16
|
+
value(search: string | number): EnumValue<T> | null
|
|
17
|
+
readonly keys: string[]
|
|
18
|
+
readonly values: Array<EnumValue<T>>
|
|
19
|
+
toObject(): EnumType<T>
|
|
20
|
+
} & EnumType<T>
|
|
21
|
+
|
|
22
|
+
export function Enum<T extends string[] | Record<string, string | number>>(definition: T): EnumStatic<T> {
|
|
23
|
+
const enumObj = Array.isArray(definition)
|
|
24
|
+
? Object.fromEntries(definition.map(key => [key, key])) as EnumType<T>
|
|
25
|
+
: definition as EnumType<T>
|
|
26
|
+
|
|
27
|
+
const keyToValueMap = new Map<string | number, string | number>()
|
|
28
|
+
const valueToKeyMap = new Map<string | number, string>()
|
|
29
|
+
|
|
30
|
+
Object.entries(enumObj).forEach(([key, value]) => {
|
|
31
|
+
keyToValueMap.set(key, value)
|
|
32
|
+
valueToKeyMap.set(String(value), key)
|
|
33
|
+
if (typeof value == 'number') valueToKeyMap.set(value.toString(), key)
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
const GeneratedEnum = class {
|
|
37
|
+
static has(search: string | number): boolean {
|
|
38
|
+
return keyToValueMap.has(search as string) || valueToKeyMap.has(String(search))
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
static key(search: string | number): string | null {
|
|
42
|
+
if (keyToValueMap.has(search as string))
|
|
43
|
+
return search as string
|
|
44
|
+
|
|
45
|
+
return valueToKeyMap.get(String(search)) || null
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
static value(search: string | number): EnumValue<T> | null {
|
|
49
|
+
if (keyToValueMap.has(search as string))
|
|
50
|
+
return keyToValueMap.get(search as string) as EnumValue<T>
|
|
51
|
+
|
|
52
|
+
return valueToKeyMap.has(String(search))
|
|
53
|
+
? keyToValueMap.get(valueToKeyMap.get(String(search))!) as EnumValue<T>
|
|
54
|
+
: null
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
static get keys(): string[] {
|
|
58
|
+
return Object.keys(enumObj)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
static get values(): Array<EnumValue<T>> {
|
|
62
|
+
return Object.values(enumObj) as Array<EnumValue<T>>
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
static toObject(): EnumType<T> {
|
|
66
|
+
return { ...enumObj }
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
Object.entries(enumObj).forEach(([key, value]) => {
|
|
71
|
+
(GeneratedEnum as any)[key] = value
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
return GeneratedEnum as unknown as EnumStatic<T>
|
|
75
|
+
}
|
package/src/envir.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { DataBag } from './databag'
|
|
2
|
+
|
|
3
|
+
export class Envir {
|
|
4
|
+
static #memory: DataBag
|
|
5
|
+
|
|
6
|
+
static {
|
|
7
|
+
this.#memory = new DataBag()
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
static has(key: string): boolean {
|
|
11
|
+
try {
|
|
12
|
+
return this.#memory.has(key) || key in process.env
|
|
13
|
+
} catch {
|
|
14
|
+
return false
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
static get<T = any>(key: string, defaultValue?: T): T | undefined {
|
|
19
|
+
if (this.#memory.has(key))
|
|
20
|
+
return this.#memory.get(key, defaultValue) as T
|
|
21
|
+
|
|
22
|
+
try {
|
|
23
|
+
const envValue = process?.env[key]
|
|
24
|
+
return envValue !== undefined ? envValue as T : defaultValue
|
|
25
|
+
} catch {
|
|
26
|
+
return defaultValue
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
static set<T = any>(key: string, value: T): void {
|
|
31
|
+
this.#memory.set(key, value)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
static remove(key: string): void {
|
|
35
|
+
this.#memory.remove(key)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
static add<T = any>(data: Record<string, T> = {}): void {
|
|
39
|
+
this.#memory.add(data)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
static replace<T = any>(data: Record<string, T> = {}): void {
|
|
43
|
+
this.#memory.replace(data)
|
|
44
|
+
}
|
|
45
|
+
}
|
package/src/import.ts
ADDED
package/src/index.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export * from './attempt'
|
|
2
|
+
export * from './base64'
|
|
3
|
+
export * from './databag'
|
|
4
|
+
export * from './date'
|
|
5
|
+
export * from './enum'
|
|
6
|
+
export * from './envir'
|
|
7
|
+
export * from './import'
|
|
8
|
+
export * from './lenght'
|
|
9
|
+
export * from './timestamp'
|
|
10
|
+
export * from './toint'
|
package/src/lenght.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
export function getLength(item: any, type?: string): number {
|
|
2
|
+
if (!type) type = typeof item
|
|
3
|
+
|
|
4
|
+
switch (type) {
|
|
5
|
+
case 'string':
|
|
6
|
+
return item.length
|
|
7
|
+
|
|
8
|
+
case 'number':
|
|
9
|
+
case 'bigint':
|
|
10
|
+
// case 'function':
|
|
11
|
+
return item.toString().length
|
|
12
|
+
|
|
13
|
+
// case 'boolean':
|
|
14
|
+
// return item ? 1 : 0
|
|
15
|
+
|
|
16
|
+
// case 'symbol':
|
|
17
|
+
// return item.toString().length - 8
|
|
18
|
+
|
|
19
|
+
case 'object':
|
|
20
|
+
if (item === null)
|
|
21
|
+
return 0
|
|
22
|
+
|
|
23
|
+
if (Array.isArray(item))
|
|
24
|
+
return item.length
|
|
25
|
+
|
|
26
|
+
return Object.keys(item).length
|
|
27
|
+
|
|
28
|
+
case 'undefined':
|
|
29
|
+
default:
|
|
30
|
+
return 0
|
|
31
|
+
}
|
|
32
|
+
}
|
package/src/timestamp.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
export class Timestamp {
|
|
2
|
+
static timestamp(date: number | Date | null = null): number {
|
|
3
|
+
if (date === null || date === undefined) {
|
|
4
|
+
return 0
|
|
5
|
+
} else if (typeof date === 'number') {
|
|
6
|
+
return date
|
|
7
|
+
} else if (date instanceof Date) {
|
|
8
|
+
return Math.floor(date.getTime() / 1000)
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
return 0
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
static minuteToSecond(date: number | Date | null = null): number {
|
|
15
|
+
return Timestamp.timestamp(date) || Math.floor(Date.now() / 1000)
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
static strToDate(str: string): Date {
|
|
19
|
+
return new Date(str)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
static now(): number {
|
|
23
|
+
return Date.now() / 1000 | 0 // limited up to 32 bits - until 2038
|
|
24
|
+
// return Math.floor(Date.now() / 1000)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
static addSeconds(timestamp: number, seconds: number): number {
|
|
28
|
+
return timestamp + seconds
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
static addMinutes(timestamp: number, minutes: number): number {
|
|
32
|
+
return timestamp + (minutes * 60)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
static addHours(timestamp: number, hours: number): number {
|
|
36
|
+
return timestamp + (hours * 3600)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
static addDays(timestamp: number, days: number): number {
|
|
40
|
+
return timestamp + (days * 86400)
|
|
41
|
+
}
|
|
42
|
+
}
|
package/src/toint.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export function toInt(data: any[] | string | number): number {
|
|
2
|
+
if (typeof data === 'number' && Number.isInteger(data))
|
|
3
|
+
return data
|
|
4
|
+
|
|
5
|
+
const jsonString = JSON.stringify(data)
|
|
6
|
+
// Simplified implementation without MD5 (less accurate than the original)
|
|
7
|
+
// TODO: add o md5 and stable-hash
|
|
8
|
+
const numericString = jsonString.replace(/\D/g, '')
|
|
9
|
+
return numericString ? parseInt(numericString.slice(0, 16), 10) : 0
|
|
10
|
+
}
|
package/dist/index.d.ts
DELETED
|
@@ -1,109 +0,0 @@
|
|
|
1
|
-
type VALUE<T = any> = T | null | undefined | Symbol;
|
|
2
|
-
declare class Attempt<T = any> {
|
|
3
|
-
static readonly UNDEFINED: unique symbol;
|
|
4
|
-
static resolve<T>(callback: () => T, defaultValue?: T): Attempt<T>;
|
|
5
|
-
static defined(value: any): boolean;
|
|
6
|
-
static undefined(value: any): boolean;
|
|
7
|
-
static isUnset(value: any): boolean;
|
|
8
|
-
private value;
|
|
9
|
-
private _default?;
|
|
10
|
-
constructor(value: VALUE<T>, defaultValue?: T);
|
|
11
|
-
or(callback: () => T): Attempt<T>;
|
|
12
|
-
default(defaultValue: T): this;
|
|
13
|
-
tap(callback?: (value: T | null | undefined) => void): this;
|
|
14
|
-
get(): T;
|
|
15
|
-
getOrThrow(error?: Error): T;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
declare class Base64 {
|
|
19
|
-
static encode(data: any): string;
|
|
20
|
-
static decode(data: string): string;
|
|
21
|
-
static toString(data: any, type?: string): string | Uint8Array | ArrayBuffer;
|
|
22
|
-
}
|
|
23
|
-
declare class UrlSafeBase64 {
|
|
24
|
-
static encode(data: any): string;
|
|
25
|
-
static decode(data: string): string;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
interface Baggable<T = any> {
|
|
29
|
-
all(): Record<string, T>;
|
|
30
|
-
keys(): string[];
|
|
31
|
-
has(key: string): boolean;
|
|
32
|
-
add(data: Record<string, T>): void;
|
|
33
|
-
replace(data: Record<string, T>): void;
|
|
34
|
-
get(key: string, defaultValue?: T): T;
|
|
35
|
-
remove(key: string): void;
|
|
36
|
-
set(key: string, value: T): void;
|
|
37
|
-
clear(): void;
|
|
38
|
-
values(): T[];
|
|
39
|
-
toArray(): [string, T][];
|
|
40
|
-
toJson(options?: number): string;
|
|
41
|
-
}
|
|
42
|
-
declare class DataBag<T = any> implements Baggable<T> {
|
|
43
|
-
protected data: Record<string, T>;
|
|
44
|
-
constructor(data?: Record<string, T>);
|
|
45
|
-
all(): Record<string, T>;
|
|
46
|
-
has(key: string): boolean;
|
|
47
|
-
add(data?: Record<string, T>): void;
|
|
48
|
-
replace(data?: Record<string, T>): void;
|
|
49
|
-
get(key: string, defaultValue?: T): T;
|
|
50
|
-
set(key: string, value: T): void;
|
|
51
|
-
remove(key: string): void;
|
|
52
|
-
clear(): void;
|
|
53
|
-
get length(): number;
|
|
54
|
-
get size(): number;
|
|
55
|
-
keys(): string[];
|
|
56
|
-
values(): T[];
|
|
57
|
-
entries(): [string, T][];
|
|
58
|
-
toArray(): [string, T][];
|
|
59
|
-
jsonSerialize(): Record<string, T>;
|
|
60
|
-
toJson(options?: number): string;
|
|
61
|
-
[Symbol.iterator](): IterableIterator<[string, T]>;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
declare class Datte {
|
|
65
|
-
static dateTime(date?: Date): string;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
type EnumType<T> = T extends string[] ? {
|
|
69
|
-
[K in T[number]]: K;
|
|
70
|
-
} : T extends Record<string, string | number> ? T : never;
|
|
71
|
-
type EnumValue<T> = T extends string[] ? T[number] : T extends Record<string, infer V> ? V : never;
|
|
72
|
-
type EnumStatic<T> = {
|
|
73
|
-
has(search: string | number): boolean;
|
|
74
|
-
key(search: string | number): string | null;
|
|
75
|
-
value(search: string | number): EnumValue<T> | null;
|
|
76
|
-
readonly keys: string[];
|
|
77
|
-
readonly values: Array<EnumValue<T>>;
|
|
78
|
-
toObject(): EnumType<T>;
|
|
79
|
-
} & EnumType<T>;
|
|
80
|
-
declare function Enum<T extends string[] | Record<string, string | number>>(definition: T): EnumStatic<T>;
|
|
81
|
-
|
|
82
|
-
declare class Envir {
|
|
83
|
-
#private;
|
|
84
|
-
static has(key: string): boolean;
|
|
85
|
-
static get<T = any>(key: string, defaultValue?: T): T | undefined;
|
|
86
|
-
static set<T = any>(key: string, value: T): void;
|
|
87
|
-
static remove(key: string): void;
|
|
88
|
-
static add<T = any>(data?: Record<string, T>): void;
|
|
89
|
-
static replace<T = any>(data?: Record<string, T>): void;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
declare function IMPORT(path: string): Promise<any>;
|
|
93
|
-
|
|
94
|
-
declare function getLength(item: any, type?: string): number;
|
|
95
|
-
|
|
96
|
-
declare class Timestamp {
|
|
97
|
-
static timestamp(date?: number | Date | null): number;
|
|
98
|
-
static minuteToSecond(date?: number | Date | null): number;
|
|
99
|
-
static strToDate(str: string): Date;
|
|
100
|
-
static now(): number;
|
|
101
|
-
static addSeconds(timestamp: number, seconds: number): number;
|
|
102
|
-
static addMinutes(timestamp: number, minutes: number): number;
|
|
103
|
-
static addHours(timestamp: number, hours: number): number;
|
|
104
|
-
static addDays(timestamp: number, days: number): number;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
declare function toInt(data: any[] | string | number): number;
|
|
108
|
-
|
|
109
|
-
export { Attempt, type Baggable, Base64, DataBag, Datte, Enum, type EnumStatic, type EnumType, type EnumValue, Envir, IMPORT, Timestamp, UrlSafeBase64, type VALUE, getLength, toInt };
|
package/dist/index.js
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
var l=class r{static{this.UNDEFINED=Symbol("UNDEFINED");}static resolve(t,e){let s=r.UNDEFINED;try{s=t();}catch{}return new r(s,e)}static defined(t){return !this.undefined(t)&&!this.isUnset(t)}static undefined(t){return t===r.UNDEFINED||this.isUnset(t)}static isUnset(t){return t==null}constructor(t,e){this.value=t,this._default=e;}or(t){return r.defined(this.value)?this:r.resolve(t,this._default)}default(t){return this._default=t,this}tap(t){let e=this.get();return t?.(e),this}get(){return r.undefined(this.value)?this._default:this.value}getOrThrow(t){let e=this.get();if(r.undefined(e))throw t??new Error("Value is null or undefined");return e}};var a=class{static encode(t){let e=typeof t;return t=this.toString(t,e),(t?.byteLength||t.length)<1?"":Buffer.from(t).toString("base64")}static decode(t){if(typeof t!="string")throw new TypeError("Expected input to be a string");if(!/^[A-Za-z0-9+/]*={0,2}$/.test(t))throw new Error("Invalid Base64 string");try{return Buffer.from(t,"base64").toString("utf8")}catch{throw new Error("Invalid base64 characters")}}static toString(t,e){return e??=typeof t,Buffer.isBuffer(t)||e==="string"?t:Array.isArray(t)||["function","symbol"].includes(e)?t.toString():t===null||["number","bigint","boolean","undefined"].includes(e)?String(t):e==="object"&&!(t instanceof Uint8Array)&&!(t instanceof ArrayBuffer)?JSON.stringify(t):t}},g=class{static encode(t){return a.encode(t)?.replace(/=/g,"").replace(/\+/g,"-").replace(/\//g,"_")||""}static decode(t){let e=t.length%4;return e&&(t+="=".repeat(4-e)),a.decode(t.replace(/-/g,"+").replace(/_/g,"/"))}};var u=class{constructor(t={}){this.data={};this.data={...t};}all(){return {...this.data}}has(t){return t in this.data}add(t={}){this.data={...this.data,...t};}replace(t={}){this.data={...t};}get(t,e){return this.has(t)?this.data[t]:e}set(t,e){if(this.has(t)&&typeof e!=typeof this.data[t])throw new TypeError(`Type mismatch for key ${t}. Expected ${typeof this.data[t]}, got ${typeof e}`);this.data[t]=e;}remove(t){delete this.data[t];}clear(){this.data={};}get length(){return this.keys().length}get size(){return this.length}keys(){return Object.keys(this.data)}values(){return Object.values(this.data)}entries(){return Object.entries(this.data)}toArray(){return this.entries()}jsonSerialize(){return this.all()}toJson(t=0){return JSON.stringify(this.all(),null,t)}[Symbol.iterator](){return this.entries()[Symbol.iterator]()}};var f=Date.prototype.getFullYear,h=Date.prototype.getMonth,p=Date.prototype.getDate,b=Date.prototype.getHours,y=Date.prototype.getMinutes,v=Date.prototype.getSeconds,o=r=>r>9?r:"0"+r,T=class{static dateTime(t=new Date){return f.call(t)+"-"+o(h.call(t)+1)+"-"+o(p.call(t))+" "+o(b.call(t))+":"+o(y.call(t))+":"+o(v.call(t))}};function w(r){let t=Array.isArray(r)?Object.fromEntries(r.map(n=>[n,n])):r,e=new Map,s=new Map;Object.entries(t).forEach(([n,i])=>{e.set(n,i),s.set(String(i),n),typeof i=="number"&&s.set(i.toString(),n);});let c=class{static has(n){return e.has(n)||s.has(String(n))}static key(n){return e.has(n)?n:s.get(String(n))||null}static value(n){return e.has(n)?e.get(n):s.has(String(n))?e.get(s.get(String(n))):null}static get keys(){return Object.keys(t)}static get values(){return Object.values(t)}static toObject(){return {...t}}};return Object.entries(t).forEach(([n,i])=>{c[n]=i;}),c}var m=class{static#t;static{this.#t=new u;}static has(t){try{return this.#t.has(t)||t in process.env}catch{return false}}static get(t,e){if(this.#t.has(t))return this.#t.get(t,e);try{let s=process?.env[t];return s!==void 0?s:e}catch{return e}}static set(t,e){this.#t.set(t,e);}static remove(t){this.#t.remove(t);}static add(t={}){this.#t.add(t);}static replace(t={}){this.#t.replace(t);}};async function R(r){return import(r+"?v="+Date.now())}function M(r,t){switch(t||(t=typeof r),t){case "string":return r.length;case "number":case "bigint":return r.toString().length;case "object":return r===null?0:Array.isArray(r)?r.length:Object.keys(r).length;case "undefined":default:return 0}}var d=class r{static timestamp(t=null){return t==null?0:typeof t=="number"?t:t instanceof Date?Math.floor(t.getTime()/1e3):0}static minuteToSecond(t=null){return r.timestamp(t)||Math.floor(Date.now()/1e3)}static strToDate(t){return new Date(t)}static now(){return Date.now()/1e3|0}static addSeconds(t,e){return t+e}static addMinutes(t,e){return t+e*60}static addHours(t,e){return t+e*3600}static addDays(t,e){return t+e*86400}};function B(r){if(typeof r=="number"&&Number.isInteger(r))return r;let e=JSON.stringify(r).replace(/\D/g,"");return e?parseInt(e.slice(0,16),10):0}
|
|
2
|
-
export{l as Attempt,a as Base64,u as DataBag,T as Datte,w as Enum,m as Envir,R as IMPORT,d as Timestamp,g as UrlSafeBase64,M as getLength,B as toInt};
|