diff --git a/.travis.yml b/.travis.yml index 58c605f..ba52fe3 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,9 +6,6 @@ os: language: node_js node_js: - node - - '11' - - '10' - - '9' - - '8' - - '7' - - '6' + - '16' + - '14' + - '12' diff --git a/README.md b/README.md index 5ac602a..78cfdd4 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,8 @@ $ npm install --save union-value ## Usage ```js -const union = require('union-value'); +import union from 'union-value'; + const obj = {}; union(obj, 'a.b.c', ['one', 'two']); @@ -70,10 +71,10 @@ You might also be interested in these projects: ### Contributors -| **Commits** | **Contributor** | -| --- | --- | -| 23 | [jonschlinkert](https://github.com/jonschlinkert) | -| 1 | [wtgtybhertgeghgtwtg](https://github.com/wtgtybhertgeghgtwtg) | +| **Commits** | **Contributor** | +| --- | --- | +| 23 | [jonschlinkert](https://github.com/jonschlinkert) | +| 1 | [wtgtybhertgeghgtwtg](https://github.com/wtgtybhertgeghgtwtg) | ### Author @@ -90,4 +91,4 @@ Released under the [MIT License](LICENSE). *** -_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.8.0, on June 20, 2019._ \ No newline at end of file +_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.8.0, on June 20, 2019._ diff --git a/index.js b/index.js index 1841d07..85fb4dd 100644 --- a/index.js +++ b/index.js @@ -1,26 +1,14 @@ -'use strict'; - -const get = require('get-value'); -const set = require('set-value'); +import get from 'get-value'; +import set from 'set-value'; const isObject = val => { return val != null && typeof val === 'object' && !Array.isArray(val); }; -const flatten = (...args) => { - let res = []; - let flat = arr => { - for (let ele of arr) Array.isArray(ele) ? flat(ele, res) : res.push(ele); - }; - flat(args); - return res; -}; - -const unique = arr => arr.filter((v, i) => arr.indexOf(v) === i); -const union = (...args) => unique(flatten(...args)); +const union = (...args) => [...new Set([...args].flat())]; const first = (...args) => args.find(v => v != null); -module.exports = (obj, prop, value) => { +export default (obj, prop, value) => { if (!isObject(obj)) { throw new TypeError('expected the first argument to be an object'); } @@ -29,7 +17,7 @@ module.exports = (obj, prop, value) => { throw new TypeError('expected the second argument to be a string'); } - let arr = [].concat(first(get(obj, prop), [])); + const arr = [].concat(first(get(obj, prop), [])); set(obj, prop, union(arr, [].concat(first(value, [])))); return obj; }; diff --git a/package.json b/package.json index e8b0df2..eb3dbf2 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,7 @@ "homepage": "https://github.com/jonschlinkert/union-value", "author": "Jon Schlinkert (https://github.com/jonschlinkert)", "repository": "jonschlinkert/union-value", + "type": "module", "bugs": { "url": "https://github.com/jonschlinkert/union-value/issues" }, @@ -14,15 +15,11 @@ ], "main": "index.js", "engines": { - "node": ">=6" + "node": ">=12.20" }, "scripts": { "test": "mocha" }, - "dependencies": { - "get-value": "^3.0.1", - "set-value": "^3.0.0" - }, "keywords": [ "array", "dot", @@ -60,8 +57,12 @@ "reflinks": true } }, + "dependencies": { + "get-value": "^3.0.1", + "set-value": "^4.1.0" + }, "devDependencies": { "gulp-format-md": "^2.0.0", - "mocha": "^6.1.4" + "mocha": "^9.0.3" } } diff --git a/test.js b/test.js index 057ff48..3035638 100644 --- a/test.js +++ b/test.js @@ -1,38 +1,36 @@ -'use strict'; - -require('mocha'); -const assert = require('assert'); -const union = require('./'); +import { deepEqual, throws } from 'node:assert'; +import 'mocha'; +import union from './index.js'; describe('union', () => { it('should add a value:', () => { let obj = {}; union(obj, 'foo', ['a', 'b']) - assert.deepEqual(obj.foo, ['a', 'b']); + deepEqual(obj.foo, ['a', 'b']); }); it('should union a value:', () => { let obj = {foo: ['a', 'b']}; union(obj, 'foo', ['c', 'd', 'e']) - assert.deepEqual(obj.foo, ['a', 'b', 'c', 'd', 'e']); + deepEqual(obj.foo, ['a', 'b', 'c', 'd', 'e']); }); it('should union a deeply nested value:', () => { let obj = {}; union(obj, 'a.b.c', ['one', 'two']); union(obj, 'a.b.c', ['three']); - assert.deepEqual(obj.a.b.c, ['one', 'two', 'three']); + deepEqual(obj.a.b.c, ['one', 'two', 'three']); }); it('should support escaped dots:', () => { let obj = {}; union(obj, 'a\\.b.c', ['one', 'two']); union(obj, 'a\\.b.c', ['three']); - assert.deepEqual(obj['a.b'].c, ['one', 'two', 'three']); + deepEqual(obj['a.b'].c, ['one', 'two', 'three']); }); it('should throw an error:', () => { - assert.throws(() => union(), /expected the first argument to be an object/); - assert.throws(() => union({}), /expected the second argument to be a string/); + throws(() => union(), /expected the first argument to be an object/); + throws(() => union({}), /expected the second argument to be a string/); }); });