UNPKG

assign-deep

Version:

Deeply assign the values of all enumerable-own-properties and symbols from one or more source objects to a target object. Returns the target object.

45 lines (38 loc) 1.14 kB
/*! * assign-deep <https://github.com/jonschlinkert/assign-deep> * * Copyright (c) 2017-present, Jon Schlinkert. * Released under the MIT License. */ 'use strict'; const toString = Object.prototype.toString; const assignSymbols = require('assign-symbols'); const isValidKey = key => { return key !== '__proto__' && key !== 'constructor' && key !== 'prototype'; }; const assign = module.exports = (target, ...args) => { let i = 0; if (isPrimitive(target)) target = args[i++]; if (!target) target = {}; for (; i < args.length; i++) { if (isObject(args[i])) { for (const key of Object.keys(args[i])) { if (isValidKey(key)) { if (isObject(target[key]) && isObject(args[i][key])) { assign(target[key], args[i][key]); } else { target[key] = args[i][key]; } } } assignSymbols(target, args[i]); } } return target; }; function isObject(val) { return typeof val === 'function' || toString.call(val) === '[object Object]'; } function isPrimitive(val) { return typeof val === 'object' ? val === null : typeof val !== 'function'; }