simpleflakes 3.1.0 → 4.0.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/README.md +115 -122
- package/dist/simpleflakes.d.ts +11 -13
- package/dist/simpleflakes.d.ts.map +1 -1
- package/dist/simpleflakes.js +1 -1
- package/dist/simpleflakes.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -2,30 +2,43 @@
|
|
|
2
2
|
|
|
3
3
|
[](https://github.com/leodutra/simpleflakes/actions/workflows/ci.yml)
|
|
4
4
|
[![npm][npm-badge]][npm-link]
|
|
5
|
+
[![coveralls status][coveralls-badge]][coveralls-link]
|
|
5
6
|
[](https://www.npmjs.com/package/simpleflakes)
|
|
6
7
|
[](https://bundlephobia.com/package/simpleflakes)
|
|
7
|
-
[](https://nodejs.org/)
|
|
9
|
-
[](https://www.npmjs.com/package/simpleflakes)
|
|
8
|
+
[](https://www.npmjs.com/package/simpleflakes)
|
|
10
9
|
[](https://www.typescriptlang.org/)
|
|
10
|
+
[](https://nodejs.org/)
|
|
11
|
+
[](https://github.com/leodutra/simpleflakes/actions/workflows/ci.yml)
|
|
11
12
|
[](https://github.com/leodutra/simpleflakes)
|
|
12
|
-
[](https://app.fossa.io/projects/git%2Bgithub.com%2Fleodutra%2Fsimpleflakes?ref=badge_shield)
|
|
13
14
|
|
|
14
|
-
> **Fast, lightweight, and reliable distributed 64-bit ID generation for Node.js**
|
|
15
|
+
> **Fast, lightweight, and reliable distributed 64-bit ID generation for Node.js**
|
|
15
16
|
> Zero dependencies • TypeScript-ready • 8.8M+ ops/sec performance
|
|
16
17
|
|
|
17
|
-
##
|
|
18
|
+
## Features
|
|
19
|
+
|
|
20
|
+
- ⚡ **8.8M+ ops/sec** - Ultra-fast performance
|
|
21
|
+
- 🔢 **Time-oriented 64-bit IDs** - Globally unique, sortable by creation time
|
|
22
|
+
- 0️⃣ **Zero dependencies** - Pure JavaScript, lightweight bundle
|
|
23
|
+
- 🏷️ **TypeScript-ready** - Full type safety and universal module support
|
|
24
|
+
- 🚀 **Production-ready** - 100% test coverage, Snowflake compatible
|
|
25
|
+
|
|
26
|
+
## Table of Contents
|
|
18
27
|
|
|
19
|
-
-
|
|
20
|
-
-
|
|
21
|
-
-
|
|
22
|
-
-
|
|
23
|
-
-
|
|
24
|
-
-
|
|
25
|
-
-
|
|
26
|
-
-
|
|
28
|
+
- [What is Simpleflake?](#what-is-simpleflake)
|
|
29
|
+
- [Installation](#installation)
|
|
30
|
+
- [Quick Start](#quick-start)
|
|
31
|
+
- [Advanced Usage](#advanced-usage)
|
|
32
|
+
- [ID Structure](#id-structure)
|
|
33
|
+
- [Performance](#performance)
|
|
34
|
+
- [Architecture](#architecture)
|
|
35
|
+
- [Use Cases](#use-cases)
|
|
36
|
+
- [API Reference](#api-reference)
|
|
37
|
+
- [Migration Guide](#migration-guide)
|
|
38
|
+
- [Comparison](#comparison)
|
|
39
|
+
- [Contributing](#contributing)
|
|
27
40
|
|
|
28
|
-
##
|
|
41
|
+
## What is Simpleflake?
|
|
29
42
|
|
|
30
43
|
Simpleflake generates **unique 64-bit integers** that are:
|
|
31
44
|
|
|
@@ -36,15 +49,20 @@ Simpleflake generates **unique 64-bit integers** that are:
|
|
|
36
49
|
|
|
37
50
|
Perfect for database primary keys, distributed system IDs, and anywhere you need fast, unique identifiers.
|
|
38
51
|
|
|
39
|
-
|
|
52
|
+
## References
|
|
40
53
|
|
|
41
|
-
|
|
54
|
+
- **[Original Presentation](http://akmanalp.com/simpleflake_presentation/)** - Introduction to the concept by Mali Akmanalp
|
|
55
|
+
- **[Video Discussion](http://www.youtube.com/watch?v=SCQPBGi_QRk)** - Detailed explanation and use cases
|
|
56
|
+
- **[Python Implementation](https://simpleflake.readthedocs.org/en/latest/)** - Original reference implementation
|
|
57
|
+
- **[Twitter Snowflake](https://blog.twitter.com/engineering/en_us/a/2010/announcing-snowflake.html)** - Similar distributed ID system
|
|
58
|
+
|
|
59
|
+
## Installation
|
|
42
60
|
|
|
43
61
|
```bash
|
|
44
62
|
npm install simpleflakes
|
|
45
63
|
```
|
|
46
64
|
|
|
47
|
-
##
|
|
65
|
+
## Quick Start
|
|
48
66
|
|
|
49
67
|
### JavaScript (CommonJS)
|
|
50
68
|
```javascript
|
|
@@ -62,18 +80,18 @@ console.log(id.toString(36)); // "w68acyhy50hc" (base36 - shortest)
|
|
|
62
80
|
|
|
63
81
|
### TypeScript / ES Modules
|
|
64
82
|
```typescript
|
|
65
|
-
import { simpleflake, parseSimpleflake, type
|
|
83
|
+
import { simpleflake, parseSimpleflake, type SimpleflakeStruct } from 'simpleflakes';
|
|
66
84
|
|
|
67
85
|
// Generate with full type safety
|
|
68
86
|
const id: bigint = simpleflake();
|
|
69
87
|
|
|
70
88
|
// Parse the ID to extract timestamp and random bits
|
|
71
|
-
const parsed:
|
|
89
|
+
const parsed: SimpleflakeStruct = parseSimpleflake(id);
|
|
72
90
|
console.log(parsed.timestamp); // "1693244847123" (Unix timestamp as string)
|
|
73
91
|
console.log(parsed.randomBits); // "4567234" (Random component as string)
|
|
74
92
|
```
|
|
75
93
|
|
|
76
|
-
##
|
|
94
|
+
## Advanced Usage
|
|
77
95
|
|
|
78
96
|
### Custom Parameters
|
|
79
97
|
```javascript
|
|
@@ -115,15 +133,14 @@ const batch = generateBatch(1000);
|
|
|
115
133
|
console.log(`Generated ${batch.length} unique IDs`);
|
|
116
134
|
```
|
|
117
135
|
|
|
118
|
-
##
|
|
136
|
+
## ID Structure
|
|
119
137
|
|
|
120
138
|
Each 64-bit simpleflake ID contains:
|
|
121
139
|
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
|
125
|
-
|
|
|
126
|
-
```
|
|
140
|
+
|<------- 41 bits ------->|<- 23 bits ->|
|
|
141
|
+
|-------------------------|-------------|
|
|
142
|
+
|Timestamp |Random |
|
|
143
|
+
|(milliseconds from epoch)|(0-8388607) |
|
|
127
144
|
|
|
128
145
|
- **41 bits timestamp**: Milliseconds since epoch (Year 2000)
|
|
129
146
|
- **23 bits random**: Random number for uniqueness within the same millisecond
|
|
@@ -132,22 +149,23 @@ Each 64-bit simpleflake ID contains:
|
|
|
132
149
|
This gives you:
|
|
133
150
|
- **69+ years** of timestamp range (until year 2069)
|
|
134
151
|
- **8.3 million** unique IDs per millisecond
|
|
152
|
+
- **Extremely low collision chance** - 1 in 8.3 million per millisecond
|
|
135
153
|
- **Sortable by creation time** when converted to integers
|
|
136
154
|
|
|
137
|
-
##
|
|
155
|
+
## Performance
|
|
138
156
|
|
|
139
157
|
This library is optimized for speed:
|
|
140
158
|
|
|
141
159
|
```javascript
|
|
142
160
|
// Benchmark results (operations per second)
|
|
143
|
-
simpleflake()
|
|
144
|
-
parseSimpleflake()
|
|
145
|
-
binary()
|
|
161
|
+
simpleflake() // ~8.8M+ ops/sec
|
|
162
|
+
parseSimpleflake() // ~3.9M+ ops/sec
|
|
163
|
+
binary() // ~26M+ ops/sec
|
|
146
164
|
```
|
|
147
165
|
|
|
148
166
|
Perfect for high-throughput applications requiring millions of IDs per second.
|
|
149
167
|
|
|
150
|
-
##
|
|
168
|
+
## Architecture
|
|
151
169
|
|
|
152
170
|
### Why 64-bit IDs?
|
|
153
171
|
|
|
@@ -164,7 +182,38 @@ No coordination required between multiple ID generators:
|
|
|
164
182
|
- **Random collision protection**: 23 random bits provide 8.3M combinations per millisecond
|
|
165
183
|
- **High availability**: Each service can generate IDs independently
|
|
166
184
|
|
|
167
|
-
##
|
|
185
|
+
## Use Cases
|
|
186
|
+
|
|
187
|
+
### Database Primary Keys
|
|
188
|
+
```javascript
|
|
189
|
+
// Perfect for database IDs - time-ordered and unique
|
|
190
|
+
const userId = simpleflake();
|
|
191
|
+
await db.users.create({ id: userId.toString(), name: "John" });
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
### Distributed System IDs
|
|
195
|
+
```javascript
|
|
196
|
+
// Each service can generate IDs independently
|
|
197
|
+
const serviceAId = simpleflake(); // Service A
|
|
198
|
+
const serviceBId = simpleflake(); // Service B
|
|
199
|
+
// No coordination needed, guaranteed unique across services
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
### Short URLs
|
|
203
|
+
```javascript
|
|
204
|
+
// Generate compact URL identifiers
|
|
205
|
+
const shortId = simpleflake().toString(36); // "w68acyhy50hc"
|
|
206
|
+
const url = `https://short.ly/${shortId}`;
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
### Event Tracking
|
|
210
|
+
```javascript
|
|
211
|
+
// Time-ordered event IDs for chronological processing
|
|
212
|
+
const eventId = simpleflake();
|
|
213
|
+
await analytics.track({ eventId, userId, action: "click" });
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
## API Reference
|
|
168
217
|
|
|
169
218
|
### Core Functions
|
|
170
219
|
|
|
@@ -183,13 +232,13 @@ const id = simpleflake();
|
|
|
183
232
|
const customId = simpleflake(Date.now(), 12345, Date.UTC(2000, 0, 1));
|
|
184
233
|
```
|
|
185
234
|
|
|
186
|
-
#### `parseSimpleflake(flake):
|
|
235
|
+
#### `parseSimpleflake(flake): SimpleflakeStruct`
|
|
187
236
|
Parses a simpleflake ID into its components.
|
|
188
237
|
|
|
189
238
|
**Parameters:**
|
|
190
239
|
- `flake` (bigint | string | number): The ID to parse
|
|
191
240
|
|
|
192
|
-
**Returns:** Object with `timestamp` and `randomBits` properties (both
|
|
241
|
+
**Returns:** Object with `timestamp` and `randomBits` properties (both bigint)
|
|
193
242
|
|
|
194
243
|
```javascript
|
|
195
244
|
const parsed = parseSimpleflake(4234673179811182512n);
|
|
@@ -239,13 +288,13 @@ console.log(SIMPLEFLAKE_EPOCH); // 946684800000
|
|
|
239
288
|
### TypeScript Types
|
|
240
289
|
|
|
241
290
|
```typescript
|
|
242
|
-
interface
|
|
243
|
-
timestamp:
|
|
244
|
-
randomBits:
|
|
291
|
+
interface SimpleflakeStruct {
|
|
292
|
+
timestamp: bigint; // Unix timestamp as bigint (since 2000)
|
|
293
|
+
randomBits: bigint; // Random component as bigint
|
|
245
294
|
}
|
|
246
295
|
```
|
|
247
296
|
|
|
248
|
-
##
|
|
297
|
+
## Migration Guide
|
|
249
298
|
|
|
250
299
|
### From UUID
|
|
251
300
|
```javascript
|
|
@@ -264,89 +313,33 @@ const id = simpleflake().toString(36); // "w68acyhy50hc" (shorter!)
|
|
|
264
313
|
// Just different bit allocation:
|
|
265
314
|
// - Snowflake: 41 bits timestamp + 10 bits machine + 12 bits sequence
|
|
266
315
|
// - Simpleflake: 41 bits timestamp + 23 bits random
|
|
316
|
+
//
|
|
317
|
+
// *double-check epoch
|
|
267
318
|
```
|
|
268
319
|
|
|
269
|
-
##
|
|
270
|
-
|
|
271
|
-
###
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
### Event Tracking
|
|
294
|
-
```javascript
|
|
295
|
-
// Time-ordered event IDs for chronological processing
|
|
296
|
-
const eventId = simpleflake();
|
|
297
|
-
await analytics.track({ eventId, userId, action: "click" });
|
|
298
|
-
```
|
|
299
|
-
|
|
300
|
-
## 🔧 Development
|
|
301
|
-
|
|
302
|
-
This project is written in TypeScript and includes comprehensive test coverage.
|
|
303
|
-
|
|
304
|
-
```bash
|
|
305
|
-
# Install dependencies
|
|
306
|
-
npm install
|
|
307
|
-
|
|
308
|
-
# Build TypeScript to JavaScript
|
|
309
|
-
npm run build
|
|
310
|
-
|
|
311
|
-
# Run tests (automatically builds first)
|
|
312
|
-
npm test
|
|
313
|
-
|
|
314
|
-
# Run with coverage
|
|
315
|
-
npm run test:coverage
|
|
316
|
-
|
|
317
|
-
# Run all CI tests (coverage)
|
|
318
|
-
npm run test:ci
|
|
319
|
-
|
|
320
|
-
# Type checking
|
|
321
|
-
npm run type-check
|
|
322
|
-
|
|
323
|
-
# Run benchmarks
|
|
324
|
-
npm run benchmark
|
|
325
|
-
|
|
326
|
-
# Clean build artifacts
|
|
327
|
-
npm run clean
|
|
328
|
-
```
|
|
329
|
-
|
|
330
|
-
## 📚 References
|
|
331
|
-
|
|
332
|
-
- **[Original Presentation](http://akmanalp.com/simpleflake_presentation/)** - Introduction to the concept
|
|
333
|
-
- **[Python Implementation](https://simpleflake.readthedocs.org/en/latest/)** - Original reference implementation
|
|
334
|
-
- **[Twitter Snowflake](https://blog.twitter.com/engineering/en_us/a/2010/announcing-snowflake.html)** - Similar distributed ID system
|
|
335
|
-
|
|
336
|
-
## 🆚 Comparison
|
|
337
|
-
|
|
338
|
-
| Feature | Simpleflake | UUID v4 | UUID v7 | Nanoid | KSUID | Twitter Snowflake |
|
|
339
|
-
|---------|-------------|---------|---------|--------|-------|------------------|
|
|
340
|
-
| **Size** | 64-bit | 128-bit | 128-bit | Variable | 160-bit | 64-bit |
|
|
341
|
-
| **Time-ordered** | ✅ Yes | ❌ No | ✅ Yes | ❌ No | ✅ Yes | ✅ Yes |
|
|
342
|
-
| **Distributed** | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes | ⚠️ Needs config |
|
|
343
|
-
| **Dependencies** | ✅ Zero | ❌ crypto | ❌ crypto | ✅ Zero | ❌ crypto | ❌ System clock |
|
|
344
|
-
| **Performance** | 🚀 8.8M/sec | 🐌 ~2M/sec | 🐌 ~2M/sec | 🚀 ~5M/sec | 🐌 ~1M/sec | 🚀 ~10M/sec |
|
|
345
|
-
| **URL-friendly** | ✅ Base36 | ❌ Long hex | ❌ Long hex | ✅ Custom | ✅ Base62 | ✅ Base36 |
|
|
346
|
-
| **Database-friendly** | ✅ Integer | ❌ String | ❌ String | ❌ String | ❌ String | ✅ Integer |
|
|
347
|
-
| **Collision-resistant** | ✅ 8.3M/ms | ✅ ~0 chance | ✅ ~0 chance | ✅ Configurable | ✅ ~0 chance | ✅ 4096/ms |
|
|
348
|
-
|
|
349
|
-
## 🤝 Contributing
|
|
320
|
+
## Comparison
|
|
321
|
+
|
|
322
|
+
### Core Characteristics
|
|
323
|
+
| Library | Size | Time-ordered | Performance |
|
|
324
|
+
|---------|------|--------------|-------------|
|
|
325
|
+
| **Simpleflake** | 64-bit | ✅ Yes | ⚡ 8.8M/sec |
|
|
326
|
+
| UUID v4 | 128-bit | ❌ No | 🔸 ~2M/sec |
|
|
327
|
+
| UUID v7 | 128-bit | ✅ Yes | 🔸 ~2M/sec |
|
|
328
|
+
| Nanoid | Variable | ❌ No | ⚡ ~5M/sec |
|
|
329
|
+
| KSUID | 160-bit | ✅ Yes | 🔸 ~1M/sec |
|
|
330
|
+
| Twitter Snowflake | 64-bit | ✅ Yes | ⚡ ~10M/sec |
|
|
331
|
+
|
|
332
|
+
### Technical Features
|
|
333
|
+
| Library | Dependencies | Database-friendly | URL-friendly | Distributed |
|
|
334
|
+
|---------|--------------|-------------------|--------------|-------------|
|
|
335
|
+
| **Simpleflake** | ✅ Zero | ✅ Integer | ✅ Base36 | ✅ Yes |
|
|
336
|
+
| UUID v4 | ❌ crypto | ❌ String | ❌ Long hex | ✅ Yes |
|
|
337
|
+
| UUID v7 | ❌ crypto | ❌ String | ❌ Long hex | ✅ Yes |
|
|
338
|
+
| Nanoid | ✅ Zero | ❌ String | ✅ Custom | ✅ Yes |
|
|
339
|
+
| KSUID | ❌ crypto | ❌ String | ✅ Base62 | ✅ Yes |
|
|
340
|
+
| Twitter Snowflake | ❌ System clock | ✅ Integer | ✅ Base36 | ⚠️ Needs config |
|
|
341
|
+
|
|
342
|
+
## Contributing
|
|
350
343
|
|
|
351
344
|
1. Fork the repository
|
|
352
345
|
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
|
|
@@ -354,13 +347,13 @@ npm run clean
|
|
|
354
347
|
4. Push to the branch (`git push origin feature/amazing-feature`)
|
|
355
348
|
5. Open a Pull Request
|
|
356
349
|
|
|
357
|
-
##
|
|
350
|
+
## License
|
|
358
351
|
|
|
359
352
|
[MIT](https://raw.githubusercontent.com/leodutra/simpleflakes/master/LICENSE)
|
|
360
353
|
|
|
361
354
|
---
|
|
362
355
|
|
|
363
|
-
##
|
|
356
|
+
## Credits
|
|
364
357
|
|
|
365
358
|
- Original concept by [Mali Akmanalp](http://akmanalp.com/)
|
|
366
359
|
- TypeScript port and optimizations by [Leo Dutra](https://github.com/leodutra)
|
package/dist/simpleflakes.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
declare const SIMPLEFLAKE_EPOCH =
|
|
1
|
+
declare const SIMPLEFLAKE_EPOCH = 946684800000n;
|
|
2
2
|
/**
|
|
3
3
|
* Generates a simpleflake ID
|
|
4
4
|
* @param ts - Timestamp in milliseconds (defaults to current time)
|
|
@@ -6,7 +6,7 @@ declare const SIMPLEFLAKE_EPOCH = 946684800000;
|
|
|
6
6
|
* @param epoch - Epoch timestamp in milliseconds (defaults to SIMPLEFLAKE_EPOCH)
|
|
7
7
|
* @returns Generated simpleflake as a BigInt
|
|
8
8
|
*/
|
|
9
|
-
export declare function simpleflake(ts?: number, randomBits?: number, epoch?: number): bigint;
|
|
9
|
+
export declare function simpleflake(ts?: number | bigint, randomBits?: number | bigint, epoch?: number | bigint): bigint;
|
|
10
10
|
/**
|
|
11
11
|
* Converts a value to binary representation
|
|
12
12
|
* @param value - The value to convert to binary
|
|
@@ -25,27 +25,25 @@ export declare function extractBits(data: bigint | number | string, shift: bigin
|
|
|
25
25
|
/**
|
|
26
26
|
* Structure representing a parsed simpleflake
|
|
27
27
|
*/
|
|
28
|
-
export declare class
|
|
29
|
-
readonly timestamp:
|
|
30
|
-
readonly randomBits:
|
|
31
|
-
constructor(timestamp:
|
|
28
|
+
export declare class SimpleflakeStruct {
|
|
29
|
+
readonly timestamp: bigint;
|
|
30
|
+
readonly randomBits: bigint;
|
|
31
|
+
constructor(timestamp: bigint, randomBits: bigint);
|
|
32
32
|
}
|
|
33
33
|
/**
|
|
34
34
|
* Parses a simpleflake into its components
|
|
35
35
|
* @param flake - The simpleflake to parse
|
|
36
|
-
* @returns
|
|
36
|
+
* @returns SimpleflakeStruct containing timestamp and random bits
|
|
37
37
|
*/
|
|
38
|
-
export declare function parseSimpleflake(flake: bigint | number | string):
|
|
39
|
-
export declare const simpleflakeStruct: typeof SimpleFlakeStruct;
|
|
38
|
+
export declare function parseSimpleflake(flake: bigint | number | string): SimpleflakeStruct;
|
|
40
39
|
export { SIMPLEFLAKE_EPOCH };
|
|
41
40
|
declare const _default: {
|
|
42
|
-
|
|
43
|
-
simpleflakeStruct: typeof SimpleFlakeStruct;
|
|
41
|
+
binary: typeof binary;
|
|
44
42
|
extractBits: typeof extractBits;
|
|
45
43
|
parseSimpleflake: typeof parseSimpleflake;
|
|
46
|
-
|
|
47
|
-
SIMPLEFLAKE_EPOCH: number;
|
|
44
|
+
SIMPLEFLAKE_EPOCH: bigint;
|
|
48
45
|
simpleflake: typeof simpleflake;
|
|
46
|
+
SimpleflakeStruct: typeof SimpleflakeStruct;
|
|
49
47
|
};
|
|
50
48
|
export default _default;
|
|
51
49
|
//# sourceMappingURL=simpleflakes.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"simpleflakes.d.ts","sourceRoot":"","sources":["../src/simpleflakes.ts"],"names":[],"mappings":"AAAA,QAAA,MAAM,iBAAiB,
|
|
1
|
+
{"version":3,"file":"simpleflakes.d.ts","sourceRoot":"","sources":["../src/simpleflakes.ts"],"names":[],"mappings":"AAAA,QAAA,MAAM,iBAAiB,gBAAgB,CAAC;AAYxC;;;;;;GAMG;AACH,wBAAgB,WAAW,CACzB,EAAE,GAAE,MAAM,GAAG,MAAmB,EAChC,UAAU,CAAC,EAAE,MAAM,GAAG,MAAM,EAC5B,KAAK,GAAE,MAAM,GAAG,MAA0B,GACzC,MAAM,CAOR;AAED;;;;;GAKG;AACH,wBAAgB,MAAM,CACpB,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,EAC/B,OAAO,GAAE,OAAc,GACtB,MAAM,CAKR;AAED;;;;;;GAMG;AACH,wBAAgB,WAAW,CACzB,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,EAC9B,KAAK,EAAE,MAAM,GAAG,MAAM,EACtB,MAAM,EAAE,MAAM,GAAG,MAAM,GACtB,MAAM,CAKR;AAED;;GAEG;AACH,qBAAa,iBAAiB;IAC5B,SAAgB,SAAS,EAAE,MAAM,CAAC;IAClC,SAAgB,UAAU,EAAE,MAAM,CAAC;gBAEvB,SAAS,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM;CAOlD;AAED;;;;GAIG;AACH,wBAAgB,gBAAgB,CAC9B,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,GAC9B,iBAAiB,CAWnB;AAGD,OAAO,EAAE,iBAAiB,EAAE,CAAC;;;;;;;;;AAG7B,wBAOE"}
|
package/dist/simpleflakes.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
"use strict";function _define_property(t,
|
|
1
|
+
"use strict";function _define_property(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}Object.defineProperty(exports,"__esModule",{value:!0}),function(e,t){for(var r in t)Object.defineProperty(e,r,{enumerable:!0,get:Object.getOwnPropertyDescriptor(t,r).get})}(exports,{get SIMPLEFLAKE_EPOCH(){return SIMPLEFLAKE_EPOCH},get SimpleflakeStruct(){return SimpleflakeStruct},get binary(){return binary},get default(){return _default},get extractBits(){return extractBits},get parseSimpleflake(){return parseSimpleflake},get simpleflake(){return simpleflake}});const SIMPLEFLAKE_EPOCH=946684800000n;function simpleflake(e=Date.now(),t,r=SIMPLEFLAKE_EPOCH){return BigInt(e)-BigInt(r)<<23n|BigInt(t??Math.round(8388607*Math.random()))}function binary(e,t=!0){let r=BigInt(e).toString(2);return t&&r.length<64?"0000000000000000000000000000000000000000000000000000000000000000".substr(0,64-r.length)+r:r}function extractBits(e,t,r){let n=BigInt(t),i=BigInt(r);return BigInt(e)>>n&(1n<<i)-1n}class SimpleflakeStruct{constructor(e,t){if(_define_property(this,"timestamp",void 0),_define_property(this,"randomBits",void 0),null==e||null==t)throw Error("Missing argument for SimpleflakeStruct.");this.timestamp=e,this.randomBits=t}}function parseSimpleflake(e){return new SimpleflakeStruct(extractBits(e,23n,41n)+SIMPLEFLAKE_EPOCH,extractBits(e,0n,23n))}const _default={binary,extractBits,parseSimpleflake,SIMPLEFLAKE_EPOCH,simpleflake,SimpleflakeStruct};
|
|
2
2
|
|
|
3
3
|
//# sourceMappingURL=simpleflakes.js.map
|
package/dist/simpleflakes.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/simpleflakes.ts"],"names":["SIMPLEFLAKE_EPOCH","
|
|
1
|
+
{"version":3,"sources":["../src/simpleflakes.ts"],"names":["SIMPLEFLAKE_EPOCH","SimpleflakeStruct","binary","extractBits","parseSimpleflake","simpleflake","ts","Date","now","randomBits","epoch","BigInt","Math","round","random","value","padding","binValue","toString","length","CACHE_64_BIT_ZEROS","substr","data","shift","shiftN","lengthN","timestamp","Error","flake"],"mappings":"8UAuGSA,2BAAAA,uBAlCIC,2BAAAA,uBA/BGC,gBAAAA,YAoEhB,iBAAA,cAnDgBC,qBAAAA,iBAgCAC,0BAAAA,sBApEAC,qBAAAA,eAnBhB,MAAML,kBAAoB,YAAA,AAAa,CAAA,CAmBhC,SAASK,YACdC,EAAsBC,KAAKC,GAAG,EAAE,CAChCC,CAA4B,CAC5BC,EAAyBV,iBAAiB,EAI1C,OACE,AAAEW,OAAOL,GAAMK,OAAOD,IApBU,EAAA,AAAG,CAAA,CAqBnCC,OAAOF,GAAcG,KAAKC,KAAK,CA3BR,AA2BSD,QAAAA,KAAKE,MAAM,IAE/C,CAQO,SAASZ,OACda,CAA+B,CAC/BC,EAAmB,CAAA,CAAI,EAEvB,IAAMC,EAAWN,OAAOI,GAAOG,QAAQ,CAAC,GACxC,OAAOF,GAAWC,EAASE,MAAM,CAAG,GAjCpC,AAkCIC,mEAAmBC,MAAM,CAAC,EAAG,GAAKJ,EAASE,MAAM,EAAIF,EACrDA,CACN,CASO,SAASd,YACdmB,CAA8B,CAC9BC,CAAsB,CACtBJ,CAAuB,EAEvB,IAAMK,EAASb,OAAOY,GAChBE,EAAUd,OAAOQ,GAEvB,OAAO,AAACR,OAAOW,IAASE,EAAW,AAAC,CAAA,CAAA,AAAE,CAAA,EAAIC,CAAM,EAAK,CAAA,AAAE,CAAA,AACzD,CAKO,MAAMxB,kBAIX,YAAYyB,CAAiB,CAAEjB,CAAkB,CAAE,CACjD,GAJF,sBAAgBiB,YAAhB,KAAA,GACA,sBAAgBjB,aAAhB,KAAA,GAGMiB,AAAa,MAAbA,GAAqBjB,AAAc,MAAdA,EACvB,MAAM,AAAIkB,MAAM,0CAElB,CAAA,IAAI,CAACD,SAAS,CAAGA,EACjB,IAAI,CAACjB,UAAU,CAAGA,CACpB,CACF,CAOO,SAASL,iBACdwB,CAA+B,EAE/B,OAAO,IAAI3B,kBAETE,YACEyB,EAtF8B,EAAA,AAAG,CAAA,CAJF,EAAA,AAAG,CAAA,EA6FhC5B,kBAEJG,YAAYyB,EA5FiB,CAAA,AAAE,CAAA,CAFD,EAAA,AAAG,CAAA,EAgGrC,OAMA,SAAe,CACb1B,OACAC,YACAC,iBACAJ,kBACAK,YACAJ,iBACF","file":"simpleflakes.js","sourcesContent":["const SIMPLEFLAKE_EPOCH = 946684800000n; // Date.UTC(2000, 0, 1) == epoch ms, since 1 Jan 2000 00:00\nconst UNSIGNED_23BIT_MAX = 8388607; // (Math.pow(2, 23) - 1) >> 0\n\nconst SIMPLEFLAKE_TIMESTAMP_LENGTH = 41n;\nconst SIMPLEFLAKE_RANDOM_LENGTH = 23n;\n\nconst SIMPLEFLAKE_RANDOM_SHIFT = 0n;\nconst SIMPLEFLAKE_TIMESTAMP_SHIFT = 23n;\n\nconst CACHE_64_BIT_ZEROS =\n \"0000000000000000000000000000000000000000000000000000000000000000\";\n\n/**\n * Generates a simpleflake ID\n * @param ts - Timestamp in milliseconds (defaults to current time)\n * @param randomBits - Random bits for the ID (defaults to a random value)\n * @param epoch - Epoch timestamp in milliseconds (defaults to SIMPLEFLAKE_EPOCH)\n * @returns Generated simpleflake as a BigInt\n */\nexport function simpleflake(\n ts: number | bigint = Date.now(),\n randomBits?: number | bigint,\n epoch: number | bigint = SIMPLEFLAKE_EPOCH\n): bigint {\n // Use bitwise OR instead of addition since bit ranges don't overlap\n\n return (\n ((BigInt(ts) - BigInt(epoch)) << SIMPLEFLAKE_TIMESTAMP_SHIFT) |\n BigInt(randomBits ?? Math.round(Math.random() * UNSIGNED_23BIT_MAX))\n );\n}\n\n/**\n * Converts a value to binary representation\n * @param value - The value to convert to binary\n * @param padding - Whether to pad to 64 bits (defaults to true)\n * @returns Binary string representation\n */\nexport function binary(\n value: bigint | number | string,\n padding: boolean = true\n): string {\n const binValue = BigInt(value).toString(2);\n return padding && binValue.length < 64\n ? CACHE_64_BIT_ZEROS.substr(0, 64 - binValue.length) + binValue\n : binValue;\n}\n\n/**\n * Extracts bits from a data value\n * @param data - The data to extract bits from\n * @param shift - Number of bits to shift\n * @param length - Number of bits to extract\n * @returns Extracted bits as a BigInt\n */\nexport function extractBits(\n data: bigint | number | string,\n shift: bigint | number,\n length: bigint | number\n): bigint {\n const shiftN = BigInt(shift);\n const lengthN = BigInt(length);\n // Optimize: shift right first, then mask (avoids creating large bitmask)\n return (BigInt(data) >> shiftN) & ((1n << lengthN) - 1n);\n}\n\n/**\n * Structure representing a parsed simpleflake\n */\nexport class SimpleflakeStruct {\n public readonly timestamp: bigint;\n public readonly randomBits: bigint;\n\n constructor(timestamp: bigint, randomBits: bigint) {\n if (timestamp == null || randomBits == null) {\n throw new Error(\"Missing argument for SimpleflakeStruct.\");\n }\n this.timestamp = timestamp;\n this.randomBits = randomBits;\n }\n}\n\n/**\n * Parses a simpleflake into its components\n * @param flake - The simpleflake to parse\n * @returns SimpleflakeStruct containing timestamp and random bits\n */\nexport function parseSimpleflake(\n flake: bigint | number | string\n): SimpleflakeStruct {\n return new SimpleflakeStruct(\n // timestamp\n extractBits(\n flake,\n SIMPLEFLAKE_TIMESTAMP_SHIFT,\n SIMPLEFLAKE_TIMESTAMP_LENGTH\n ) + SIMPLEFLAKE_EPOCH,\n // random bits\n extractBits(flake, SIMPLEFLAKE_RANDOM_SHIFT, SIMPLEFLAKE_RANDOM_LENGTH)\n );\n}\n\n// Export constants\nexport { SIMPLEFLAKE_EPOCH };\n\n// Default export for CommonJS compatibility\nexport default {\n binary,\n extractBits,\n parseSimpleflake,\n SIMPLEFLAKE_EPOCH,\n simpleflake,\n SimpleflakeStruct,\n};\n"]}
|
package/package.json
CHANGED