Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ os:
language: node_js
node_js:
- node
- '11'
- '10'
- '9'
- '8'
- '7'
- '6'
- '16'
- '14'
- '12'
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down Expand Up @@ -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

Expand All @@ -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._
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.8.0, on June 20, 2019._
22 changes: 5 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
@@ -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');
}
Expand All @@ -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;
};
13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand All @@ -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",
Expand Down Expand Up @@ -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"
}
}
20 changes: 9 additions & 11 deletions test.js
Original file line number Diff line number Diff line change
@@ -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/);
});
});