From 40933d3af83c238214b80b5ea92af2d841d40150 Mon Sep 17 00:00:00 2001 From: mhkeller Date: Mon, 20 Mar 2023 22:24:22 -0400 Subject: [PATCH] more getDisplay tests --- .eslintrc.cjs | 7 +++-- .prettierrc | 6 +++++ package-lock.json | 35 +++++++++++++++--------- package.json | 4 +-- src/index.js | 2 +- src/lib/getDisplay.js | 17 +++++++----- src/lib/normalize.js | 6 ++--- src/utils/isObject.js | 5 ++++ test/formatTime.test.js | 52 ++++++++++++++++++++++++++++++++++++ test/formatTimestamp.test.js | 4 +-- test/getProjectName.test.js | 19 +++++++++++++ test/isObject.test.js | 28 +++++++++++++++++++ test/x_getDisplay.test.js | 43 +++++++++++++++++++++++++++++ test/x_normalize.test.js | 0 14 files changed, 196 insertions(+), 32 deletions(-) create mode 100644 .prettierrc create mode 100644 src/utils/isObject.js create mode 100644 test/formatTime.test.js create mode 100644 test/getProjectName.test.js create mode 100644 test/isObject.test.js create mode 100644 test/x_getDisplay.test.js create mode 100644 test/x_normalize.test.js diff --git a/.eslintrc.cjs b/.eslintrc.cjs index e93ae9d..7c2dde6 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -1,7 +1,5 @@ module.exports = { - extends: [ - 'semistandard' - ], + extends: ['semistandard'], rules: { quotes: [ 'error', @@ -9,6 +7,7 @@ module.exports = { { allowTemplateLiterals: true } - ] + ], + 'operator-linebreak': ['error', 'after'] } }; diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000..5f4511a --- /dev/null +++ b/.prettierrc @@ -0,0 +1,6 @@ +{ + "useTabs": false, + "singleQuote": true, + "trailingComma": "none", + "printWidth": 100 +} diff --git a/package-lock.json b/package-lock.json index 255a373..367fe5b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,8 +11,7 @@ "dependencies": { "chalk": "^4.1.2", "clean-stack": "^5.1.0", - "node-notifier": "^10.0.1", - "underscore": "^1.13.6" + "node-notifier": "^10.0.1" }, "devDependencies": { "eslint": "^8.36.0", @@ -22,6 +21,7 @@ "eslint-plugin-n": "^15.6.1", "eslint-plugin-promise": "^6.1.1", "mocha": "^10.2.0", + "prettier": "^2.8.5", "strip-ansi": "^7.0.1" } }, @@ -2358,6 +2358,21 @@ "node": ">= 0.8.0" } }, + "node_modules/prettier": { + "version": "2.8.5", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-2.8.5.tgz", + "integrity": "sha512-3gzuxrHbKUePRBB4ZeU08VNkUcqEHaUaouNt0m7LGP4Hti/NuB07C7PPTM/LkWqXoJYJn2McEo5+kxPNrtQkLQ==", + "dev": true, + "bin": { + "prettier": "bin-prettier.js" + }, + "engines": { + "node": ">=10.13.0" + }, + "funding": { + "url": "https://github.com/prettier/prettier?sponsor=1" + } + }, "node_modules/punycode": { "version": "2.3.0", "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.3.0.tgz", @@ -2855,11 +2870,6 @@ "url": "https://github.com/sponsors/ljharb" } }, - "node_modules/underscore": { - "version": "1.13.6", - "resolved": "https://registry.npmjs.org/underscore/-/underscore-1.13.6.tgz", - "integrity": "sha512-+A5Sja4HP1M08MaXya7p5LvjuM7K6q/2EaC0+iovj/wOcMsTzMvDFbasi/oSapiwOlt252IqsKqPjCl7huKS0A==" - }, "node_modules/uri-js": { "version": "4.4.1", "resolved": "https://registry.npmjs.org/uri-js/-/uri-js-4.4.1.tgz", @@ -4716,6 +4726,12 @@ "integrity": "sha512-vkcDPrRZo1QZLbn5RLGPpg/WmIQ65qoWWhcGKf/b5eplkkarX0m9z8ppCat4mlOqUsWpyNuYgO3VRyrYHSzX5g==", "dev": true }, + "prettier": { + "version": "2.8.5", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-2.8.5.tgz", + "integrity": "sha512-3gzuxrHbKUePRBB4ZeU08VNkUcqEHaUaouNt0m7LGP4Hti/NuB07C7PPTM/LkWqXoJYJn2McEo5+kxPNrtQkLQ==", + "dev": true + }, "punycode": { "version": "2.3.0", "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.3.0.tgz", @@ -5051,11 +5067,6 @@ "which-boxed-primitive": "^1.0.2" } }, - "underscore": { - "version": "1.13.6", - "resolved": "https://registry.npmjs.org/underscore/-/underscore-1.13.6.tgz", - "integrity": "sha512-+A5Sja4HP1M08MaXya7p5LvjuM7K6q/2EaC0+iovj/wOcMsTzMvDFbasi/oSapiwOlt252IqsKqPjCl7huKS0A==" - }, "uri-js": { "version": "4.4.1", "resolved": "https://registry.npmjs.org/uri-js/-/uri-js-4.4.1.tgz", diff --git a/package.json b/package.json index eece981..1fc8d02 100644 --- a/package.json +++ b/package.json @@ -13,8 +13,7 @@ "dependencies": { "chalk": "^4.1.2", "clean-stack": "^5.1.0", - "node-notifier": "^10.0.1", - "underscore": "^1.13.6" + "node-notifier": "^10.0.1" }, "devDependencies": { "eslint": "^8.36.0", @@ -24,6 +23,7 @@ "eslint-plugin-n": "^15.6.1", "eslint-plugin-promise": "^6.1.1", "mocha": "^10.2.0", + "prettier": "^2.8.5", "strip-ansi": "^7.0.1" }, "repository": { diff --git a/src/index.js b/src/index.js index 13e9fa8..1d976ce 100644 --- a/src/index.js +++ b/src/index.js @@ -1,4 +1,3 @@ -import { isObject } from 'underscore'; import notifier from 'node-notifier'; import cS from 'clean-stack'; import stripAnsi from 'strip-ansi'; @@ -10,6 +9,7 @@ import constructPrefix from './lib/constructPrefix.js'; import normalize from './lib/normalize.js'; import print from './lib/print.js'; import getDisplay from './lib/getDisplay.js'; +import isObject from './utils/isObject'; /** Send a notification diff --git a/src/lib/getDisplay.js b/src/lib/getDisplay.js index 28b9f9a..7569838 100644 --- a/src/lib/getDisplay.js +++ b/src/lib/getDisplay.js @@ -1,4 +1,4 @@ -import { isObject, isBoolean } from 'underscore'; +import isObject from '../utils/isObject.js'; /** * If we are passing an object, set the entire display @@ -9,9 +9,12 @@ function construct (val) { if (isObject(val)) { return val; } - return { - messageStyle: val - }; + if (val) { + return { + messageStyle: val + }; + } + return {}; } export default function getDisplay ({ baseDisplay, displays }, display, desktop) { @@ -24,9 +27,9 @@ export default function getDisplay ({ baseDisplay, displays }, display, desktop) result = construct(displays[display]); } else { result = construct(display); - if (isBoolean(desktop) === true) { - result.desktop = desktop; - } + } + if (typeof desktop === 'boolean') { + result.desktop = desktop; } return { ...baseDisplay, ...result }; } diff --git a/src/lib/normalize.js b/src/lib/normalize.js index 3f02270..f5db042 100644 --- a/src/lib/normalize.js +++ b/src/lib/normalize.js @@ -1,14 +1,12 @@ /* -------------------------------------------- * Normalize what we pass as the message and value strings */ -import { isNumber, isBoolean } from 'underscore'; - export default function normalize (val) { // Simple stringify numbers - if (isNumber(val)) { + if (typeof val === 'number') { return String(val); // Pretty print objects (arrays, dates, dictionaries, errors) and booleans - } else if (typeof val === 'object' || isBoolean(val)) { + } else if (typeof val === 'object' || typeof val === 'boolean') { return JSON.stringify(val); } else { return val; diff --git a/src/utils/isObject.js b/src/utils/isObject.js new file mode 100644 index 0000000..77683d7 --- /dev/null +++ b/src/utils/isObject.js @@ -0,0 +1,5 @@ +export default function isObject (val) { + return Array.isArray(val) === false && + typeof val === 'object' && + val !== null; +} diff --git a/test/formatTime.test.js b/test/formatTime.test.js new file mode 100644 index 0000000..c12e722 --- /dev/null +++ b/test/formatTime.test.js @@ -0,0 +1,52 @@ +/* globals describe it */ +import assert from 'assert'; + +import fn from '../src/utils/formatTime.js'; + +const name = 'formatTime'; + +const rows = [ + { args: [0], expected: '0ms' }, + { args: [0.1 * 0.2], expected: '0ms' }, + { args: [2], expected: '2ms' }, + { args: [20], expected: '20ms' }, + { args: [6814], expected: '6.81s' }, + { args: [7565], expected: '7.56s' }, + { args: [7686], expected: '7.69s' }, + { args: [7692], expected: '7.69s' }, + { args: [7805], expected: '7.81s' }, + { args: [7944], expected: '7.94s' }, + { args: [20.4], expected: '20.4ms' }, + { args: [20.5], expected: '20.5ms' }, + { args: [20.6], expected: '20.6ms' }, + { args: [60.02000000000004], expected: '60ms' }, + { args: [60.1], expected: '60.1ms' }, + { args: [1500], expected: '1.5s' }, + { args: [1750], expected: '1.75s' }, + { args: [40000], expected: '40s' }, + { args: [40500], expected: '40.5s' }, + { args: [40750], expected: '40.75s' }, + { args: [60000], expected: '1m' }, + { args: [61000], expected: '1m 1s' }, + { args: [61750], expected: '1m 1s' }, + { args: [10 * 60 * 1000], expected: '10m' }, + { args: [60 * 1000 * 10 + 500], expected: '10m' }, + { args: [60 * 1000 * 10 + 1600], expected: '10m 1s' }, + { args: [51 * 60 * 1000 + 40 * 1000], expected: '51m 40s' }, + { args: [59 * 60 * 1000 + 59 * 1000], expected: '59m 59s' }, + { args: [60 * 60 * 1000 + 59 * 1000], expected: '1h 59s' }, + { args: [2 * 60 * 60 * 1000 + 30 * 1000 + 59 * 1000], expected: '2h 1m 29s' }, + { args: [2 * 60 * 60 * 1000 + 30 * 1000], expected: '2h 30s' }, + { args: [2 * 60 * 60 * 1000 + 300], expected: '2h 1s' }, + { args: [2 * 60 * 60 * 1000 + 1200], expected: '2h 1s' }, + { args: [2 * 60 * 60 * 1000 + 300], expected: '2h 1s' } +]; + +describe(name, () => { + rows.forEach(t => { + it(`should match expected ${t.args[0]}`, () => { + const actual = fn(t.args[0]); + assert.equal(actual, t.expected); + }); + }); +}); diff --git a/test/formatTimestamp.test.js b/test/formatTimestamp.test.js index e1304b9..035a2f4 100644 --- a/test/formatTimestamp.test.js +++ b/test/formatTimestamp.test.js @@ -5,7 +5,7 @@ import fn from '../src/utils/formatTimestamp.js'; const name = 'formatTimestamp'; -const timesToTest = [ +const rows = [ { args: [new Date(2017, 0, 1)], expected: '00:00:00.00' }, { args: [new Date(2017, 0, 1, 11, 33, 30, 0)], expected: '11:33:30.00' }, { args: [new Date(2017, 0, 1, 11, 33, 30, 50)], expected: '11:33:30.05' }, @@ -21,7 +21,7 @@ const timesToTest = [ ]; describe(name, () => { - timesToTest.forEach(t => { + rows.forEach((t) => { it(`should match expected ${t.args[0]}`, () => { const actual = fn(t.args[0]); assert.equal(actual, t.expected); diff --git a/test/getProjectName.test.js b/test/getProjectName.test.js new file mode 100644 index 0000000..6c71a19 --- /dev/null +++ b/test/getProjectName.test.js @@ -0,0 +1,19 @@ +/* globals describe it */ +import assert from 'assert'; + +import fn from '../src/lib/getProjectName.js'; + +const name = 'getProjectName'; + +const rows = [ + { args: [], expected: 'notify' } +]; + +describe(name, () => { + rows.forEach((t) => { + it(`should match expected`, () => { + const actual = fn(t.args[0]); + assert.equal(actual, t.expected); + }); + }); +}); diff --git a/test/isObject.test.js b/test/isObject.test.js new file mode 100644 index 0000000..79484cf --- /dev/null +++ b/test/isObject.test.js @@ -0,0 +1,28 @@ +/* globals describe it */ +import assert from 'assert'; + +import fn from '../src/utils/isObject.js'; + +const name = 'isObject'; + +const rows = [ + { args: [{}], expected: true }, + { args: [0], expected: false }, + { args: [1], expected: false }, + { args: [NaN], expected: false }, + { args: [undefined], expected: false }, + { args: [null], expected: false }, + { args: [false], expected: false }, + { args: [true], expected: false }, + { args: [Infinity], expected: false }, + { args: [-Infinity], expected: false } +]; + +describe(name, () => { + rows.forEach((t) => { + it(`should match expected`, () => { + const actual = fn(t.args[0]); + assert.equal(actual, t.expected); + }); + }); +}); diff --git a/test/x_getDisplay.test.js b/test/x_getDisplay.test.js new file mode 100644 index 0000000..3855b77 --- /dev/null +++ b/test/x_getDisplay.test.js @@ -0,0 +1,43 @@ +/* globals describe it */ +import assert from 'assert'; + +import fn from '../src/lib/getDisplay.js'; +import defaults from '../src/defaults.js'; + +const name = 'getDisplay'; + +const rows = [ + // Empty + { args: [defaults, undefined, undefined], expected: defaults.baseDisplay }, + + // Setting desktop + { args: [defaults, {}, true], expected: { ...defaults.baseDisplay, desktop: true } }, + { args: [defaults, {}, false], expected: { ...defaults.baseDisplay, desktop: false } }, + { args: [defaults, {}, {}], expected: { ...defaults.baseDisplay, desktop: false } }, + { args: [defaults, {}, []], expected: { ...defaults.baseDisplay, desktop: false } }, + { args: [defaults, {}, 1], expected: { ...defaults.baseDisplay, desktop: false } }, + { args: [defaults, {}, ''], expected: { ...defaults.baseDisplay, desktop: false } }, + + // Setting colors + { args: [defaults, 'red', ''], expected: { ...defaults.baseDisplay, messageStyle: 'red' } }, + { args: [defaults, 'bold', ''], expected: { ...defaults.baseDisplay, messageStyle: 'bold' } }, + { args: [defaults, ['red'], ''], expected: { ...defaults.baseDisplay, messageStyle: ['red'] } }, + { args: [defaults, ['bold'], ''], expected: { ...defaults.baseDisplay, messageStyle: ['bold'] } }, + { args: [defaults, ['bold', 'blue'], ''], expected: { ...defaults.baseDisplay, messageStyle: ['bold', 'blue'] } }, + + // Setting colors and desktop + { args: [defaults, 'red', true], expected: { ...defaults.baseDisplay, messageStyle: 'red', desktop: true } }, + { args: [defaults, 'bold', false], expected: { ...defaults.baseDisplay, messageStyle: 'bold', desktop: false } }, + { args: [defaults, ['red'], {}], expected: { ...defaults.baseDisplay, messageStyle: ['red'], desktop: false } }, + { args: [defaults, ['bold'], []], expected: { ...defaults.baseDisplay, messageStyle: ['bold'], desktop: false } }, + { args: [defaults, ['bold', 'blue'], ''], expected: { ...defaults.baseDisplay, messageStyle: ['bold', 'blue'], desktop: false } }, +]; + +describe(name, () => { + rows.forEach((t) => { + it(`should match expected ${t.args[1]}`, () => { + const actual = fn(...t.args); + assert.deepEqual(actual, t.expected); + }); + }); +}); diff --git a/test/x_normalize.test.js b/test/x_normalize.test.js new file mode 100644 index 0000000..e69de29