123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- 'use strict';
- var typeOf = require('kind-of');
- var copyDescriptor = require('copy-descriptor');
- var define = require('define-property');
- function copy(receiver, provider, omit) {
- if (!isObject(receiver)) {
- throw new TypeError('expected receiving object to be an object.');
- }
- if (!isObject(provider)) {
- throw new TypeError('expected providing object to be an object.');
- }
- var props = nativeKeys(provider);
- var keys = Object.keys(provider);
- var len = props.length;
- omit = arrayify(omit);
- while (len--) {
- var key = props[len];
- if (has(keys, key)) {
- define(receiver, key, provider[key]);
- } else if (!(key in receiver) && !has(omit, key)) {
- copyDescriptor(receiver, provider, key);
- }
- }
- };
- function isObject(val) {
- return typeOf(val) === 'object' || typeof val === 'function';
- }
- function has(obj, val) {
- val = arrayify(val);
- var len = val.length;
- if (isObject(obj)) {
- for (var key in obj) {
- if (val.indexOf(key) > -1) {
- return true;
- }
- }
- var keys = nativeKeys(obj);
- return has(keys, val);
- }
- if (Array.isArray(obj)) {
- var arr = obj;
- while (len--) {
- if (arr.indexOf(val[len]) > -1) {
- return true;
- }
- }
- return false;
- }
- throw new TypeError('expected an array or object.');
- }
- function arrayify(val) {
- return val ? (Array.isArray(val) ? val : [val]) : [];
- }
- function hasConstructor(val) {
- return isObject(val) && typeof val.constructor !== 'undefined';
- }
- function nativeKeys(val) {
- if (!hasConstructor(val)) return [];
- return Object.getOwnPropertyNames(val);
- }
- module.exports = copy;
- module.exports.has = has;
|