Socket
Socket
Sign inDemoInstall

@thi.ng/defmulti

Package Overview
Dependencies
Maintainers
1
Versions
183
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@thi.ng/defmulti - npm Package Compare versions

Comparing version 0.1.0 to 0.2.0

11

CHANGELOG.md

@@ -6,2 +6,13 @@ # Change Log

<a name="0.2.0"></a>
# [0.2.0](https://github.com/thi-ng/umbrella/compare/@thi.ng/defmulti@0.1.0...@thi.ng/defmulti@0.2.0) (2018-05-10)
### Features
* **defmulti:** add defmultiN(), update readme, add tests ([126ecf3](https://github.com/thi-ng/umbrella/commit/126ecf3))
<a name="0.1.0"></a>

@@ -8,0 +19,0 @@ # 0.1.0 (2018-05-10)

@@ -23,1 +23,31 @@ export declare const DEFAULT: unique symbol;

export declare function defmulti<T>(f: DispatchFn): MultiFn<T>;
/**
* Returns a multi-dispatch function which delegates to one of the
* provided implementations, based on the arity (number of args) when
* the function is called. Internally uses `defmulti`, so new arities
* can be dynamically added (or removed) at a later time. `defmultiN`
* also registers a `DEFAULT` implementation which simply throws an
* `IllegalArityError` when invoked.
*
* ```
* const foo = defmultiN({
* 0: () => "zero",
* 1: (x) => `one: ${x}`,
* 3: (x, y, z) => `three: ${x}, ${y}, ${z}`
* });
*
* foo();
* // zero
* foo(23);
* // one: 23
* foo(1, 2, 3);
* // three: 1, 2, 3
* foo(1, 2);
* // Error: illegal arity: 2
* ```
*
* @param impls
*/
export declare function defmultiN<T>(impls: {
[id: number]: Implementation<T>;
}): MultiFn<{}>;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const illegal_arguments_1 = require("@thi.ng/errors/illegal-arguments");
const illegal_arity_1 = require("@thi.ng/errors/illegal-arity");
exports.DEFAULT = Symbol("DEFAULT");

@@ -42,1 +43,37 @@ /**

;
/**
* Returns a multi-dispatch function which delegates to one of the
* provided implementations, based on the arity (number of args) when
* the function is called. Internally uses `defmulti`, so new arities
* can be dynamically added (or removed) at a later time. `defmultiN`
* also registers a `DEFAULT` implementation which simply throws an
* `IllegalArityError` when invoked.
*
* ```
* const foo = defmultiN({
* 0: () => "zero",
* 1: (x) => `one: ${x}`,
* 3: (x, y, z) => `three: ${x}, ${y}, ${z}`
* });
*
* foo();
* // zero
* foo(23);
* // one: 23
* foo(1, 2, 3);
* // three: 1, 2, 3
* foo(1, 2);
* // Error: illegal arity: 2
* ```
*
* @param impls
*/
function defmultiN(impls) {
const fn = defmulti((...args) => args.length);
fn.add(exports.DEFAULT, (...args) => illegal_arity_1.illegalArity(args.length));
for (let id in impls) {
fn.add(id, impls[id]);
}
return fn;
}
exports.defmultiN = defmultiN;

6

package.json
{
"name": "@thi.ng/defmulti",
"version": "0.1.0",
"version": "0.2.0",
"description": "Dynamically extensible multiple dispatch via user supplied dispatch function.",

@@ -27,4 +27,4 @@ "main": "./index.js",

"dependencies": {
"@thi.ng/api": "^3.0.0",
"@thi.ng/errors": "^0.1.0"
"@thi.ng/api": "^3.0.1",
"@thi.ng/errors": "^0.1.1"
},

@@ -31,0 +31,0 @@ "keywords": [

@@ -22,2 +22,4 @@ # @thi.ng/defmulti

### defmulti
`defmulti` returns a new multi-dispatch function using the provided

@@ -57,4 +59,8 @@ dispatcher function. The dispatcher can take any number of arguments and

### Dynamic dispatch: Simple S-expression interpreter
See
[/test/index.ts](https://github.com/thi-ng/umbrella/tree/master/packages/defmulti/test/index.ts)
for a variation of this example.
#### Dynamic dispatch: Simple S-expression interpreter
```ts

@@ -72,3 +78,3 @@ const exec = defmulti((x)=> Array.isArray(x) ? x[0] : typeof x);

### True multiple arg dispatch
#### True multiple arg dispatch

@@ -99,2 +105,28 @@ ```ts

### defmultiN
Returns a multi-dispatch function which delegates to one of the provided
implementations, based on the arity (number of args) when the function
is called. Internally uses `defmulti`, so new arities can be dynamically
added (or removed) at a later time. `defmultiN` also registers a
`DEFAULT` implementation which simply throws an `IllegalArityError` when
invoked.
```ts
const foo = defmultiN({
0: () => "zero",
1: (x) => `one: ${x}`,
3: (x, y, z) => `three: ${x}, ${y}, ${z}`
});
foo();
// zero
foo(23);
// one: 23
foo(1, 2, 3);
// three: 1, 2, 3
foo(1, 2);
// Error: illegal arity: 2
```
## Authors

@@ -101,0 +133,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc