@common.js/mimic-fn 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,5 @@
1
+ # @common.js/mimic-fn
2
+
3
+ The [mimic-fn](https://www.npmjs.com/package/mimic-fn) package exported as CommonJS modules.
4
+
5
+ Exported from [mimic-fn@4.0.0](https://www.npmjs.com/package/mimic-fn/v/4.0.0) using https://github.com/etienne-martin/common.js.
package/index.d.ts ADDED
@@ -0,0 +1,52 @@
1
+ export interface Options {
2
+ /**
3
+ 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.
4
+
5
+ @default false
6
+ */
7
+ readonly ignoreNonConfigurable?: boolean;
8
+ }
9
+
10
+ /**
11
+ Modifies the `to` function to mimic the `from` function. Returns the `to` function.
12
+
13
+ `name`, `displayName`, and any other properties of `from` are copied. The `length` property is not copied. Prototype, class, and inherited properties are copied.
14
+
15
+ `to.toString()` will return the same as `from.toString()` but prepended with a `Wrapped with to()` comment.
16
+
17
+ @param to - Mimicking function.
18
+ @param from - Function to mimic.
19
+ @returns The modified `to` function.
20
+
21
+ @example
22
+ ```
23
+ import mimicFunction from 'mimic-fn';
24
+
25
+ function foo() {}
26
+ foo.unicorn = '🦄';
27
+
28
+ function wrapper() {
29
+ return foo();
30
+ }
31
+
32
+ console.log(wrapper.name);
33
+ //=> 'wrapper'
34
+
35
+ mimicFunction(wrapper, foo);
36
+
37
+ console.log(wrapper.name);
38
+ //=> 'foo'
39
+
40
+ console.log(wrapper.unicorn);
41
+ //=> '🦄'
42
+ ```
43
+ */
44
+ export default function mimicFunction<
45
+ ArgumentsType extends unknown[],
46
+ ReturnType,
47
+ FunctionType extends (...arguments: ArgumentsType) => ReturnType
48
+ >(
49
+ to: (...arguments: ArgumentsType) => ReturnType,
50
+ from: FunctionType,
51
+ options?: Options,
52
+ ): FunctionType;
package/index.js ADDED
@@ -0,0 +1,136 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "default", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return mimicFunction;
9
+ }
10
+ });
11
+ function _defineProperty(obj, key, value) {
12
+ if (key in obj) {
13
+ Object.defineProperty(obj, key, {
14
+ value: value,
15
+ enumerable: true,
16
+ configurable: true,
17
+ writable: true
18
+ });
19
+ } else {
20
+ obj[key] = value;
21
+ }
22
+ return obj;
23
+ }
24
+ function _objectSpread(target) {
25
+ for(var i = 1; i < arguments.length; i++){
26
+ var source = arguments[i] != null ? arguments[i] : {};
27
+ var ownKeys = Object.keys(source);
28
+ if (typeof Object.getOwnPropertySymbols === "function") {
29
+ ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
30
+ return Object.getOwnPropertyDescriptor(source, sym).enumerable;
31
+ }));
32
+ }
33
+ ownKeys.forEach(function(key) {
34
+ _defineProperty(target, key, source[key]);
35
+ });
36
+ }
37
+ return target;
38
+ }
39
+ function ownKeys(object, enumerableOnly) {
40
+ var keys = Object.keys(object);
41
+ if (Object.getOwnPropertySymbols) {
42
+ var symbols = Object.getOwnPropertySymbols(object);
43
+ if (enumerableOnly) {
44
+ symbols = symbols.filter(function(sym) {
45
+ return Object.getOwnPropertyDescriptor(object, sym).enumerable;
46
+ });
47
+ }
48
+ keys.push.apply(keys, symbols);
49
+ }
50
+ return keys;
51
+ }
52
+ function _objectSpreadProps(target, source) {
53
+ source = source != null ? source : {};
54
+ if (Object.getOwnPropertyDescriptors) {
55
+ Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
56
+ } else {
57
+ ownKeys(Object(source)).forEach(function(key) {
58
+ Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
59
+ });
60
+ }
61
+ return target;
62
+ }
63
+ var copyProperty = function(to, from, property, ignoreNonConfigurable) {
64
+ // `Function#length` should reflect the parameters of `to` not `from` since we keep its body.
65
+ // `Function#prototype` is non-writable and non-configurable so can never be modified.
66
+ if (property === "length" || property === "prototype") {
67
+ return;
68
+ }
69
+ // `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.
70
+ if (property === "arguments" || property === "caller") {
71
+ return;
72
+ }
73
+ var toDescriptor = Object.getOwnPropertyDescriptor(to, property);
74
+ var fromDescriptor = Object.getOwnPropertyDescriptor(from, property);
75
+ if (!canCopyProperty(toDescriptor, fromDescriptor) && ignoreNonConfigurable) {
76
+ return;
77
+ }
78
+ Object.defineProperty(to, property, fromDescriptor);
79
+ };
80
+ // `Object.defineProperty()` throws if the property exists, is not configurable and either:
81
+ // - one its descriptors is changed
82
+ // - it is non-writable and its value is changed
83
+ var canCopyProperty = function canCopyProperty(toDescriptor, fromDescriptor) {
84
+ return toDescriptor === undefined || toDescriptor.configurable || toDescriptor.writable === fromDescriptor.writable && toDescriptor.enumerable === fromDescriptor.enumerable && toDescriptor.configurable === fromDescriptor.configurable && (toDescriptor.writable || toDescriptor.value === fromDescriptor.value);
85
+ };
86
+ var changePrototype = function(to, from) {
87
+ var fromPrototype = Object.getPrototypeOf(from);
88
+ if (fromPrototype === Object.getPrototypeOf(to)) {
89
+ return;
90
+ }
91
+ Object.setPrototypeOf(to, fromPrototype);
92
+ };
93
+ var wrappedToString = function(withName, fromBody) {
94
+ return "/* Wrapped ".concat(withName, "*/\n").concat(fromBody);
95
+ };
96
+ var toStringDescriptor = Object.getOwnPropertyDescriptor(Function.prototype, "toString");
97
+ var toStringName = Object.getOwnPropertyDescriptor(Function.prototype.toString, "name");
98
+ // We call `from.toString()` early (not lazily) to ensure `from` can be garbage collected.
99
+ // We use `bind()` instead of a closure for the same reason.
100
+ // Calling `from.toString()` early also allows caching it in case `to.toString()` is called several times.
101
+ var changeToString = function(to, from, name) {
102
+ var withName = name === "" ? "" : "with ".concat(name.trim(), "() ");
103
+ var newToString = wrappedToString.bind(null, withName, from.toString());
104
+ // Ensure `to.toString.toString` is non-enumerable and has the same `same`
105
+ Object.defineProperty(newToString, "name", toStringName);
106
+ Object.defineProperty(to, "toString", _objectSpreadProps(_objectSpread({}, toStringDescriptor), {
107
+ value: newToString
108
+ }));
109
+ };
110
+ function mimicFunction(to, from) {
111
+ var ref = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : {}, _ignoreNonConfigurable = ref.ignoreNonConfigurable, ignoreNonConfigurable = _ignoreNonConfigurable === void 0 ? false : _ignoreNonConfigurable;
112
+ var name = to.name;
113
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
114
+ try {
115
+ for(var _iterator = Reflect.ownKeys(from)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
116
+ var property = _step.value;
117
+ copyProperty(to, from, property, ignoreNonConfigurable);
118
+ }
119
+ } catch (err) {
120
+ _didIteratorError = true;
121
+ _iteratorError = err;
122
+ } finally{
123
+ try {
124
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
125
+ _iterator.return();
126
+ }
127
+ } finally{
128
+ if (_didIteratorError) {
129
+ throw _iteratorError;
130
+ }
131
+ }
132
+ }
133
+ changePrototype(to, from);
134
+ changeToString(to, from, name);
135
+ return to;
136
+ }
package/license ADDED
@@ -0,0 +1,9 @@
1
+ MIT License
2
+
3
+ Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
4
+
5
+ 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:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ 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.
package/package.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "name": "@common.js/mimic-fn",
3
+ "version": "4.0.0",
4
+ "description": "Make a function mimic another one",
5
+ "license": "MIT",
6
+ "repository": "etienne-martin/common.js",
7
+ "funding": "https://github.com/sponsors/sindresorhus",
8
+ "type": "commonjs",
9
+ "engines": {
10
+ "node": ">=12"
11
+ },
12
+ "scripts": {
13
+ "test": "xo && ava && tsd"
14
+ },
15
+ "files": [
16
+ "index.js",
17
+ "index.d.ts"
18
+ ],
19
+ "devDependencies": {
20
+ "ava": "^3.15.0",
21
+ "tsd": "^0.14.0",
22
+ "xo": "^0.38.2"
23
+ },
24
+ "homepage": "https://github.com/etienne-martin/common.js#readme",
25
+ "dependencies": {},
26
+ "main": "./index.js"
27
+ }
package/readme.md ADDED
@@ -0,0 +1,90 @@
1
+ <img src="media/logo.svg" alt="mimic-fn" width="400">
2
+ <br>
3
+
4
+ > Make a function mimic another one
5
+
6
+ Useful when you wrap a function in another function and like to preserve the original name and other properties.
7
+
8
+ ## Install
9
+
10
+ ```
11
+ $ npm install mimic-fn
12
+ ```
13
+
14
+ ## Usage
15
+
16
+ ```js
17
+ import mimicFunction from 'mimic-fn';
18
+
19
+ function foo() {}
20
+ foo.unicorn = '🦄';
21
+
22
+ function wrapper() {
23
+ return foo();
24
+ }
25
+
26
+ console.log(wrapper.name);
27
+ //=> 'wrapper'
28
+
29
+ mimicFunction(wrapper, foo);
30
+
31
+ console.log(wrapper.name);
32
+ //=> 'foo'
33
+
34
+ console.log(wrapper.unicorn);
35
+ //=> '🦄'
36
+
37
+ console.log(String(wrapper));
38
+ //=> '/* Wrapped with wrapper() */\nfunction foo() {}'
39
+ ```
40
+
41
+
42
+ ## API
43
+
44
+ ### mimicFunction(to, from, options?)
45
+
46
+ Modifies the `to` function to mimic the `from` function. Returns the `to` function.
47
+
48
+ `name`, `displayName`, and any other properties of `from` are copied. The `length` property is not copied. Prototype, class, and inherited properties are copied.
49
+
50
+ `to.toString()` will return the same as `from.toString()` but prepended with a `Wrapped with to()` comment.
51
+
52
+ #### to
53
+
54
+ Type: `Function`
55
+
56
+ Mimicking function.
57
+
58
+ #### from
59
+
60
+ Type: `Function`
61
+
62
+ Function to mimic.
63
+
64
+ #### options
65
+
66
+ Type: `object`
67
+
68
+ ##### ignoreNonConfigurable
69
+
70
+ Type: `boolean`\
71
+ Default: `false`
72
+
73
+ 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.
74
+
75
+ ## Related
76
+
77
+ - [rename-fn](https://github.com/sindresorhus/rename-fn) - Rename a function
78
+ - [keep-func-props](https://github.com/ehmicky/keep-func-props) - Wrap a function without changing its name and other properties
79
+
80
+ ---
81
+
82
+ <div align="center">
83
+ <b>
84
+ <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>
85
+ </b>
86
+ <br>
87
+ <sub>
88
+ Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
89
+ </sub>
90
+ </div>