diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 00000000..586a1c8e --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,30 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "Build watcher", + "group": "build", + "type": "shell", + "command": "npm run build:watch", + "runOptions": { + "runOn": "folderOpen" + } + }, + { + "label": "Test watcher", + "group": "build", + "type": "shell", + "command": "npm run test:watch", + "windows": { + "command": "npm run test:watch" + }, + "presentation": { + "reveal": "always", + "panel": "new" + }, + "runOptions": { + "runOn": "folderOpen" + } + } + ] +} diff --git a/package.json b/package.json index 4c41625d..e4ae53ef 100644 --- a/package.json +++ b/package.json @@ -89,6 +89,7 @@ }, "scripts": { "build": "./scripts/build.sh", + "build:watch": "tsc --watch -p tsconfig.esm.json", "bundlewatch": "npm run pretest:browser && bundlewatch --config bundlewatch.config.json", "docs:diff": "npm run docs && git diff --quiet README.md", "docs": "npm run build && npx runmd --output=README.md README_js.md", @@ -115,7 +116,8 @@ "test:matching": "BABEL_ENV=commonjsNode node --throw-deprecation node_modules/.bin/jest test/unit/ -t", "test:node": "npm-run-all --parallel examples:node:**", "test:pack": "./scripts/testpack.sh", - "test": "BABEL_ENV=commonjsNode node --throw-deprecation node_modules/.bin/jest dist/esm/test/" + "test:watch": "node --test --watch dist/esm/test", + "test": "BABEL_ENV=commonjsNode node --throw-deprecation node_modules/.bin/jest dist/esm/test" }, "repository": { "type": "git", diff --git a/src/test/parse.test.ts b/src/test/parse.test.ts index 845f0504..c170cb36 100644 --- a/src/test/parse.test.ts +++ b/src/test/parse.test.ts @@ -1,5 +1,5 @@ import * as assert from 'assert'; -import { describe } from 'node:test'; +import test, { describe } from 'node:test'; import * as gen from 'random-seed'; import parse from '../parse.js'; import stringify from '../stringify.js'; diff --git a/src/test/rng.test.ts b/src/test/rng.test.ts index feb8ce42..01dff467 100644 --- a/src/test/rng.test.ts +++ b/src/test/rng.test.ts @@ -1,5 +1,5 @@ import * as assert from 'assert'; -import { describe } from 'node:test'; +import test, { describe } from 'node:test'; import rng from '../rng.js'; describe('rng', () => { diff --git a/src/test/stringify.test.ts b/src/test/stringify.test.ts index 6748a875..5bf4c044 100644 --- a/src/test/stringify.test.ts +++ b/src/test/stringify.test.ts @@ -1,5 +1,5 @@ import * as assert from 'assert'; -import { describe } from 'node:test'; +import test, { describe } from 'node:test'; import stringify, { unsafeStringify } from '../stringify.js'; const BYTES = Uint8Array.of( diff --git a/src/test/v1-random.test.ts b/src/test/v1-random.test.ts index 1e0963f1..8f504e36 100644 --- a/src/test/v1-random.test.ts +++ b/src/test/v1-random.test.ts @@ -1,5 +1,5 @@ import * as assert from 'assert'; -import { describe } from 'node:test'; +import test, { describe } from 'node:test'; import v1 from '../v1.js'; // Since the clockseq is cached in the module this test must run in a separate file in order to diff --git a/src/test/v1-rng.test.ts b/src/test/v1-rng.test.ts index caf21d20..27b06892 100644 --- a/src/test/v1-rng.test.ts +++ b/src/test/v1-rng.test.ts @@ -1,5 +1,5 @@ import * as assert from 'assert'; -import { describe } from 'node:test'; +import test, { describe } from 'node:test'; import v1 from '../v1.js'; // Since the clockseq is cached in the module this test must run in a separate file in order to diff --git a/src/test/v1.test.ts b/src/test/v1.test.ts index 0edd863d..908bed1f 100644 --- a/src/test/v1.test.ts +++ b/src/test/v1.test.ts @@ -1,5 +1,5 @@ import * as assert from 'assert'; -import { describe } from 'node:test'; +import test, { describe } from 'node:test'; import v1 from '../v1.js'; // Verify ordering of v1 ids created with explicit times @@ -39,7 +39,10 @@ describe('v1', () => { test('msec', () => { // eslint-disable-next-line no-self-compare - assert(v1({ msecs: TIME }) !== v1({ msecs: TIME }), 'IDs created at same msec are different'); + assert.ok( + v1({ msecs: TIME }) !== v1({ msecs: TIME }), + 'IDs created at same msec are different' + ); }); test('exception thrown when > 10k ids created in 1ms', () => { @@ -52,7 +55,7 @@ describe('v1', () => { // Verify clock regression bumps clockseq const uidt = v1({ msecs: TIME }); const uidtb = v1({ msecs: TIME - 1 }); - assert( + assert.ok( parseInt(uidtb.split('-')[3], 16) - parseInt(uidt.split('-')[3], 16) === 1, 'Clock regression by msec increments the clockseq' ); @@ -62,7 +65,7 @@ describe('v1', () => { // Verify clock regression bumps clockseq const uidtn = v1({ msecs: TIME, nsecs: 10 }); const uidtnb = v1({ msecs: TIME, nsecs: 9 }); - assert( + assert.ok( parseInt(uidtnb.split('-')[3], 16) - parseInt(uidtn.split('-')[3], 16) === 1, 'Clock regression by nsec increments the clockseq' ); @@ -78,7 +81,10 @@ describe('v1', () => { test('explicit options produce expected id', () => { // Verify explicit options produce expected id const id = v1(fullOptions); - assert(id === 'd9428888-122b-11e1-b85c-61cd3cbb3210', 'Explicit options produce expected id'); + assert.ok( + id === 'd9428888-122b-11e1-b85c-61cd3cbb3210', + 'Explicit options produce expected id' + ); }); test('ids spanning 1ms boundary are 100ns apart', () => { @@ -89,10 +95,27 @@ describe('v1', () => { const before = u0.split('-')[0]; const after = u1.split('-')[0]; const dt = parseInt(after, 16) - parseInt(before, 16); - assert(dt === 1, 'Ids spanning 1ms boundary are 100ns apart'); + assert.ok(dt === 1, 'Ids spanning 1ms boundary are 100ns apart'); }); - const expectedBytes = [217, 66, 136, 136, 18, 43, 17, 225, 184, 92, 97, 205, 60, 187, 50, 16]; + const expectedBytes = Uint8Array.of( + 217, + 66, + 136, + 136, + 18, + 43, + 17, + 225, + 184, + 92, + 97, + 205, + 60, + 187, + 50, + 16 + ); test('fills one UUID into a buffer as expected', () => { const buffer = new Uint8Array(16); diff --git a/src/test/v35.test.ts b/src/test/v35.test.ts index 7fbcf9d5..7b7748d0 100644 --- a/src/test/v35.test.ts +++ b/src/test/v35.test.ts @@ -1,5 +1,5 @@ import * as assert from 'assert'; -import { describe } from 'node:test'; +import test, { describe } from 'node:test'; import md5Browser from '../md5-browser.js'; import md5 from '../md5.js'; import sha1Browser from '../sha1-browser.js'; @@ -43,27 +43,27 @@ describe('v35', () => { return chars.join(''); } - test('sha1 node', () => { - HASH_SAMPLES.forEach(function (sample) { + HASH_SAMPLES.forEach(function (sample, i) { + test(`sha1(node) HASH_SAMPLES[${i}]`, () => { assert.equal(hashToHex(sha1(sample.input)), sample.sha1); }); }); - test('sha1 browser', () => { - HASH_SAMPLES.forEach(function (sample) { + HASH_SAMPLES.forEach(function (sample, i) { + test('sha1(browser) HASH_SAMPLES[${i}]', () => { assert.equal(hashToHex(sha1Browser(sample.input)), sample.sha1); }); }); - test('md5 node', () => { - HASH_SAMPLES.forEach(function (sample) { + HASH_SAMPLES.forEach(function (sample, i) { + test('md5(node) HASH_SAMPLES[${i}]', () => { assert.equal(hashToHex(md5(sample.input)), sample.md5); }); }); - test('md5 browser', () => { - HASH_SAMPLES.forEach(function (sample) { - assert.equal(hashToHex(md5Browser(sample.input)), sample.md5); + HASH_SAMPLES.forEach(function (sample, i) { + test(`md5(browser) (HASH_SAMPLES[${i}])`, () => { + assert.equal(hashToHex(md5Browser(sample.input) as unknown as Uint8Array), sample.md5); }); }); @@ -151,7 +151,7 @@ describe('v35', () => { assert.strictEqual(result, buf); // test offsets as well - buf = new Uint8Array(19); + buf = new Uint8Array(19).fill(0xaa); const expectedBuf = new Uint8Array(19).fill(0xaa); expectedBuf.set(expectedUuid, 3); @@ -256,7 +256,7 @@ describe('v35', () => { assert.strictEqual(result, buf); // test offsets as well - buf = new Uint8Array(19); + buf = new Uint8Array(19).fill(0xaa); const expectedBuf = new Uint8Array(19).fill(0xaa); expectedBuf.set(expectedUuid, 3); diff --git a/src/test/v4.test.ts b/src/test/v4.test.ts index 5971ae77..36574f39 100644 --- a/src/test/v4.test.ts +++ b/src/test/v4.test.ts @@ -1,5 +1,5 @@ import * as assert from 'assert'; -import { describe } from 'node:test'; +import test, { describe } from 'node:test'; import v4 from '../v4.js'; const randomBytesFixture = Uint8Array.of( @@ -44,8 +44,8 @@ describe('v4', () => { test('subsequent UUIDs are different', () => { const id1 = v4(); const id2 = v4(); - console.log('FJAFDSJKLFDSJA', id1, id2); - assert(id1 !== id2); + + assert.ok(id1 !== id2); }); test('explicit options.random produces expected result', () => { @@ -67,7 +67,7 @@ describe('v4', () => { }); test('fills two UUIDs into a buffer as expected', () => { - const buffer = new Uint8Array(16); + const buffer = new Uint8Array(32); v4({ random: randomBytesFixture }, buffer, 0); v4({ random: randomBytesFixture }, buffer, 16); diff --git a/src/test/v6.test.ts b/src/test/v6.test.ts index 7bbdf62f..aaad4521 100644 --- a/src/test/v6.test.ts +++ b/src/test/v6.test.ts @@ -1,5 +1,5 @@ import * as assert from 'assert'; -import { describe } from 'node:test'; +import test, { describe } from 'node:test'; import v1ToV6 from '../v1ToV6.js'; import v6 from '../v6.js'; import v6ToV1 from '../v6ToV1.js'; @@ -37,7 +37,7 @@ describe('v6', () => { test('default behavior', () => { // Verify explicit options produce expected id const id = v6(); - assert( + assert.ok( /[0-9a-f]{8}-[0-9a-f]{4}-6[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}/.test(id), 'id is valid v6 UUID' diff --git a/src/test/v7.test.ts b/src/test/v7.test.ts index 4914bd89..4b7aa480 100644 --- a/src/test/v7.test.ts +++ b/src/test/v7.test.ts @@ -1,5 +1,5 @@ import * as assert from 'assert'; -import { describe } from 'node:test'; +import test, { describe } from 'node:test'; import v7 from '../v7.js'; /** @@ -68,7 +68,7 @@ describe('v7', () => { test('subsequent UUIDs are different', () => { const id1 = v7(); const id2 = v7(); - assert(id1 !== id2); + assert.ok(id1 !== id2); }); test('explicit options.random and options.msecs produces expected result', () => { @@ -111,7 +111,8 @@ describe('v7', () => { }); test('fills two UUIDs into a buffer as expected', () => { - const buffer = new Uint8Array(16); + const buffer = new Uint8Array(32); + v7( { random: randomBytesFixture, @@ -154,7 +155,7 @@ describe('v7', () => { id = v7({ msecs }); if (prior !== undefined) { - assert(prior < id, `${prior} < ${id}`); + assert.ok(prior < id, `${prior} < ${id}`); } prior = id; @@ -172,7 +173,7 @@ describe('v7', () => { const c = v7({ msecs }); - assert(a < c, `${a} < ${c}`); + assert.ok(a < c, `${a} < ${c}`); }); test('can supply seq', () => { @@ -203,6 +204,6 @@ describe('v7', () => { msecs: msecsFixture + 1, }); - assert(uuid.indexOf('fff') !== 15); + assert.ok(uuid.indexOf('fff') !== 15); }); }); diff --git a/src/test/validate.test.ts b/src/test/validate.test.ts index 276e7d19..a4498b4e 100644 --- a/src/test/validate.test.ts +++ b/src/test/validate.test.ts @@ -1,5 +1,5 @@ import * as assert from 'assert'; -import { describe } from 'node:test'; +import test, { describe } from 'node:test'; import validate from '../validate.js'; import { TESTS } from './test_constants.js'; diff --git a/src/test/version.test.ts b/src/test/version.test.ts index ea8e39a4..dcd1939e 100644 --- a/src/test/version.test.ts +++ b/src/test/version.test.ts @@ -1,5 +1,5 @@ import * as assert from 'assert'; -import { describe } from 'node:test'; +import test, { describe } from 'node:test'; import version from '../version.js'; import { TESTS } from './test_constants.js'; @@ -10,10 +10,10 @@ describe('version() tests', () => { // @ts-expect-error const actualVersion = version(value); - assert(expectedValidate, `version(${value}) should throw`); + assert.ok(expectedValidate, `version(${value}) should throw`); assert.strictEqual(actualVersion, expectedVersion); } catch (err) { - assert(!expectedValidate, `version(${value}) threw unexpectedly`); + assert.ok(!expectedValidate, `version(${value}) threw unexpectedly`); } } }); diff --git a/src/uuid-bin.ts b/src/uuid-bin.ts index 66f2ce0c..0694a918 100644 --- a/src/uuid-bin.ts +++ b/src/uuid-bin.ts @@ -41,8 +41,8 @@ switch (version) { const name = args.shift(); let namespace = args.shift(); - assert(name != null, 'v3 name not specified'); - assert(namespace != null, 'v3 namespace not specified'); + assert.ok(name != null, 'v3 name not specified'); + assert.ok(namespace != null, 'v3 namespace not specified'); if (namespace === 'URL') { namespace = URL; @@ -64,8 +64,8 @@ switch (version) { const name = args.shift(); let namespace = args.shift(); - assert(name != null, 'v5 name not specified'); - assert(namespace != null, 'v5 namespace not specified'); + assert.ok(name != null, 'v5 name not specified'); + assert.ok(namespace != null, 'v5 namespace not specified'); if (namespace === 'URL') { namespace = URL; diff --git a/src/v35.ts b/src/v35.ts index a3166efc..cadb8117 100644 --- a/src/v35.ts +++ b/src/v35.ts @@ -37,9 +37,6 @@ export default function v35( ) { const valueBytes: Uint8Array = typeof value === 'string' ? stringToBytes(value) : value; const namespaceBytes: Uint8Array = typeof namespace === 'string' ? parse(namespace) : namespace; - if (typeof value === 'string') { - value = stringToBytes(value); - } if (typeof namespace === 'string') { namespace = parse(namespace);