Skip to content

Commit

Permalink
Require Node.js 12 and move to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Apr 7, 2021
1 parent 1154be7 commit 08c06ad
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 70 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ jobs:
node-version:
- 14
- 12
- 10
- 8
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
Expand Down
26 changes: 11 additions & 15 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
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.
@default false
*/
readonly ignoreNonConfigurable?: boolean;
}
export 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;
}

/**
Expand All @@ -22,7 +20,7 @@ Modifies the `to` function to mimic the `from` function. Returns the `to` functi
@example
```
import mimicFn = require('mimic-fn');
import mimicFunction from 'mimic-fn';
function foo() {}
foo.unicorn = '🦄';
Expand All @@ -34,7 +32,7 @@ function wrapper() {
console.log(wrapper.name);
//=> 'wrapper'
mimicFn(wrapper, foo);
mimicFunction(wrapper, foo);
console.log(wrapper.name);
//=> 'foo'
Expand All @@ -43,14 +41,12 @@ console.log(wrapper.unicorn);
//=> '🦄'
```
*/
declare function mimicFn<
export default function mimicFunction<
ArgumentsType extends unknown[],
ReturnType,
FunctionType extends (...arguments: ArgumentsType) => ReturnType
>(
to: (...arguments: ArgumentsType) => ReturnType,
from: FunctionType,
options?: mimicFn.Options,
options?: Options,
): FunctionType;

export = mimicFn;
12 changes: 4 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'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.
Expand All @@ -23,8 +21,8 @@ const copyProperty = (to, from, property, ignoreNonConfigurable) => {
};

// `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
// - 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 &&
Expand Down Expand Up @@ -59,7 +57,7 @@ const changeToString = (to, from, name) => {
Object.defineProperty(to, 'toString', {...toStringDescriptor, value: newToString});
};

const mimicFn = (to, from, {ignoreNonConfigurable = false} = {}) => {
export default function mimicFunction(to, from, {ignoreNonConfigurable = false} = {}) {
const {name} = to;

for (const property of Reflect.ownKeys(from)) {
Expand All @@ -70,6 +68,4 @@ const mimicFn = (to, from, {ignoreNonConfigurable = false} = {}) => {
changeToString(to, from, name);

return to;
};

module.exports = mimicFn;
}
15 changes: 7 additions & 8 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import {expectType} from 'tsd';
import mimicFn = require('.');
import {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 mimickedFn = mimicFn(wrapper, foo);

expectType<typeof foo & {unicorn: string}>(mimickedFn);

expectType<boolean>(mimickedFn('bar'));
expectType<string>(mimickedFn.unicorn);
const mimickedFunction = mimicFunction(wrapper, foo);
expectAssignable<typeof foo & {unicorn: string}>(mimickedFunction);
expectType<boolean>(mimickedFunction('bar'));
expectType<string>(mimickedFunction.unicorn);
2 changes: 1 addition & 1 deletion license
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (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:

Expand Down
13 changes: 8 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
"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": "sindresorhus.com"
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=8"
"node": ">=12"
},
"scripts": {
"test": "xo && ava && tsd"
Expand All @@ -35,8 +38,8 @@
"change"
],
"devDependencies": {
"ava": "^2.1.0",
"tsd": "^0.7.1",
"xo": "^0.24.0"
"ava": "^3.15.0",
"tsd": "^0.14.0",
"xo": "^0.38.2"
}
}
11 changes: 4 additions & 7 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,16 @@
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
const mimicFn = require('mimic-fn');
import mimicFunction from 'mimic-fn';

function foo() {}
foo.unicorn = '🦄';
Expand All @@ -28,7 +26,7 @@ function wrapper() {
console.log(wrapper.name);
//=> 'wrapper'

mimicFn(wrapper, foo);
mimicFunction(wrapper, foo);

console.log(wrapper.name);
//=> 'foo'
Expand All @@ -43,7 +41,7 @@ console.log(String(wrapper));

## API

### mimicFn(to, from, options?)
### mimicFunction(to, from, options?)

Modifies the `to` function to mimic the `from` function. Returns the `to` function.

Expand All @@ -69,7 +67,7 @@ Type: `object`

##### ignoreNonConfigurable

Type: `boolean`<br>
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.
Expand All @@ -79,7 +77,6 @@ Skip modifying [non-configurable properties](https://developer.mozilla.org/en-US
- [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


---

<div align="center">
Expand Down

0 comments on commit 08c06ad

Please sign in to comment.