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

refactor: upgraded code to es6 standard #427

Merged
merged 14 commits into from May 2, 2020
53 changes: 2 additions & 51 deletions .eslintrc.json
Expand Up @@ -7,7 +7,7 @@
"jest": true,
"node": true
},
"plugins": ["node", "standard", "prettier"],
"plugins": ["node", "standard"],
"extends": [
"eslint:recommended",
"standard",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we decide to go for a standard eslint preset, I think we should try to get rid of as many custom rules as possible. I would be all in for that!

How did you decide which custom rules to keep and which to drop? Are there maybe more rules we can drop?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I'll see what rules can be safely removed.

Expand All @@ -19,54 +19,5 @@
"msCrypto": true
},
"parser": "babel-eslint",
"rules": {
"array-bracket-spacing": ["warn", "never"],
"arrow-body-style": ["warn", "as-needed"],
"arrow-spacing": "warn",
"brace-style": ["warn", "1tbs"],
"camelcase": "warn",
"comma-spacing": [
"warn",
{
"after": true
}
],
"dot-notation": "warn",
"indent": [
"warn",
2,
{
"SwitchCase": 1,
"FunctionDeclaration": {
"parameters": 1
},
"MemberExpression": 1,
"CallExpression": {
"arguments": 1
}
}
],
"key-spacing": [
"warn",
{
"beforeColon": false,
"afterColon": true,
"mode": "minimum"
}
],
"keyword-spacing": "warn",
"no-multi-spaces": "warn",
"no-trailing-spaces": "warn",
"no-undef": "error",
"no-unused-vars": [
"warn",
{
"args": "none"
}
],
"one-var": ["warn", "never"],
"no-var": "error",
"padded-blocks": ["warn", "never"],
"semi": ["warn", "always"]
}
"rules": {}
}
13 changes: 10 additions & 3 deletions examples/benchmark/benchmark.js
@@ -1,3 +1,4 @@
// eslint-disable-next-line no-unused-vars
/* global Benchmark:false, uuidv1:false, uuidv3:false, uuidv4:false, uuidv5:false */
const Benchmark = (typeof window !== 'undefined' && window.Benchmark) || require('benchmark');
const uuidv1 = (typeof window !== 'undefined' && window.uuidv1) || require('uuid').v1;
Expand All @@ -8,7 +9,13 @@ const uuidv5 = (typeof window !== 'undefined' && window.uuidv5) || require('uuid
console.log('Starting. Tests take ~1 minute to run ...');

const array = new Array(16);
const suite = new Benchmark.Suite();

const suite = new Benchmark.Suite({
onError(event) {
console.error(event.target.error);
},
});

suite
.add('uuidv1()', function () {
uuidv1();
Expand All @@ -29,9 +36,9 @@ suite
uuidv5('hello.example.com', uuidv5.DNS);
})
.on('cycle', function (event) {
console.log(String(event.target));
console.log(event.target.toString());
})
.on('complete', function () {
console.log('Fastest is ' + this.filter('fastest').map('name'));
console.log('Fastest is', this.filter('fastest').map('name'));
})
.run();
44 changes: 22 additions & 22 deletions src/bytesToUuid.js
Expand Up @@ -14,28 +14,28 @@ function bytesToUuid(buf, offset) {
const bth = byteToHex;

// join used to fix memory issue caused by concatenation: https://bugs.chromium.org/p/v8/issues/detail?id=3175#c4
ctavan marked this conversation as resolved.
Show resolved Hide resolved
return [
bth[buf[i + 0]],
bth[buf[i + 1]],
bth[buf[i + 2]],
bth[buf[i + 3]],
'-',
bth[buf[i + 4]],
bth[buf[i + 5]],
'-',
bth[buf[i + 6]],
bth[buf[i + 7]],
'-',
bth[buf[i + 8]],
bth[buf[i + 9]],
'-',
bth[buf[i + 10]],
bth[buf[i + 11]],
bth[buf[i + 12]],
bth[buf[i + 13]],
bth[buf[i + 14]],
bth[buf[i + 15]],
].join('');
return (
bth[buf[i + 0]] +
bth[buf[i + 1]] +
bth[buf[i + 2]] +
bth[buf[i + 3]] +
'-' +
bth[buf[i + 4]] +
bth[buf[i + 5]] +
'-' +
bth[buf[i + 6]] +
bth[buf[i + 7]] +
'-' +
bth[buf[i + 8]] +
bth[buf[i + 9]] +
'-' +
bth[buf[i + 10]] +
bth[buf[i + 11]] +
bth[buf[i + 12]] +
bth[buf[i + 13]] +
bth[buf[i + 14]] +
bth[buf[i + 15]]
).toLowerCase();
}

export default bytesToUuid;
4 changes: 2 additions & 2 deletions src/rng-browser.js
Expand Up @@ -12,14 +12,14 @@ const getRandomValues =
typeof msCrypto.getRandomValues === 'function' &&
msCrypto.getRandomValues.bind(msCrypto));

const rnds8 = new Uint8Array(16);

export default function rng() {
if (!getRandomValues) {
throw new Error(
'crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported',
);
}

const rnds8 = new Uint8Array(16);

return getRandomValues(rnds8);
}
4 changes: 3 additions & 1 deletion src/rng.js
@@ -1,5 +1,7 @@
import crypto from 'crypto';

const rnds8 = new Uint8Array(16);

export default function rng() {
return crypto.randomBytes(16);
return crypto.randomFillSync(rnds8);
}
82 changes: 67 additions & 15 deletions src/v35.js
@@ -1,54 +1,106 @@
import bytesToUuid from './bytesToUuid.js';

function fillBytesFromStr(buf, start, strBytes) {
for (let i = 0; i < strBytes.length; i += 2) {
buf[(i >> 1) + start] = parseInt(strBytes.slice(i, i + 2), 16);
}
}

function uuidToBytes(uuid) {
// Note: We assume we're being passed a valid uuid string
const bytes = [];

uuid.replace(/[a-fA-F0-9]{2}/g, function (hex) {
bytes.push(parseInt(hex, 16));
});
const bytes = new Uint8Array(16);

const parts = uuid.split('-');

if (parts.length === 5) {
const [chunk1, chunk2, chunk3, chunk4, chunk5] = parts;

if (
chunk1.length === 8 &&
chunk2.length === 4 &&
chunk3.length === 4 &&
chunk4.length === 4 &&
chunk5.length === 12
) {
fillBytesFromStr(bytes, 0, chunk1);
fillBytesFromStr(bytes, 4, chunk2);
fillBytesFromStr(bytes, 6, chunk3);
fillBytesFromStr(bytes, 8, chunk4);
fillBytesFromStr(bytes, 10, chunk5);

return bytes;
}
}

return bytes;
throw TypeError('namespace must be valid uuid string');
}

function stringToBytes(str) {
str = unescape(encodeURIComponent(str)); // UTF8 escape

const bytes = [];
const bytes = new Uint8Array(str.length);

for (let i = 0; i < str.length; ++i) {
bytes.push(str.charCodeAt(i));
bytes[i] = str.charCodeAt(i);
}

return bytes;
}

// Uint8Array.from for ie11
function arrayToUint8(source) {
if (typeof Uint8Array.from === 'function') {
return Uint8Array.from(source);
}

const arr = new Uint8Array(source.length);

for (let i = 0; i < source.length; ++i) {
arr[i] = source[i];
}

return arr;
}

export const DNS = '6ba7b810-9dad-11d1-80b4-00c04fd430c8';
export const URL = '6ba7b811-9dad-11d1-80b4-00c04fd430c8';

export default function (name, version, hashfunc) {
function generateUUID(value, namespace, buf, offset) {
const off = (buf && offset) || 0;
const start = (buf && offset) || 0;

if (typeof value === 'string') value = stringToBytes(value);
if (typeof namespace === 'string') namespace = uuidToBytes(namespace);
if (typeof value === 'string') {
value = stringToBytes(value);
} else if (Array.isArray(value)) {
value = arrayToUint8(value);
}

if (typeof namespace === 'string') {
namespace = uuidToBytes(namespace);
} else if (Array.isArray(namespace)) {
namespace = arrayToUint8(namespace);
}

if (!Array.isArray(value)) {
if (!(value instanceof Uint8Array)) {
throw TypeError('value must be an array of bytes');
}

if (!Array.isArray(namespace) || namespace.length !== 16) {
if (!(namespace instanceof Uint8Array) || namespace.length !== 16) {
throw TypeError('namespace must be uuid string or an Array of 16 byte values');
}

const val = new Uint8Array(namespace.length + value.length);
val.set(namespace);
val.set(value, namespace.length);

// Per 4.3
const bytes = hashfunc(namespace.concat(value));
const bytes = hashfunc(val);
bytes[6] = (bytes[6] & 0x0f) | version;
bytes[8] = (bytes[8] & 0x3f) | 0x80;

if (buf) {
for (let idx = 0; idx < 16; ++idx) {
buf[off + idx] = bytes[idx];
buf[start + idx] = bytes[idx];
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/v4.js
Expand Up @@ -2,10 +2,10 @@ import rng from './rng.js';
import bytesToUuid from './bytesToUuid.js';

function v4(options, buf, offset) {
const i = (buf && offset) || 0;
const start = (buf && offset) || 0;

if (typeof options === 'string') {
buf = options === 'binary' ? new Uint32Array(16) : null;
buf = options === 'binary' ? new Uint8Array(16) : null;
options = null;
}

Expand All @@ -19,8 +19,8 @@ function v4(options, buf, offset) {

// Copy bytes to buffer, if provided
if (buf) {
for (let ii = 0; ii < 16; ++ii) {
buf[i + ii] = rnds[ii];
for (let i = 0; i < 16; ++i) {
buf[start + i] = rnds[i];
}
}

Expand Down