Skip to content

Commit

Permalink
add x option
Browse files Browse the repository at this point in the history
  • Loading branch information
mhkeller committed Apr 1, 2023
1 parent c25d310 commit 4cb1425
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 5 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Changelog
===

## 1.1.0

> not yet on npm
Add an `x` prop to allow for easy extensions of shothand displays.

## 1.0.1

> 2023-03-27
Expand Down
5 changes: 3 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import isObject from './utils/isObject.js';
@param {String|Array|Object} notification.d The display style. Can be a chalk style name, array of chalk styles, the name of a built-in display, or a full display config
@param {Object} [notification.error] An optional error object to log out
@param {Boolean} [notification.k = false] Send a desktop notification
@param {Object} [notification.x] Extend shorthand displays with other properties
*/
export default function notify (notification, dflts = {}) {
if (!isObject(notification)) {
Expand All @@ -30,12 +31,12 @@ export default function notify (notification, dflts = {}) {
*/
const defaults = { ...notifyDefaults, ...dflts };

const { m = '', v = '', d = '', k, error } = notification;
const { m = '', v = '', d = '', k, error, x = {} } = notification;

/**
* Configure the display object
*/
const display = getDisplay(defaults, d, k);
const display = getDisplay(defaults, d, k, x);

/**
* Normalize json objects, numbers and falsey values
Expand Down
4 changes: 2 additions & 2 deletions src/lib/getDisplay.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function construct (val) {
return {};
}

export default function getDisplay ({ baseDisplay, displays }, display, desktop) {
export default function getDisplay ({ baseDisplay, displays }, display, desktop, extension) {
let result;
/**
* Set the display if this is a display name
Expand All @@ -31,5 +31,5 @@ export default function getDisplay ({ baseDisplay, displays }, display, desktop)
if (typeof desktop === 'boolean') {
result.desktop = desktop;
}
return { ...baseDisplay, ...result };
return { ...baseDisplay, ...result, ...extension };
}
3 changes: 2 additions & 1 deletion test/getDisplay.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ const tests = [
{ 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'] } },
{ args: [defaults, ['bold', 'blue'], '', { preString: '\t' }], expected: { ...defaults.baseDisplay, messageStyle: ['bold', 'blue'], preString: '\t' } },
{ args: [defaults, 'magenta', '', { postString: '\t' }], expected: { ...defaults.baseDisplay, messageStyle: 'magenta', postString: '\t' } },

// Setting colors and desktop
{ args: [defaults, 'red', true], expected: { ...defaults.baseDisplay, messageStyle: 'red', desktop: true } },
Expand Down
10 changes: 10 additions & 0 deletions test/notify.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ const tests = [
expected:
'[\x1B[90m12:00:00.10\x1B[39m | \x1B[1m\x1B[34mnotify\x1B[39m\x1B[22m] \x1B[1m\x1B[34mhello\x1B[39m\x1B[22m'
},
{
args: [{ m: 'hello', d: ['blue', 'bold'], x: { preString: '\t' } }, { time: '12:00:00.10' }],
expected:
'\t[\x1B[90m12:00:00.10\x1B[39m | \x1B[1m\x1B[34mnotify\x1B[39m\x1B[22m] \x1B[1m\x1B[34mhello\x1B[39m\x1B[22m'
},
{
args: [{ m: 'hello', d: ['blue', 'bold'], x: { postString: '\n' } }, { time: '12:00:00.10' }],
expected:
'[\x1B[90m12:00:00.10\x1B[39m | \x1B[1m\x1B[34mnotify\x1B[39m\x1B[22m] \x1B[1m\x1B[34mhello\x1B[39m\x1B[22m\n'
},
{
args: [{ m: 'hello', d: ['blue', 'bold'], k: true }, { time: '12:00:00.10' }],
expected:
Expand Down

0 comments on commit 4cb1425

Please sign in to comment.