Skip to content

Commit

Permalink
introduce es6 modules and bump dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
schnittstabil committed Aug 23, 2020
1 parent ab46409 commit 5e88bce
Show file tree
Hide file tree
Showing 13 changed files with 59 additions and 55 deletions.
6 changes: 1 addition & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ sudo: false
language: node_js
node_js:
- 'stable'
- '12'
- '11'
- '10'
- '9'
- '8'
- '14'
after_success:
- './node_modules/.bin/nyc report --reporter=text-lcov | ./node_modules/.bin/coveralls'
20 changes: 10 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
'use strict';
const isOptionObject = require('is-plain-obj');
import isOptionObject from 'is-plain-obj';

const {hasOwnProperty} = Object.prototype;
const {propertyIsEnumerable} = Object;
const defineProperty = (obj, name, value) => Object.defineProperty(obj, name, {
const defineProperty = (object, name, value) => Object.defineProperty(object, name, {
value,
writable: true,
enumerable: true,
configurable: true
});

const globalThis = this;
const defaultMergeOpts = {
const defaultMergeOptions = {
concatArrays: false,
ignoreUndefined: false
};
Expand Down Expand Up @@ -61,11 +61,11 @@ function cloneArray(array) {
return result;
}

function cloneOptionObject(obj) {
const result = Object.getPrototypeOf(obj) === null ? Object.create(null) : {};
function cloneOptionObject(object) {
const result = Object.getPrototypeOf(object) === null ? Object.create(null) : {};

getEnumerableOwnPropertyKeys(obj).forEach(key => {
defineProperty(result, key, clone(obj[key]));
getEnumerableOwnPropertyKeys(object).forEach(key => {
defineProperty(result, key, clone(object[key]));
});

return result;
Expand Down Expand Up @@ -151,8 +151,8 @@ function merge(merged, source, config) {
return mergeKeys(merged, source, getEnumerableOwnPropertyKeys(source), config);
}

module.exports = function (...options) {
const config = merge(clone(defaultMergeOpts), (this !== globalThis && this) || {}, defaultMergeOpts);
export default function mergeOptions(...options) {
const config = merge(clone(defaultMergeOptions), (this !== globalThis && this) || {}, defaultMergeOptions);
let merged = {_: {}};

for (const option of options) {
Expand All @@ -168,4 +168,4 @@ module.exports = function (...options) {
}

return merged._;
};
}
22 changes: 15 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "merge-options",
"version": "2.0.0",
"type": "module",
"description": "Merge Option Objects",
"license": "MIT",
"repository": "schnittstabil/merge-options",
Expand All @@ -9,7 +10,7 @@
"email": "michael@schnittstabil.de"
},
"engines": {
"node": ">=8"
"node": ">=14"
},
"scripts": {
"test": "xo && nyc ava",
Expand All @@ -29,13 +30,20 @@
"clone"
],
"devDependencies": {
"ava": "^2.4.0",
"coveralls": "^3.0.3",
"nyc": "^14.1.1",
"rimraf": "^3.0.0",
"xo": "^0.25.3"
"ava": "^3.11.1",
"coveralls": "^3.1.0",
"nyc": "^15.1.0",
"rimraf": "^3.0.2",
"xo": "^0.33.0"
},
"dependencies": {
"is-plain-obj": "^2.0.0"
"is-plain-obj": "^2.1.0"
},
"xo": {
"rules": {
"import/extensions": "off",
"import/no-useless-path-segments": "off",
"unicorn/import-index": "off"
}
}
}
14 changes: 7 additions & 7 deletions test/arrays.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from 'ava';
import mergeOptions from '..';
import mergeOptions from '../index.js';

test('support array values', t => {
const array1 = ['foo', 'bar'];
Expand Down Expand Up @@ -51,10 +51,10 @@ test('support concatenation of sparsed arrays via apply', t => {
});

test('clone option objects', t => {
const plainObj1 = {value: 'foo'};
const plainObj2 = {value: 'bar'};
const result = mergeOptions({array: [plainObj1]}, {array: [plainObj2]});
t.deepEqual(result.array, [plainObj2]);
t.not(result.array[0], plainObj1);
t.not(result.array[0], plainObj2);
const plainObject1 = {value: 'foo'};
const plainObject2 = {value: 'bar'};
const result = mergeOptions({array: [plainObject1]}, {array: [plainObject2]});
t.deepEqual(result.array, [plainObject2]);
t.not(result.array[0], plainObject1);
t.not(result.array[0], plainObject2);
});
2 changes: 1 addition & 1 deletion test/option-objects.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from 'ava';
import mergeOptions from '..';
import mergeOptions from '../index.js';

test('ignore `undefined` Option Objects', t => {
t.deepEqual(mergeOptions(undefined), {});
Expand Down
12 changes: 6 additions & 6 deletions test/option-values.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import test from 'ava';
import mergeOptions from '..';
import mergeOptions from '../index.js';

function toString(value) {
try {
return String(value);
} catch (_) {
} catch {
return typeof value;
}
}
Expand All @@ -21,10 +21,10 @@ test('throw TypeError on non-option-objects', async t => {
function () {},
null
].forEach(value => {
t.throws(() => mergeOptions(value), TypeError, toString(value));
t.throws(() => mergeOptions({}, value), TypeError, toString(value));
t.throws(() => mergeOptions({foo: 'bar'}, value), TypeError, toString(value));
t.throws(() => mergeOptions(Object.create(null), value), TypeError, toString(value));
t.throws(() => mergeOptions(value), {instanceOf: TypeError}, toString(value));
t.throws(() => mergeOptions({}, value), {instanceOf: TypeError}, toString(value));
t.throws(() => mergeOptions({foo: 'bar'}, value), {instanceOf: TypeError}, toString(value));
t.throws(() => mergeOptions(Object.create(null), value), {instanceOf: TypeError}, toString(value));
});

await t.throwsAsync(promise);
Expand Down
2 changes: 1 addition & 1 deletion test/property-order.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from 'ava';
import mergeOptions from '..';
import mergeOptions from '../index.js';

test('preserve property order', t => {
const letters = 'abcdefghijklmnopqrst';
Expand Down
4 changes: 2 additions & 2 deletions test/prototype-pollution.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import test from 'ava';
import mergeOptions from '..';
import mergeOptions from '../index.js';

const defineProtoProperty = (obj, value) => Object.defineProperty(obj, '__proto__', {
const defineProtoProperty = (options, value) => Object.defineProperty(options, '__proto__', {
value,
writable: true,
enumerable: true,
Expand Down
24 changes: 12 additions & 12 deletions test/readme-api.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
import test from 'ava';
import mergeOptions from '..';
import mergeOptions from '../index.js';

test('cloning example', async t => {
const defaultPromise = Promise.reject(new Error());
const optsPromise = Promise.resolve('bar');
const defaultOpts = {
const optionsPromise = Promise.resolve('bar');
const defaultOptions = {
fn: () => false,
promise: defaultPromise,
array: ['foo'],
nested: {unicorns: 'none'}
};
const opts = {
const options = {
fn: () => true,
promise: optsPromise,
promise: optionsPromise,
array: ['baz'],
nested: {unicorns: 'many'}
};
const result = mergeOptions(defaultOpts, opts);
t.deepEqual(result, opts);
t.is(result.fn, opts.fn);
t.is(result.promise, opts.promise);
t.not(result.array, opts.array);
t.not(result.nested, opts.nested);
const result = mergeOptions(defaultOptions, options);
t.deepEqual(result, options);
t.is(result.fn, options.fn);
t.is(result.promise, options.promise);
t.not(result.array, options.array);
t.not(result.nested, options.nested);
await t.throwsAsync(defaultPromise);
await t.notThrowsAsync(optsPromise);
await t.notThrowsAsync(optionsPromise);
});

test('array.concat example', t => {
Expand Down
2 changes: 1 addition & 1 deletion test/readme-usage.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from 'ava';
import mergeOptions from '..';
import mergeOptions from '../index.js';

test('basic examples', t => {
t.deepEqual(
Expand Down
2 changes: 1 addition & 1 deletion test/symbol-keys.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from 'ava';
import mergeOptions from '..';
import mergeOptions from '../index.js';

test('return new option objects', t => {
const fooKey = Symbol('foo');
Expand Down
2 changes: 1 addition & 1 deletion test/undefined-values.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from 'ava';
import mergeOptions from '..';
import mergeOptions from '../index.js';

test('undefined values', t => {
t.deepEqual(
Expand Down
2 changes: 1 addition & 1 deletion test/user-extended-natives.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint no-extend-native:0, no-use-extend-native/no-use-extend-native:0 */
import test from 'ava';
import fn from '..';
import fn from '../index.js';

test('ignore non-own properties', t => {
const optionObject = {foo: 'bar'};
Expand Down

0 comments on commit 5e88bce

Please sign in to comment.