Skip to content

Commit 41e63b3

Browse files
Upgrade rollup generation
1 parent 1455c01 commit 41e63b3

9 files changed

Lines changed: 1732 additions & 390 deletions

File tree

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99
runs-on: ubuntu-latest
1010
strategy:
1111
matrix:
12-
node-version: [18.x, 22.x]
12+
node-version: [14.x, 16.x, 22.x]
1313
steps:
1414
- uses: actions/checkout@v2
1515
- name: Node.js specs ${{ matrix.node-version }}

dist/index.esm.js

Lines changed: 51 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,38 @@
1+
Object.defineProperty(exports, "__esModule", { value: true });
2+
exports.ulid = undefined;
3+
exports.replaceCharAt = replaceCharAt;
4+
exports.incrementBase32 = incrementBase32;
5+
exports.randomChar = randomChar;
6+
exports.encodeTime = encodeTime;
7+
exports.encodeRandom = encodeRandom;
8+
exports.decodeTime = decodeTime;
9+
exports.detectPrng = detectPrng;
10+
exports.factory = factory;
11+
exports.monotonicFactory = monotonicFactory;
112
function createError(message) {
2-
var err = new Error(message);
13+
const err = new Error(message);
314
err.source = "ulid";
415
return err;
516
}
617
// These values should NEVER change. If
718
// they do, we're no longer making ulids!
8-
var ENCODING = "0123456789ABCDEFGHJKMNPQRSTVWXYZ"; // Crockford's Base32
9-
var ENCODING_LEN = ENCODING.length;
10-
var TIME_MAX = Math.pow(2, 48) - 1;
11-
var TIME_LEN = 10;
12-
var RANDOM_LEN = 16;
19+
const ENCODING = "0123456789ABCDEFGHJKMNPQRSTVWXYZ"; // Crockford's Base32
20+
const ENCODING_LEN = ENCODING.length;
21+
const TIME_MAX = Math.pow(2, 48) - 1;
22+
const TIME_LEN = 10;
23+
const RANDOM_LEN = 16;
1324
function replaceCharAt(str, index, char) {
1425
if (index > str.length - 1) {
1526
return str;
1627
}
1728
return str.substr(0, index) + char + str.substr(index + 1);
1829
}
1930
function incrementBase32(str) {
20-
var done = undefined;
21-
var index = str.length;
22-
var char = void 0;
23-
var charIndex = void 0;
24-
var maxCharIndex = ENCODING_LEN - 1;
31+
let done = undefined;
32+
let index = str.length;
33+
let char;
34+
let charIndex;
35+
const maxCharIndex = ENCODING_LEN - 1;
2536
while (!done && index-- >= 0) {
2637
char = str[index];
2738
charIndex = ENCODING.indexOf(char);
@@ -40,7 +51,7 @@ function incrementBase32(str) {
4051
throw createError("cannot increment this string");
4152
}
4253
function randomChar(prng) {
43-
var rand = Math.floor(prng() * ENCODING_LEN);
54+
let rand = Math.floor(prng() * ENCODING_LEN);
4455
if (rand === ENCODING_LEN) {
4556
rand = ENCODING_LEN - 1;
4657
}
@@ -59,8 +70,8 @@ function encodeTime(now, len) {
5970
if (Number.isInteger(now) === false) {
6071
throw createError("time must be an integer");
6172
}
62-
var mod = void 0;
63-
var str = "";
73+
let mod;
74+
let str = "";
6475
for (; len > 0; len--) {
6576
mod = now % ENCODING_LEN;
6677
str = ENCODING.charAt(mod) + str;
@@ -69,7 +80,7 @@ function encodeTime(now, len) {
6980
return str;
7081
}
7182
function encodeRandom(len, prng) {
72-
var str = "";
83+
let str = "";
7384
for (; len > 0; len--) {
7485
str = randomChar(prng) + str;
7586
}
@@ -79,47 +90,47 @@ function decodeTime(id) {
7990
if (id.length !== TIME_LEN + RANDOM_LEN) {
8091
throw createError("malformed ulid");
8192
}
82-
var time = id.substr(0, TIME_LEN).split("").reverse().reduce(function (carry, char, index) {
83-
var encodingIndex = ENCODING.indexOf(char);
93+
var time = id
94+
.substr(0, TIME_LEN)
95+
.split("")
96+
.reverse()
97+
.reduce((carry, char, index) => {
98+
const encodingIndex = ENCODING.indexOf(char);
8499
if (encodingIndex === -1) {
85100
throw createError("invalid character found: " + char);
86101
}
87-
return carry += encodingIndex * Math.pow(ENCODING_LEN, index);
102+
return (carry += encodingIndex * Math.pow(ENCODING_LEN, index));
88103
}, 0);
89104
if (time > TIME_MAX) {
90105
throw createError("malformed ulid, timestamp too large");
91106
}
92107
return time;
93108
}
94-
function detectPrng() {
95-
var allowInsecure = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
96-
var root = arguments[1];
97-
109+
function detectPrng(allowInsecure = false, root) {
98110
if (!root) {
99111
root = typeof window !== "undefined" ? window : null;
100112
}
101-
var browserCrypto = root && (root.crypto || root.msCrypto);
113+
const browserCrypto = root && (root.crypto || root.msCrypto);
102114
if (browserCrypto) {
103-
return function () {
104-
var buffer = new Uint8Array(1);
115+
return () => {
116+
const buffer = new Uint8Array(1);
105117
browserCrypto.getRandomValues(buffer);
106118
return buffer[0] / 0xff;
107119
};
108-
} else {
120+
}
121+
else {
109122
try {
110-
var nodeCrypto = require("crypto");
111-
return function () {
112-
return nodeCrypto.randomBytes(1).readUInt8() / 0xff;
113-
};
114-
} catch (e) {}
123+
const nodeCrypto = require("crypto");
124+
return () => nodeCrypto.randomBytes(1).readUInt8() / 0xff;
125+
}
126+
catch (e) { }
115127
}
116128
if (allowInsecure) {
117129
try {
118130
console.error("secure crypto unusable, falling back to insecure Math.random()!");
119-
} catch (e) {}
120-
return function () {
121-
return Math.random();
122-
};
131+
}
132+
catch (e) { }
133+
return () => Math.random();
123134
}
124135
throw createError("secure crypto unusable, insecure Math.random not allowed");
125136
}
@@ -138,21 +149,19 @@ function monotonicFactory(currPrng) {
138149
if (!currPrng) {
139150
currPrng = detectPrng();
140151
}
141-
var lastTime = 0;
142-
var lastRandom = void 0;
152+
let lastTime = 0;
153+
let lastRandom;
143154
return function ulid(seedTime) {
144155
if (isNaN(seedTime)) {
145156
seedTime = Date.now();
146157
}
147158
if (seedTime <= lastTime) {
148-
var incrementedRandom = lastRandom = incrementBase32(lastRandom);
159+
const incrementedRandom = (lastRandom = incrementBase32(lastRandom));
149160
return encodeTime(lastTime, TIME_LEN) + incrementedRandom;
150161
}
151162
lastTime = seedTime;
152-
var newRandom = lastRandom = encodeRandom(RANDOM_LEN, currPrng);
163+
const newRandom = (lastRandom = encodeRandom(RANDOM_LEN, currPrng));
153164
return encodeTime(seedTime, TIME_LEN) + newRandom;
154165
};
155166
}
156-
var ulid = factory();
157-
158-
export { replaceCharAt, incrementBase32, randomChar, encodeTime, encodeRandom, decodeTime, detectPrng, factory, monotonicFactory, ulid };
167+
exports.ulid = factory();

dist/index.js

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.ulid = void 0;
4+
exports.replaceCharAt = replaceCharAt;
5+
exports.incrementBase32 = incrementBase32;
6+
exports.randomChar = randomChar;
7+
exports.encodeTime = encodeTime;
8+
exports.encodeRandom = encodeRandom;
9+
exports.decodeTime = decodeTime;
10+
exports.detectPrng = detectPrng;
11+
exports.factory = factory;
12+
exports.monotonicFactory = monotonicFactory;
113
function createError(message) {
214
const err = new Error(message);
315
err.source = "ulid";
@@ -10,13 +22,13 @@ const ENCODING_LEN = ENCODING.length;
1022
const TIME_MAX = Math.pow(2, 48) - 1;
1123
const TIME_LEN = 10;
1224
const RANDOM_LEN = 16;
13-
export function replaceCharAt(str, index, char) {
25+
function replaceCharAt(str, index, char) {
1426
if (index > str.length - 1) {
1527
return str;
1628
}
1729
return str.substr(0, index) + char + str.substr(index + 1);
1830
}
19-
export function incrementBase32(str) {
31+
function incrementBase32(str) {
2032
let done = undefined;
2133
let index = str.length;
2234
let char;
@@ -39,14 +51,14 @@ export function incrementBase32(str) {
3951
}
4052
throw createError("cannot increment this string");
4153
}
42-
export function randomChar(prng) {
54+
function randomChar(prng) {
4355
let rand = Math.floor(prng() * ENCODING_LEN);
4456
if (rand === ENCODING_LEN) {
4557
rand = ENCODING_LEN - 1;
4658
}
4759
return ENCODING.charAt(rand);
4860
}
49-
export function encodeTime(now, len) {
61+
function encodeTime(now, len) {
5062
if (isNaN(now)) {
5163
throw new Error(now + " must be a number");
5264
}
@@ -68,14 +80,14 @@ export function encodeTime(now, len) {
6880
}
6981
return str;
7082
}
71-
export function encodeRandom(len, prng) {
83+
function encodeRandom(len, prng) {
7284
let str = "";
7385
for (; len > 0; len--) {
7486
str = randomChar(prng) + str;
7587
}
7688
return str;
7789
}
78-
export function decodeTime(id) {
90+
function decodeTime(id) {
7991
if (id.length !== TIME_LEN + RANDOM_LEN) {
8092
throw createError("malformed ulid");
8193
}
@@ -95,7 +107,7 @@ export function decodeTime(id) {
95107
}
96108
return time;
97109
}
98-
export function detectPrng(allowInsecure = false, root) {
110+
function detectPrng(allowInsecure = false, root) {
99111
if (!root) {
100112
root = typeof window !== "undefined" ? window : null;
101113
}
@@ -123,7 +135,7 @@ export function detectPrng(allowInsecure = false, root) {
123135
}
124136
throw createError("secure crypto unusable, insecure Math.random not allowed");
125137
}
126-
export function factory(currPrng) {
138+
function factory(currPrng) {
127139
if (!currPrng) {
128140
currPrng = detectPrng();
129141
}
@@ -134,7 +146,7 @@ export function factory(currPrng) {
134146
return encodeTime(seedTime, TIME_LEN) + encodeRandom(RANDOM_LEN, currPrng);
135147
};
136148
}
137-
export function monotonicFactory(currPrng) {
149+
function monotonicFactory(currPrng) {
138150
if (!currPrng) {
139151
currPrng = detectPrng();
140152
}
@@ -153,4 +165,4 @@ export function monotonicFactory(currPrng) {
153165
return encodeTime(seedTime, TIME_LEN) + newRandom;
154166
};
155167
}
156-
export const ulid = factory();
168+
exports.ulid = factory();

0 commit comments

Comments
 (0)