pythonlib 1.0.2 → 2.0.1
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/README.md +123 -49
- package/dist/base64-C_am75Sv.d.ts +165 -0
- package/dist/base64.d.ts +1 -0
- package/dist/base64.js +37 -0
- package/dist/chunk-2QEMKE7U.js +252 -0
- package/dist/chunk-2YTO24B5.js +78 -0
- package/dist/chunk-3HQSBCEC.js +348 -0
- package/dist/{chunk-7BZB2TQF.js → chunk-3M3PB4RO.js} +4 -6
- package/dist/chunk-5OKGPZBQ.js +1106 -0
- package/dist/chunk-B5LQJODJ.js +37 -0
- package/dist/{chunk-CUP6EPDB.js → chunk-BCMGGBWG.js} +2 -5
- package/dist/chunk-BCVIH2BN.js +175 -0
- package/dist/chunk-CANUXHVB.js +349 -0
- package/dist/{chunk-QURVRHY2.js → chunk-D35YI363.js} +15 -12
- package/dist/{chunk-RSBRI27Z.js → chunk-ETBW6XNR.js} +3 -4
- package/dist/chunk-HSTC277I.js +255 -0
- package/dist/chunk-HTCQ4OO5.js +238 -0
- package/dist/chunk-IANXD4D4.js +250 -0
- package/dist/{chunk-G6PUQTVZ.js → chunk-JAUU3HMH.js} +1 -2
- package/dist/chunk-JVTQM7CI.js +195 -0
- package/dist/chunk-KKJHGY4C.js +551 -0
- package/dist/chunk-LHPQS75Z.js +192 -0
- package/dist/{chunk-OSX7QZAW.js → chunk-LK2L2TFG.js} +5 -6
- package/dist/{chunk-CEZSBJJV.js → chunk-LWYBTWBZ.js} +2 -3
- package/dist/chunk-MEEU4SH5.js +362 -0
- package/dist/{chunk-TAYRRHJ3.js → chunk-MFKIEN7N.js} +1 -2
- package/dist/{chunk-PZ5AY32C.js → chunk-MLKGABMK.js} +0 -1
- package/dist/chunk-OZRS5PC4.js +312 -0
- package/dist/chunk-PVK4F3ME.js +194 -0
- package/dist/{chunk-56DGFWIJ.js → chunk-QKJBQKPY.js} +1 -2
- package/dist/chunk-RB6BYCIQ.js +141 -0
- package/dist/chunk-RRSOZXZE.js +155 -0
- package/dist/chunk-THMJVAK6.js +282 -0
- package/dist/chunk-UGZ5OY5Z.js +165 -0
- package/dist/chunk-WTZSAITC.js +82 -0
- package/dist/chunk-YONWJHJU.js +265 -0
- package/dist/collections.js +2 -3
- package/dist/copy-6ZaZ__ge.d.ts +59 -0
- package/dist/copy.d.ts +1 -0
- package/dist/copy.js +9 -0
- package/dist/datetime.js +2 -3
- package/dist/functools.js +2 -3
- package/dist/glob.browser-B6d_YdV5.d.ts +50 -0
- package/dist/glob.browser.d.ts +1 -0
- package/dist/glob.browser.js +15 -0
- package/dist/glob.node-wn5ehtyM.d.ts +80 -0
- package/dist/glob.node.d.ts +1 -0
- package/dist/glob.node.js +15 -0
- package/dist/hashlib-vKP511NY.d.ts +205 -0
- package/dist/hashlib.d.ts +1 -0
- package/dist/hashlib.js +39 -0
- package/dist/index.browser.d.ts +172 -0
- package/dist/index.browser.js +305 -0
- package/dist/index.d.ts +36 -602
- package/dist/index.js +138 -1073
- package/dist/itertools.js +2 -3
- package/dist/json.js +2 -3
- package/dist/logging.browser-Dux4L5kz.d.ts +185 -0
- package/dist/logging.browser.d.ts +1 -0
- package/dist/logging.browser.js +63 -0
- package/dist/logging.node-CH75bVdE.d.ts +185 -0
- package/dist/logging.node.d.ts +1 -0
- package/dist/logging.node.js +61 -0
- package/dist/math.js +2 -3
- package/dist/{os-CqGKe872.d.ts → os.browser-krjSx5kF.d.ts} +32 -55
- package/dist/os.browser.d.ts +2 -0
- package/dist/{os.js → os.browser.js} +41 -16
- package/dist/os.node-CQjch8Ht.d.ts +124 -0
- package/dist/os.node.d.ts +2 -0
- package/dist/os.node.js +83 -0
- package/dist/os.shared-C3x70nhO.d.ts +59 -0
- package/dist/pathlib.browser-NC7kKQYe.d.ts +91 -0
- package/dist/pathlib.browser.d.ts +1 -0
- package/dist/pathlib.browser.js +17 -0
- package/dist/pathlib.node-CY3yUFdT.d.ts +331 -0
- package/dist/pathlib.node.d.ts +2 -0
- package/dist/pathlib.node.js +17 -0
- package/dist/random.js +2 -3
- package/dist/re.js +2 -3
- package/dist/set-JQbLAq8B.d.ts +592 -0
- package/dist/shutil.browser-Ck5oWhnP.d.ts +113 -0
- package/dist/shutil.browser.d.ts +1 -0
- package/dist/shutil.browser.js +33 -0
- package/dist/shutil.node-C9kwvhAf.d.ts +160 -0
- package/dist/shutil.node.d.ts +2 -0
- package/dist/shutil.node.js +33 -0
- package/dist/string.js +2 -3
- package/dist/subprocess-CZVYzQ3V.d.ts +214 -0
- package/dist/subprocess.d.ts +1 -0
- package/dist/subprocess.js +29 -0
- package/dist/sys-IL8LgI_W.d.ts +212 -0
- package/dist/sys.d.ts +1 -0
- package/dist/sys.js +51 -0
- package/dist/tempfile.browser-DciOKfNm.d.ts +78 -0
- package/dist/tempfile.browser.d.ts +1 -0
- package/dist/tempfile.browser.js +19 -0
- package/dist/tempfile.node-bVMY60Cu.d.ts +195 -0
- package/dist/tempfile.node.d.ts +2 -0
- package/dist/tempfile.node.js +23 -0
- package/dist/time-CcYhdiSu.d.ts +245 -0
- package/dist/time.d.ts +1 -0
- package/dist/time.js +49 -0
- package/dist/urllib-DJtkj-0f.d.ts +198 -0
- package/dist/urllib.d.ts +1 -0
- package/dist/urllib.js +37 -0
- package/dist/uuid-C0ypmn56.d.ts +160 -0
- package/dist/uuid.d.ts +1 -0
- package/dist/uuid.js +23 -0
- package/package.json +128 -5
- package/dist/chunk-56DGFWIJ.js.map +0 -1
- package/dist/chunk-5AOJFFYN.js +0 -313
- package/dist/chunk-5AOJFFYN.js.map +0 -1
- package/dist/chunk-7BZB2TQF.js.map +0 -1
- package/dist/chunk-CEZSBJJV.js.map +0 -1
- package/dist/chunk-CUP6EPDB.js.map +0 -1
- package/dist/chunk-G6PUQTVZ.js.map +0 -1
- package/dist/chunk-OSX7QZAW.js.map +0 -1
- package/dist/chunk-PZ5AY32C.js.map +0 -1
- package/dist/chunk-QURVRHY2.js.map +0 -1
- package/dist/chunk-RSBRI27Z.js.map +0 -1
- package/dist/chunk-TAYRRHJ3.js.map +0 -1
- package/dist/collections.js.map +0 -1
- package/dist/datetime.js.map +0 -1
- package/dist/functools.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/itertools.js.map +0 -1
- package/dist/json.js.map +0 -1
- package/dist/math.js.map +0 -1
- package/dist/os.d.ts +0 -1
- package/dist/os.js.map +0 -1
- package/dist/random.js.map +0 -1
- package/dist/re.js.map +0 -1
- package/dist/string.js.map +0 -1
package/README.md
CHANGED
|
@@ -1,85 +1,159 @@
|
|
|
1
1
|
# pythonlib
|
|
2
2
|
|
|
3
|
+
<div align="center">
|
|
4
|
+
|
|
5
|
+
**Python's Beloved Standard Library — Now in TypeScript**
|
|
6
|
+
|
|
3
7
|
[](https://www.npmjs.com/package/pythonlib)
|
|
4
8
|
[](https://www.npmjs.com/package/pythonlib)
|
|
5
|
-
[](https://www.npmjs.com/package/pythonlib?activeTab=dependencies)
|
|
6
10
|
[](https://www.typescriptlang.org/)
|
|
11
|
+
[](https://nodejs.org/)
|
|
12
|
+
[](https://bun.sh/)
|
|
7
13
|
[](https://github.com/sebastian-software/python2ts/blob/main/LICENSE)
|
|
8
14
|
|
|
9
|
-
|
|
10
|
-
|
|
15
|
+
</div>
|
|
16
|
+
|
|
17
|
+
---
|
|
11
18
|
|
|
12
|
-
|
|
13
|
-
|
|
19
|
+
Miss `itertools.combinations`? Love `collections.Counter`? Wish you had `functools.lru_cache` in
|
|
20
|
+
JavaScript?
|
|
14
21
|
|
|
15
|
-
|
|
22
|
+
**pythonlib** brings Python's most powerful utilities to TypeScript — zero dependencies, full type
|
|
23
|
+
safety, tree-shakeable.
|
|
24
|
+
|
|
25
|
+
## Install
|
|
16
26
|
|
|
17
27
|
```bash
|
|
18
28
|
npm install pythonlib
|
|
19
29
|
```
|
|
20
30
|
|
|
31
|
+
## Why pythonlib?
|
|
32
|
+
|
|
33
|
+
| What You Get | Why It Matters |
|
|
34
|
+
| --------------------------- | ------------------------------------------------- |
|
|
35
|
+
| **Zero dependencies** | No supply chain bloat, minimal attack surface |
|
|
36
|
+
| **Full TypeScript support** | Generics, type inference, autocomplete everywhere |
|
|
37
|
+
| **Tree-shakeable** | Bundle only what you use |
|
|
38
|
+
| **camelCase API** | Feels native in TypeScript |
|
|
39
|
+
| **Works everywhere** | Browsers, Node.js, Deno, Bun, Workers |
|
|
40
|
+
|
|
41
|
+
## Quick Examples
|
|
42
|
+
|
|
43
|
+
### itertools — Combinatorics Made Easy
|
|
44
|
+
|
|
21
45
|
```typescript
|
|
22
|
-
import {
|
|
23
|
-
import { combinations } from "pythonlib/itertools"
|
|
24
|
-
import { Counter } from "pythonlib/collections"
|
|
25
|
-
import { lruCache } from "pythonlib/functools"
|
|
26
|
-
|
|
27
|
-
// Python-style iteration
|
|
28
|
-
for (const i of range(10)) {
|
|
29
|
-
console.log(i)
|
|
30
|
-
}
|
|
46
|
+
import { combinations, permutations, product } from "pythonlib/itertools"
|
|
31
47
|
|
|
32
|
-
//
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
48
|
+
// All 2-element combinations
|
|
49
|
+
[...combinations([1, 2, 3], 2)]
|
|
50
|
+
// → [[1, 2], [1, 3], [2, 3]]
|
|
51
|
+
|
|
52
|
+
// Cartesian product
|
|
53
|
+
[...product(["a", "b"], [1, 2])]
|
|
54
|
+
// → [["a", 1], ["a", 2], ["b", 1], ["b", 2]]
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### collections — Data Structures That Just Work
|
|
36
58
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
59
|
+
```typescript
|
|
60
|
+
import { Counter, defaultdict, deque } from "pythonlib/collections"
|
|
61
|
+
|
|
62
|
+
// Count anything
|
|
63
|
+
const votes = new Counter(["alice", "bob", "alice", "alice"])
|
|
64
|
+
votes.mostCommon(1) // → [["alice", 3]]
|
|
40
65
|
|
|
41
|
-
//
|
|
66
|
+
// No more "undefined" checks
|
|
67
|
+
const graph = defaultdict(() => [])
|
|
68
|
+
graph.get("node1").push("node2") // Just works!
|
|
69
|
+
|
|
70
|
+
// Double-ended queue with O(1) operations
|
|
71
|
+
const dq = new deque([1, 2, 3])
|
|
72
|
+
dq.appendLeft(0) // [0, 1, 2, 3]
|
|
73
|
+
dq.pop() // [0, 1, 2]
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### functools — Functional Programming Utilities
|
|
77
|
+
|
|
78
|
+
```typescript
|
|
79
|
+
import { lruCache, partial, reduce } from "pythonlib/functools"
|
|
80
|
+
|
|
81
|
+
// Memoization with one line
|
|
42
82
|
const fib = lruCache((n: number): number => (n <= 1 ? n : fib(n - 1) + fib(n - 2)))
|
|
83
|
+
fib(100) // Instant, even for large values
|
|
84
|
+
|
|
85
|
+
// Partial application
|
|
86
|
+
const greet = (greeting: string, name: string) => `${greeting}, ${name}!`
|
|
87
|
+
const sayHello = partial(greet, "Hello")
|
|
88
|
+
sayHello("World") // → "Hello, World!"
|
|
43
89
|
```
|
|
44
90
|
|
|
45
|
-
|
|
91
|
+
### Builtins — Python's Greatest Hits
|
|
46
92
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
| `pythonlib` | Builtins: `len`, `range`, `sorted`, `enumerate`... | [→](https://sebastian-software.github.io/python2ts/docs/api/index) |
|
|
50
|
-
| `pythonlib/itertools` | `chain`, `combinations`, `zipLongest`, `takeWhile` | [→](https://sebastian-software.github.io/python2ts/docs/api/itertools) |
|
|
51
|
-
| `pythonlib/functools` | `partial`, `reduce`, `lruCache`, `pipe` | [→](https://sebastian-software.github.io/python2ts/docs/api/functools) |
|
|
52
|
-
| `pythonlib/collections` | `Counter`, `defaultdict`, `deque` | [→](https://sebastian-software.github.io/python2ts/docs/api/collections) |
|
|
53
|
-
| `pythonlib/math` | `sqrt`, `floor`, `ceil`, `factorial`, `pi`, `e` | [→](https://sebastian-software.github.io/python2ts/docs/api/math) |
|
|
54
|
-
| `pythonlib/random` | `randInt`, `choice`, `shuffle`, `sample` | [→](https://sebastian-software.github.io/python2ts/docs/api/random) |
|
|
55
|
-
| `pythonlib/datetime` | `datetime`, `date`, `time`, `timedelta` | [→](https://sebastian-software.github.io/python2ts/docs/api/datetime) |
|
|
56
|
-
| `pythonlib/json` | `loads`, `dumps` | [→](https://sebastian-software.github.io/python2ts/docs/api/json) |
|
|
57
|
-
| `pythonlib/re` | `search`, `match`, `findAll`, `sub`, `compile` | [→](https://sebastian-software.github.io/python2ts/docs/api/re) |
|
|
58
|
-
| `pythonlib/string` | `Template`, `capWords`, `asciiLowercase` | [→](https://sebastian-software.github.io/python2ts/docs/api/string) |
|
|
59
|
-
|
|
60
|
-
> All function names use **camelCase** to feel native in TypeScript.
|
|
93
|
+
```typescript
|
|
94
|
+
import { range, enumerate, zip, sorted, reversed } from "pythonlib"
|
|
61
95
|
|
|
62
|
-
|
|
96
|
+
// Python-style range
|
|
97
|
+
for (const i of range(5)) {
|
|
98
|
+
} // 0, 1, 2, 3, 4
|
|
99
|
+
for (const i of range(1, 10, 2)) {
|
|
100
|
+
} // 1, 3, 5, 7, 9
|
|
63
101
|
|
|
64
|
-
|
|
102
|
+
// Enumerate with index
|
|
103
|
+
for (const [i, item] of enumerate(["a", "b", "c"])) {
|
|
104
|
+
console.log(i, item) // 0 "a", 1 "b", 2 "c"
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// Zip multiple iterables
|
|
108
|
+
;[...zip([1, 2, 3], ["a", "b", "c"])]
|
|
109
|
+
// → [[1, "a"], [2, "b"], [3, "c"]]
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
## Available Modules
|
|
65
113
|
|
|
66
|
-
|
|
|
67
|
-
|
|
|
68
|
-
| [
|
|
69
|
-
| [
|
|
70
|
-
| [
|
|
71
|
-
| [
|
|
114
|
+
| Module | Highlights | Docs |
|
|
115
|
+
| ----------------------- | ----------------------------------------------------------- | ------------------------------------------------------------------------ |
|
|
116
|
+
| `pythonlib` | `range`, `enumerate`, `zip`, `sorted`, `len`, `min`, `max` | [→](https://sebastian-software.github.io/python2ts/docs/api/index) |
|
|
117
|
+
| `pythonlib/itertools` | `chain`, `combinations`, `permutations`, `product`, `cycle` | [→](https://sebastian-software.github.io/python2ts/docs/api/itertools) |
|
|
118
|
+
| `pythonlib/functools` | `lruCache`, `partial`, `reduce`, `pipe`, `compose` | [→](https://sebastian-software.github.io/python2ts/docs/api/functools) |
|
|
119
|
+
| `pythonlib/collections` | `Counter`, `defaultdict`, `deque`, `OrderedDict` | [→](https://sebastian-software.github.io/python2ts/docs/api/collections) |
|
|
120
|
+
| `pythonlib/math` | `sqrt`, `floor`, `ceil`, `factorial`, `gcd`, `pi` | [→](https://sebastian-software.github.io/python2ts/docs/api/math) |
|
|
121
|
+
| `pythonlib/random` | `randInt`, `choice`, `shuffle`, `sample`, `random` | [→](https://sebastian-software.github.io/python2ts/docs/api/random) |
|
|
122
|
+
| `pythonlib/datetime` | `datetime`, `date`, `time`, `timedelta` | [→](https://sebastian-software.github.io/python2ts/docs/api/datetime) |
|
|
123
|
+
| `pythonlib/re` | `search`, `match`, `findAll`, `sub`, `compile` | [→](https://sebastian-software.github.io/python2ts/docs/api/re) |
|
|
124
|
+
| `pythonlib/json` | `loads`, `dumps` with Python semantics | [→](https://sebastian-software.github.io/python2ts/docs/api/json) |
|
|
125
|
+
| `pythonlib/string` | `Template`, `capWords`, `ascii_lowercase` | [→](https://sebastian-software.github.io/python2ts/docs/api/string) |
|
|
126
|
+
| `pythonlib/os` | `path.join`, `environ`, `getcwd` | [→](https://sebastian-software.github.io/python2ts/docs/api/os) |
|
|
127
|
+
| `pythonlib/pathlib` | `Path` class for filesystem operations | [→](https://sebastian-software.github.io/python2ts/docs/api/pathlib) |
|
|
128
|
+
| `pythonlib/glob` | File pattern matching | [→](https://sebastian-software.github.io/python2ts/docs/api/glob) |
|
|
129
|
+
| `pythonlib/hashlib` | `md5`, `sha256`, `sha512` hashing | [→](https://sebastian-software.github.io/python2ts/docs/api/hashlib) |
|
|
130
|
+
| `pythonlib/base64` | Base64 encoding/decoding | [→](https://sebastian-software.github.io/python2ts/docs/api/base64) |
|
|
131
|
+
| `pythonlib/uuid` | UUID generation | [→](https://sebastian-software.github.io/python2ts/docs/api/uuid) |
|
|
72
132
|
|
|
73
133
|
## Runtime Support
|
|
74
134
|
|
|
75
|
-
|
|
135
|
+
Works everywhere JavaScript runs:
|
|
136
|
+
|
|
137
|
+
- **Node.js** (v22, v24)
|
|
138
|
+
- **Bun**
|
|
139
|
+
- **Deno**
|
|
140
|
+
- **Browsers** (Chrome, Firefox, Safari)
|
|
141
|
+
- **Edge** (Cloudflare Workers, Vercel, AWS Lambda)
|
|
142
|
+
|
|
143
|
+
## Documentation
|
|
76
144
|
|
|
77
|
-
|
|
145
|
+
| Resource | Description |
|
|
146
|
+
| ---------------------------------------------------------------------------- | ------------------------------------------ |
|
|
147
|
+
| [Homepage](https://sebastian-software.github.io/python2ts/) | Project overview and quick start |
|
|
148
|
+
| [Runtime Guide](https://sebastian-software.github.io/python2ts/docs/runtime) | Detailed usage guide |
|
|
149
|
+
| [API Reference](https://sebastian-software.github.io/python2ts/docs/api) | Complete API documentation for all modules |
|
|
78
150
|
|
|
79
151
|
## Related
|
|
80
152
|
|
|
81
153
|
- [**python2ts**](https://www.npmjs.com/package/python2ts) — Transpile Python to TypeScript
|
|
82
|
-
|
|
154
|
+
automatically
|
|
155
|
+
- [**GitHub**](https://github.com/sebastian-software/python2ts) — Source code, issues, contributions
|
|
156
|
+
welcome
|
|
83
157
|
|
|
84
158
|
## License
|
|
85
159
|
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Python base64 module for TypeScript
|
|
3
|
+
*
|
|
4
|
+
* Provides encoding and decoding using Base64 and URL-safe Base64.
|
|
5
|
+
*
|
|
6
|
+
* @see {@link https://docs.python.org/3/library/base64.html | Python base64 documentation}
|
|
7
|
+
* @module
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Encode bytes-like object using Base64 and return bytes.
|
|
11
|
+
*
|
|
12
|
+
* @param s - Bytes to encode (Uint8Array or string)
|
|
13
|
+
* @returns Base64 encoded Uint8Array
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* const encoded = b64encode(new TextEncoder().encode("hello"))
|
|
18
|
+
* console.log(new TextDecoder().decode(encoded)) // "aGVsbG8="
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
declare function b64encode(s: Uint8Array | string): Uint8Array;
|
|
22
|
+
/**
|
|
23
|
+
* Decode Base64 encoded bytes-like object or ASCII string and return bytes.
|
|
24
|
+
*
|
|
25
|
+
* @param s - Base64 encoded data (Uint8Array or string)
|
|
26
|
+
* @returns Decoded Uint8Array
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```typescript
|
|
30
|
+
* const decoded = b64decode("aGVsbG8=")
|
|
31
|
+
* console.log(new TextDecoder().decode(decoded)) // "hello"
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
declare function b64decode(s: Uint8Array | string): Uint8Array;
|
|
35
|
+
/**
|
|
36
|
+
* Encode bytes-like object using standard Base64 and return bytes.
|
|
37
|
+
* This is identical to b64encode().
|
|
38
|
+
*
|
|
39
|
+
* @param s - Bytes to encode
|
|
40
|
+
* @returns Base64 encoded Uint8Array
|
|
41
|
+
*/
|
|
42
|
+
declare function standardB64encode(s: Uint8Array | string): Uint8Array;
|
|
43
|
+
/**
|
|
44
|
+
* Decode standard Base64 encoded bytes-like object and return bytes.
|
|
45
|
+
* This is identical to b64decode().
|
|
46
|
+
*
|
|
47
|
+
* @param s - Base64 encoded data
|
|
48
|
+
* @returns Decoded Uint8Array
|
|
49
|
+
*/
|
|
50
|
+
declare function standardB64decode(s: Uint8Array | string): Uint8Array;
|
|
51
|
+
/**
|
|
52
|
+
* Encode bytes-like object using URL-safe Base64 and return bytes.
|
|
53
|
+
*
|
|
54
|
+
* URL-safe Base64 uses - instead of + and _ instead of /,
|
|
55
|
+
* and omits padding characters.
|
|
56
|
+
*
|
|
57
|
+
* @param s - Bytes to encode
|
|
58
|
+
* @returns URL-safe Base64 encoded Uint8Array
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* ```typescript
|
|
62
|
+
* const encoded = urlsafeB64encode(new Uint8Array([255, 239]))
|
|
63
|
+
* // Uses - and _ instead of + and /
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
66
|
+
declare function urlsafeB64encode(s: Uint8Array | string): Uint8Array;
|
|
67
|
+
/**
|
|
68
|
+
* Decode URL-safe Base64 encoded bytes-like object and return bytes.
|
|
69
|
+
*
|
|
70
|
+
* @param s - URL-safe Base64 encoded data
|
|
71
|
+
* @returns Decoded Uint8Array
|
|
72
|
+
*/
|
|
73
|
+
declare function urlsafeB64decode(s: Uint8Array | string): Uint8Array;
|
|
74
|
+
/**
|
|
75
|
+
* Encode bytes-like object using Base16 (hexadecimal) and return bytes.
|
|
76
|
+
*
|
|
77
|
+
* @param s - Bytes to encode
|
|
78
|
+
* @returns Hexadecimal encoded Uint8Array (uppercase)
|
|
79
|
+
*/
|
|
80
|
+
declare function b16encode(s: Uint8Array | string): Uint8Array;
|
|
81
|
+
/**
|
|
82
|
+
* Decode Base16 (hexadecimal) encoded bytes-like object and return bytes.
|
|
83
|
+
*
|
|
84
|
+
* @param s - Hexadecimal encoded data
|
|
85
|
+
* @returns Decoded Uint8Array
|
|
86
|
+
*/
|
|
87
|
+
declare function b16decode(s: Uint8Array | string): Uint8Array;
|
|
88
|
+
/**
|
|
89
|
+
* Encode bytes-like object using Base32 and return bytes.
|
|
90
|
+
*
|
|
91
|
+
* @param s - Bytes to encode
|
|
92
|
+
* @returns Base32 encoded Uint8Array
|
|
93
|
+
*/
|
|
94
|
+
declare function b32encode(s: Uint8Array | string): Uint8Array;
|
|
95
|
+
/**
|
|
96
|
+
* Decode Base32 encoded bytes-like object and return bytes.
|
|
97
|
+
*
|
|
98
|
+
* @param s - Base32 encoded data
|
|
99
|
+
* @returns Decoded Uint8Array
|
|
100
|
+
*/
|
|
101
|
+
declare function b32decode(s: Uint8Array | string): Uint8Array;
|
|
102
|
+
/**
|
|
103
|
+
* Encode bytes-like object using Ascii85 (also known as Base85) and return bytes.
|
|
104
|
+
*
|
|
105
|
+
* @param s - Bytes to encode
|
|
106
|
+
* @returns Ascii85 encoded Uint8Array
|
|
107
|
+
*/
|
|
108
|
+
declare function a85encode(s: Uint8Array | string): Uint8Array;
|
|
109
|
+
/**
|
|
110
|
+
* Decode Ascii85 encoded bytes-like object and return bytes.
|
|
111
|
+
*
|
|
112
|
+
* @param s - Ascii85 encoded data
|
|
113
|
+
* @returns Decoded Uint8Array
|
|
114
|
+
*/
|
|
115
|
+
declare function a85decode(s: Uint8Array | string): Uint8Array;
|
|
116
|
+
/**
|
|
117
|
+
* Encode bytes-like object using Base85 (same as a85encode) and return bytes.
|
|
118
|
+
*
|
|
119
|
+
* @param s - Bytes to encode
|
|
120
|
+
* @returns Base85 encoded Uint8Array
|
|
121
|
+
*/
|
|
122
|
+
declare function b85encode(s: Uint8Array | string): Uint8Array;
|
|
123
|
+
/**
|
|
124
|
+
* Decode Base85 encoded bytes-like object and return bytes.
|
|
125
|
+
*
|
|
126
|
+
* @param s - Base85 encoded data
|
|
127
|
+
* @returns Decoded Uint8Array
|
|
128
|
+
*/
|
|
129
|
+
declare function b85decode(s: Uint8Array | string): Uint8Array;
|
|
130
|
+
/**
|
|
131
|
+
* Encode bytes-like object using Base64 and return as ASCII string.
|
|
132
|
+
*
|
|
133
|
+
* @param s - Bytes to encode
|
|
134
|
+
* @returns Base64 encoded string
|
|
135
|
+
*/
|
|
136
|
+
declare function encodeString(s: Uint8Array | string): string;
|
|
137
|
+
/**
|
|
138
|
+
* Decode Base64 ASCII string and return bytes.
|
|
139
|
+
*
|
|
140
|
+
* @param s - Base64 encoded string
|
|
141
|
+
* @returns Decoded Uint8Array
|
|
142
|
+
*/
|
|
143
|
+
declare function decodeString(s: string): Uint8Array;
|
|
144
|
+
|
|
145
|
+
declare const base64Module_a85decode: typeof a85decode;
|
|
146
|
+
declare const base64Module_a85encode: typeof a85encode;
|
|
147
|
+
declare const base64Module_b16decode: typeof b16decode;
|
|
148
|
+
declare const base64Module_b16encode: typeof b16encode;
|
|
149
|
+
declare const base64Module_b32decode: typeof b32decode;
|
|
150
|
+
declare const base64Module_b32encode: typeof b32encode;
|
|
151
|
+
declare const base64Module_b64decode: typeof b64decode;
|
|
152
|
+
declare const base64Module_b64encode: typeof b64encode;
|
|
153
|
+
declare const base64Module_b85decode: typeof b85decode;
|
|
154
|
+
declare const base64Module_b85encode: typeof b85encode;
|
|
155
|
+
declare const base64Module_decodeString: typeof decodeString;
|
|
156
|
+
declare const base64Module_encodeString: typeof encodeString;
|
|
157
|
+
declare const base64Module_standardB64decode: typeof standardB64decode;
|
|
158
|
+
declare const base64Module_standardB64encode: typeof standardB64encode;
|
|
159
|
+
declare const base64Module_urlsafeB64decode: typeof urlsafeB64decode;
|
|
160
|
+
declare const base64Module_urlsafeB64encode: typeof urlsafeB64encode;
|
|
161
|
+
declare namespace base64Module {
|
|
162
|
+
export { base64Module_a85decode as a85decode, base64Module_a85encode as a85encode, base64Module_b16decode as b16decode, base64Module_b16encode as b16encode, base64Module_b32decode as b32decode, base64Module_b32encode as b32encode, base64Module_b64decode as b64decode, base64Module_b64encode as b64encode, base64Module_b85decode as b85decode, base64Module_b85encode as b85encode, base64Module_decodeString as decodeString, base64Module_encodeString as encodeString, base64Module_standardB64decode as standardB64decode, base64Module_standardB64encode as standardB64encode, base64Module_urlsafeB64decode as urlsafeB64decode, base64Module_urlsafeB64encode as urlsafeB64encode };
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export { b64encode as a, base64Module as b, b64decode as c, standardB64decode as d, urlsafeB64decode as e, b16encode as f, b16decode as g, b32encode as h, b32decode as i, a85encode as j, a85decode as k, b85encode as l, b85decode as m, encodeString as n, decodeString as o, standardB64encode as s, urlsafeB64encode as u };
|
package/dist/base64.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { k as a85decode, j as a85encode, g as b16decode, f as b16encode, i as b32decode, h as b32encode, c as b64decode, a as b64encode, m as b85decode, l as b85encode, o as decodeString, n as encodeString, d as standardB64decode, s as standardB64encode, e as urlsafeB64decode, u as urlsafeB64encode } from './base64-C_am75Sv.js';
|
package/dist/base64.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import {
|
|
2
|
+
a85decode,
|
|
3
|
+
a85encode,
|
|
4
|
+
b16decode,
|
|
5
|
+
b16encode,
|
|
6
|
+
b32decode,
|
|
7
|
+
b32encode,
|
|
8
|
+
b64decode,
|
|
9
|
+
b64encode,
|
|
10
|
+
b85decode,
|
|
11
|
+
b85encode,
|
|
12
|
+
decodeString,
|
|
13
|
+
encodeString,
|
|
14
|
+
standardB64decode,
|
|
15
|
+
standardB64encode,
|
|
16
|
+
urlsafeB64decode,
|
|
17
|
+
urlsafeB64encode
|
|
18
|
+
} from "./chunk-HTCQ4OO5.js";
|
|
19
|
+
import "./chunk-MLKGABMK.js";
|
|
20
|
+
export {
|
|
21
|
+
a85decode,
|
|
22
|
+
a85encode,
|
|
23
|
+
b16decode,
|
|
24
|
+
b16encode,
|
|
25
|
+
b32decode,
|
|
26
|
+
b32encode,
|
|
27
|
+
b64decode,
|
|
28
|
+
b64encode,
|
|
29
|
+
b85decode,
|
|
30
|
+
b85encode,
|
|
31
|
+
decodeString,
|
|
32
|
+
encodeString,
|
|
33
|
+
standardB64decode,
|
|
34
|
+
standardB64encode,
|
|
35
|
+
urlsafeB64decode,
|
|
36
|
+
urlsafeB64encode
|
|
37
|
+
};
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
import {
|
|
2
|
+
__export
|
|
3
|
+
} from "./chunk-MLKGABMK.js";
|
|
4
|
+
|
|
5
|
+
// src/uuid.ts
|
|
6
|
+
var uuid_exports = {};
|
|
7
|
+
__export(uuid_exports, {
|
|
8
|
+
NAMESPACE_DNS: () => NAMESPACE_DNS,
|
|
9
|
+
NAMESPACE_OID: () => NAMESPACE_OID,
|
|
10
|
+
NAMESPACE_URL: () => NAMESPACE_URL,
|
|
11
|
+
NAMESPACE_X500: () => NAMESPACE_X500,
|
|
12
|
+
UUID: () => UUID,
|
|
13
|
+
uuid1: () => uuid1,
|
|
14
|
+
uuid3: () => uuid3,
|
|
15
|
+
uuid4: () => uuid4,
|
|
16
|
+
uuid5: () => uuid5
|
|
17
|
+
});
|
|
18
|
+
var UUID = class _UUID {
|
|
19
|
+
_bytes;
|
|
20
|
+
/** Helper to safely get byte at index */
|
|
21
|
+
getByte(index) {
|
|
22
|
+
return this._bytes[index] ?? 0;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Create a UUID from various input formats.
|
|
26
|
+
*
|
|
27
|
+
* @param hex - Hexadecimal string (with or without hyphens)
|
|
28
|
+
* @param bytes - 16 bytes as Uint8Array
|
|
29
|
+
* @param int - Integer value
|
|
30
|
+
*/
|
|
31
|
+
constructor(options) {
|
|
32
|
+
if (typeof options === "string") {
|
|
33
|
+
options = { hex: options };
|
|
34
|
+
}
|
|
35
|
+
if ("hex" in options) {
|
|
36
|
+
const hex = options.hex.replace(/-/g, "").replace(/[{}]/g, "");
|
|
37
|
+
if (hex.length !== 32) {
|
|
38
|
+
throw new Error("badly formed hexadecimal UUID string");
|
|
39
|
+
}
|
|
40
|
+
this._bytes = new Uint8Array(16);
|
|
41
|
+
for (let i = 0; i < 16; i++) {
|
|
42
|
+
this._bytes[i] = parseInt(hex.substring(i * 2, i * 2 + 2), 16);
|
|
43
|
+
}
|
|
44
|
+
} else if ("bytes" in options) {
|
|
45
|
+
if (options.bytes.length !== 16) {
|
|
46
|
+
throw new Error("bytes must be a 16-byte array");
|
|
47
|
+
}
|
|
48
|
+
this._bytes = new Uint8Array(options.bytes);
|
|
49
|
+
} else if ("int" in options) {
|
|
50
|
+
this._bytes = new Uint8Array(16);
|
|
51
|
+
let value = options.int;
|
|
52
|
+
for (let i = 15; i >= 0; i--) {
|
|
53
|
+
this._bytes[i] = Number(value & 0xffn);
|
|
54
|
+
value = value >> 8n;
|
|
55
|
+
}
|
|
56
|
+
} else {
|
|
57
|
+
throw new Error("one of hex, bytes, or int is required");
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* The UUID as a 32-character lowercase hexadecimal string.
|
|
62
|
+
*/
|
|
63
|
+
get hex() {
|
|
64
|
+
let result = "";
|
|
65
|
+
for (const byte of this._bytes) {
|
|
66
|
+
result += byte.toString(16).padStart(2, "0");
|
|
67
|
+
}
|
|
68
|
+
return result;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* The UUID as a 16-byte Uint8Array.
|
|
72
|
+
*/
|
|
73
|
+
get bytes() {
|
|
74
|
+
return new Uint8Array(this._bytes);
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* The UUID as a 128-bit integer.
|
|
78
|
+
*/
|
|
79
|
+
get int() {
|
|
80
|
+
let value = 0n;
|
|
81
|
+
for (const byte of this._bytes) {
|
|
82
|
+
value = value << 8n | BigInt(byte);
|
|
83
|
+
}
|
|
84
|
+
return value;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* The version number (1 through 5, as an integer).
|
|
88
|
+
*/
|
|
89
|
+
get version() {
|
|
90
|
+
return this.getByte(6) >> 4 & 15;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* The variant (reserved bits).
|
|
94
|
+
*/
|
|
95
|
+
get variant() {
|
|
96
|
+
const byte = this.getByte(8);
|
|
97
|
+
if ((byte & 128) === 0) {
|
|
98
|
+
return "reserved for NCS compatibility";
|
|
99
|
+
} else if ((byte & 192) === 128) {
|
|
100
|
+
return "specified in RFC 4122";
|
|
101
|
+
} else if ((byte & 224) === 192) {
|
|
102
|
+
return "reserved for Microsoft compatibility";
|
|
103
|
+
} else {
|
|
104
|
+
return "reserved for future definition";
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* The UUID as a hyphenated string (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).
|
|
109
|
+
*/
|
|
110
|
+
toString() {
|
|
111
|
+
const h = this.hex;
|
|
112
|
+
return `${h.slice(0, 8)}-${h.slice(8, 12)}-${h.slice(12, 16)}-${h.slice(16, 20)}-${h.slice(20)}`;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* The UUID as a URN (urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).
|
|
116
|
+
*/
|
|
117
|
+
get urn() {
|
|
118
|
+
return `urn:uuid:${this.toString()}`;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Compare UUIDs for equality.
|
|
122
|
+
*/
|
|
123
|
+
equals(other) {
|
|
124
|
+
if (!(other instanceof _UUID)) return false;
|
|
125
|
+
for (let i = 0; i < 16; i++) {
|
|
126
|
+
if (this._bytes[i] !== other._bytes[i]) return false;
|
|
127
|
+
}
|
|
128
|
+
return true;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Get the time_low field (first 32 bits).
|
|
132
|
+
*/
|
|
133
|
+
get timeLow() {
|
|
134
|
+
return (this.getByte(0) << 24 | this.getByte(1) << 16 | this.getByte(2) << 8 | this.getByte(3)) >>> 0;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Get the time_mid field (next 16 bits).
|
|
138
|
+
*/
|
|
139
|
+
get timeMid() {
|
|
140
|
+
return this.getByte(4) << 8 | this.getByte(5);
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Get the time_hi_version field (next 16 bits).
|
|
144
|
+
*/
|
|
145
|
+
get timeHiVersion() {
|
|
146
|
+
return this.getByte(6) << 8 | this.getByte(7);
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Get the clock_seq_hi_variant field.
|
|
150
|
+
*/
|
|
151
|
+
get clockSeqHiVariant() {
|
|
152
|
+
return this.getByte(8);
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Get the clock_seq_low field.
|
|
156
|
+
*/
|
|
157
|
+
get clockSeqLow() {
|
|
158
|
+
return this.getByte(9);
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Get the node field (last 48 bits).
|
|
162
|
+
*/
|
|
163
|
+
get node() {
|
|
164
|
+
let value = 0n;
|
|
165
|
+
for (let i = 10; i < 16; i++) {
|
|
166
|
+
value = value << 8n | BigInt(this.getByte(i));
|
|
167
|
+
}
|
|
168
|
+
return value;
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
function uuid4() {
|
|
172
|
+
if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
|
|
173
|
+
return new UUID({ hex: crypto.randomUUID() });
|
|
174
|
+
}
|
|
175
|
+
const bytes = new Uint8Array(16);
|
|
176
|
+
if (typeof crypto !== "undefined" && typeof crypto.getRandomValues === "function") {
|
|
177
|
+
crypto.getRandomValues(bytes);
|
|
178
|
+
} else {
|
|
179
|
+
for (let i = 0; i < 16; i++) {
|
|
180
|
+
bytes[i] = Math.floor(Math.random() * 256);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
bytes[6] = (bytes[6] ?? 0) & 15 | 64;
|
|
184
|
+
bytes[8] = (bytes[8] ?? 0) & 63 | 128;
|
|
185
|
+
return new UUID({ bytes });
|
|
186
|
+
}
|
|
187
|
+
function uuid1(node, clockSeq) {
|
|
188
|
+
const bytes = new Uint8Array(16);
|
|
189
|
+
const now = Date.now();
|
|
190
|
+
const UUID_EPOCH_OFFSET = 122192928000000000n;
|
|
191
|
+
const timestamp = BigInt(now) * 10000n + UUID_EPOCH_OFFSET;
|
|
192
|
+
const timeLow = Number(timestamp & 0xffffffffn);
|
|
193
|
+
bytes[0] = timeLow >> 24 & 255;
|
|
194
|
+
bytes[1] = timeLow >> 16 & 255;
|
|
195
|
+
bytes[2] = timeLow >> 8 & 255;
|
|
196
|
+
bytes[3] = timeLow & 255;
|
|
197
|
+
const timeMid = Number(timestamp >> 32n & 0xffffn);
|
|
198
|
+
bytes[4] = timeMid >> 8 & 255;
|
|
199
|
+
bytes[5] = timeMid & 255;
|
|
200
|
+
const timeHi = Number(timestamp >> 48n & 0x0fffn);
|
|
201
|
+
bytes[6] = timeHi >> 8 & 15 | 16;
|
|
202
|
+
bytes[7] = timeHi & 255;
|
|
203
|
+
const seq = clockSeq ?? Math.floor(Math.random() * 16384);
|
|
204
|
+
bytes[8] = seq >> 8 & 63 | 128;
|
|
205
|
+
bytes[9] = seq & 255;
|
|
206
|
+
let nodeId = node;
|
|
207
|
+
if (nodeId === void 0) {
|
|
208
|
+
const randomNode = new Uint8Array(6);
|
|
209
|
+
if (typeof crypto !== "undefined" && typeof crypto.getRandomValues === "function") {
|
|
210
|
+
crypto.getRandomValues(randomNode);
|
|
211
|
+
} else {
|
|
212
|
+
for (let i = 0; i < 6; i++) {
|
|
213
|
+
randomNode[i] = Math.floor(Math.random() * 256);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
randomNode[0] = (randomNode[0] ?? 0) | 1;
|
|
217
|
+
nodeId = 0n;
|
|
218
|
+
for (const byte of randomNode) {
|
|
219
|
+
nodeId = nodeId << 8n | BigInt(byte);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
for (let i = 15; i >= 10; i--) {
|
|
223
|
+
bytes[i] = Number(nodeId & 0xffn);
|
|
224
|
+
nodeId = nodeId >> 8n;
|
|
225
|
+
}
|
|
226
|
+
return new UUID({ bytes });
|
|
227
|
+
}
|
|
228
|
+
function uuid3(_namespace, _name) {
|
|
229
|
+
throw new Error("uuid3 requires hashlib.md5 - import from 'pythonlib/hashlib'");
|
|
230
|
+
}
|
|
231
|
+
function uuid5(_namespace, _name) {
|
|
232
|
+
throw new Error("uuid5 requires hashlib.sha1 - import from 'pythonlib/hashlib'");
|
|
233
|
+
}
|
|
234
|
+
var NAMESPACE_DNS = new UUID({ hex: "6ba7b810-9dad-11d1-80b4-00c04fd430c8" });
|
|
235
|
+
var NAMESPACE_URL = new UUID({ hex: "6ba7b811-9dad-11d1-80b4-00c04fd430c8" });
|
|
236
|
+
var NAMESPACE_OID = new UUID({ hex: "6ba7b812-9dad-11d1-80b4-00c04fd430c8" });
|
|
237
|
+
var NAMESPACE_X500 = new UUID({ hex: "6ba7b814-9dad-11d1-80b4-00c04fd430c8" });
|
|
238
|
+
|
|
239
|
+
export {
|
|
240
|
+
UUID,
|
|
241
|
+
uuid4,
|
|
242
|
+
uuid1,
|
|
243
|
+
uuid3,
|
|
244
|
+
uuid5,
|
|
245
|
+
NAMESPACE_DNS,
|
|
246
|
+
NAMESPACE_URL,
|
|
247
|
+
NAMESPACE_OID,
|
|
248
|
+
NAMESPACE_X500,
|
|
249
|
+
uuid_exports
|
|
250
|
+
};
|
|
251
|
+
/* v8 ignore start -- fallback for environments without crypto.randomUUID @preserve */
|
|
252
|
+
/* v8 ignore start -- fallback for environments without crypto @preserve */
|