overridable-fn 1.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,45 @@
1
+ # overridable-fn
2
+
3
+ Create functions whose behavior can be swapped out dynamically.
4
+
5
+ Intended to be a lightweight less-intrusive alternative to IoC containers.
6
+
7
+ Let's say you have this function:
8
+
9
+ ```ts
10
+ const getUserById = async (id: string) => {
11
+ // ... fetch users from database
12
+ }
13
+ ```
14
+
15
+ Now if you just wrap this function with fn:
16
+
17
+ ```ts
18
+ import { fn } from "overridable-fn"
19
+
20
+ const getUserById = fn(async (id: string) => {
21
+ // ... fetch users from database
22
+ })
23
+ ```
24
+
25
+ The behavior of `getUserById` remains unchanged
26
+
27
+ ```ts
28
+ getUserById(10) //-> fetches users from database
29
+ ```
30
+
31
+ However, you can now override the implementation (eg. in tests)
32
+
33
+ ```ts
34
+ getUserById.override((prevImpl) => async (id: string) => {
35
+ console.log('[WARN] Returning fake user')
36
+ return new User({ id })
37
+ })
38
+ ```
39
+
40
+ So if you call `getUserById()` now, your overriden implementation be invoked instead.
41
+
42
+ ```ts
43
+ $ getUserById(10)
44
+ [WARN] Returning fake user
45
+ ```
package/dist/index.cjs ADDED
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ fn: () => fn
24
+ });
25
+ module.exports = __toCommonJS(src_exports);
26
+ var fn = (defaultImpl) => {
27
+ let impl = defaultImpl;
28
+ const unwrap = () => impl;
29
+ const override = (provideImpl) => {
30
+ impl = provideImpl(impl);
31
+ return invoker;
32
+ };
33
+ const invoker = (...args) => {
34
+ return impl(...args);
35
+ };
36
+ invoker.unwrap = unwrap;
37
+ invoker.override = override;
38
+ return invoker;
39
+ };
40
+ // Annotate the CommonJS export names for ESM import in node:
41
+ 0 && (module.exports = {
42
+ fn
43
+ });
44
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["interface FnImpl<TArgs extends any[], TRet> {\n (...args: TArgs): TRet;\n}\n\ninterface WrappedFnImpl<TArgs extends any[], TRet> {\n (...args: TArgs): TRet;\n unwrap: () => FnImpl<TArgs, TRet>;\n override: (\n provideImpl: (curImpl: FnImpl<TArgs, TRet>) => FnImpl<TArgs, TRet>\n ) => WrappedFnImpl<TArgs, TRet>;\n}\n\nexport const fn = <TArgs extends any[], TRet>(\n defaultImpl: FnImpl<TArgs, TRet>\n): WrappedFnImpl<TArgs, TRet> => {\n let impl = defaultImpl;\n\n const unwrap = () => impl;\n\n const override = (\n provideImpl: (curImpl: FnImpl<TArgs, TRet>) => FnImpl<TArgs, TRet>\n ) => {\n impl = provideImpl(impl);\n return invoker;\n };\n\n const invoker: WrappedFnImpl<TArgs, TRet> = (...args) => {\n return impl(...args);\n };\n invoker.unwrap = unwrap;\n invoker.override = override;\n\n return invoker;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAYO,IAAM,KAAK,CACd,gBAC6B;AAC7B,MAAI,OAAO;AAEX,QAAM,SAAS,MAAM;AAErB,QAAM,WAAW,CACb,gBACC;AACD,WAAO,YAAY,IAAI;AACvB,WAAO;AAAA,EACX;AAEA,QAAM,UAAsC,IAAI,SAAS;AACrD,WAAO,KAAK,GAAG,IAAI;AAAA,EACvB;AACA,UAAQ,SAAS;AACjB,UAAQ,WAAW;AAEnB,SAAO;AACX;","names":[]}
@@ -0,0 +1,11 @@
1
+ interface FnImpl<TArgs extends any[], TRet> {
2
+ (...args: TArgs): TRet;
3
+ }
4
+ interface WrappedFnImpl<TArgs extends any[], TRet> {
5
+ (...args: TArgs): TRet;
6
+ unwrap: () => FnImpl<TArgs, TRet>;
7
+ override: (provideImpl: (curImpl: FnImpl<TArgs, TRet>) => FnImpl<TArgs, TRet>) => WrappedFnImpl<TArgs, TRet>;
8
+ }
9
+ declare const fn: <TArgs extends any[], TRet>(defaultImpl: FnImpl<TArgs, TRet>) => WrappedFnImpl<TArgs, TRet>;
10
+
11
+ export { fn };
package/dist/index.js ADDED
@@ -0,0 +1,19 @@
1
+ // src/index.ts
2
+ var fn = (defaultImpl) => {
3
+ let impl = defaultImpl;
4
+ const unwrap = () => impl;
5
+ const override = (provideImpl) => {
6
+ impl = provideImpl(impl);
7
+ return invoker;
8
+ };
9
+ const invoker = (...args) => {
10
+ return impl(...args);
11
+ };
12
+ invoker.unwrap = unwrap;
13
+ invoker.override = override;
14
+ return invoker;
15
+ };
16
+ export {
17
+ fn
18
+ };
19
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["interface FnImpl<TArgs extends any[], TRet> {\n (...args: TArgs): TRet;\n}\n\ninterface WrappedFnImpl<TArgs extends any[], TRet> {\n (...args: TArgs): TRet;\n unwrap: () => FnImpl<TArgs, TRet>;\n override: (\n provideImpl: (curImpl: FnImpl<TArgs, TRet>) => FnImpl<TArgs, TRet>\n ) => WrappedFnImpl<TArgs, TRet>;\n}\n\nexport const fn = <TArgs extends any[], TRet>(\n defaultImpl: FnImpl<TArgs, TRet>\n): WrappedFnImpl<TArgs, TRet> => {\n let impl = defaultImpl;\n\n const unwrap = () => impl;\n\n const override = (\n provideImpl: (curImpl: FnImpl<TArgs, TRet>) => FnImpl<TArgs, TRet>\n ) => {\n impl = provideImpl(impl);\n return invoker;\n };\n\n const invoker: WrappedFnImpl<TArgs, TRet> = (...args) => {\n return impl(...args);\n };\n invoker.unwrap = unwrap;\n invoker.override = override;\n\n return invoker;\n};\n"],"mappings":";AAYO,IAAM,KAAK,CACd,gBAC6B;AAC7B,MAAI,OAAO;AAEX,QAAM,SAAS,MAAM;AAErB,QAAM,WAAW,CACb,gBACC;AACD,WAAO,YAAY,IAAI;AACvB,WAAO;AAAA,EACX;AAEA,QAAM,UAAsC,IAAI,SAAS;AACrD,WAAO,KAAK,GAAG,IAAI;AAAA,EACvB;AACA,UAAQ,SAAS;AACjB,UAAQ,WAAW;AAEnB,SAAO;AACX;","names":[]}
package/package.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "name": "overridable-fn",
3
+ "version": "1.0.0",
4
+ "description": "Simple utility to define functions whose behavior can be overriden at runtime",
5
+ "author": "Lorefnon <lorefnon@tutanota.com> (https://lorefnon.me)",
6
+ "main": "index.js",
7
+ "files": [
8
+ "dist/*",
9
+ "README.md"
10
+ ],
11
+ "exports": {
12
+ "import": "./dist/index.mjs",
13
+ "require": "./dist/index.js"
14
+ },
15
+ "type": "module",
16
+ "license": "MIT",
17
+ "devDependencies": {
18
+ "@types/node": "^18.11.12",
19
+ "ts-node": "^10.9.1",
20
+ "tsup": "^6.5.0",
21
+ "typescript": "^4.9.4"
22
+ },
23
+ "scripts": {
24
+ "build": "tsup",
25
+ "test": "ts-node ./test/index.test.ts"
26
+ }
27
+ }