pax_global_header00006660000000000000000000000064140334032650014512gustar00rootroot0000000000000052 comment=c8994d7c70a5e1e74efe97e12394b9878f943dab
mimic-fn-4.0.0/000077500000000000000000000000001403340326500132125ustar00rootroot00000000000000mimic-fn-4.0.0/.editorconfig000066400000000000000000000002571403340326500156730ustar00rootroot00000000000000root = true
[*]
indent_style = tab
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
[*.yml]
indent_style = space
indent_size = 2
mimic-fn-4.0.0/.gitattributes000066400000000000000000000000231403340326500161000ustar00rootroot00000000000000* text=auto eol=lf
mimic-fn-4.0.0/.github/000077500000000000000000000000001403340326500145525ustar00rootroot00000000000000mimic-fn-4.0.0/.github/funding.yml000066400000000000000000000001621403340326500167260ustar00rootroot00000000000000github: sindresorhus
open_collective: sindresorhus
tidelift: npm/mimic-fn
custom: https://sindresorhus.com/donate
mimic-fn-4.0.0/.github/security.md000066400000000000000000000002631403340326500167440ustar00rootroot00000000000000# Security Policy
To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure.
mimic-fn-4.0.0/.github/workflows/000077500000000000000000000000001403340326500166075ustar00rootroot00000000000000mimic-fn-4.0.0/.github/workflows/main.yml000066400000000000000000000006451403340326500202630ustar00rootroot00000000000000name: CI
on:
- push
- pull_request
jobs:
test:
name: Node.js ${{ matrix.node-version }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
node-version:
- 14
- 12
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test
mimic-fn-4.0.0/.gitignore000066400000000000000000000000271403340326500152010ustar00rootroot00000000000000node_modules
yarn.lock
mimic-fn-4.0.0/.npmrc000066400000000000000000000000231403340326500143250ustar00rootroot00000000000000package-lock=false
mimic-fn-4.0.0/index.d.ts000066400000000000000000000024511403340326500151150ustar00rootroot00000000000000export interface Options {
/**
Skip modifying [non-configurable properties](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyDescriptor#Description) instead of throwing an error.
@default false
*/
readonly ignoreNonConfigurable?: boolean;
}
/**
Modifies the `to` function to mimic the `from` function. Returns the `to` function.
`name`, `displayName`, and any other properties of `from` are copied. The `length` property is not copied. Prototype, class, and inherited properties are copied.
`to.toString()` will return the same as `from.toString()` but prepended with a `Wrapped with to()` comment.
@param to - Mimicking function.
@param from - Function to mimic.
@returns The modified `to` function.
@example
```
import mimicFunction from 'mimic-fn';
function foo() {}
foo.unicorn = '🦄';
function wrapper() {
return foo();
}
console.log(wrapper.name);
//=> 'wrapper'
mimicFunction(wrapper, foo);
console.log(wrapper.name);
//=> 'foo'
console.log(wrapper.unicorn);
//=> '🦄'
```
*/
export default function mimicFunction<
ArgumentsType extends unknown[],
ReturnType,
FunctionType extends (...arguments: ArgumentsType) => ReturnType
>(
to: (...arguments: ArgumentsType) => ReturnType,
from: FunctionType,
options?: Options,
): FunctionType;
mimic-fn-4.0.0/index.js000066400000000000000000000055271403340326500146700ustar00rootroot00000000000000const copyProperty = (to, from, property, ignoreNonConfigurable) => {
// `Function#length` should reflect the parameters of `to` not `from` since we keep its body.
// `Function#prototype` is non-writable and non-configurable so can never be modified.
if (property === 'length' || property === 'prototype') {
return;
}
// `Function#arguments` and `Function#caller` should not be copied. They were reported to be present in `Reflect.ownKeys` for some devices in React Native (#41), so we explicitly ignore them here.
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);
};
// `Object.defineProperty()` throws if the property exists, is not configurable and either:
// - one its descriptors is changed
// - it is non-writable and its value is changed
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');
// We call `from.toString()` early (not lazily) to ensure `from` can be garbage collected.
// We use `bind()` instead of a closure for the same reason.
// Calling `from.toString()` early also allows caching it in case `to.toString()` is called several times.
const changeToString = (to, from, name) => {
const withName = name === '' ? '' : `with ${name.trim()}() `;
const newToString = wrappedToString.bind(null, withName, from.toString());
// Ensure `to.toString.toString` is non-enumerable and has the same `same`
Object.defineProperty(newToString, 'name', toStringName);
Object.defineProperty(to, 'toString', {...toStringDescriptor, value: newToString});
};
export default function mimicFunction(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;
}
mimic-fn-4.0.0/index.test-d.ts000066400000000000000000000006641403340326500160760ustar00rootroot00000000000000import {expectType, expectAssignable} from 'tsd';
import mimicFunction from './index.js';
function foo(string: string) {
return false;
}
foo.unicorn = '🦄';
function wrapper(string: string) {
return foo(string);
}
const mimickedFunction = mimicFunction(wrapper, foo);
expectAssignable(mimickedFunction);
expectType(mimickedFunction('bar'));
expectType(mimickedFunction.unicorn);
mimic-fn-4.0.0/license000066400000000000000000000021351403340326500145600ustar00rootroot00000000000000MIT License
Copyright (c) Sindre Sorhus (https://sindresorhus.com)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
mimic-fn-4.0.0/media/000077500000000000000000000000001403340326500142715ustar00rootroot00000000000000mimic-fn-4.0.0/media/logo.svg000066400000000000000000000047561403340326500157660ustar00rootroot00000000000000mimic-fn-4.0.0/package.json000066400000000000000000000013611403340326500155010ustar00rootroot00000000000000{
"name": "mimic-fn",
"version": "4.0.0",
"description": "Make a function mimic another one",
"license": "MIT",
"repository": "sindresorhus/mimic-fn",
"funding": "https://github.com/sponsors/sindresorhus",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=12"
},
"scripts": {
"test": "xo && ava && tsd"
},
"files": [
"index.js",
"index.d.ts"
],
"keywords": [
"function",
"mimic",
"imitate",
"rename",
"copy",
"inherit",
"properties",
"name",
"func",
"fn",
"set",
"infer",
"change"
],
"devDependencies": {
"ava": "^3.15.0",
"tsd": "^0.14.0",
"xo": "^0.38.2"
}
}
mimic-fn-4.0.0/readme.md000066400000000000000000000040451403340326500147740ustar00rootroot00000000000000
> Make a function mimic another one
Useful when you wrap a function in another function and like to preserve the original name and other properties.
## Install
```
$ npm install mimic-fn
```
## Usage
```js
import mimicFunction from 'mimic-fn';
function foo() {}
foo.unicorn = '🦄';
function wrapper() {
return foo();
}
console.log(wrapper.name);
//=> 'wrapper'
mimicFunction(wrapper, foo);
console.log(wrapper.name);
//=> 'foo'
console.log(wrapper.unicorn);
//=> '🦄'
console.log(String(wrapper));
//=> '/* Wrapped with wrapper() */\nfunction foo() {}'
```
## API
### mimicFunction(to, from, options?)
Modifies the `to` function to mimic the `from` function. Returns the `to` function.
`name`, `displayName`, and any other properties of `from` are copied. The `length` property is not copied. Prototype, class, and inherited properties are copied.
`to.toString()` will return the same as `from.toString()` but prepended with a `Wrapped with to()` comment.
#### to
Type: `Function`
Mimicking function.
#### from
Type: `Function`
Function to mimic.
#### options
Type: `object`
##### ignoreNonConfigurable
Type: `boolean`\
Default: `false`
Skip modifying [non-configurable properties](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyDescriptor#Description) instead of throwing an error.
## Related
- [rename-fn](https://github.com/sindresorhus/rename-fn) - Rename a function
- [keep-func-props](https://github.com/ehmicky/keep-func-props) - Wrap a function without changing its name and other properties
---