diff --git a/README.md b/README.md index 11b7cc7e..a34274f8 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,10 @@ For timestamp UUIDs, namespace UUIDs, and other options read on ... | [`uuid.v4()`](#uuidv4options-buffer-offset) | Create a version 4 (random) UUID | | | [`uuid.v5()`](#uuidv5name-namespace-buffer-offset) | Create a version 5 (namespace w/ SHA-1) UUID | | | [`uuid.validate()`](#uuidvalidatestr) | Test a string to see if it is a valid UUID | New in `uuid@8.3` | +| [`uuid.isV1()`](#uuidisv1str) | Test a string to see if it is a valid V1 UUID | | +| [`uuid.isV3()`](#uuidisv3str) | Test a string to see if it is a valid V3 UUID | | +| [`uuid.isV4()`](#uuidisv4str) | Test a string to see if it is a valid V4 UUID | | +| [`uuid.isV5()`](#uuidisv5str) | Test a string to see if it is a valid V5 UUID | | | [`uuid.version()`](#uuidversionstr) | Detect RFC version of a UUID | New in `uuid@8.3` | ## API @@ -296,6 +300,82 @@ uuidValidate('not a UUID'); // ⇨ false uuidValidate('6ec0bd7f-11c0-43da-975e-2a8ad9ebae0b'); // ⇨ true ``` +### uuid.isV1(str) + +Test a string to see if it is a valid V1 UUID + +| | | +| --------- | ------------------------------------------------------ | +| `str` | `String` to validate | +| _returns_ | `true` if string is a valid V1 UUID, `false` otherwise | + +Example: + +```javascript +import { isV1 as uuidIsV1 } from 'uuid'; + +uuidIsV1('not a UUID'); // ⇨ false +uuidIsV1('d9428888-122b-11e1-b85c-61cd3cbb3210'); // ⇨ true +uuidIsV1('a981a0c2-68b1-35dc-bcfc-296e52ab01ec'); // ⇨ false +``` + +### uuid.isV3(str) + +Test a string to see if it is a valid V3 UUID + +| | | +| --------- | ------------------------------------------------------ | +| `str` | `String` to validate | +| _returns_ | `true` if string is a valid V3 UUID, `false` otherwise | + +Example: + +```javascript +import { isV3 as uuidIsV3 } from 'uuid'; + +uuidIsV3('not a UUID'); // ⇨ false +uuidIsV3('a981a0c2-68b1-35dc-bcfc-296e52ab01ec'); // ⇨ true +uuidIsV3('109156be-c4fb-41ea-b1b4-efe1671c5836'); // ⇨ false +``` + +### uuid.isV4(str) + +Test a string to see if it is a valid V4 UUID + +| | | +| --------- | ------------------------------------------------------ | +| `str` | `String` to validate | +| _returns_ | `true` if string is a valid V4 UUID, `false` otherwise | + +Example: + +```javascript +import { isV4 as uuidIsV4 } from 'uuid'; + +uuidIsV4('not a UUID'); // ⇨ false +uuidIsV4('109156be-c4fb-41ea-b1b4-efe1671c5836'); // ⇨ true +uuidIsV4('90123e1c-7512-523e-bb28-76fab9f2f73d'); // ⇨ false +``` + +### uuid.isV5(str) + +Test a string to see if it is a valid V5 UUID + +| | | +| --------- | ------------------------------------------------------ | +| `str` | `String` to validate | +| _returns_ | `true` if string is a valid V5 UUID, `false` otherwise | + +Example: + +```javascript +import { isV5 as uuidIsV5 } from 'uuid'; + +uuidIsV5('not a UUID'); // ⇨ false +uuidIsV5('90123e1c-7512-523e-bb28-76fab9f2f73d'); // ⇨ true +uuidIsV5('109156be-c4fb-41ea-b1b4-efe1671c5836'); // ⇨ false +``` + ### uuid.version(str) Detect RFC version of a UUID diff --git a/README_js.md b/README_js.md index a2769a22..c4fddc31 100644 --- a/README_js.md +++ b/README_js.md @@ -69,6 +69,10 @@ For timestamp UUIDs, namespace UUIDs, and other options read on ... | [`uuid.v4()`](#uuidv4options-buffer-offset) | Create a version 4 (random) UUID | | | [`uuid.v5()`](#uuidv5name-namespace-buffer-offset) | Create a version 5 (namespace w/ SHA-1) UUID | | | [`uuid.validate()`](#uuidvalidatestr) | Test a string to see if it is a valid UUID | New in `uuid@8.3` | +| [`uuid.isV1()`](#uuidisv1str) | Test a string to see if it is a valid V1 UUID | | +| [`uuid.isV3()`](#uuidisv3str) | Test a string to see if it is a valid V3 UUID | | +| [`uuid.isV4()`](#uuidisv4str) | Test a string to see if it is a valid V4 UUID | | +| [`uuid.isV5()`](#uuidisv5str) | Test a string to see if it is a valid V5 UUID | | | [`uuid.version()`](#uuidversionstr) | Detect RFC version of a UUID | New in `uuid@8.3` | ## API @@ -302,6 +306,82 @@ uuidValidate('not a UUID'); // RESULT uuidValidate('6ec0bd7f-11c0-43da-975e-2a8ad9ebae0b'); // RESULT ``` +### uuid.isV1(str) + +Test a string to see if it is a valid V1 UUID + +| | | +| --------- | ------------------------------------------------------ | +| `str` | `String` to validate | +| _returns_ | `true` if string is a valid V1 UUID, `false` otherwise | + +Example: + +```javascript --run +import { isV1 as uuidIsV1 } from 'uuid'; + +uuidIsV1('not a UUID'); // RESULT +uuidIsV1('d9428888-122b-11e1-b85c-61cd3cbb3210'); // RESULT +uuidIsV1('a981a0c2-68b1-35dc-bcfc-296e52ab01ec'); // RESULT +``` + +### uuid.isV3(str) + +Test a string to see if it is a valid V3 UUID + +| | | +| --------- | ------------------------------------------------------ | +| `str` | `String` to validate | +| _returns_ | `true` if string is a valid V3 UUID, `false` otherwise | + +Example: + +```javascript --run +import { isV3 as uuidIsV3 } from 'uuid'; + +uuidIsV3('not a UUID'); // RESULT +uuidIsV3('a981a0c2-68b1-35dc-bcfc-296e52ab01ec'); // RESULT +uuidIsV3('109156be-c4fb-41ea-b1b4-efe1671c5836'); // RESULT +``` + +### uuid.isV4(str) + +Test a string to see if it is a valid V4 UUID + +| | | +| --------- | ------------------------------------------------------ | +| `str` | `String` to validate | +| _returns_ | `true` if string is a valid V4 UUID, `false` otherwise | + +Example: + +```javascript --run +import { isV4 as uuidIsV4 } from 'uuid'; + +uuidIsV4('not a UUID'); // RESULT +uuidIsV4('109156be-c4fb-41ea-b1b4-efe1671c5836'); // RESULT +uuidIsV4('90123e1c-7512-523e-bb28-76fab9f2f73d'); // RESULT +``` + +### uuid.isV5(str) + +Test a string to see if it is a valid V5 UUID + +| | | +| --------- | ------------------------------------------------------ | +| `str` | `String` to validate | +| _returns_ | `true` if string is a valid V5 UUID, `false` otherwise | + +Example: + +```javascript --run +import { isV5 as uuidIsV5 } from 'uuid'; + +uuidIsV5('not a UUID'); // RESULT +uuidIsV5('90123e1c-7512-523e-bb28-76fab9f2f73d'); // RESULT +uuidIsV5('109156be-c4fb-41ea-b1b4-efe1671c5836'); // RESULT +``` + ### uuid.version(str) Detect RFC version of a UUID diff --git a/src/index.js b/src/index.js index 142ce9e4..00b7ccba 100644 --- a/src/index.js +++ b/src/index.js @@ -5,5 +5,9 @@ export { default as v5 } from './v5.js'; export { default as NIL } from './nil.js'; export { default as version } from './version.js'; export { default as validate } from './validate.js'; +export { default as isV1 } from './isV1.js'; +export { default as isV3 } from './isV3.js'; +export { default as isV4 } from './isV4.js'; +export { default as isV5 } from './isV5.js'; export { default as stringify } from './stringify.js'; export { default as parse } from './parse.js'; diff --git a/src/isV1.js b/src/isV1.js new file mode 100644 index 00000000..15e8403c --- /dev/null +++ b/src/isV1.js @@ -0,0 +1,7 @@ +import validateByVersion from './validate-by-version.js'; + +function isV1(uuid) { + return validateByVersion(uuid, 1); +} + +export default isV1; diff --git a/src/isV3.js b/src/isV3.js new file mode 100644 index 00000000..3db00e5f --- /dev/null +++ b/src/isV3.js @@ -0,0 +1,7 @@ +import validateByVersion from './validate-by-version.js'; + +function isV3(uuid) { + return validateByVersion(uuid, 3); +} + +export default isV3; diff --git a/src/isV4.js b/src/isV4.js new file mode 100644 index 00000000..4d7b6cfb --- /dev/null +++ b/src/isV4.js @@ -0,0 +1,7 @@ +import validateByVersion from './validate-by-version.js'; + +function isV4(uuid) { + return validateByVersion(uuid, 4); +} + +export default isV4; diff --git a/src/isV5.js b/src/isV5.js new file mode 100644 index 00000000..4bd6fff8 --- /dev/null +++ b/src/isV5.js @@ -0,0 +1,7 @@ +import validateByVersion from './validate-by-version.js'; + +function isV5(uuid) { + return validateByVersion(uuid, 5); +} + +export default isV5; diff --git a/src/validate-by-version.js b/src/validate-by-version.js new file mode 100644 index 00000000..b5272de4 --- /dev/null +++ b/src/validate-by-version.js @@ -0,0 +1,8 @@ +import validate from './validate.js'; +import version from './version.js'; + +export function validateByVersion(uuid, uuidVersion) { + return validate(uuid) && version(uuid) === uuidVersion; +} + +export default validateByVersion; diff --git a/test/unit/validate.test.js b/test/unit/validate.test.js index 3ed9a948..2569ae81 100644 --- a/test/unit/validate.test.js +++ b/test/unit/validate.test.js @@ -1,18 +1,29 @@ import assert from 'assert'; import validate from '../../src/validate.js'; +import isV1 from '../../src/isV1.js'; +import isV3 from '../../src/isV3.js'; +import isV4 from '../../src/isV4.js'; +import isV5 from '../../src/isV5.js'; import NIL from '../../src/nil.js'; +const validUuids = { + v1: 'd9428888-122b-11e1-b85c-61cd3cbb3210', + v3: 'a981a0c2-68b1-35dc-bcfc-296e52ab01ec', + v4: '109156be-c4fb-41ea-b1b4-efe1671c5836', + v5: '90123e1c-7512-523e-bb28-76fab9f2f73d', +}; + describe('validate', () => { test('validate uuid', () => { assert.strictEqual(validate(NIL), true); - assert.strictEqual(validate('d9428888-122b-11e1-b85c-61cd3cbb3210'), true); + assert.strictEqual(validate(validUuids.v1), true); - assert.strictEqual(validate('109156be-c4fb-41ea-b1b4-efe1671c5836'), true); + assert.strictEqual(validate(validUuids.v3), true); - assert.strictEqual(validate('a981a0c2-68b1-35dc-bcfc-296e52ab01ec'), true); + assert.strictEqual(validate(validUuids.v4), true); - assert.strictEqual(validate('90123e1c-7512-523e-bb28-76fab9f2f73d'), true); + assert.strictEqual(validate(validUuids.v5), true); assert.strictEqual(validate(), false); @@ -29,4 +40,32 @@ describe('validate', () => { false ); }); + + test('validate v1 uuid', () => { + assert.strictEqual(isV1(validUuids.v1), true); + assert.strictEqual(isV1(validUuids.v3), false); + assert.strictEqual(isV1(validUuids.v4), false); + assert.strictEqual(isV1(validUuids.v5), false); + }); + + test('validate v3 uuid', () => { + assert.strictEqual(isV3(validUuids.v1), false); + assert.strictEqual(isV3(validUuids.v3), true); + assert.strictEqual(isV3(validUuids.v4), false); + assert.strictEqual(isV3(validUuids.v5), false); + }); + + test('validate v4 uuid', () => { + assert.strictEqual(isV4(validUuids.v1), false); + assert.strictEqual(isV4(validUuids.v3), false); + assert.strictEqual(isV4(validUuids.v4), true); + assert.strictEqual(isV4(validUuids.v5), false); + }); + + test('validate v5 uuid', () => { + assert.strictEqual(isV5(validUuids.v1), false); + assert.strictEqual(isV5(validUuids.v3), false); + assert.strictEqual(isV5(validUuids.v4), false); + assert.strictEqual(isV5(validUuids.v5), true); + }); });