Comparing version 2.0.2 to 2.1.0
@@ -41,7 +41,5 @@ /** | ||
var _arr = Object.keys(object); | ||
for (var _i = 0, _Object$keys = Object.keys(object); _i < _Object$keys.length; _i++) { | ||
var key = _Object$keys[_i]; | ||
for (var _i = 0; _i < _arr.length; _i++) { | ||
var key = _arr[_i]; | ||
if (Object.prototype.hasOwnProperty.call(bound, key)) { | ||
@@ -52,6 +50,5 @@ continue; | ||
var descriptor = Object.getOwnPropertyDescriptor(object, key); | ||
var _arr2 = ["value", "set", "get"]; | ||
for (var _i2 = 0; _i2 < _arr2.length; _i2++) { | ||
var _key2 = _arr2[_i2]; | ||
for (var _i2 = 0, _arr = ["value", "set", "get"]; _i2 < _arr.length; _i2++) { | ||
var _key2 = _arr[_i2]; | ||
@@ -58,0 +55,0 @@ if (descriptor[_key2]) { |
{ | ||
"name": "bind-deep", | ||
"version": "2.0.2", | ||
"version": "2.1.0", | ||
"description": "Lightweight module for binding a function or object deeply.", | ||
@@ -22,6 +22,4 @@ "keywords": [ | ||
"author": "Evelyn Hathaway <npm@evelyn.dev> (https://evelyn.dev)", | ||
"files": [ | ||
"/lib" | ||
], | ||
"main": "./lib/index.js", | ||
"types": "./index.d.ts", | ||
"directories": { | ||
@@ -31,18 +29,31 @@ "lib": "./lib", | ||
}, | ||
"files": [ | ||
"/lib", | ||
"/index.d.ts" | ||
], | ||
"scripts": { | ||
"build": "babel src -d lib", | ||
"lint": "eslint --ext .js ./", | ||
"lint": "eslint ./", | ||
"test": "mocha test", | ||
"watch": "mocha test -w" | ||
"test:watch": "mocha test -w" | ||
}, | ||
"dependencies": { | ||
"ts-toolbelt": "^6.9.4" | ||
}, | ||
"devDependencies": { | ||
"@babel/cli": "^7.2.3", | ||
"@babel/core": "^7.4.0", | ||
"@babel/preset-env": "^7.4.2", | ||
"@babel/cli": "^7.10.1", | ||
"@babel/core": "^7.10.2", | ||
"@babel/preset-env": "^7.10.2", | ||
"@typescript-eslint/eslint-plugin": "^3.1.0", | ||
"@typescript-eslint/parser": "^3.1.0", | ||
"babel-eslint": "^10.1.0", | ||
"chai": "^4.2.0", | ||
"eslint": "^5.15.3", | ||
"eslint-plugin-evelyn": "0.0.0", | ||
"eslint-plugin-mocha": "^5.3.0", | ||
"eslint-plugin-node": "^8.0.1", | ||
"mocha": "^6.0.2" | ||
"eslint": "^7.2.0", | ||
"eslint-plugin-evelyn": "^3.0.0-alpha.3", | ||
"eslint-plugin-import": "^2.20.2", | ||
"eslint-plugin-mocha": "^7.0.1", | ||
"eslint-plugin-node": "^11.1.0", | ||
"eslint-plugin-unicorn": "^20.1.0", | ||
"mocha": "^7.2.0", | ||
"typescript": "^3.9.5" | ||
}, | ||
@@ -49,0 +60,0 @@ "engines": { |
108
README.md
@@ -1,20 +0,28 @@ | ||
# bind-deep | ||
<div align="center"> | ||
<img alt="Bind Deep icon" width="128" height="128" align="center" src=".github/icon.png"/> | ||
# Bind Deep | ||
**Lightweight module for binding a function or object deeply** | ||
[![npm version](https://badgen.net/npm/v/bind-deep?icon=npm)](https://www.npmjs.com/package/bind-deep) | ||
[![build status](https://badgen.net/travis/evelynhathaway/bind-deep/master?icon=travis)](https://travis-ci.com/evelynhathaway/bind-deep) | ||
[![check status](https://badgen.net/github/checks/evelynhathaway/bind-deep/master?icon=github)](https://github.com/evelynhathaway/bind-deep/actions) | ||
[![minified + gzip bundle size](https://badgen.net/bundlephobia/minzip/bind-deep)](https://bundlephobia.com/result?p=bind-deep) | ||
[![license](https://badgen.net/badge/license/MIT/blue)](/LICENSE) | ||
[![license: MIT](https://badgen.net/badge/license/MIT/blue)](/LICENSE) | ||
</div> | ||
## Description | ||
Bind an object to `this` in all methods in a function, object, or array. A simple, dependency-free alternative to [deep-bind](https://github.com/jonschlinkert/deep-bind). | ||
Bind an object to `this` in all methods in a function, object, or array. A simple, single-dependency (only for TypeScript types) alternative to [deep-bind](https://github.com/jonschlinkert/deep-bind). | ||
## Features | ||
- Works with functions, arrays, and other objects | ||
- Works with most custom classes, and array-like objects | ||
- Binds root function (if passed a function) and all own, enumerable property functions | ||
- Includes binding accessors — getters and setters | ||
- Binds the root function and all own, enumerable property functions including property accessors | ||
- Compatible with functions, arrays, objects, custom classes, and array-likes | ||
- Binds the `this` value and optionally, additional arguments just like [`func.bind()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Function/bind) | ||
- Copies objects and enumerable properties deeply | ||
- Preserves and copies prototype for all types | ||
- Preserves and copies prototypes for all types | ||
- **New in `v2.1.0`:** Strict TypeScript type definitions | ||
@@ -29,9 +37,15 @@ ## Installation | ||
## API | ||
## Usage | ||
```js | ||
bindDeep(object: Function | Object | Array, thisArg: Object, ...args:) | ||
// => bound: Function | Object | Array | ||
``` | ||
### `bindDeep(object, thisArg, [...args])` | ||
**Returns**: `Function` \| `Object` \| `Array` - The function or object passed as `object` but with | ||
itself and all methods bound to `thisArg` | ||
| Parameter | Type | Description | | ||
| --------- | --------------------------------- | --------------------------------------------------------------------------- | | ||
| object | `Function` \| `Object` \| `Array` | Function or object to bind itself and all of its methods | | ||
| thisArg | `Object` | The value bound to `this` for each bound function and method when called | | ||
| [...args] | `any` | Arguments provided to the bound function when the bound function is invoked | | ||
## Example | ||
@@ -69,2 +83,68 @@ | ||
### TypeScript | ||
All types inferred or annotated are preserved from the original functions and objects. The type definitions are incredibly strong deep types as the only negative side-effects are: | ||
- If bound arguments are added, the arguments in call signatures are renamed by their bound position. | ||
- If more than around 39 bound arguments are added, TypeScript will error `Type instantiation is excessively deep and possibly infinite.` | ||
- If you somehow do this, slap on an `as any` or your manually created type | ||
An in-depth explanation is commented inside of [`index.d.ts`](./index.d.ts) and below with example code. | ||
```ts | ||
// Import bind-deep | ||
import bindDeep from "bind-deep"; | ||
interface OriginalThis { | ||
discriminator: string; | ||
} | ||
// Original function | ||
const myFunction = function (this: OriginalThis, arg1: string, arg2: number) { | ||
return this; | ||
}; | ||
myFunction.method = function (this: OriginalThis, arg1: string) { | ||
return this; | ||
}; | ||
myFunction.primitive = "string"; | ||
// `thisArg` value | ||
const newThis = { newThis: "that's me!"}; | ||
// Deeply bound functions | ||
const boundFunction = bindDeep(myFunction, newThis); | ||
const boundFunctionWithArgs = bindDeep(myFunction, newThis, "add arg1 for each function"); | ||
/* | ||
Root call signature: `(arg1: string, arg2: number) => OriginalThis` | ||
- `this` argument type omitted from the original call signature as it is now bound | ||
- All other arugment types and names are preserved | ||
- Returns `newThis` as `OriginalThis` due to the return value inferred by TypeScript | ||
*/ | ||
boundFunction("arg1", 10); // returns `newThis` | ||
/* | ||
Root call signature when passing an argument: `((args_0: number) => OriginalThis)` | ||
- Similar explanation to the root call signature | ||
- `arg1` argument type omitted from the original call signature as it is now bound | ||
- `arg2` is represented as `args_0` with the number type preserved | ||
- This unfortunate renaming only occurs when binding arguments | ||
*/ | ||
boundFunctionWithArgs(10); // returns `newThis` | ||
/* | ||
Method call signature: `(method) method(arg1: string): OriginalThis` | ||
*/ | ||
boundFunction.method("arg1"); // returns `newThis` | ||
/* | ||
Method call signature when passing an argument: `(method) method(): OriginalThis` | ||
*/ | ||
boundFunctionWithArgs.method(); // returns `newThis` | ||
/* | ||
Primitive property type: `primitive: string` | ||
*/ | ||
const myString: string = boundFunction.primitive; // still "string", typings preserved | ||
``` | ||
--- | ||
@@ -71,0 +151,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12797
5
128
153
1
15
+ Addedts-toolbelt@^6.9.4
+ Addedts-toolbelt@6.15.5(transitive)