t0n 0.0.0 → 0.1.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025-present, ZUNQ.com
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,16 @@
1
+ # `t0n`
2
+
3
+ Collection of elegant typescript resources that are used across ZUNQ web applications.
4
+
5
+ ## Install
6
+
7
+ To install the package, run:
8
+
9
+ ```bash
10
+ # bun
11
+ bun i t0n
12
+ ```
13
+
14
+ ## License
15
+
16
+ This package is licensed under the [MIT license](https://github.com/attla/t0n/blob/main/LICENSE) © [Zunq](https://zunq.com)
@@ -0,0 +1,97 @@
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
+ toArray(): [string, T][];
58
+ jsonSerialize(): Record<string, T>;
59
+ toJson(options?: number): string;
60
+ [Symbol.iterator](): IterableIterator<[string, T]>;
61
+ }
62
+
63
+ declare class Envir {
64
+ private static _memory;
65
+ private static memory;
66
+ static has(key: string): boolean;
67
+ static get<T = any>(key: string, defaultValue?: T): T | undefined;
68
+ static set<T = any>(key: string, value: T): void;
69
+ static remove(key: string): void;
70
+ }
71
+
72
+ type BinaryToTextEncoding = 'base64' | 'base64url' | 'hex' | 'binary' | null;
73
+ type Encoding = BinaryToTextEncoding | 'buffer';
74
+ declare function md5(str: string): string;
75
+ declare function md5(str: Buffer): Buffer;
76
+ declare function md5(str: Buffer, encoding: 'buffer'): Buffer;
77
+ declare function md5(str: string, encoding: 'buffer' | null | undefined): Buffer;
78
+ declare function md5(str: string, encoding: Encoding): string;
79
+ declare function sha1(str: string): string;
80
+ declare function sha1(str: Buffer): Buffer;
81
+ declare function sha1(str: Buffer, encoding: 'buffer'): Buffer;
82
+ declare function sha1(str: string, encoding: 'buffer' | null | undefined): Buffer;
83
+ declare function sha1(str: string, encoding: Encoding): string;
84
+ declare function sha256(str: string): string;
85
+ declare function sha256(str: Buffer): Buffer;
86
+ declare function sha256(str: Buffer, encoding: 'buffer'): Buffer;
87
+ declare function sha256(str: string, encoding: 'buffer' | null | undefined): Buffer;
88
+ declare function sha256(str: string, encoding: Encoding): string;
89
+ declare function sha512(str: string): string;
90
+ declare function sha512(str: Buffer): Buffer;
91
+ declare function sha512(str: Buffer, encoding: 'buffer'): Buffer;
92
+ declare function sha512(str: string, encoding: 'buffer' | null | undefined): Buffer;
93
+ declare function sha512(str: string, encoding: Encoding): string;
94
+
95
+ declare function toInt(data: any[] | string | number): number;
96
+
97
+ export { Attempt, type Baggable, Base64, DataBag, Envir, UrlSafeBase64, type VALUE, md5, sha1, sha256, sha512, toInt };
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ import {createHash}from'crypto';var i=class e{static{this.UNDEFINED=Symbol("UNDEFINED");}static resolve(t,r){let n=e.UNDEFINED;try{n=t();}catch{}return new e(n,r)}static defined(t){return !this.undefined(t)&&!this.isUnset(t)}static undefined(t){return t===e.UNDEFINED||this.isUnset(t)}static isUnset(t){return t==null}constructor(t,r){this.value=t,this._default=r;}or(t){return e.defined(this.value)?this:e.resolve(t,this._default)}default(t){return this._default=t,this}tap(t){let r=this.get();return t?.(r),this}get(){return e.undefined(this.value)?this._default:this.value}getOrThrow(t){let r=this.get();if(e.undefined(r))throw t??new Error("Value is null or undefined");return r}};var s=class{static encode(t){let r=typeof t;return t=this.toString(t,r),(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,r){return r??=typeof t,Buffer.isBuffer(t)||r==="string"?t:Array.isArray(t)||["function","symbol"].includes(r)?t.toString():t===null||["number","bigint","boolean","undefined"].includes(r)?String(t):r==="object"&&!(t instanceof Uint8Array)&&!(t instanceof ArrayBuffer)?JSON.stringify(t):t}},f=class{static encode(t){return s.encode(t)?.replace(/=/g,"").replace(/\+/g,"-").replace(/\//g,"_")||""}static decode(t){let r=t.length%4;return r&&(t+="=".repeat(4-r)),s.decode(t.replace(/-/g,"+").replace(/_/g,"/"))}};var o=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,r){return this.has(t)?this.data[t]:r}set(t,r){if(this.has(t)&&typeof r!=typeof this.data[t])throw new TypeError(`Type mismatch for key ${t}. Expected ${typeof this.data[t]}, got ${typeof r}`);this.data[t]=r;}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)}toArray(){return Object.entries(this.data)}jsonSerialize(){return this.all()}toJson(t=0){return JSON.stringify(this.all(),null,t)}[Symbol.iterator](){return Object.entries(this.data)[Symbol.iterator]()}};var a=class{static memory(){return this._memory||(this._memory=new o),this._memory}static has(t){return i.UNDEFINED!==this.get(t,i.UNDEFINED)}static get(t,r){if(this.memory().has(t))return this.memory().get(t,r);let n=process.env[t];return n!==void 0?n:r}static set(t,r){this.memory().set(t,r);}static remove(t){this.memory().remove(t);}};function u(e,t,r){let n=createHash(t);return typeof e=="string"?n.update(e,"utf8"):n.update(e),!r||r==="buffer"?n.digest():n.digest(r)}function T(e,t="hex"){return u(e,"md5",t)}function x(e,t="hex"){return u(e,"sha1",t)}function B(e,t="hex"){return u(e,"sha256",t)}function E(e,t="hex"){return u(e,"sha512",t)}function A(e){if(typeof e=="number"&&Number.isInteger(e))return e;let r=JSON.stringify(e).replace(/\D/g,"");return r?parseInt(r.slice(0,16),10):0}
2
+ export{i as Attempt,s as Base64,o as DataBag,a as Envir,f as UrlSafeBase64,T as md5,x as sha1,B as sha256,E as sha512,A as toInt};
package/package.json CHANGED
@@ -1 +1,53 @@
1
- {"name":"t0n","version":"0.0.0"}
1
+ {
2
+ "name": "t0n",
3
+ "description": "Collection of elegant typescript resources for web artisans",
4
+ "version": "0.1.0",
5
+ "type": "module",
6
+ "main": "dist/index.js",
7
+ "types": "dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "import": "./dist/index.js",
11
+ "types": "./dist/index.d.ts"
12
+ }
13
+ },
14
+ "scripts": {
15
+ "bench": "tsx tests/benchmark/*.bench.ts",
16
+ "build": "tsup src/index.ts --dts --format esm",
17
+ "prepublishOnly": "bun run build"
18
+ },
19
+ "devDependencies": {
20
+ "bun-types": "^1.2.13",
21
+ "tsup": "^8.5.0",
22
+ "tsx": "^4.19.4",
23
+ "typescript": "^5.8.3"
24
+ },
25
+ "engines": {
26
+ "node": ">=18.0.0"
27
+ },
28
+ "files": ["dist"],
29
+ "publishConfig": {
30
+ "registry": "https://registry.npmjs.org"
31
+ },
32
+ "author": "Zunq <open-source@zunq.com>",
33
+ "license": "MIT",
34
+ "homepage": "https://zunq.dev",
35
+ "repository": "git://github.com/attla/t0n",
36
+ "bugs": "https://github.com/attla/t0n/issues",
37
+ "keywords": [
38
+ "t0n",
39
+ "attla",
40
+ "resources",
41
+ "support",
42
+ "bitwise",
43
+ "bit-wise",
44
+ "hash",
45
+ "hashing",
46
+ "aws",
47
+ "lambda",
48
+ "llrt",
49
+ "deno",
50
+ "bun",
51
+ "nodejs"
52
+ ]
53
+ }