| Leo Repp | 58b9f11 | 2021-11-22 11:57:47 +0100 | [diff] [blame^] | 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 | } |