@thi.ng/defmulti
Advanced tools
Comparing version 1.3.17 to 2.0.0
11
api.d.ts
@@ -56,2 +56,11 @@ import type { Fn, Fn2, Fn3, Fn4, Fn5, Fn6, Fn7, Fn8, FnAny, IObjectOf, Pair } from "@thi.ng/api"; | ||
/** | ||
* Sets default/fallback implementation. | ||
* | ||
* @remarks | ||
* Same as `.add(DEFAULT, impl)` | ||
* | ||
* @param impl | ||
*/ | ||
setDefault(impl: I): boolean; | ||
/** | ||
* Removes implementation for dispatch value `id`. Returns true, if | ||
@@ -144,3 +153,3 @@ * successful. | ||
} | ||
export declare type AncestorDefs = IObjectOf<Iterable<PropertyKey>>; | ||
export declare type AncestorDefs = IObjectOf<PropertyKey | PropertyKey[] | Set<PropertyKey>>; | ||
//# sourceMappingURL=api.d.ts.map |
119
CHANGELOG.md
@@ -6,109 +6,124 @@ # Change Log | ||
## [1.3.17](https://github.com/thi-ng/umbrella/compare/@thi.ng/defmulti@1.3.16...@thi.ng/defmulti@1.3.17) (2021-09-03) | ||
# [2.0.0](https://github.com/thi-ng/umbrella/compare/@thi.ng/defmulti@1.3.17...@thi.ng/defmulti@2.0.0) (2021-10-12) | ||
**Note:** Version bump only for package @thi.ng/defmulti | ||
### Bug Fixes | ||
* **defmulti:** add DEFAULT handling for .addAll() ([40ba6f9](https://github.com/thi-ng/umbrella/commit/40ba6f9bc39c713267ac3736fe65c32315ac6980)) | ||
### Build System | ||
## [1.3.6](https://github.com/thi-ng/umbrella/compare/@thi.ng/defmulti@1.3.5...@thi.ng/defmulti@1.3.6) (2021-03-03) | ||
* major update of ALL pkgs (export maps, ESM only) ([0d1d6ea](https://github.com/thi-ng/umbrella/commit/0d1d6ea9fab2a645d6c5f2bf2591459b939c09b6)) | ||
### Bug Fixes | ||
### Features | ||
* **defmulti:** add missing type anno (TS4.2) ([bc74d21](https://github.com/thi-ng/umbrella/commit/bc74d21264f2d3b76fc288eeccab398ad66f76da)) | ||
* **defmulti:** add .setDefault(), pkg restructure ([0a8641a](https://github.com/thi-ng/umbrella/commit/0a8641a27f3d340880462541311f5a82360be62b)) | ||
* **defmulti:** update defmulti args ([a10d36f](https://github.com/thi-ng/umbrella/commit/a10d36fcee855220eacbdd1f86d443d888603ac6)) | ||
### BREAKING CHANGES | ||
* discontinue CommonJS & UMD versions | ||
- only ESM modules will be published from now on | ||
- CJS obsolete due to ESM support in recent versions of node: | ||
- i.e. launch NodeJS via: | ||
- `node --experimental-specifier-resolution=node --experimental-repl-await` | ||
- in the node REPL use `await import(...)` instead of `require()` | ||
- UMD obsolete due to widespread browser support for ESM | ||
# [1.3.0](https://github.com/thi-ng/umbrella/compare/@thi.ng/defmulti@1.2.26...@thi.ng/defmulti@1.3.0) (2020-11-24) | ||
Also: | ||
- normalize/restructure/reorg all package.json files | ||
- cleanup all build scripts, remove obsolete | ||
- switch from mocha to @thi.ng/testament for all tests | ||
### Features | ||
* **defmulti:** add .dependencies(), add tests ([d15a159](https://github.com/thi-ng/umbrella/commit/d15a1594750ac171b1ab93da18d908f1ca6c3897)) | ||
## [1.3.6](https://github.com/thi-ng/umbrella/compare/@thi.ng/defmulti@1.3.5...@thi.ng/defmulti@1.3.6) (2021-03-03) | ||
### Bug Fixes | ||
# [1.2.0](https://github.com/thi-ng/umbrella/compare/@thi.ng/defmulti@1.1.4...@thi.ng/defmulti@1.2.0) (2019-11-09) | ||
- **defmulti:** add missing type anno (TS4.2) ([bc74d21](https://github.com/thi-ng/umbrella/commit/bc74d21264f2d3b76fc288eeccab398ad66f76da)) | ||
### Features | ||
# [1.3.0](https://github.com/thi-ng/umbrella/compare/@thi.ng/defmulti@1.2.26...@thi.ng/defmulti@1.3.0) (2020-11-24) | ||
* **defmulti:** allow .add() to overwrite existing impl, add logger ([e387622](https://github.com/thi-ng/umbrella/commit/e387622d3ad44bc0df029c5ba641244dc12c6353)) | ||
### Features | ||
# [1.1.0](https://github.com/thi-ng/umbrella/compare/@thi.ng/defmulti@1.0.9...@thi.ng/defmulti@1.1.0) (2019-07-07) | ||
- **defmulti:** add .dependencies(), add tests ([d15a159](https://github.com/thi-ng/umbrella/commit/d15a1594750ac171b1ab93da18d908f1ca6c3897)) | ||
### Features | ||
# [1.2.0](https://github.com/thi-ng/umbrella/compare/@thi.ng/defmulti@1.1.4...@thi.ng/defmulti@1.2.0) (2019-11-09) | ||
* **defmulti:** enable TS strict compiler flags (refactor) ([d51ecf9](https://github.com/thi-ng/umbrella/commit/d51ecf9)) | ||
### Features | ||
# [1.0.0](https://github.com/thi-ng/umbrella/compare/@thi.ng/defmulti@0.7.0...@thi.ng/defmulti@1.0.0) (2019-01-21) | ||
- **defmulti:** allow .add() to overwrite existing impl, add logger ([e387622](https://github.com/thi-ng/umbrella/commit/e387622d3ad44bc0df029c5ba641244dc12c6353)) | ||
### Build System | ||
# [1.1.0](https://github.com/thi-ng/umbrella/compare/@thi.ng/defmulti@1.0.9...@thi.ng/defmulti@1.1.0) (2019-07-07) | ||
* update package build scripts & outputs, imports in ~50 packages ([b54b703](https://github.com/thi-ng/umbrella/commit/b54b703)) | ||
### Features | ||
### Features | ||
- **defmulti:** enable TS strict compiler flags (refactor) ([d51ecf9](https://github.com/thi-ng/umbrella/commit/d51ecf9)) | ||
* **defmulti:** add callable() & implementations(), update readme ([fde2db2](https://github.com/thi-ng/umbrella/commit/fde2db2)) | ||
* **defmulti:** add relations() ([4066c80](https://github.com/thi-ng/umbrella/commit/4066c80)) | ||
* **defmulti:** add versions w/ 1 optional typed arg, add .impls() ([125c784](https://github.com/thi-ng/umbrella/commit/125c784)) | ||
# [1.0.0](https://github.com/thi-ng/umbrella/compare/@thi.ng/defmulti@0.7.0...@thi.ng/defmulti@1.0.0) (2019-01-21) | ||
### BREAKING CHANGES | ||
### Build System | ||
* enabled multi-outputs (ES6 modules, CJS, UMD) | ||
- update package build scripts & outputs, imports in ~50 packages ([b54b703](https://github.com/thi-ng/umbrella/commit/b54b703)) | ||
- build scripts now first build ES6 modules in package root, then call | ||
`scripts/bundle-module` to build minified CJS & UMD bundles in `/lib` | ||
- all imports MUST be updated to only refer to package level | ||
(not individual files anymore). tree shaking in user land will get rid of | ||
all unused imported symbols. | ||
### Features | ||
# [0.7.0](https://github.com/thi-ng/umbrella/compare/@thi.ng/defmulti@0.6.0...@thi.ng/defmulti@0.7.0) (2019-01-02) | ||
- **defmulti:** add callable() & implementations(), update readme ([fde2db2](https://github.com/thi-ng/umbrella/commit/fde2db2)) | ||
- **defmulti:** add relations() ([4066c80](https://github.com/thi-ng/umbrella/commit/4066c80)) | ||
- **defmulti:** add versions w/ 1 optional typed arg, add .impls() ([125c784](https://github.com/thi-ng/umbrella/commit/125c784)) | ||
### Features | ||
### BREAKING CHANGES | ||
* **defmulti:** add opt fallback arg for defmultiN(), update docs ([1d29153](https://github.com/thi-ng/umbrella/commit/1d29153)) | ||
- enabled multi-outputs (ES6 modules, CJS, UMD) | ||
- build scripts now first build ES6 modules in package root, then call `scripts/bundle-module` to build minified CJS & UMD bundles in `/lib` | ||
- all imports MUST be updated to only refer to package level (not individual files anymore). tree shaking in user land will get rid of all unused imported symbols. | ||
# [0.6.0](https://github.com/thi-ng/umbrella/compare/@thi.ng/defmulti@0.5.1...@thi.ng/defmulti@0.6.0) (2019-01-01) | ||
# [0.7.0](https://github.com/thi-ng/umbrella/compare/@thi.ng/defmulti@0.6.0...@thi.ng/defmulti@0.7.0) (2019-01-02) | ||
### Features | ||
### Features | ||
* **defmulti:** add addAll(), add/update doc strings ([488698a](https://github.com/thi-ng/umbrella/commit/488698a)) | ||
- **defmulti:** add opt fallback arg for defmultiN(), update docs ([1d29153](https://github.com/thi-ng/umbrella/commit/1d29153)) | ||
# [0.5.0](https://github.com/thi-ng/umbrella/compare/@thi.ng/defmulti@0.4.1...@thi.ng/defmulti@0.5.0) (2018-10-24) | ||
# [0.6.0](https://github.com/thi-ng/umbrella/compare/@thi.ng/defmulti@0.5.1...@thi.ng/defmulti@0.6.0) (2019-01-01) | ||
### Features | ||
### Features | ||
* **defmulti:** add support for dispatch value relationships / hierarchy ([a8c3898](https://github.com/thi-ng/umbrella/commit/a8c3898)) | ||
- **defmulti:** add addAll(), add/update doc strings ([488698a](https://github.com/thi-ng/umbrella/commit/488698a)) | ||
# [0.4.0](https://github.com/thi-ng/umbrella/compare/@thi.ng/defmulti@0.3.11...@thi.ng/defmulti@0.4.0) (2018-10-17) | ||
# [0.5.0](https://github.com/thi-ng/umbrella/compare/@thi.ng/defmulti@0.4.1...@thi.ng/defmulti@0.5.0) (2018-10-24) | ||
### Features | ||
### Features | ||
* **defmulti:** add varargs support ([6094738](https://github.com/thi-ng/umbrella/commit/6094738)) | ||
- **defmulti:** add support for dispatch value relationships / hierarchy ([a8c3898](https://github.com/thi-ng/umbrella/commit/a8c3898)) | ||
<a name="0.3.0"></a> | ||
# [0.3.0](https://github.com/thi-ng/umbrella/compare/@thi.ng/defmulti@0.2.0...@thi.ng/defmulti@0.3.0) (2018-05-11) | ||
# [0.4.0](https://github.com/thi-ng/umbrella/compare/@thi.ng/defmulti@0.3.11...@thi.ng/defmulti@0.4.0) (2018-10-17) | ||
### Features | ||
### Features | ||
* **defmulti:** add generics, update docs & readme ([eeed25e](https://github.com/thi-ng/umbrella/commit/eeed25e)) | ||
- **defmulti:** add varargs support ([6094738](https://github.com/thi-ng/umbrella/commit/6094738)) | ||
<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) | ||
# [0.3.0](https://github.com/thi-ng/umbrella/compare/@thi.ng/defmulti@0.2.0...@thi.ng/defmulti@0.3.0) (2018-05-11) | ||
### Features | ||
### Features | ||
* **defmulti:** add defmultiN(), update readme, add tests ([126ecf3](https://github.com/thi-ng/umbrella/commit/126ecf3)) | ||
- **defmulti:** add generics, update docs & readme ([eeed25e](https://github.com/thi-ng/umbrella/commit/eeed25e)) | ||
<a name="0.1.0"></a> | ||
# 0.1.0 (2018-05-10) | ||
# [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 | ||
### Features | ||
* **defmulti:** add [@thi](https://github.com/thi).ng/defmulti package ([edc66bf](https://github.com/thi-ng/umbrella/commit/edc66bf)) | ||
- **defmulti:** add defmultiN(), update readme, add tests ([126ecf3](https://github.com/thi-ng/umbrella/commit/126ecf3)) | ||
# 0.1.0 (2018-05-10) | ||
### Features | ||
- **defmulti:** add [@thi](https://github.com/thi).ng/defmulti package ([edc66bf](https://github.com/thi-ng/umbrella/commit/edc66bf)) |
@@ -1,4 +0,3 @@ | ||
import { illegalArity } from "@thi.ng/errors"; | ||
import { DEFAULT } from "./constants"; | ||
import { defmulti } from "./defmulti"; | ||
import { illegalArity } from "@thi.ng/errors/illegal-arity"; | ||
import { DEFAULT, defmulti } from "./defmulti"; | ||
/** | ||
@@ -5,0 +4,0 @@ * Returns a multi-dispatch function which delegates to one of the |
@@ -1,41 +0,45 @@ | ||
import type { AncestorDefs, DispatchFn, DispatchFn1, DispatchFn1O, DispatchFn2, DispatchFn2O, DispatchFn3, DispatchFn3O, DispatchFn4, DispatchFn4O, DispatchFn5, DispatchFn5O, DispatchFn6, DispatchFn6O, DispatchFn7, DispatchFn7O, DispatchFn8, DispatchFn8O, MultiFn, MultiFn1, MultiFn1O, MultiFn2, MultiFn2O, MultiFn3, MultiFn3O, MultiFn4, MultiFn4O, MultiFn5, MultiFn5O, MultiFn6, MultiFn6O, MultiFn7, MultiFn7O, MultiFn8, MultiFn8O } from "./api"; | ||
import type { IObjectOf } from "@thi.ng/api"; | ||
import type { AncestorDefs, DispatchFn, DispatchFn1, DispatchFn1O, DispatchFn2, DispatchFn2O, DispatchFn3, DispatchFn3O, DispatchFn4, DispatchFn4O, DispatchFn5, DispatchFn5O, DispatchFn6, DispatchFn6O, DispatchFn7, DispatchFn7O, DispatchFn8, DispatchFn8O, Implementation, Implementation1, Implementation1O, Implementation2, Implementation2O, Implementation3, Implementation3O, Implementation4, Implementation4O, Implementation5, Implementation5O, Implementation6, Implementation6O, Implementation7, Implementation7O, Implementation8, Implementation8O, MultiFn, MultiFn1, MultiFn1O, MultiFn2, MultiFn2O, MultiFn3, MultiFn3O, MultiFn4, MultiFn4O, MultiFn5, MultiFn5O, MultiFn6, MultiFn6O, MultiFn7, MultiFn7O, MultiFn8, MultiFn8O } from "./api"; | ||
/** | ||
* Returns a new multi-dispatch function using the provided dispatch | ||
* value function. | ||
* Unique symbol used for registering a default / fallback | ||
* implementation. | ||
*/ | ||
export declare const DEFAULT: unique symbol; | ||
/** | ||
* Returns a new multi-dispatch function using the provided dispatch value | ||
* function. Additionally, optional ancestor relationships (`rels`) and set of | ||
* implementations (`impls`) can be given. | ||
* | ||
* @remarks | ||
* The dispatcher can take any number of arguments and must produce a | ||
* dispatch value (string, number or symbol) used to lookup an | ||
* implementation. If found, the impl is called with the same args and | ||
* its return value used as own return value. If no matching | ||
* implementation is available, attempts to dispatch to DEFAULT impl. If | ||
* none is registered, an error is thrown. | ||
* The dispatcher can take any number of arguments and must produce a dispatch | ||
* value (string, number or symbol) used to lookup an implementation. If found, | ||
* the impl is called with the same args and its return value used as own return | ||
* value. If no matching implementation is available, attempts to dispatch to | ||
* DEFAULT impl. If none is registered, an error is thrown. | ||
* | ||
* `defmulti` provides generics for type checking up to 8 args (plus the | ||
* return type) and the generics will also apply to all implementations. | ||
* If more than 8 args are required, `defmulti` will fall back to an | ||
* untyped varargs solution. | ||
* `defmulti` provides generics for type checking up to 8 args (plus the return | ||
* type) and the generics will also apply to all implementations. If more than 8 | ||
* args are required, `defmulti` will fall back to an untyped varargs solution. | ||
* | ||
* Implementations for different dispatch values can be added and | ||
* removed dynamically by calling `.add(id, fn)` or `.remove(id)` on the | ||
* returned function. Each returns `true` if the operation was | ||
* successful. | ||
* Implementations for different dispatch values can be added and removed | ||
* dynamically by calling `.add(id, fn)`, `.addAll(impls)` or `.remove(id)` on | ||
* the returned function. Each returns `true` if the operation was successful. | ||
*/ | ||
export declare function defmulti<T>(f: DispatchFn, rels?: AncestorDefs): MultiFn<T>; | ||
export declare function defmulti<A, T>(f: DispatchFn1<A>, rels?: AncestorDefs): MultiFn1<A, T>; | ||
export declare function defmulti<A, B, T>(f: DispatchFn2<A, B>, rels?: AncestorDefs): MultiFn2<A, B, T>; | ||
export declare function defmulti<A, B, T>(f: DispatchFn1O<A, B>, rels?: AncestorDefs): MultiFn1O<A, B, T>; | ||
export declare function defmulti<A, B, C, T>(f: DispatchFn3<A, B, C>, rels?: AncestorDefs): MultiFn3<A, B, C, T>; | ||
export declare function defmulti<A, B, C, T>(f: DispatchFn2O<A, B, C>, rels?: AncestorDefs): MultiFn2O<A, B, C, T>; | ||
export declare function defmulti<A, B, C, D, T>(f: DispatchFn4<A, B, C, D>, rels?: AncestorDefs): MultiFn4<A, B, C, D, T>; | ||
export declare function defmulti<A, B, C, D, T>(f: DispatchFn3O<A, B, C, D>, rels?: AncestorDefs): MultiFn3O<A, B, C, D, T>; | ||
export declare function defmulti<A, B, C, D, E, T>(f: DispatchFn5<A, B, C, D, E>, rels?: AncestorDefs): MultiFn5<A, B, C, D, E, T>; | ||
export declare function defmulti<A, B, C, D, E, T>(f: DispatchFn4O<A, B, C, D, E>, rels?: AncestorDefs): MultiFn4O<A, B, C, D, E, T>; | ||
export declare function defmulti<A, B, C, D, E, F, T>(f: DispatchFn6<A, B, C, D, E, F>, rels?: AncestorDefs): MultiFn6<A, B, C, D, E, F, T>; | ||
export declare function defmulti<A, B, C, D, E, F, T>(f: DispatchFn5O<A, B, C, D, E, F>, rels?: AncestorDefs): MultiFn5O<A, B, C, D, E, F, T>; | ||
export declare function defmulti<A, B, C, D, E, F, G, T>(f: DispatchFn7<A, B, C, D, E, F, G>, rels?: AncestorDefs): MultiFn7<A, B, C, D, E, F, G, T>; | ||
export declare function defmulti<A, B, C, D, E, F, G, T>(f: DispatchFn6O<A, B, C, D, E, F, G>, rels?: AncestorDefs): MultiFn6O<A, B, C, D, E, F, G, T>; | ||
export declare function defmulti<A, B, C, D, E, F, G, H, T>(f: DispatchFn8<A, B, C, D, E, F, G, H>, rels?: AncestorDefs): MultiFn8<A, B, C, D, E, F, G, H, T>; | ||
export declare function defmulti<A, B, C, D, E, F, G, H, T>(f: DispatchFn7O<A, B, C, D, E, F, G, H>, rels?: AncestorDefs): MultiFn7O<A, B, C, D, E, F, G, H, T>; | ||
export declare function defmulti<A, B, C, D, E, F, G, H, I, T>(f: DispatchFn8O<A, B, C, D, E, F, G, H, I>, rels?: AncestorDefs): MultiFn8O<A, B, C, D, E, F, G, H, I, T>; | ||
export declare function defmulti<T>(f: DispatchFn, rels?: AncestorDefs, impls?: IObjectOf<Implementation<T>>): MultiFn<T>; | ||
export declare function defmulti<A, T>(f: DispatchFn1<A>, rels?: AncestorDefs, impls?: IObjectOf<Implementation1<A, T>>): MultiFn1<A, T>; | ||
export declare function defmulti<A, B, T>(f: DispatchFn2<A, B>, rels?: AncestorDefs, impls?: IObjectOf<Implementation2<A, B, T>>): MultiFn2<A, B, T>; | ||
export declare function defmulti<A, B, T>(f: DispatchFn1O<A, B>, rels?: AncestorDefs, impls?: IObjectOf<Implementation1O<A, B, T>>): MultiFn1O<A, B, T>; | ||
export declare function defmulti<A, B, C, T>(f: DispatchFn3<A, B, C>, rels?: AncestorDefs, impls?: IObjectOf<Implementation3<A, B, C, T>>): MultiFn3<A, B, C, T>; | ||
export declare function defmulti<A, B, C, T>(f: DispatchFn2O<A, B, C>, rels?: AncestorDefs, impls?: IObjectOf<Implementation2O<A, B, C, T>>): MultiFn2O<A, B, C, T>; | ||
export declare function defmulti<A, B, C, D, T>(f: DispatchFn4<A, B, C, D>, rels?: AncestorDefs, impls?: IObjectOf<Implementation4<A, B, C, D, T>>): MultiFn4<A, B, C, D, T>; | ||
export declare function defmulti<A, B, C, D, T>(f: DispatchFn3O<A, B, C, D>, rels?: AncestorDefs, impls?: IObjectOf<Implementation3O<A, B, C, D, T>>): MultiFn3O<A, B, C, D, T>; | ||
export declare function defmulti<A, B, C, D, E, T>(f: DispatchFn5<A, B, C, D, E>, rels?: AncestorDefs, impls?: IObjectOf<Implementation5<A, B, C, D, E, T>>): MultiFn5<A, B, C, D, E, T>; | ||
export declare function defmulti<A, B, C, D, E, T>(f: DispatchFn4O<A, B, C, D, E>, rels?: AncestorDefs, impls?: IObjectOf<Implementation4O<A, B, C, D, E, T>>): MultiFn4O<A, B, C, D, E, T>; | ||
export declare function defmulti<A, B, C, D, E, F, T>(f: DispatchFn6<A, B, C, D, E, F>, rels?: AncestorDefs, impls?: IObjectOf<Implementation6<A, B, C, D, E, F, T>>): MultiFn6<A, B, C, D, E, F, T>; | ||
export declare function defmulti<A, B, C, D, E, F, T>(f: DispatchFn5O<A, B, C, D, E, F>, rels?: AncestorDefs, impls?: IObjectOf<Implementation5O<A, B, C, D, E, F, T>>): MultiFn5O<A, B, C, D, E, F, T>; | ||
export declare function defmulti<A, B, C, D, E, F, G, T>(f: DispatchFn7<A, B, C, D, E, F, G>, rels?: AncestorDefs, impls?: IObjectOf<Implementation7<A, B, C, D, E, F, G, T>>): MultiFn7<A, B, C, D, E, F, G, T>; | ||
export declare function defmulti<A, B, C, D, E, F, G, T>(f: DispatchFn6O<A, B, C, D, E, F, G>, rels?: AncestorDefs, impls?: IObjectOf<Implementation6O<A, B, C, D, E, F, G, T>>): MultiFn6O<A, B, C, D, E, F, G, T>; | ||
export declare function defmulti<A, B, C, D, E, F, G, H, T>(f: DispatchFn8<A, B, C, D, E, F, G, H>, rels?: AncestorDefs, impls?: IObjectOf<Implementation8<A, B, C, D, E, F, G, H, T>>): MultiFn8<A, B, C, D, E, F, G, H, T>; | ||
export declare function defmulti<A, B, C, D, E, F, G, H, T>(f: DispatchFn7O<A, B, C, D, E, F, G, H>, rels?: AncestorDefs, impls?: IObjectOf<Implementation7O<A, B, C, D, E, F, G, H, T>>): MultiFn7O<A, B, C, D, E, F, G, H, T>; | ||
export declare function defmulti<A, B, C, D, E, F, G, H, I, T>(f: DispatchFn8O<A, B, C, D, E, F, G, H, I>, rels?: AncestorDefs, impls?: IObjectOf<Implementation8O<A, B, C, D, E, F, G, H, I, T>>): MultiFn8O<A, B, C, D, E, F, G, H, I, T>; | ||
//# sourceMappingURL=defmulti.d.ts.map |
@@ -1,8 +0,11 @@ | ||
import { unsupported } from "@thi.ng/errors"; | ||
import { DEFAULT, LOGGER } from "./constants"; | ||
export function defmulti(f, ancestors) { | ||
import { unsupported } from "@thi.ng/errors/unsupported"; | ||
import { LOGGER } from "./logger"; | ||
/** | ||
* Unique symbol used for registering a default / fallback | ||
* implementation. | ||
*/ | ||
export const DEFAULT = Symbol(); | ||
export function defmulti(f, _rels, _impls) { | ||
const impls = {}; | ||
const rels = ancestors | ||
? makeRels(ancestors) | ||
: {}; | ||
const rels = _rels ? makeRels(_rels) : {}; | ||
const fn = (...args) => { | ||
@@ -15,7 +18,7 @@ const id = f(...args); | ||
}; | ||
fn.add = (id, g) => { | ||
fn.add = (id, _impl) => { | ||
if (impls[id]) { | ||
LOGGER.warn(`overwriting '${id.toString()}' impl`); | ||
} | ||
impls[id] = g; | ||
impls[id] = _impl; | ||
return true; | ||
@@ -28,4 +31,6 @@ }; | ||
} | ||
DEFAULT in _impls && fn.setDefault(_impls[DEFAULT]); | ||
return ok; | ||
}; | ||
fn.setDefault = (impl) => fn.add(DEFAULT, impl); | ||
fn.remove = (id) => { | ||
@@ -68,2 +73,3 @@ if (!impls[id]) | ||
}; | ||
_impls && fn.addAll(_impls); | ||
return fn; | ||
@@ -95,5 +101,10 @@ } | ||
const val = spec[k]; | ||
rels[k] = val instanceof Set ? val : new Set(val); | ||
rels[k] = | ||
val instanceof Set | ||
? val | ||
: Array.isArray(val) | ||
? new Set(val) | ||
: new Set([val]); | ||
} | ||
return rels; | ||
}; |
@@ -1,2 +0,2 @@ | ||
import { illegalArgs } from "@thi.ng/errors"; | ||
import { illegalArgs } from "@thi.ng/errors/illegal-arguments"; | ||
/** | ||
@@ -3,0 +3,0 @@ * Syntax-sugar intended for sets of multi-methods sharing same dispatch |
export * from "./api"; | ||
export * from "./constants"; | ||
export * from "./defmulti"; | ||
export * from "./defmulti-n"; | ||
export * from "./impls"; | ||
export * from "./logger"; | ||
//# sourceMappingURL=index.d.ts.map |
export * from "./api"; | ||
export * from "./constants"; | ||
export * from "./defmulti"; | ||
export * from "./defmulti-n"; | ||
export * from "./impls"; | ||
export * from "./logger"; |
{ | ||
"name": "@thi.ng/defmulti", | ||
"version": "1.3.17", | ||
"version": "2.0.0", | ||
"description": "Dynamic, extensible multiple dispatch via user supplied dispatch function.", | ||
"type": "module", | ||
"module": "./index.js", | ||
"main": "./lib/index.js", | ||
"umd:main": "./lib/index.umd.js", | ||
"typings": "./index.d.ts", | ||
"sideEffects": false, | ||
"repository": { | ||
@@ -27,24 +27,19 @@ "type": "git", | ||
"scripts": { | ||
"build": "yarn clean && yarn build:es6 && node ../../scripts/bundle-module", | ||
"build:release": "yarn clean && yarn build:es6 && node ../../scripts/bundle-module all", | ||
"build:es6": "tsc --declaration", | ||
"build:test": "rimraf build && tsc -p test/tsconfig.json", | ||
"build:check": "tsc --isolatedModules --noEmit", | ||
"test": "mocha test", | ||
"cover": "nyc mocha test && nyc report --reporter=lcov", | ||
"clean": "rimraf *.js *.d.ts *.map .nyc_output build coverage doc lib", | ||
"doc:readme": "ts-node -P ../../tools/tsconfig.json ../../tools/src/readme.ts", | ||
"doc": "typedoc --excludePrivate --out doc --theme ../../tools/doc/typedoc-theme src/index.ts", | ||
"build": "yarn clean && tsc --declaration", | ||
"clean": "rimraf *.js *.d.ts *.map doc", | ||
"doc": "typedoc --excludePrivate --excludeInternal --out doc src/index.ts", | ||
"doc:ae": "mkdir -p .ae/doc .ae/temp && node_modules/.bin/api-extractor run --local --verbose", | ||
"pub": "yarn build:release && yarn publish --access public" | ||
"doc:readme": "yarn doc:stats && ../../scripts/node-esm ../../tools/src/readme.ts", | ||
"doc:stats": "../../scripts/node-esm ../../tools/src/module-stats.ts", | ||
"pub": "yarn build && yarn publish --access public", | ||
"test": "testament test" | ||
}, | ||
"dependencies": { | ||
"@thi.ng/api": "^7.2.0", | ||
"@thi.ng/errors": "^1.3.4" | ||
"@thi.ng/api": "^8.0.0", | ||
"@thi.ng/errors": "^2.0.0", | ||
"@thi.ng/logger": "^0.1.0" | ||
}, | ||
"files": [ | ||
"*.js", | ||
"*.d.ts", | ||
"lib" | ||
], | ||
"devDependencies": { | ||
"@thi.ng/testament": "^0.1.0" | ||
}, | ||
"keywords": [ | ||
@@ -64,7 +59,30 @@ "agent", | ||
}, | ||
"sideEffects": false, | ||
"files": [ | ||
"*.js", | ||
"*.d.ts" | ||
], | ||
"exports": { | ||
".": { | ||
"import": "./index.js" | ||
}, | ||
"./api": { | ||
"import": "./api.js" | ||
}, | ||
"./defmulti-n": { | ||
"import": "./defmulti-n.js" | ||
}, | ||
"./defmulti": { | ||
"import": "./defmulti.js" | ||
}, | ||
"./impls": { | ||
"import": "./impls.js" | ||
}, | ||
"./logger": { | ||
"import": "./logger.js" | ||
} | ||
}, | ||
"thi.ng": { | ||
"year": 2018 | ||
}, | ||
"gitHead": "d971cb3f9b215a95483f78f1a8614015e331146f" | ||
"gitHead": "9ac1344b38b565eb894306fbf72233b6c0b2d115" | ||
} |
@@ -49,11 +49,19 @@ <!-- This file is generated - DO NOT EDIT! --> | ||
ES module import: | ||
```html | ||
// ES module | ||
<script type="module" src="https://unpkg.com/@thi.ng/defmulti?module" crossorigin></script> | ||
<script type="module" src="https://cdn.skypack.dev/@thi.ng/defmulti"></script> | ||
``` | ||
// UMD | ||
<script src="https://unpkg.com/@thi.ng/defmulti/lib/index.umd.js" crossorigin></script> | ||
[Skypack documentation](https://docs.skypack.dev/) | ||
For NodeJS (v14.6+): | ||
```text | ||
node --experimental-specifier-resolution=node --experimental-repl-await | ||
> const defmulti = await import("@thi.ng/defmulti"); | ||
``` | ||
Package sizes (gzipped, pre-treeshake): ESM: 801 bytes / CJS: 863 bytes / UMD: 939 bytes | ||
Package sizes (gzipped, pre-treeshake): ESM: 876 bytes | ||
@@ -64,2 +72,3 @@ ## Dependencies | ||
- [@thi.ng/errors](https://github.com/thi-ng/umbrella/tree/develop/packages/errors) | ||
- [@thi.ng/logger](https://github.com/thi-ng/umbrella/tree/develop/packages/logger) | ||
@@ -103,2 +112,4 @@ ## Usage examples | ||
value | ||
- `.addAll(impls)` - add/override multiple implementations (given as object, | ||
with keys referring to dispatch values) | ||
- `.remove(id)` - removes implementation for dispatch value | ||
@@ -135,3 +146,3 @@ - `.callable(...args)` - takes same args as if calling the | ||
```ts | ||
const foo = defmulti((x )=> x); | ||
const foo = defmulti((x) => x); | ||
foo.isa(23, "odd"); | ||
@@ -167,6 +178,6 @@ foo.isa(42, "even"); | ||
const foo = defmulti((x) => x, { | ||
23: ["odd"], | ||
42: ["even"], | ||
"odd": ["number"], | ||
"even": ["number"], | ||
23: "odd", | ||
42: "even", | ||
"odd": "number", | ||
"even": "number", | ||
}); | ||
@@ -173,0 +184,0 @@ |
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
0
401
Yes
56228
3
1
16
547
+ Added@thi.ng/logger@^0.1.0
+ Added@thi.ng/api@8.11.18(transitive)
+ Added@thi.ng/errors@2.5.24(transitive)
+ Added@thi.ng/logger@0.1.0(transitive)
- Removed@thi.ng/api@7.2.0(transitive)
- Removed@thi.ng/errors@1.3.4(transitive)
Updated@thi.ng/api@^8.0.0
Updated@thi.ng/errors@^2.0.0