uuid-mongodb
Advanced tools
Comparing version 1.1.4 to 2.0.0
@@ -28,7 +28,6 @@ /* | ||
if (typeof uuid === 'string') { | ||
if (!validateUuid(uuid)) throw Err.InvalidUUID; | ||
return generateUUID(uuid); | ||
} else if (uuid instanceof Binary) { | ||
if (uuid.length() !== 36 && uuid.length() !== 16) throw Err.InvalidUUID; | ||
return stringify(uuid.read(0, uuid.length())); | ||
} else if (uuid instanceof Binary && uuid.sub_type === Binary.SUBTYPE_UUID) { | ||
if (uuid.length() !== 16) throw Err.InvalidUUID; | ||
return apply(new Binary(uuid.read(0, uuid.length()), Binary.SUBTYPE_UUID)); | ||
} else { | ||
@@ -46,10 +45,2 @@ throw Err.UnexpectedUUIDType; | ||
const apply = mu => { | ||
// add to string method | ||
mu.toString = function() { | ||
return stringify(this.buffer); | ||
}.bind(mu); | ||
return mu; | ||
}; | ||
if (uuid) { | ||
@@ -63,3 +54,3 @@ if (!validateUuid(uuid)) throw Err.InvalidUUID; | ||
} else { | ||
const uuid = uuidv(null, new Buffer(16)); | ||
const uuid = uuidv(null, Buffer.alloc(16)); | ||
return apply(Binary(uuid, Binary.SUBTYPE_UUID)); | ||
@@ -81,3 +72,2 @@ } | ||
if (!stripped.match(/^[a-f0-9]{32}$/)) return false; | ||
return stripped; | ||
@@ -95,1 +85,9 @@ } | ||
} | ||
function apply(mu) { | ||
// add to string method | ||
mu.toString = function() { | ||
return stringify(this.buffer); | ||
}.bind(mu); | ||
return mu; | ||
} |
{ | ||
"name": "uuid-mongodb", | ||
"version": "1.1.4", | ||
"version": "2.0.0", | ||
"description": "", | ||
"main": "lib/index.js", | ||
"types": "./lib/index.d.ts", | ||
"scripts": { | ||
@@ -30,5 +31,5 @@ "test": "mocha test/**/*.js " | ||
"devDependencies": { | ||
"mocha": "^6.0.2", | ||
"mocha": "^6.1.4", | ||
"mongodb": "^3.1.13" | ||
} | ||
} |
# uuid-mongodb | ||
![](https://travis-ci.org/cdimascio/uuid-mongodb.svg?branch=master) [![Codacy Badge](https://api.codacy.com/project/badge/Grade/a42f61ffc97b4bcbbca184ab838092c8)](https://www.codacy.com/app/cdimascio/uuid-mongodb?utm_source=github.com&utm_medium=referral&utm_content=cdimascio/uuid-mongodb&utm_campaign=Badge_Grade) ![](https://img.shields.io/badge/license-MIT-blue.svg) | ||
![](https://travis-ci.org/cdimascio/uuid-mongodb.svg?branch=master) [![Codacy Badge](https://api.codacy.com/project/badge/Grade/a42f61ffc97b4bcbbca184ab838092c8)](https://www.codacy.com/app/cdimascio/uuid-mongodb?utm_source=github.com&utm_medium=referral&utm_content=cdimascio/uuid-mongodb&utm_campaign=Badge_Grade) ![](https://img.shields.io/npm/v/uuid-mongodb.svg) ![](https://img.shields.io/npm/dm/uuid-mongodb.svg) ![](https://img.shields.io/badge/license-MIT-blue.svg) | ||
@@ -5,0 +5,0 @@ Generates and parses [BSON UUIDs](https://docs.mongodb.com/manual/reference/method/UUID/) for use with MongoDB. BSON UUIDs provide better performance than their string counterparts. |
@@ -7,3 +7,3 @@ const assert = require('assert'); | ||
const hasHexUpperCase = s => /[A-F]/.exec(s); | ||
const hasHexUpperCase = s => !!/[A-F]/.exec(s); | ||
@@ -17,3 +17,3 @@ describe('MUUID (accept and generate uuids according to spec - (see https://www.itu.int/rec/T-REC-X.667-201210-I/en)', function() { | ||
// as per spec - (see https://www.itu.int/rec/T-REC-X.667-201210-I/en) | ||
assert.equal(hasHexUpperCase(mUUID.toString(), false)); | ||
assert.equal(hasHexUpperCase(mUUID.toString()), false); | ||
}); | ||
@@ -28,3 +28,3 @@ }); | ||
// as per spec - (see https://www.itu.int/rec/T-REC-X.667-201210-I/en) | ||
assert.equal(hasHexUpperCase(mUUID.toString(), false)); | ||
assert.equal(hasHexUpperCase(mUUID.toString()), false); | ||
}); | ||
@@ -41,3 +41,3 @@ }); | ||
const mUUID = MUUID.from(uuidv4().toUpperCase()); | ||
assert.equal(hasHexUpperCase(mUUID.toString(), false)); | ||
assert.equal(hasHexUpperCase(mUUID.toString()), false); | ||
assert.equal(validate(mUUID.toString()), true); | ||
@@ -52,14 +52,20 @@ }); | ||
it('should throw when converting an invalid uuid', () => { | ||
assert.throws(() => MUUID.from('invalid-uuid'), 'Invalid uuid'); | ||
assert.throws(() => MUUID.from('802ac0f09b7311e89bb69baebe1aa0bf'), 'Invalid uuid'); | ||
assert.throws(() => MUUID.from('invalid-uuid'), /Invalid UUID\.$/); | ||
assert.throws(() => MUUID.from('802ac0f09b7311e89bb69baebe1aa0bf'), /Invalid UUID\.$/); | ||
}); | ||
it('should convert uuid from mongo BSON binary', () => { | ||
const mUUID = MUUID.from(Binary(uuidv1(), Binary.SUBTYPE_UUID)); | ||
const mUUID = MUUID.from(Binary(uuidv1(null, Buffer.alloc(16)), Binary.SUBTYPE_UUID)); | ||
assert.equal(mUUID instanceof Binary, true); | ||
assert.equal(validate(mUUID.toString()), true); | ||
}); | ||
it('should throw when attempting to store 36 byte string representation as UUID', () => { | ||
const binary = new Binary(Buffer.from(uuidv1()), Binary.SUBTYPE_UUID); | ||
assert.throws(() => MUUID.from(binary), /Invalid UUID\.$/); | ||
}); | ||
it('should throw when converting an Binary non SUBTYPE_UUID', () => { | ||
const binary = Binary('tests', Binary.SUBTYPE_USER_DEFINED); | ||
assert.throws(() => MUUID.from(binary), 'Invalid uuid'); | ||
const binary = new Binary(uuidv1(null, Buffer.alloc(16)), Binary.SUBTYPE_USER_DEFINED); | ||
assert.throws(() => MUUID.from(binary), /Unexpected UUID type\. UUID must be a string or a MongoDB Binary \(SUBTYPE_UUID\)\.$/); | ||
}); | ||
@@ -66,0 +72,0 @@ }); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
26685
8
144