123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- 'use strict';
- const copyProperty = (to, from, property, ignoreNonConfigurable) => {
-
-
- if (property === 'length' || property === 'prototype') {
- return;
- }
-
- if (property === 'arguments' || property === 'caller') {
- return;
- }
- const toDescriptor = Object.getOwnPropertyDescriptor(to, property);
- const fromDescriptor = Object.getOwnPropertyDescriptor(from, property);
- if (!canCopyProperty(toDescriptor, fromDescriptor) && ignoreNonConfigurable) {
- return;
- }
- Object.defineProperty(to, property, fromDescriptor);
- };
- const canCopyProperty = function (toDescriptor, fromDescriptor) {
- return toDescriptor === undefined || toDescriptor.configurable || (
- toDescriptor.writable === fromDescriptor.writable &&
- toDescriptor.enumerable === fromDescriptor.enumerable &&
- toDescriptor.configurable === fromDescriptor.configurable &&
- (toDescriptor.writable || toDescriptor.value === fromDescriptor.value)
- );
- };
- const changePrototype = (to, from) => {
- const fromPrototype = Object.getPrototypeOf(from);
- if (fromPrototype === Object.getPrototypeOf(to)) {
- return;
- }
- Object.setPrototypeOf(to, fromPrototype);
- };
- const wrappedToString = (withName, fromBody) => `/* Wrapped ${withName}*/\n${fromBody}`;
- const toStringDescriptor = Object.getOwnPropertyDescriptor(Function.prototype, 'toString');
- const toStringName = Object.getOwnPropertyDescriptor(Function.prototype.toString, 'name');
- const changeToString = (to, from, name) => {
- const withName = name === '' ? '' : `with ${name.trim()}() `;
- const newToString = wrappedToString.bind(null, withName, from.toString());
-
- Object.defineProperty(newToString, 'name', toStringName);
- Object.defineProperty(to, 'toString', {...toStringDescriptor, value: newToString});
- };
- const mimicFn = (to, from, {ignoreNonConfigurable = false} = {}) => {
- const {name} = to;
- for (const property of Reflect.ownKeys(from)) {
- copyProperty(to, from, property, ignoreNonConfigurable);
- }
- changePrototype(to, from);
- changeToString(to, from, name);
- return to;
- };
- module.exports = mimicFn;
|