unbounded
Advanced tools
Comparing version 5.4.0 to 6.0.0-next.1700972251.2650039ae9d6b96aa009e7399338de263ed977bb
@@ -1,35 +0,31 @@ | ||
'use strict'; | ||
const bind = Function.prototype.bind; | ||
/** | ||
* Attach the `unbounded` property on the `bounded` function. | ||
* If `unbounded.unbounded` already exists, then it is used instead, to ensure that the `unbounded` property is always the original function. | ||
*/ | ||
function define(bounded, unbounded) { | ||
if (bounded.unbounded !== unbounded) { | ||
Object.defineProperty(bounded, 'unbounded', { | ||
value: unbounded.unbounded || unbounded, | ||
enumerable: false, | ||
configurable: false, | ||
writable: false | ||
}); | ||
} | ||
return bounded; | ||
if (bounded.unbounded !== unbounded) { | ||
Object.defineProperty(bounded, 'unbounded', { | ||
value: unbounded.unbounded || unbounded, | ||
enumerable: false, | ||
configurable: false, | ||
writable: false, | ||
}); | ||
} | ||
} | ||
function binder() { | ||
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
const bounded = bind.apply(this, args); | ||
define(bounded, this); | ||
return bounded; | ||
/** | ||
* Alternative to `Function.prototype.bind` that sets the `unbounded` property on the bounded function. | ||
* Types of this are not intelligent, as using the intelligent bind types causes this/unknown incompatibility. This is not important however, as the masquerading binder of {@link patch} has the intelligent types. | ||
*/ | ||
export function binder(thisArg, ...argArray) { | ||
const bounded = bind.call(this, thisArg, ...argArray); | ||
define(bounded, this); | ||
return bounded; | ||
} | ||
function patch() { | ||
if (Function.prototype.bind !== binder) { | ||
/* eslint no-extend-native:0 */ | ||
Function.prototype.bind = binder; | ||
} | ||
return module.exports; | ||
/** Patch the native `Function.prototype.bind` so that it sets the `unbounded` property on the bounded function. */ | ||
export function patch() { | ||
if (Function.prototype.bind !== binder) { | ||
/* eslint no-extend-native:0 */ | ||
Function.prototype.bind = binder; | ||
} | ||
} | ||
module.exports = { | ||
bind, | ||
binder, | ||
patch, | ||
define | ||
}; |
176
package.json
{ | ||
"name": "unbounded", | ||
"version": "5.4.0", | ||
"version": "6.0.0-next.1700972251.2650039ae9d6b96aa009e7399338de263ed977bb", | ||
"description": "Function.prototype.bind replacement that provides an `unbounded` hidden property on the returned bounded function, that contains the original unbounded function", | ||
@@ -11,5 +11,14 @@ "homepage": "https://github.com/bevry/unbounded", | ||
"browser", | ||
"esnext", | ||
"deno", | ||
"deno-edition", | ||
"deno-entry", | ||
"denoland", | ||
"es2022", | ||
"es5", | ||
"function", | ||
"module", | ||
"node", | ||
"typed", | ||
"types", | ||
"typescript", | ||
"unbound", | ||
@@ -77,9 +86,34 @@ "unbounded" | ||
{ | ||
"description": "ESNext source code for Node.js 6 || 8 || 10 || 12 || 14 || 16 || 18 || 20 || 21 with Require for modules", | ||
"description": "TypeScript source code with Import for modules", | ||
"directory": "source", | ||
"entry": "index.ts", | ||
"tags": [ | ||
"source", | ||
"typescript", | ||
"import" | ||
], | ||
"engines": false | ||
}, | ||
{ | ||
"description": "TypeScript compiled against ES2022 for web browsers with Import for modules", | ||
"directory": "edition-browsers", | ||
"entry": "index.js", | ||
"tags": [ | ||
"source", | ||
"compiled", | ||
"javascript", | ||
"esnext", | ||
"import" | ||
], | ||
"engines": { | ||
"node": false, | ||
"browsers": "defaults" | ||
} | ||
}, | ||
{ | ||
"description": "TypeScript compiled against ES2022 for Node.js 6 || 8 || 10 || 12 || 14 || 16 || 18 || 20 || 21 with Require for modules", | ||
"directory": "edition-es2022", | ||
"entry": "index.js", | ||
"tags": [ | ||
"compiled", | ||
"javascript", | ||
"es2022", | ||
"require" | ||
@@ -93,4 +127,4 @@ ], | ||
{ | ||
"description": "ESNext compiled for web browsers with Require for modules", | ||
"directory": "edition-browsers", | ||
"description": "TypeScript compiled against ES5 for Node.js 4 || 6 || 8 || 10 || 12 || 14 || 16 || 18 || 20 || 21 with Require for modules", | ||
"directory": "edition-es5", | ||
"entry": "index.js", | ||
@@ -100,12 +134,13 @@ "tags": [ | ||
"javascript", | ||
"es5", | ||
"require" | ||
], | ||
"engines": { | ||
"node": false, | ||
"browsers": "defaults" | ||
"node": "4 || 6 || 8 || 10 || 12 || 14 || 16 || 18 || 20 || 21", | ||
"browsers": false | ||
} | ||
}, | ||
{ | ||
"description": "ESNext compiled for Node.js 4 with Require for modules", | ||
"directory": "edition-node-4", | ||
"description": "TypeScript compiled against ES2022 for Node.js 12 || 14 || 16 || 18 || 20 || 21 with Import for modules", | ||
"directory": "edition-es2022-esm", | ||
"entry": "index.js", | ||
@@ -115,13 +150,54 @@ "tags": [ | ||
"javascript", | ||
"require" | ||
"es2022", | ||
"import" | ||
], | ||
"engines": { | ||
"node": "4 || 6 || 8 || 10 || 12 || 14 || 16 || 18 || 20 || 21", | ||
"node": "12 || 14 || 16 || 18 || 20 || 21", | ||
"browsers": false | ||
} | ||
}, | ||
{ | ||
"description": "TypeScript compiled Types with Import for modules", | ||
"directory": "edition-types", | ||
"entry": "index.d.ts", | ||
"tags": [ | ||
"compiled", | ||
"types", | ||
"import" | ||
], | ||
"engines": false | ||
}, | ||
{ | ||
"description": "TypeScript source code made to be compatible with Deno", | ||
"directory": "edition-deno", | ||
"entry": "index.ts", | ||
"tags": [ | ||
"typescript", | ||
"import", | ||
"deno" | ||
], | ||
"engines": { | ||
"deno": true, | ||
"browsers": true | ||
} | ||
} | ||
], | ||
"type": "commonjs", | ||
"types": "edition-types/index.d.ts", | ||
"type": "module", | ||
"main": "index.cjs", | ||
"exports": { | ||
"node": { | ||
"types": "./edition-types/index.d.ts", | ||
"import": "./edition-es2022-esm/index.js", | ||
"default": "./index.cjs", | ||
"require": "./edition-es2022/index.js" | ||
}, | ||
"browser": { | ||
"types": "./edition-types/index.d.ts", | ||
"import": "./edition-browsers/index.js" | ||
} | ||
}, | ||
"deno": "edition-deno/index.ts", | ||
"browser": "edition-browsers/index.js", | ||
"module": "edition-browsers/index.js", | ||
"dependencies": { | ||
@@ -131,7 +207,6 @@ "editions": "^6.16.0" | ||
"devDependencies": { | ||
"@babel/cli": "^7.23.4", | ||
"@babel/core": "^7.23.3", | ||
"@babel/plugin-proposal-object-rest-spread": "^7.20.7", | ||
"@babel/preset-env": "^7.23.3", | ||
"@bevry/update-contributors": "^1.23.0", | ||
"@types/node": "^20.10.0", | ||
"@typescript-eslint/eslint-plugin": "^6.12.0", | ||
"@typescript-eslint/parser": "^6.12.0", | ||
"assert-helpers": "^11.6.0", | ||
@@ -143,14 +218,25 @@ "eslint": "^8.54.0", | ||
"kava": "^7.4.0", | ||
"make-deno-edition": "^2.0.0", | ||
"prettier": "^3.1.0", | ||
"projectz": "^3.4.0", | ||
"valid-directory": "^4.5.0" | ||
"surge": "^0.23.1", | ||
"typedoc": "^0.25.3", | ||
"typescript": "5.2.2", | ||
"valid-directory": "^4.6.0", | ||
"valid-module": "^2.6.0" | ||
}, | ||
"scripts": { | ||
"our:clean": "rm -rf ./docs ./edition* ./es2015 ./es5 ./out ./.next", | ||
"our:compile": "npm run our:compile:edition-browsers && npm run our:compile:edition-node-4", | ||
"our:compile:edition-browsers": "env BABEL_ENV=edition-browsers babel --out-dir ./edition-browsers ./source", | ||
"our:compile:edition-node-4": "env BABEL_ENV=edition-node-4 babel --out-dir ./edition-node-4 ./source && printf '%s' '{\"type\": \"commonjs\"}' > edition-node-4/package.json", | ||
"our:compile": "npm run our:compile:deno && npm run our:compile:edition-browsers && npm run our:compile:edition-es2022 && npm run our:compile:edition-es2022-esm && npm run our:compile:edition-es5 && npm run our:compile:edition-types", | ||
"our:compile:deno": "make-deno-edition --attempt", | ||
"our:compile:edition-browsers": "tsc --module ESNext --target ES2022 --outDir ./edition-browsers --project tsconfig.json && ( test ! -d edition-browsers/source || ( mv edition-browsers/source edition-temp && rm -rf edition-browsers && mv edition-temp edition-browsers ) )", | ||
"our:compile:edition-es2022": "tsc --module commonjs --target ES2022 --outDir ./edition-es2022 --project tsconfig.json && ( test ! -d edition-es2022/source || ( mv edition-es2022/source edition-temp && rm -rf edition-es2022 && mv edition-temp edition-es2022 ) ) && printf '%s' '{\"type\": \"commonjs\"}' > edition-es2022/package.json", | ||
"our:compile:edition-es2022-esm": "tsc --module ESNext --target ES2022 --outDir ./edition-es2022-esm --project tsconfig.json && ( test ! -d edition-es2022-esm/source || ( mv edition-es2022-esm/source edition-temp && rm -rf edition-es2022-esm && mv edition-temp edition-es2022-esm ) ) && printf '%s' '{\"type\": \"module\"}' > edition-es2022-esm/package.json", | ||
"our:compile:edition-es5": "tsc --module commonjs --target ES5 --outDir ./edition-es5 --project tsconfig.json && ( test ! -d edition-es5/source || ( mv edition-es5/source edition-temp && rm -rf edition-es5 && mv edition-temp edition-es5 ) ) && printf '%s' '{\"type\": \"commonjs\"}' > edition-es5/package.json", | ||
"our:compile:edition-types": "tsc --emitDeclarationOnly --declaration --declarationMap --declarationDir ./edition-types --project tsconfig.json && ( test ! -d edition-types/source || ( mv edition-types/source edition-temp && rm -rf edition-types && mv edition-temp edition-types ) )", | ||
"our:deploy": "printf '%s\n' 'no need for this project'", | ||
"our:meta": "npm run our:meta:contributors && npm run our:meta:projectz", | ||
"our:meta": "npm run our:meta:contributors && npm run our:meta:docs && npm run our:meta:projectz", | ||
"our:meta:contributors": "update-contributors", | ||
"our:meta:docs": "npm run our:meta:docs:typedoc", | ||
"our:meta:docs:typedoc": "rm -rf ./docs && typedoc --exclude '**/+(*test*|node_modules)' --excludeExternals --out ./docs ./source", | ||
"our:meta:projectz": "projectz compile", | ||
@@ -166,5 +252,6 @@ "our:release": "npm run our:release:prepare && npm run our:release:check-changelog && npm run our:release:check-dirty && npm run our:release:tag && npm run our:release:push", | ||
"our:test": "npm run our:verify && npm test", | ||
"our:verify": "npm run our:verify:directory && npm run our:verify:eslint && npm run our:verify:prettier", | ||
"our:verify": "npm run our:verify:directory && npm run our:verify:eslint && npm run our:verify:module && npm run our:verify:prettier", | ||
"our:verify:directory": "valid-directory", | ||
"our:verify:eslint": "eslint --fix --ignore-pattern '**/*.d.ts' --ignore-pattern '**/vendor/' --ignore-pattern '**/node_modules/' --ext .mjs,.js,.jsx,.ts,.tsx ./source", | ||
"our:verify:module": "valid-module", | ||
"our:verify:prettier": "prettier --write .", | ||
@@ -182,42 +269,3 @@ "test": "node ./test.cjs" | ||
"trailingComma": "es5" | ||
}, | ||
"babel": { | ||
"env": { | ||
"edition-browsers": { | ||
"sourceType": "script", | ||
"presets": [ | ||
[ | ||
"@babel/preset-env", | ||
{ | ||
"targets": { | ||
"esmodules": false, | ||
"browsers": "defaults" | ||
}, | ||
"modules": "commonjs" | ||
} | ||
] | ||
], | ||
"plugins": [ | ||
"@babel/proposal-object-rest-spread" | ||
] | ||
}, | ||
"edition-node-4": { | ||
"sourceType": "script", | ||
"presets": [ | ||
[ | ||
"@babel/preset-env", | ||
{ | ||
"targets": { | ||
"node": "4" | ||
}, | ||
"modules": "commonjs" | ||
} | ||
] | ||
], | ||
"plugins": [ | ||
"@babel/proposal-object-rest-spread" | ||
] | ||
} | ||
} | ||
} | ||
} | ||
} |
@@ -38,30 +38,44 @@ <!-- TITLE/ --> | ||
[Complete API Documentation.](http://master.unbounded.bevry.surge.sh/docs/) | ||
This package functions like [`Function.prototype.bind`](https://devdocs.io/javascript/global_objects/function/bind), however it exposes a hidden `unbounded` property on the returned bounded function that contains the original unbounded function. | ||
This is very useful as it means you can do `(fn.unbounded || fn).length` to always get the correct amount of arguments, or `(fn.unbounded || fn).toString()` to always get the source code of any function instead of getting `function () { [native code] }` for bounded functions. | ||
This is very useful as it means you can do: | ||
You can use unbounded via `require('unbounded').binder` like so: | ||
- `(fn.unbounded || fn).length` to always get the correct amount of arguments | ||
- `(fn.unbounded || fn).toString()` to always get the source code of any function instead of getting `function () { [native code] }` for bounded functions | ||
- `fn.bind(firstContext).unbounded.bind(secondContext)` to have `fn` actually bind to the second context, as `fn.bind(firstContext).bind(secondContext)` only binds to the first context | ||
To have all bounded functions have the `unbounded` property, use `patch`: | ||
```javascript | ||
const { binder } = require('unbounded') | ||
import { patch } from 'unbounded' | ||
patch() | ||
const context = { hello: 'world' } | ||
function a() { | ||
function myFunction() { | ||
return this.hello | ||
} | ||
const b = binder.call(a, context) | ||
equal(b(), context.hello, 'context was correct') | ||
equal(b.unbounded, a, 'unbounded was correct') | ||
import { equal } from 'assert' | ||
const boundedFunction = myFunction.bind(context) | ||
equal(boundedFunction(), context.hello, 'context was correct') | ||
equal(boundedFunction.unbounded, myFunction, 'unbounded was correct') | ||
``` | ||
Or you can patch `Function.prototype.bind` directly: | ||
To have only specific functions have the `unbounded` property, use `binder`: | ||
```javascript | ||
require('unbounded').patch() | ||
import { equal } from 'assert' | ||
import { binder } from 'unbounded' | ||
const context = { hello: 'world' } | ||
function a() { | ||
function myFunction() { | ||
return this.hello | ||
} | ||
const b = a.bind(context) | ||
equal(b(), context.hello, 'context was correct') | ||
equal(b.unbounded, a, 'unbounded was correct') | ||
import { equal } from 'assert' | ||
const boundedFunction = binder.call(myFunction, context) | ||
equal(boundedFunction(), context.hello, 'context was correct') | ||
equal(boundedFunction.unbounded, myFunction, 'unbounded was correct') | ||
``` | ||
@@ -80,2 +94,24 @@ | ||
<a href="https://deno.land" title="Deno is a secure runtime for JavaScript and TypeScript, it is an alternative for Node.js"><h3>Deno</h3></a> | ||
``` typescript | ||
import * as pkg from 'https://unpkg.com/unbounded@^6.0.0/edition-deno/index.ts' | ||
``` | ||
<a href="https://www.skypack.dev" title="Skypack is a JavaScript Delivery Network for modern web apps"><h3>Skypack</h3></a> | ||
``` html | ||
<script type="module"> | ||
import * as pkg from '//cdn.skypack.dev/unbounded@^6.0.0' | ||
</script> | ||
``` | ||
<a href="https://unpkg.com" title="unpkg is a fast, global content delivery network for everything on npm"><h3>unpkg</h3></a> | ||
``` html | ||
<script type="module"> | ||
import * as pkg from '//unpkg.com/unbounded@^6.0.0' | ||
</script> | ||
``` | ||
<a href="https://jspm.io" title="Native ES Modules CDN"><h3>jspm</h3></a> | ||
@@ -85,3 +121,3 @@ | ||
<script type="module"> | ||
import * as pkg from '//dev.jspm.io/unbounded@5.4.0' | ||
import * as pkg from '//dev.jspm.io/unbounded@6.0.0' | ||
</script> | ||
@@ -95,5 +131,9 @@ ``` | ||
<ul><li><code>unbounded</code> aliases <code>unbounded/index.cjs</code> which uses the <a href="https://github.com/bevry/editions" title="You can use the Editions Autoloader to autoload the appropriate edition for your consumers environment">Editions Autoloader</a> to automatically select the correct edition for the consumer's environment</li> | ||
<li><code>unbounded/source/index.js</code> is <a href="https://en.wikipedia.org/wiki/ECMAScript#ES.Next" title="ECMAScript Next">ESNext</a> source code for <a href="https://nodejs.org" title="Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine">Node.js</a> 6 || 8 || 10 || 12 || 14 || 16 || 18 || 20 || 21 with <a href="https://nodejs.org/dist/latest-v5.x/docs/api/modules.html" title="Node/CJS Modules">Require</a> for modules</li> | ||
<li><code>unbounded/edition-browsers/index.js</code> is <a href="https://en.wikipedia.org/wiki/ECMAScript#ES.Next" title="ECMAScript Next">ESNext</a> compiled for web browsers with <a href="https://nodejs.org/dist/latest-v5.x/docs/api/modules.html" title="Node/CJS Modules">Require</a> for modules</li> | ||
<li><code>unbounded/edition-node-4/index.js</code> is <a href="https://en.wikipedia.org/wiki/ECMAScript#ES.Next" title="ECMAScript Next">ESNext</a> compiled for <a href="https://nodejs.org" title="Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine">Node.js</a> 4 with <a href="https://nodejs.org/dist/latest-v5.x/docs/api/modules.html" title="Node/CJS Modules">Require</a> for modules</li></ul> | ||
<li><code>unbounded/source/index.ts</code> is <a href="https://www.typescriptlang.org/" title="TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. ">TypeScript</a> source code with <a href="https://babeljs.io/docs/learn-es2015/#modules" title="ECMAScript Modules">Import</a> for modules</li> | ||
<li><code>unbounded/edition-browsers/index.js</code> is <a href="https://www.typescriptlang.org/" title="TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. ">TypeScript</a> compiled against ES2022 for web browsers with <a href="https://babeljs.io/docs/learn-es2015/#modules" title="ECMAScript Modules">Import</a> for modules</li> | ||
<li><code>unbounded/edition-es2022/index.js</code> is <a href="https://www.typescriptlang.org/" title="TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. ">TypeScript</a> compiled against ES2022 for <a href="https://nodejs.org" title="Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine">Node.js</a> 6 || 8 || 10 || 12 || 14 || 16 || 18 || 20 || 21 with <a href="https://nodejs.org/dist/latest-v5.x/docs/api/modules.html" title="Node/CJS Modules">Require</a> for modules</li> | ||
<li><code>unbounded/edition-es5/index.js</code> is <a href="https://www.typescriptlang.org/" title="TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. ">TypeScript</a> compiled against ES5 for <a href="https://nodejs.org" title="Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine">Node.js</a> 4 || 6 || 8 || 10 || 12 || 14 || 16 || 18 || 20 || 21 with <a href="https://nodejs.org/dist/latest-v5.x/docs/api/modules.html" title="Node/CJS Modules">Require</a> for modules</li> | ||
<li><code>unbounded/edition-es2022-esm/index.js</code> is <a href="https://www.typescriptlang.org/" title="TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. ">TypeScript</a> compiled against ES2022 for <a href="https://nodejs.org" title="Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine">Node.js</a> 12 || 14 || 16 || 18 || 20 || 21 with <a href="https://babeljs.io/docs/learn-es2015/#modules" title="ECMAScript Modules">Import</a> for modules</li> | ||
<li><code>unbounded/edition-types/index.d.ts</code> is <a href="https://www.typescriptlang.org/" title="TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. ">TypeScript</a> compiled Types with <a href="https://babeljs.io/docs/learn-es2015/#modules" title="ECMAScript Modules">Import</a> for modules</li> | ||
<li><code>unbounded/edition-deno/index.ts</code> is <a href="https://www.typescriptlang.org/" title="TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. ">TypeScript</a> source code made to be compatible with <a href="https://deno.land" title="Deno is a secure runtime for JavaScript and TypeScript, it is an alternative to Node.js">Deno</a></li></ul> | ||
@@ -100,0 +140,0 @@ <!-- /INSTALL --> |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
45889
18
268
204
Yes
18
1
1