Skip to content

Commit 4c6a1dc

Browse files
Merge pull request #109 from ulid/maint/refresh
maint(dependencies): Cleanup, update dependencies
2 parents c5c4231 + d2d6af3 commit 4c6a1dc

8 files changed

Lines changed: 4681 additions & 1277 deletions

File tree

.github/workflows/test.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: Tests
2+
3+
on:
4+
push:
5+
pull_request:
6+
7+
jobs:
8+
nodejs:
9+
runs-on: ubuntu-latest
10+
strategy:
11+
matrix:
12+
node-version: [16.x, 22.x]
13+
steps:
14+
- uses: actions/checkout@v2
15+
- name: Node.js specs ${{ matrix.node-version }}
16+
uses: actions/setup-node@v1
17+
with:
18+
node-version: ${{ matrix.node-version }}
19+
- run: npm ci
20+
- run: npm run build
21+
- run: npm run test
22+
types:
23+
runs-on: ubuntu-latest
24+
strategy:
25+
matrix:
26+
node-version: [22.x]
27+
steps:
28+
- uses: actions/checkout@v2
29+
- name: Type checks ${{ matrix.node-version }}
30+
uses: actions/setup-node@v1
31+
with:
32+
node-version: ${{ matrix.node-version }}
33+
- run: npm ci
34+
- run: npm run build
35+
- run: npx --yes @arethetypeswrong/cli@latest --pack .

dist/index.esm.js

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
function createError(message) {
2-
var err = new Error(message);
2+
const err = new Error(message);
33
err.source = "ulid";
44
return err;
55
}
66
// These values should NEVER change. If
77
// 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;
8+
const ENCODING = "0123456789ABCDEFGHJKMNPQRSTVWXYZ"; // Crockford's Base32
9+
const ENCODING_LEN = ENCODING.length;
10+
const TIME_MAX = Math.pow(2, 48) - 1;
11+
const TIME_LEN = 10;
12+
const RANDOM_LEN = 16;
1313
function replaceCharAt(str, index, char) {
1414
if (index > str.length - 1) {
1515
return str;
1616
}
1717
return str.substr(0, index) + char + str.substr(index + 1);
1818
}
1919
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;
20+
let done = undefined;
21+
let index = str.length;
22+
let char;
23+
let charIndex;
24+
const maxCharIndex = ENCODING_LEN - 1;
2525
while (!done && index-- >= 0) {
2626
char = str[index];
2727
charIndex = ENCODING.indexOf(char);
@@ -40,7 +40,7 @@ function incrementBase32(str) {
4040
throw createError("cannot increment this string");
4141
}
4242
function randomChar(prng) {
43-
var rand = Math.floor(prng() * ENCODING_LEN);
43+
let rand = Math.floor(prng() * ENCODING_LEN);
4444
if (rand === ENCODING_LEN) {
4545
rand = ENCODING_LEN - 1;
4646
}
@@ -59,8 +59,8 @@ function encodeTime(now, len) {
5959
if (Number.isInteger(now) === false) {
6060
throw createError("time must be an integer");
6161
}
62-
var mod = void 0;
63-
var str = "";
62+
let mod;
63+
let str = "";
6464
for (; len > 0; len--) {
6565
mod = now % ENCODING_LEN;
6666
str = ENCODING.charAt(mod) + str;
@@ -69,7 +69,7 @@ function encodeTime(now, len) {
6969
return str;
7070
}
7171
function encodeRandom(len, prng) {
72-
var str = "";
72+
let str = "";
7373
for (; len > 0; len--) {
7474
str = randomChar(prng) + str;
7575
}
@@ -79,47 +79,47 @@ function decodeTime(id) {
7979
if (id.length !== TIME_LEN + RANDOM_LEN) {
8080
throw createError("malformed ulid");
8181
}
82-
var time = id.substr(0, TIME_LEN).split("").reverse().reduce(function (carry, char, index) {
83-
var encodingIndex = ENCODING.indexOf(char);
82+
var time = id
83+
.substr(0, TIME_LEN)
84+
.split("")
85+
.reverse()
86+
.reduce((carry, char, index) => {
87+
const encodingIndex = ENCODING.indexOf(char);
8488
if (encodingIndex === -1) {
8589
throw createError("invalid character found: " + char);
8690
}
87-
return carry += encodingIndex * Math.pow(ENCODING_LEN, index);
91+
return (carry += encodingIndex * Math.pow(ENCODING_LEN, index));
8892
}, 0);
8993
if (time > TIME_MAX) {
9094
throw createError("malformed ulid, timestamp too large");
9195
}
9296
return time;
9397
}
94-
function detectPrng() {
95-
var allowInsecure = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
96-
var root = arguments[1];
97-
98+
function detectPrng(allowInsecure = false, root) {
9899
if (!root) {
99100
root = typeof window !== "undefined" ? window : null;
100101
}
101-
var browserCrypto = root && (root.crypto || root.msCrypto);
102+
const browserCrypto = root && (root.crypto || root.msCrypto);
102103
if (browserCrypto) {
103-
return function () {
104-
var buffer = new Uint8Array(1);
104+
return () => {
105+
const buffer = new Uint8Array(1);
105106
browserCrypto.getRandomValues(buffer);
106107
return buffer[0] / 0xff;
107108
};
108-
} else {
109+
}
110+
else {
109111
try {
110-
var nodeCrypto = require("crypto");
111-
return function () {
112-
return nodeCrypto.randomBytes(1).readUInt8() / 0xff;
113-
};
114-
} catch (e) {}
112+
const nodeCrypto = require("crypto");
113+
return () => nodeCrypto.randomBytes(1).readUInt8() / 0xff;
114+
}
115+
catch (e) { }
115116
}
116117
if (allowInsecure) {
117118
try {
118119
console.error("secure crypto unusable, falling back to insecure Math.random()!");
119-
} catch (e) {}
120-
return function () {
121-
return Math.random();
122-
};
120+
}
121+
catch (e) { }
122+
return () => Math.random();
123123
}
124124
throw createError("secure crypto unusable, insecure Math.random not allowed");
125125
}
@@ -138,21 +138,21 @@ function monotonicFactory(currPrng) {
138138
if (!currPrng) {
139139
currPrng = detectPrng();
140140
}
141-
var lastTime = 0;
142-
var lastRandom = void 0;
141+
let lastTime = 0;
142+
let lastRandom;
143143
return function ulid(seedTime) {
144144
if (isNaN(seedTime)) {
145145
seedTime = Date.now();
146146
}
147147
if (seedTime <= lastTime) {
148-
var incrementedRandom = lastRandom = incrementBase32(lastRandom);
148+
const incrementedRandom = (lastRandom = incrementBase32(lastRandom));
149149
return encodeTime(lastTime, TIME_LEN) + incrementedRandom;
150150
}
151151
lastTime = seedTime;
152-
var newRandom = lastRandom = encodeRandom(RANDOM_LEN, currPrng);
152+
const newRandom = (lastRandom = encodeRandom(RANDOM_LEN, currPrng));
153153
return encodeTime(seedTime, TIME_LEN) + newRandom;
154154
};
155155
}
156-
var ulid = factory();
156+
const ulid = factory();
157157

158-
export { replaceCharAt, incrementBase32, randomChar, encodeTime, encodeRandom, decodeTime, detectPrng, factory, monotonicFactory, ulid };
158+
export { decodeTime, detectPrng, encodeRandom, encodeTime, factory, incrementBase32, monotonicFactory, randomChar, replaceCharAt, ulid };

0 commit comments

Comments
 (0)