Skip to content

Commit 9cdcf12

Browse files
committed
Improve exporting strategy
1 parent bcc4856 commit 9cdcf12

File tree

9 files changed

+60
-33
lines changed

9 files changed

+60
-33
lines changed
File renamed without changes.

index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
interface PRNG {
1+
export interface PRNG {
22
(): number
33
}
44

5-
interface ULID {
5+
export interface ULID {
66
(seedTime?: number): string
77
}
88

9-
interface LibError extends Error {
9+
export interface LibError extends Error {
1010
source: string
1111
}
1212

lib/index.d.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export interface PRNG {
2+
(): number;
3+
}
4+
export interface ULID {
5+
(seedTime?: number): string;
6+
}
7+
export interface LibError extends Error {
8+
source: string;
9+
}
10+
export declare function replaceCharAt(str: string, index: number, char: string): string;
11+
export declare function incrementBase32(str: string): string;
12+
export declare function randomChar(prng: PRNG): string;
13+
export declare function encodeTime(now: number, len: number): string;
14+
export declare function encodeRandom(len: number, prng: PRNG): string;
15+
export declare function decodeTime(id: string): number;
16+
export declare function detectPrng(allowInsecure?: boolean, root?: any): PRNG;
17+
export declare function factory(currPrng?: PRNG): ULID;
18+
export declare function monotonicFactory(currPrng?: PRNG): ULID;
19+
export declare const ulid: ULID;

dist/ulid.es6.js renamed to lib/index.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ const ENCODING_LEN = ENCODING.length;
1010
const TIME_MAX = Math.pow(2, 48) - 1;
1111
const TIME_LEN = 10;
1212
const RANDOM_LEN = 16;
13-
function replaceCharAt(str, index, char) {
13+
export 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
}
19-
function incrementBase32(str) {
19+
export function incrementBase32(str) {
2020
let done = undefined;
2121
let index = str.length;
2222
let char;
@@ -39,14 +39,14 @@ function incrementBase32(str) {
3939
}
4040
throw createError("cannot increment this string");
4141
}
42-
function randomChar(prng) {
42+
export function randomChar(prng) {
4343
let rand = Math.floor(prng() * ENCODING_LEN);
4444
if (rand === ENCODING_LEN) {
4545
rand = ENCODING_LEN - 1;
4646
}
4747
return ENCODING.charAt(rand);
4848
}
49-
function encodeTime(now, len) {
49+
export function encodeTime(now, len) {
5050
if (isNaN(now)) {
5151
throw new Error(now + " must be a number");
5252
}
@@ -68,14 +68,14 @@ function encodeTime(now, len) {
6868
}
6969
return str;
7070
}
71-
function encodeRandom(len, prng) {
71+
export function encodeRandom(len, prng) {
7272
let str = "";
7373
for (; len > 0; len--) {
7474
str = randomChar(prng) + str;
7575
}
7676
return str;
7777
}
78-
function decodeTime(id) {
78+
export function decodeTime(id) {
7979
if (id.length !== TIME_LEN + RANDOM_LEN) {
8080
throw createError("malformed ulid");
8181
}
@@ -95,7 +95,7 @@ function decodeTime(id) {
9595
}
9696
return time;
9797
}
98-
function detectPrng(allowInsecure = false, root) {
98+
export function detectPrng(allowInsecure = false, root) {
9999
if (!root) {
100100
root = typeof window !== "undefined" ? window : null;
101101
}
@@ -122,7 +122,7 @@ function detectPrng(allowInsecure = false, root) {
122122
}
123123
throw createError("secure crypto unusable, insecure Math.random not allowedW");
124124
}
125-
function factory(currPrng) {
125+
export function factory(currPrng) {
126126
if (!currPrng) {
127127
currPrng = detectPrng();
128128
}
@@ -133,7 +133,7 @@ function factory(currPrng) {
133133
return encodeTime(seedTime, TIME_LEN) + encodeRandom(RANDOM_LEN, currPrng);
134134
};
135135
}
136-
function monotonicFactory(currPrng) {
136+
export function monotonicFactory(currPrng) {
137137
if (!currPrng) {
138138
currPrng = detectPrng();
139139
}
@@ -152,6 +152,4 @@ function monotonicFactory(currPrng) {
152152
return encodeTime(seedTime, TIME_LEN) + newRandom;
153153
};
154154
}
155-
const ulid = factory();
156-
157-
export { replaceCharAt, incrementBase32, randomChar, encodeTime, encodeRandom, decodeTime, detectPrng, factory, monotonicFactory, ulid };
155+
export const ulid = factory();
File renamed without changes.

package.json

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
{
22
"name": "ulid",
33
"version": "2.0.1",
4-
"description": "A universally unique lexicographically sortable identifier generator",
5-
"main": "./dist/ulid.umd.js",
6-
"module": "./dist/ulid.es6.js",
4+
"description": "A universally-unique, lexicographically-sortable, identifier generator",
5+
"main": "./lib/index.umd.js",
6+
"module": "./lib/index.js",
7+
"types": "./lib/index.d.ts",
78
"repository": {
89
"type": "git",
910
"url": "git+https://github.com/alizain/ulid.git"
@@ -30,9 +31,15 @@
3031
"typescript": "^2.5.3"
3132
},
3233
"scripts": {
33-
"build": "./node_modules/.bin/rollup -c",
34+
"ts": "./node_modules/.bin/tsc -p .",
35+
"umd": "./node_modules/.bin/rollup -c",
36+
"build": "npm run ts && npm run umd",
3437
"test": "./node_modules/.bin/istanbul cover node_modules/mocha/bin/_mocha -- -R spec",
3538
"perf": "./node_modules/.bin/matcha perf.js"
3639
},
37-
"bin": "./cli.js"
40+
"files": [
41+
"bin",
42+
"lib"
43+
],
44+
"bin": "./bin/cli.js"
3845
}

rollup.config.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,20 @@ const defaultPlugins = [
1212
typescript({ typescript: compiler })
1313
]
1414

15-
const es6Config = Object.assign({}, defaultConfig, {
16-
output: {
17-
format: 'es',
18-
file: './dist/ulid.es6.js'
19-
},
20-
plugins: [
21-
...defaultPlugins
22-
]
23-
})
15+
// const es6Config = Object.assign({}, defaultConfig, {
16+
// output: {
17+
// format: 'es',
18+
// file: './lib/ulid.es6.js'
19+
// },
20+
// plugins: [
21+
// ...defaultPlugins
22+
// ]
23+
// })
2424

2525
const umdConfig = Object.assign({}, defaultConfig, {
2626
output: {
2727
format: 'umd',
28-
file: './dist/ulid.umd.js'
28+
file: './lib/index.umd.js'
2929
},
3030
plugins: [
3131
...defaultPlugins,
@@ -34,6 +34,6 @@ const umdConfig = Object.assign({}, defaultConfig, {
3434
})
3535

3636
export default [
37-
es6Config,
37+
// es6Config,
3838
umdConfig
3939
]

test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
var assert = require('assert')
22
var lolex = require('lolex')
3-
var ULID = require('./dist/ulid.umd.js')
3+
var ULID = require('./lib/index.umd.js')
44
var ulid = ULID.factory()
55

66

tsconfig.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44
"ES5", "ES6", "ES7", "DOM"
55
],
66
"types": [ "Node" ],
7+
"declaration": true,
78
"target": "ES6",
8-
"module": "ES6"
9+
"module": "ES6",
10+
"outDir": "./lib"
911
},
12+
"compileOnSave": true,
1013
"strict": true
1114
}

0 commit comments

Comments
 (0)