Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support es modules #18

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions index.esm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* cuid.js
* Collision-resistant UID generator for browsers and node.
* Sequential for fast db lookups and recency sorting.
* Safe for element IDs and server-side lookups.
*
* Extracted from CLCTR
*
* Copyright (c) Eric Elliott 2012
* MIT License
*/

import fingerprint from '#fingerprint';
import createCuid from './lib/cuid';

const cuid = createCuid(fingerprint);

export default cuid;
51 changes: 4 additions & 47 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,52 +10,9 @@
* MIT License
*/

var fingerprint = require('./lib/fingerprint.js');
var isCuid = require('./lib/is-cuid.js');
var pad = require('./lib/pad.js');
import fingerprint from './lib/fingerprint';
import createCuid from './lib/cuid';

var c = 0,
blockSize = 4,
base = 36,
discreteValues = Math.pow(base, blockSize);
const cuid = createCuid(fingerprint);

function randomBlock () {
return pad((Math.random() *
discreteValues << 0)
.toString(base), blockSize);
}

function safeCounter () {
c = c < discreteValues ? c : 0;
c++; // this is not subliminal
return c - 1;
}

function cuid () {
// Starting with a lowercase letter makes
// it HTML element ID friendly.
var letter = 'c', // hard-coded allows for sequential access

// timestamp
// warning: this exposes the exact date and time
// that the uid was created.
timestamp = new Date().getTime().toString(base),

// Prevent same-machine collisions.
counter = pad(safeCounter().toString(base), blockSize),

// A few chars to generate distinct ids for different
// clients (so different computers are far less
// likely to generate the same id)
print = fingerprint(),

// Grab some more chars from Math.random()
random = randomBlock() + randomBlock();

return letter + timestamp + counter + print + random;
}

cuid.fingerprint = fingerprint;
cuid.isCuid = isCuid;

module.exports = cuid;
export default cuid;
64 changes: 64 additions & 0 deletions lib/cuid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* cuid.js
* Collision-resistant UID generator for browsers and node.
* Sequential for fast db lookups and recency sorting.
* Safe for element IDs and server-side lookups.
*
* Extracted from CLCTR
*
* Copyright (c) Eric Elliott 2012
* MIT License
*/

import isCuid from './is-cuid';
import pad from './pad';

export default function createCuid (fingerprint) {
const blockSize = 4,
base = 36,
discreteValues = Math.pow(base, blockSize);

let c = 0;

function randomBlock () {
return pad((Math.random() *
discreteValues << 0)
.toString(base), blockSize);
}

function safeCounter () {
c = c < discreteValues ? c : 0;
c++; // this is not subliminal
return c - 1;
}

function cuid () {
// Starting with a lowercase letter makes
// it HTML element ID friendly.
var letter = 'c', // hard-coded allows for sequential access

// timestamp
// warning: this exposes the exact date and time
// that the uid was created.
timestamp = new Date().getTime().toString(base),

// Prevent same-machine collisions.
counter = pad(safeCounter().toString(base), blockSize),

// A few chars to generate distinct ids for different
// clients (so different computers are far less
// likely to generate the same id)
print = fingerprint(),

// Grab some more chars from Math.random()
random = randomBlock() + randomBlock();

return letter + timestamp + counter + print + random;
}

cuid.fingerprint = fingerprint;
cuid.isCuid = isCuid;

return cuid;
}

6 changes: 3 additions & 3 deletions lib/fingerprint.browser.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var pad = require('./pad.js');
import pad from './pad';

var env = typeof window === 'object' ? window : self;
var globalCount = 0;
Expand All @@ -10,6 +10,6 @@ var clientId = pad((mimeTypesLength +
navigator.userAgent.length).toString(36) +
globalCount.toString(36), 4);

module.exports = function fingerprint () {
export default function fingerprint () {
return clientId;
};
}
8 changes: 4 additions & 4 deletions lib/fingerprint.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var pad = require('./pad.js');
var os = require('os');
import pad from './pad';
import os from 'os';

function getHostname () {
try {
Expand Down Expand Up @@ -30,6 +30,6 @@ var padding = 2,
.toString(36),
padding);

module.exports = function fingerprint () {
export default function fingerprint () {
return pid + hostId;
};
}
6 changes: 3 additions & 3 deletions lib/fingerprint.react-native.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
var pad = require('./pad.js');
import pad from './pad';

var globalCount = Object.keys(global);
var clientId = pad(globalCount.toString(36), 4);

module.exports = function fingerprint () {
export default function fingerprint () {
return clientId;
};
}
4 changes: 2 additions & 2 deletions lib/is-cuid.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
* @param {unknown} value
* @returns
*/
module.exports = function isCuid (value) {
export default function isCuid (value) {
return typeof value === 'string' && (/^c[a-z0-9]{20,32}$/).test(value);
};
}
4 changes: 2 additions & 2 deletions lib/pad.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = function pad (num, size) {
export default function pad (num, size) {
var s = '000000000' + num;
return s.substr(s.length - size);
};
}
32 changes: 25 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,29 @@
"name": "Bugsnag",
"url": "https://bugsnag.com"
},
"main": "index.js",
"main": "dist/index.js",
"types": "cuid.d.ts",
"exports": {
".": {
"import": "./index.esm.js",
"require": "./dist/index.js"
},
"./lib/fingerprint": {
"browser": "./lib/fingerprint.browser.js",
"node": "./lib/fingerprint.js"
}
},
"imports": {
"#fingerprint": {
"browser": "./lib/fingerprint.browser.js",
"node": "./lib/fingerprint.js"
}
},
"browser": {
"./lib/fingerprint.js": "./lib/fingerprint.browser.js"
"./dist/lib/fingerprint.js": "./dist/lib/fingerprint.browser.js"
},
"react-native": {
"./lib/fingerprint.js": "./lib/fingerprint.react-native.js"
"./dist/lib/fingerprint.js": "./dist/lib/fingerprint.react-native.js"
},
"keywords": [
"id",
Expand All @@ -27,12 +43,13 @@
},
"files": [
"lib",
"dist",
"cuid.d.ts",
"index.js"
"index.esm.js"
],
"license": "MIT",
"devDependencies": {
"browserify": "17.0.0",
"@rollup/plugin-alias": "^5.1.1",
"eslint": "^8.49",
"jasmine": "^2.5.0",
"jasmine-core": "^2.8.0",
Expand All @@ -41,12 +58,13 @@
"karma-chrome-launcher": "^2.2.0",
"karma-jasmine": "^1.1.1",
"mkdirp": "^0.5.1",
"uglify-js": "^2.7.4",
"rollup": "^2.76.0",
"watchify": "^4.0.0",
"webworkify": "^1.4.0"
},
"scripts": {
"build": "mkdirp dist && browserify index.js -s cuid -o dist/cuid.js && uglifyjs dist/cuid.js -c -m -o dist/cuid.min.js",
"prebuild": "rm -rf dist",
"build": "rollup -c",
"lint": "eslint index.js lib test",
"test": "npm run lint && npm run test:server && npm run test:browser",
"test:browser": "karma start test/karma.conf.js",
Expand Down
31 changes: 31 additions & 0 deletions rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const sharedOutput = {
format: 'cjs',
dir: 'dist',
preserveModules: true,
exports: 'default',
generatedCode: {
preset: 'es2015'
}
};

export default [
{
external: ['os'],
input: 'index.js',
output: sharedOutput
},
{
input: 'lib/fingerprint.browser.js',
output: {
...sharedOutput,
dir: 'dist/lib'
}
},
{
input: 'lib/fingerprint.react-native.js',
output: {
...sharedOutput,
dir: 'dist/lib'
}
}
];