diff --git a/packages/huston/huston.js b/packages/huston/huston.js index 8d82dd9a..f9a7d827 100644 --- a/packages/huston/huston.js +++ b/packages/huston/huston.js @@ -3,4 +3,9 @@ const normalizers = require('./lib/normalizers.js') const database = require('./lib/helpers/database.js') const logger = require('./lib/mocks/logger.js') -module.exports = { time, normalizers, database, logger } +module.exports = { + ...time, + ...normalizers, + ...database, + logger +} diff --git a/packages/huston/lib/normalizers.js b/packages/huston/lib/normalizers.js index bb9d60e5..c34f7165 100644 --- a/packages/huston/lib/normalizers.js +++ b/packages/huston/lib/normalizers.js @@ -1,17 +1,15 @@ 'use strict' -function removeNulls (obj) { - if (typeof obj !== 'object') { - return obj - } +function removeNulls (object) { + if (object === null) return '' + if (typeof object !== 'object') return object - /* c8 ignore next 3 */ - if (Array.isArray(obj)) { - return obj.map(removeNulls) + if (Array.isArray(object)) { + return object.map(removeNulls) } const newObj = {} - for (const [key, value] of Object.entries(obj)) { + for (const [key, value] of Object.entries(object)) { if (value === null) { newObj[key] = '' } else { @@ -22,18 +20,17 @@ function removeNulls (obj) { return newObj } -function removeReservedWords (obj) { - if (typeof obj !== 'object') { - return obj +function removeReservedWords (object) { + if (typeof object !== 'object') { + return object } - /* c8 ignore next 3 */ - if (Array.isArray(obj)) { - return obj.map(removeReservedWords) + if (Array.isArray(object)) { + return object.map(removeReservedWords) } const newObj = {} - for (const [key, value] of Object.entries(obj)) { + for (const [key, value] of Object.entries(object)) { if (key !== 'id') { newObj[key] = removeReservedWords(value) } diff --git a/packages/huston/lib/time.js b/packages/huston/lib/time.js index 62a48faf..3d513737 100644 --- a/packages/huston/lib/time.js +++ b/packages/huston/lib/time.js @@ -4,12 +4,11 @@ function getNanoTime () { if (typeof process !== 'undefined' && process.hrtime !== undefined) { return process.hrtime.bigint() } - /* c8 ignore next */ + return BigInt(0) } function formatTime (time) { - /* c8 ignore start */ if (typeof time === 'number') { time = BigInt(time) } @@ -19,11 +18,10 @@ function formatTime (time) { } else if (time < BigInt(1e3)) { return `${time}ns` } else if (time < BigInt(1e6)) { - return `${time / BigInt(1e6)}μs` + return `${time / BigInt(1e3)}μs` } else if (time < BigInt(1e9)) { return `${time / BigInt(1e6)}ms` } - /* c8 ignore stop */ return `${time / BigInt(1e9)}s` } diff --git a/packages/huston/package.json b/packages/huston/package.json index 73a42048..59ca78bc 100644 --- a/packages/huston/package.json +++ b/packages/huston/package.json @@ -6,7 +6,7 @@ "scripts": { "lint": "standard | snazzy", "lint:fix": "standard --fix | snazzy", - "test": "npm run lint && c8 --100 tap --no-coverage test/**/*.test.js" + "test": "npm run lint:fix && c8 node --test --test-reporter spec" }, "repository": { "type": "git", @@ -20,8 +20,7 @@ "devDependencies": { "c8": "^8.0.1", "snazzy": "^9.0.0", - "standard": "^17.1.0", - "tap": "^16.3.9" + "standard": "^17.1.0" }, "author": { "email": "mateonunez95@gmail.com", diff --git a/packages/huston/test/index.test.js b/packages/huston/test/index.test.js deleted file mode 100644 index c4a52561..00000000 --- a/packages/huston/test/index.test.js +++ /dev/null @@ -1,39 +0,0 @@ -const { test } = require('tap') -const { getNanoTime, formatTime, sleep } = require('./../lib/time.js') -const { removeNulls, removeReservedWords } = require('./../lib/normalizers.js') - -test('time', ({ end }) => { - test('should get nano time', async ({ ok }) => { - const time = getNanoTime() - ok(time) - }) - - test('should format time', async ({ ok }) => { - const time = getNanoTime() - const formattedTime = formatTime(time) - ok(formattedTime) - }) - - test('should sleep', async ({ ok }) => { - await sleep(100) - ok(true) - }) - - end() -}) - -test('normalizers', ({ end }) => { - test('should remove nulls', async ({ same }) => { - const obj = { a: null, b: 1 } - const normalized = removeNulls(obj) - same(normalized, { a: '', b: 1 }) - }) - - test('should remove reserved words', async ({ same }) => { - const obj = { id: 1, a: 2 } - const normalized = removeReservedWords(obj) - same(normalized, { a: 2 }) - }) - - end() -}) diff --git a/packages/huston/test/normalizers.test.js b/packages/huston/test/normalizers.test.js new file mode 100644 index 00000000..62c418b9 --- /dev/null +++ b/packages/huston/test/normalizers.test.js @@ -0,0 +1,41 @@ +const test = require('node:test') +const assert = require('node:assert') +const { removeNulls, removeReservedWords } = require('./../lib/normalizers.js') + +test.describe('normalizers', () => { + test('should remove nulls', () => { + const object = { a: null, b: 1 } + const normalized = removeNulls(object) + assert.deepStrictEqual(normalized, { a: '', b: 1 }) + }) + + test('should remove reserved words', () => { + const object = { id: 1, a: 2 } + const normalized = removeReservedWords(object) + assert.deepStrictEqual(normalized, { a: 2 }) + }) + + test('should remove nulls in arrays', () => { + const array = [null, 1, { a: null, b: 2 }, [null, 3]] + const normalized = removeNulls(array) + assert.deepStrictEqual(normalized, ['', 1, { a: '', b: 2 }, ['', 3]]) + }) + + test('should remove reserved words in arrays', () => { + const arr = [{ id: 1, a: 2 }, [{ id: 3, b: 4 }]] + const normalized = removeReservedWords(arr) + assert.deepStrictEqual(normalized, [{ a: 2 }, [{ b: 4 }]]) + }) + + test('should remove nulls in nested objects', () => { + const object = { a: { c: null, d: 2 }, b: [null, 3] } + const normalized = removeNulls(object) + assert.deepStrictEqual(normalized, { a: { c: '', d: 2 }, b: ['', 3] }) + }) + + test('should remove reserved words in nested objects', () => { + const object = { a: { id: 1, c: 2 }, b: [{ id: 3, d: 4 }] } + const normalized = removeReservedWords(object) + assert.deepStrictEqual(normalized, { a: { c: 2 }, b: [{ d: 4 }] }) + }) +}) diff --git a/packages/huston/test/time.test.js b/packages/huston/test/time.test.js new file mode 100644 index 00000000..829bd226 --- /dev/null +++ b/packages/huston/test/time.test.js @@ -0,0 +1,71 @@ +const test = require('node:test') +const assert = require('node:assert') +const { getNanoTime, formatTime, sleep } = require('./../lib/time.js') + +test.describe('time', () => { + test.it('should get nano time', async () => { + const time = getNanoTime() + assert.equal(typeof time, 'bigint') + }) + + test.it('should format time in nanoseconds', async () => { + const time = BigInt(123) + const formattedTime = formatTime(time) + assert.equal(formattedTime, '123ns') + }) + + test.it('should format time in microseconds', async () => { + const time = BigInt(123456) + const formattedTime = formatTime(time) + assert.equal(formattedTime, '123μs') + }) + + test.it('should format time in milliseconds', async () => { + const time = BigInt(123456789) + const formattedTime = formatTime(time) + assert.equal(formattedTime, '123ms') + }) + + test.it('should format time in seconds', async () => { + const time = BigInt(1234567890) + const formattedTime = formatTime(time) + assert.equal(formattedTime, '1s') + }) + + test.it('should convert number to bigint and format time', async () => { + const time = 123456789 + const formattedTime = formatTime(time) + assert.equal(formattedTime, '123ms') + }) + + test.it('should throw error for negative time', async () => { + const time = BigInt(-123) + assert.throws(() => formatTime(time), /microtime must be positive/) + }) + + test.it('should sleep for 100ms', async () => { + const start = getNanoTime() + await sleep(100) + const end = getNanoTime() + const elapsed = end - start + assert.equal(elapsed >= BigInt(1e8), true) + }) + + test.it('should return BigInt(0) if process or process.hrtime is undefined', () => { + const originalHrtime = process.hrtime + + Object.defineProperty(process, 'hrtime', { + value: undefined, + writable: true + }) + + let time = getNanoTime() + assert.equal(time, BigInt(0)) + + process.hrtime = originalHrtime + + time = getNanoTime() + assert.ok(typeof time === 'bigint') + assert.ok(time > BigInt(0)) + }) +})