forked from daren.hsu/line_push
update
This commit is contained in:
+45
-47
@@ -1,56 +1,54 @@
|
||||
declare namespace mimicFn {
|
||||
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.
|
||||
declare const mimicFn: {
|
||||
/**
|
||||
Make a function mimic another one. It will copy over the properties `name`, `length`, `displayName`, and any custom properties you may have set.
|
||||
|
||||
@default false
|
||||
*/
|
||||
readonly ignoreNonConfigurable?: boolean;
|
||||
@param to - Mimicking function.
|
||||
@param from - Function to mimic.
|
||||
@returns The modified `to` function.
|
||||
|
||||
@example
|
||||
```
|
||||
import mimicFn = require('mimic-fn');
|
||||
|
||||
function foo() {}
|
||||
foo.unicorn = '🦄';
|
||||
|
||||
function wrapper() {
|
||||
return foo();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
Modifies the `to` function to mimic the `from` function. Returns the `to` function.
|
||||
console.log(wrapper.name);
|
||||
//=> 'wrapper'
|
||||
|
||||
`name`, `displayName`, and any other properties of `from` are copied. The `length` property is not copied. Prototype, class, and inherited properties are copied.
|
||||
mimicFn(wrapper, foo);
|
||||
|
||||
`to.toString()` will return the same as `from.toString()` but prepended with a `Wrapped with to()` comment.
|
||||
console.log(wrapper.name);
|
||||
//=> 'foo'
|
||||
|
||||
@param to - Mimicking function.
|
||||
@param from - Function to mimic.
|
||||
@returns The modified `to` function.
|
||||
console.log(wrapper.unicorn);
|
||||
//=> '🦄'
|
||||
```
|
||||
*/
|
||||
<
|
||||
ArgumentsType extends unknown[],
|
||||
ReturnType,
|
||||
FunctionType extends (...arguments: ArgumentsType) => ReturnType
|
||||
>(
|
||||
to: (...arguments: ArgumentsType) => ReturnType,
|
||||
from: FunctionType
|
||||
): FunctionType;
|
||||
|
||||
@example
|
||||
```
|
||||
import mimicFn = require('mimic-fn');
|
||||
|
||||
function foo() {}
|
||||
foo.unicorn = '🦄';
|
||||
|
||||
function wrapper() {
|
||||
return foo();
|
||||
}
|
||||
|
||||
console.log(wrapper.name);
|
||||
//=> 'wrapper'
|
||||
|
||||
mimicFn(wrapper, foo);
|
||||
|
||||
console.log(wrapper.name);
|
||||
//=> 'foo'
|
||||
|
||||
console.log(wrapper.unicorn);
|
||||
//=> '🦄'
|
||||
```
|
||||
*/
|
||||
declare function mimicFn<
|
||||
ArgumentsType extends unknown[],
|
||||
ReturnType,
|
||||
FunctionType extends (...arguments: ArgumentsType) => ReturnType
|
||||
>(
|
||||
to: (...arguments: ArgumentsType) => ReturnType,
|
||||
from: FunctionType,
|
||||
options?: mimicFn.Options,
|
||||
): FunctionType;
|
||||
// TODO: Remove this for the next major release, refactor the whole definition to:
|
||||
// declare function mimicFn<
|
||||
// ArgumentsType extends unknown[],
|
||||
// ReturnType,
|
||||
// FunctionType extends (...arguments: ArgumentsType) => ReturnType
|
||||
// >(
|
||||
// to: (...arguments: ArgumentsType) => ReturnType,
|
||||
// from: FunctionType
|
||||
// ): FunctionType;
|
||||
// export = mimicFn;
|
||||
default: typeof mimicFn;
|
||||
};
|
||||
|
||||
export = mimicFn;
|
||||
|
||||
+5
-62
@@ -1,70 +1,13 @@
|
||||
'use strict';
|
||||
|
||||
const 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;
|
||||
const mimicFn = (to, from) => {
|
||||
for (const prop of Reflect.ownKeys(from)) {
|
||||
Object.defineProperty(to, prop, Object.getOwnPropertyDescriptor(from, prop));
|
||||
}
|
||||
|
||||
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});
|
||||
};
|
||||
|
||||
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;
|
||||
// TODO: Remove this for the next major release
|
||||
module.exports.default = mimicFn;
|
||||
|
||||
+15
-15
@@ -1,32 +1,32 @@
|
||||
{
|
||||
"_args": [
|
||||
[
|
||||
"mimic-fn@3.0.0",
|
||||
"/mnt/Foxconn/Digitalent/Deverloper/liff-push_2series"
|
||||
"mimic-fn@2.1.0",
|
||||
"/home/node/nuxt"
|
||||
]
|
||||
],
|
||||
"_from": "mimic-fn@3.0.0",
|
||||
"_id": "mimic-fn@3.0.0",
|
||||
"_from": "mimic-fn@2.1.0",
|
||||
"_id": "mimic-fn@2.1.0",
|
||||
"_inBundle": false,
|
||||
"_integrity": "sha512-PiVO95TKvhiwgSwg1IdLYlCTdul38yZxZMIcnDSFIBUm4BNZha2qpQ4GpJ++15bHoKDtrW2D69lMfFwdFYtNZQ==",
|
||||
"_integrity": "sha512-OqbOk5oEQeAZ8WXWydlu9HJjz9WVdEIvamMCcXmuqUYjTknH/sqsWvhQ3vgwKFRR1HpjvNBKQ37nbJgYzGqGcg==",
|
||||
"_location": "/mimic-fn",
|
||||
"_phantomChildren": {},
|
||||
"_requested": {
|
||||
"type": "version",
|
||||
"registry": true,
|
||||
"raw": "mimic-fn@3.0.0",
|
||||
"raw": "mimic-fn@2.1.0",
|
||||
"name": "mimic-fn",
|
||||
"escapedName": "mimic-fn",
|
||||
"rawSpec": "3.0.0",
|
||||
"rawSpec": "2.1.0",
|
||||
"saveSpec": null,
|
||||
"fetchSpec": "3.0.0"
|
||||
"fetchSpec": "2.1.0"
|
||||
},
|
||||
"_requiredBy": [
|
||||
"/mem"
|
||||
"/onetime"
|
||||
],
|
||||
"_resolved": "https://registry.npmjs.org/mimic-fn/-/mimic-fn-3.0.0.tgz",
|
||||
"_spec": "3.0.0",
|
||||
"_where": "/mnt/Foxconn/Digitalent/Deverloper/liff-push_2series",
|
||||
"_resolved": "https://registry.npmjs.org/mimic-fn/-/mimic-fn-2.1.0.tgz",
|
||||
"_spec": "2.1.0",
|
||||
"_where": "/home/node/nuxt",
|
||||
"author": {
|
||||
"name": "Sindre Sorhus",
|
||||
"email": "sindresorhus@gmail.com",
|
||||
@@ -37,12 +37,12 @@
|
||||
},
|
||||
"description": "Make a function mimic another one",
|
||||
"devDependencies": {
|
||||
"ava": "^2.1.0",
|
||||
"ava": "^1.4.1",
|
||||
"tsd": "^0.7.1",
|
||||
"xo": "^0.24.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=8"
|
||||
"node": ">=6"
|
||||
},
|
||||
"files": [
|
||||
"index.js",
|
||||
@@ -73,5 +73,5 @@
|
||||
"scripts": {
|
||||
"test": "xo && ava && tsd"
|
||||
},
|
||||
"version": "3.0.0"
|
||||
"version": "2.1.0"
|
||||
}
|
||||
|
||||
+7
-33
@@ -1,7 +1,4 @@
|
||||
<img src="media/logo.svg" alt="mimic-fn" width="400">
|
||||
<br>
|
||||
|
||||
[](https://travis-ci.org/sindresorhus/mimic-fn)
|
||||
# mimic-fn [](https://travis-ci.org/sindresorhus/mimic-fn)
|
||||
|
||||
> Make a function mimic another one
|
||||
|
||||
@@ -37,21 +34,16 @@ console.log(wrapper.name);
|
||||
|
||||
console.log(wrapper.unicorn);
|
||||
//=> '🦄'
|
||||
|
||||
console.log(String(wrapper));
|
||||
//=> '/* Wrapped with wrapper() */\nfunction foo() {}'
|
||||
```
|
||||
|
||||
|
||||
## API
|
||||
|
||||
### mimicFn(to, from, options?)
|
||||
It will copy over the properties `name`, `length`, `displayName`, and any custom properties you may have set.
|
||||
|
||||
Modifies the `to` function to mimic the `from` function. Returns the `to` function.
|
||||
### mimicFn(to, from)
|
||||
|
||||
`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.
|
||||
Modifies the `to` function and returns it.
|
||||
|
||||
#### to
|
||||
|
||||
@@ -65,31 +57,13 @@ Type: `Function`
|
||||
|
||||
Function to mimic.
|
||||
|
||||
#### options
|
||||
|
||||
Type: `object`
|
||||
|
||||
##### ignoreNonConfigurable
|
||||
|
||||
Type: `boolean`<br>
|
||||
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
|
||||
- [keep-func-props](https://github.com/ehmicky/keep-func-props) - Wrap a function without changing its name, length and other properties
|
||||
|
||||
|
||||
---
|
||||
## License
|
||||
|
||||
<div align="center">
|
||||
<b>
|
||||
<a href="https://tidelift.com/subscription/pkg/npm-mimic-fn?utm_source=npm-mimic-fn&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a>
|
||||
</b>
|
||||
<br>
|
||||
<sub>
|
||||
Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
|
||||
</sub>
|
||||
</div>
|
||||
MIT © [Sindre Sorhus](https://sindresorhus.com)
|
||||
|
||||
Reference in New Issue
Block a user