Last change
on this file since 84d0fbb was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
700 bytes
|
Rev | Line | |
---|
[6a3a178] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | var isObject = require('is-extendable');
|
---|
| 4 | var union = require('arr-union');
|
---|
| 5 | var get = require('get-value');
|
---|
| 6 | var set = require('set-value');
|
---|
| 7 |
|
---|
| 8 | module.exports = function unionValue(obj, prop, value) {
|
---|
| 9 | if (!isObject(obj)) {
|
---|
| 10 | throw new TypeError('union-value expects the first argument to be an object.');
|
---|
| 11 | }
|
---|
| 12 |
|
---|
| 13 | if (typeof prop !== 'string') {
|
---|
| 14 | throw new TypeError('union-value expects `prop` to be a string.');
|
---|
| 15 | }
|
---|
| 16 |
|
---|
| 17 | var arr = arrayify(get(obj, prop));
|
---|
| 18 | set(obj, prop, union(arr, arrayify(value)));
|
---|
| 19 | return obj;
|
---|
| 20 | };
|
---|
| 21 |
|
---|
| 22 | function arrayify(val) {
|
---|
| 23 | if (val === null || typeof val === 'undefined') {
|
---|
| 24 | return [];
|
---|
| 25 | }
|
---|
| 26 | if (Array.isArray(val)) {
|
---|
| 27 | return val;
|
---|
| 28 | }
|
---|
| 29 | return [val];
|
---|
| 30 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.