@riim/fn-utils 0.1.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/LICENSE ADDED
@@ -0,0 +1,9 @@
1
+ MIT License
2
+
3
+ Copyright (c) Dmitry Vibe <riim@yandex.ru>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1 @@
1
+ # @riim/fn-utils
package/dist/eq.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ export declare function eq(y: any): (x: any) => boolean;
2
+ export declare function neq(y: any): (x: any) => boolean;
3
+ export declare function lt(y: number): (x: number) => boolean;
4
+ export declare function lte(y: number): (x: number) => boolean;
5
+ export declare function gt(y: number): (x: number) => boolean;
6
+ export declare function gte(y: number): (x: number) => boolean;
@@ -0,0 +1,153 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, {
2
+ value: 'Module'
3
+ });
4
+
5
+ //#region src/eq.ts
6
+ function eq(y) {
7
+ return x => x === y;
8
+ }
9
+ function neq(y) {
10
+ return x => x !== y;
11
+ }
12
+ function lt(y) {
13
+ return x => x < y;
14
+ }
15
+ function lte(y) {
16
+ return x => x <= y;
17
+ }
18
+ function gt(y) {
19
+ return x => x > y;
20
+ }
21
+ function gte(y) {
22
+ return x => x >= y;
23
+ }
24
+
25
+ //#endregion
26
+ //#region src/function.ts
27
+ var noop = () => {};
28
+ var identity = value => value;
29
+ var falsify = () => false;
30
+ var F = falsify;
31
+ var truthify = () => true;
32
+ var T = truthify;
33
+ var nullify = () => null;
34
+ var N = nullify;
35
+ var U = () => void 0;
36
+
37
+ //#endregion
38
+ //#region src/is.ts
39
+ function isFalsy(value) {
40
+ return !value;
41
+ }
42
+ function isTruthy(value) {
43
+ return !!value;
44
+ }
45
+
46
+ //#endregion
47
+ //#region src/logic.ts
48
+ function and(left, right) {
49
+ return value => left(value) && right(value);
50
+ }
51
+ function or(left, right) {
52
+ return value => left(value) || right(value);
53
+ }
54
+ function not(fn) {
55
+ switch (fn.length) {
56
+ case 0:
57
+ return () => !fn();
58
+ case 1:
59
+ return arg1 => !fn(arg1);
60
+ case 2:
61
+ return (arg1, arg2) => !fn(arg1, arg2);
62
+ case 3:
63
+ return (arg1, arg2, arg3) => !fn(arg1, arg2, arg3);
64
+ case 4:
65
+ return (arg1, arg2, arg3, arg4) => !fn(arg1, arg2, arg3, arg4);
66
+ case 5:
67
+ return (arg1, arg2, arg3, arg4, arg5) => !fn(arg1, arg2, arg3, arg4, arg5);
68
+ default:
69
+ return (...args) => !fn(...args);
70
+ }
71
+ }
72
+
73
+ //#endregion
74
+ //#region src/piped.ts
75
+ function piped(input, ...fns) {
76
+ var result = fns[0](input);
77
+ for (var i = 1, l = fns.length; i < l; i++) result = fns[i](result);
78
+ return result;
79
+ }
80
+ async function pipedAsync(input, ...fns) {
81
+ var result = fns[0](input instanceof Promise ? await input : input);
82
+ for (var i = 1, l = fns.length; i < l; i++) result = fns[i](input instanceof Promise ? await input : input);
83
+ return result;
84
+ }
85
+
86
+ //#endregion
87
+ //#region src/prop.ts
88
+ function prop(name) {
89
+ return input => input?.[name];
90
+ }
91
+ function nnProp(name) {
92
+ return input => input[name];
93
+ }
94
+ function propEq(name, value) {
95
+ return input => input != null && input[name] === value;
96
+ }
97
+ function propOr(name, defaultValue) {
98
+ return input => input?.[name] ?? defaultValue;
99
+ }
100
+
101
+ //#endregion
102
+ //#region src/throwIt.ts
103
+ function throwIt(err) {
104
+ throw err;
105
+ }
106
+
107
+ //#endregion
108
+ //#region src/tryIt.ts
109
+ function tryIt(fn, defaultResult) {
110
+ try {
111
+ return [null, fn()];
112
+ } catch (err) {
113
+ return [err, defaultResult ?? null];
114
+ }
115
+ }
116
+ async function tryItAsync(fn, defaultResult) {
117
+ try {
118
+ return [null, await fn()];
119
+ } catch (err) {
120
+ return [err, defaultResult ?? null];
121
+ }
122
+ }
123
+
124
+ //#endregion
125
+ exports.F = F;
126
+ exports.N = N;
127
+ exports.T = T;
128
+ exports.U = U;
129
+ exports.and = and;
130
+ exports.eq = eq;
131
+ exports.falsify = falsify;
132
+ exports.gt = gt;
133
+ exports.gte = gte;
134
+ exports.identity = identity;
135
+ exports.isFalsy = isFalsy;
136
+ exports.isTruthy = isTruthy;
137
+ exports.lt = lt;
138
+ exports.lte = lte;
139
+ exports.neq = neq;
140
+ exports.nnProp = nnProp;
141
+ exports.noop = noop;
142
+ exports.not = not;
143
+ exports.nullify = nullify;
144
+ exports.or = or;
145
+ exports.piped = piped;
146
+ exports.pipedAsync = pipedAsync;
147
+ exports.prop = prop;
148
+ exports.propEq = propEq;
149
+ exports.propOr = propOr;
150
+ exports.throwIt = throwIt;
151
+ exports.truthify = truthify;
152
+ exports.tryIt = tryIt;
153
+ exports.tryItAsync = tryItAsync;
@@ -0,0 +1,9 @@
1
+ export declare const noop: () => void;
2
+ export declare const identity: <T>(value: T) => T;
3
+ export declare const falsify: () => false;
4
+ export declare const F: () => false;
5
+ export declare const truthify: () => true;
6
+ export declare const T: () => true;
7
+ export declare const nullify: () => null;
8
+ export declare const N: () => null;
9
+ export declare const U: () => undefined;
@@ -0,0 +1,8 @@
1
+ export { eq, neq, lt, lte, gt, gte } from './eq';
2
+ export { noop, identity, falsify, F, truthify, T, nullify, N, U } from './function';
3
+ export { isFalsy, isTruthy } from './is';
4
+ export { and, or, not } from './logic';
5
+ export { piped, pipedAsync } from './piped';
6
+ export { prop, nnProp, propEq, propOr } from './prop';
7
+ export { throwIt } from './throwIt';
8
+ export { tryIt, tryItAsync } from './tryIt';
package/dist/is.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ type PickFalsy<T> = (false extends T ? false : never) | (0 extends T ? 0 : never) | ('' extends T ? '' : never) | (null extends T ? null : never) | (undefined extends T ? undefined : never);
2
+ export declare function isFalsy<T>(value: T): value is PickFalsy<T>;
3
+ export declare function isTruthy<T>(value: T): value is Exclude<T, false | 0 | '' | null | undefined>;
4
+ export {};
@@ -0,0 +1,14 @@
1
+ export type TAndResult<LeftResult, RightResult> = LeftResult extends null ? LeftResult : LeftResult extends undefined ? LeftResult : LeftResult extends false ? LeftResult : LeftResult extends 0 ? LeftResult : LeftResult extends '' ? LeftResult : RightResult;
2
+ export type TOrResult<LeftResult, RightResult> = LeftResult extends null ? RightResult : LeftResult extends undefined ? RightResult : LeftResult extends false ? RightResult : LeftResult extends 0 ? RightResult : LeftResult extends '' ? RightResult : LeftResult;
3
+ export declare function and<T, LeftResult, RightResult>(left: (value: T) => LeftResult, right: (value: T) => RightResult): (value: T) => TAndResult<LeftResult, RightResult>;
4
+ export declare function or<T, LeftResult, RightResult>(left: (value: T) => LeftResult, right: (value: T) => RightResult): (value: T) => TOrResult<LeftResult, RightResult>;
5
+ export declare function not<Arg1>(fn: (arg1: Arg1) => any): (arg1: Arg1) => boolean;
6
+ export declare function not<Arg1, Arg2>(fn: (arg1: Arg1, arg2: Arg2) => any): (arg1: Arg1, arg2: Arg2) => boolean;
7
+ export declare function not<Arg1, Arg2, Arg3>(fn: (arg1: Arg1, arg2: Arg2, arg3: Arg3) => any): (arg1: Arg1, arg2: Arg2, arg3: Arg3) => boolean;
8
+ export declare function not<Arg1, Arg2, Arg3, Arg4>(fn: (arg1: Arg1, arg2: Arg2, arg3: Arg3, arg4: Arg4) => any): (arg1: Arg1, arg2: Arg2, arg3: Arg3, arg4: Arg4) => boolean;
9
+ export declare function not<Arg1, Arg2, Arg3, Arg4, Arg5>(fn: (arg1: Arg1, arg2: Arg2, arg3: Arg3, arg4: Arg4, arg5: Arg5) => any): (arg1: Arg1, arg2: Arg2, arg3: Arg3, arg4: Arg4, arg5: Arg5) => boolean;
10
+ export declare function not<Arg1, Arg2, Arg3, Arg4, Arg5, Arg6>(fn: (arg1: Arg1, arg2: Arg2, arg3: Arg3, arg4: Arg4, arg5: Arg5, arg6: Arg6) => any): (arg1: Arg1, arg2: Arg2, arg3: Arg3, arg4: Arg4, arg5: Arg5, arg6: Arg6) => boolean;
11
+ export declare function not<Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7>(fn: (arg1: Arg1, arg2: Arg2, arg3: Arg3, arg4: Arg4, arg5: Arg5, arg6: Arg6, arg7: Arg7) => any): (arg1: Arg1, arg2: Arg2, arg3: Arg3, arg4: Arg4, arg5: Arg5, arg6: Arg6, arg7: Arg7) => boolean;
12
+ export declare function not<Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7, Arg8>(fn: (arg1: Arg1, arg2: Arg2, arg3: Arg3, arg4: Arg4, arg5: Arg5, arg6: Arg6, arg7: Arg7, arg8: Arg8) => any): (arg1: Arg1, arg2: Arg2, arg3: Arg3, arg4: Arg4, arg5: Arg5, arg6: Arg6, arg7: Arg7, arg8: Arg8) => boolean;
13
+ export declare function not<Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7, Arg8, Arg9>(fn: (arg1: Arg1, arg2: Arg2, arg3: Arg3, arg4: Arg4, arg5: Arg5, arg6: Arg6, arg7: Arg7, arg8: Arg8, arg9: Arg9) => any): (arg1: Arg1, arg2: Arg2, arg3: Arg3, arg4: Arg4, arg5: Arg5, arg6: Arg6, arg7: Arg7, arg8: Arg8, arg9: Arg9) => boolean;
14
+ export declare function not<Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7, Arg8, Arg9, Arg10>(fn: (arg1: Arg1, arg2: Arg2, arg3: Arg3, arg4: Arg4, arg5: Arg5, arg6: Arg6, arg7: Arg7, arg8: Arg8, arg9: Arg9, arg10: Arg10) => any): (arg1: Arg1, arg2: Arg2, arg3: Arg3, arg4: Arg4, arg5: Arg5, arg6: Arg6, arg7: Arg7, arg8: Arg8, arg9: Arg9, arg10: Arg10) => boolean;
@@ -0,0 +1,20 @@
1
+ export declare function piped<Input, Output1>(input: Input, fn1: (input: Input) => Output1): Output1;
2
+ export declare function piped<Input, Output1, Output2>(input: Input, fn1: (input: Input) => Output1, fn2: (input: Output1) => Output2): Output2;
3
+ export declare function piped<Input, Output1, Output2, Output3>(input: Input, fn1: (input: Input) => Output1, fn2: (input: Output1) => Output2, fn3: (input: Output2) => Output3): Output3;
4
+ export declare function piped<Input, Output1, Output2, Output3, Output4>(input: Input, fn1: (input: Input) => Output1, fn2: (input: Output1) => Output2, fn3: (input: Output2) => Output3, fn4: (input: Output3) => Output4): Output4;
5
+ export declare function piped<Input, Output1, Output2, Output3, Output4, Output5>(input: Input, fn1: (input: Input) => Output1, fn2: (input: Output1) => Output2, fn3: (input: Output2) => Output3, fn4: (input: Output3) => Output4, fn5: (input: Output4) => Output5): Output5;
6
+ export declare function piped<Input, Output1, Output2, Output3, Output4, Output5, Output6>(input: Input, fn1: (input: Input) => Output1, fn2: (input: Output1) => Output2, fn3: (input: Output2) => Output3, fn4: (input: Output3) => Output4, fn5: (input: Output4) => Output5, fn6: (input: Output5) => Output6): Output6;
7
+ export declare function piped<Input, Output1, Output2, Output3, Output4, Output5, Output6, Output7>(input: Input, fn1: (input: Input) => Output1, fn2: (input: Output1) => Output2, fn3: (input: Output2) => Output3, fn4: (input: Output3) => Output4, fn5: (input: Output4) => Output5, fn6: (input: Output5) => Output6, fn7: (input: Output6) => Output7): Output7;
8
+ export declare function piped<Input, Output1, Output2, Output3, Output4, Output5, Output6, Output7, Output8>(input: Input, fn1: (input: Input) => Output1, fn2: (input: Output1) => Output2, fn3: (input: Output2) => Output3, fn4: (input: Output3) => Output4, fn5: (input: Output4) => Output5, fn6: (input: Output5) => Output6, fn7: (input: Output6) => Output7, fn8: (input: Output7) => Output8): Output8;
9
+ export declare function piped<Input, Output1, Output2, Output3, Output4, Output5, Output6, Output7, Output8, Output9>(input: Input, fn1: (input: Input) => Output1, fn2: (input: Output1) => Output2, fn3: (input: Output2) => Output3, fn4: (input: Output3) => Output4, fn5: (input: Output4) => Output5, fn6: (input: Output5) => Output6, fn7: (input: Output6) => Output7, fn8: (input: Output7) => Output8, fn9: (input: Output8) => Output9): Output9;
10
+ export declare function piped<Input, Output1, Output2, Output3, Output4, Output5, Output6, Output7, Output8, Output9, Output10>(input: Input, fn1: (input: Input) => Output1, fn2: (input: Output1) => Output2, fn3: (input: Output2) => Output3, fn4: (input: Output3) => Output4, fn5: (input: Output4) => Output5, fn6: (input: Output5) => Output6, fn7: (input: Output6) => Output7, fn8: (input: Output7) => Output8, fn9: (input: Output8) => Output9, fn10: (input: Output9) => Output10): Output10;
11
+ export declare function pipedAsync<Input, Output1>(input: Input, fn1: (input: Awaited<Input>) => Output1): Promise<Output1>;
12
+ export declare function pipedAsync<Input, Output1, Output2>(input: Input, fn1: (input: Awaited<Input>) => Output1, fn2: (input: Awaited<Output1>) => Output2): Promise<Output2>;
13
+ export declare function pipedAsync<Input, Output1, Output2, Output3>(input: Input, fn1: (input: Awaited<Input>) => Output1, fn2: (input: Awaited<Output1>) => Output2, fn3: (input: Awaited<Output2>) => Output3): Promise<Output3>;
14
+ export declare function pipedAsync<Input, Output1, Output2, Output3, Output4>(input: Input, fn1: (input: Awaited<Input>) => Output1, fn2: (input: Awaited<Output1>) => Output2, fn3: (input: Awaited<Output2>) => Output3, fn4: (input: Awaited<Output3>) => Output4): Promise<Output4>;
15
+ export declare function pipedAsync<Input, Output1, Output2, Output3, Output4, Output5>(input: Input, fn1: (input: Awaited<Input>) => Output1, fn2: (input: Awaited<Output1>) => Output2, fn3: (input: Awaited<Output2>) => Output3, fn4: (input: Awaited<Output3>) => Output4, fn5: (input: Awaited<Output4>) => Output5): Promise<Output5>;
16
+ export declare function pipedAsync<Input, Output1, Output2, Output3, Output4, Output5, Output6>(input: Input, fn1: (input: Awaited<Input>) => Output1, fn2: (input: Awaited<Output1>) => Output2, fn3: (input: Awaited<Output2>) => Output3, fn4: (input: Awaited<Output3>) => Output4, fn5: (input: Awaited<Output4>) => Output5, fn6: (input: Awaited<Output5>) => Output6): Promise<Output6>;
17
+ export declare function pipedAsync<Input, Output1, Output2, Output3, Output4, Output5, Output6, Output7>(input: Input, fn1: (input: Awaited<Input>) => Output1, fn2: (input: Awaited<Output1>) => Output2, fn3: (input: Awaited<Output2>) => Output3, fn4: (input: Awaited<Output3>) => Output4, fn5: (input: Awaited<Output4>) => Output5, fn6: (input: Awaited<Output5>) => Output6, fn7: (input: Awaited<Output6>) => Output7): Promise<Output7>;
18
+ export declare function pipedAsync<Input, Output1, Output2, Output3, Output4, Output5, Output6, Output7, Output8>(input: Input, fn1: (input: Awaited<Input>) => Output1, fn2: (input: Awaited<Output1>) => Output2, fn3: (input: Awaited<Output2>) => Output3, fn4: (input: Awaited<Output3>) => Output4, fn5: (input: Awaited<Output4>) => Output5, fn6: (input: Awaited<Output5>) => Output6, fn7: (input: Awaited<Output6>) => Output7, fn8: (input: Awaited<Output7>) => Output8): Promise<Output8>;
19
+ export declare function pipedAsync<Input, Output1, Output2, Output3, Output4, Output5, Output6, Output7, Output8, Output9>(input: Input, fn1: (input: Awaited<Input>) => Output1, fn2: (input: Awaited<Output1>) => Output2, fn3: (input: Awaited<Output2>) => Output3, fn4: (input: Awaited<Output3>) => Output4, fn5: (input: Awaited<Output4>) => Output5, fn6: (input: Awaited<Output5>) => Output6, fn7: (input: Awaited<Output6>) => Output7, fn8: (input: Awaited<Output7>) => Output8, fn9: (input: Awaited<Output8>) => Output9): Promise<Output9>;
20
+ export declare function pipedAsync<Input, Output1, Output2, Output3, Output4, Output5, Output6, Output7, Output8, Output9, Output10>(input: Input, fn1: (input: Awaited<Input>) => Output1, fn2: (input: Awaited<Output1>) => Output2, fn3: (input: Awaited<Output2>) => Output3, fn4: (input: Awaited<Output3>) => Output4, fn5: (input: Awaited<Output4>) => Output5, fn6: (input: Awaited<Output5>) => Output6, fn7: (input: Awaited<Output6>) => Output7, fn8: (input: Awaited<Output7>) => Output8, fn9: (input: Awaited<Output8>) => Output9, fn10: (input: Awaited<Output9>) => Output10): Promise<Output10>;
package/dist/prop.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ export declare function prop<T, K extends keyof T>(name: K): (input: T | null | undefined) => typeof input extends null ? null : typeof input extends undefined ? undefined : T[K];
2
+ export declare function nnProp<T, K extends keyof T>(name: K): (input: T) => NonNullable<T[K]>;
3
+ export declare function propEq<T, K extends keyof T>(name: K, value: T[K]): (input: T | null | undefined) => boolean;
4
+ export declare function propOr<T, K extends keyof T, D>(name: K, defaultValue: D): (input: T | null | undefined) => Exclude<T[K], null | undefined> | D;
@@ -0,0 +1 @@
1
+ export declare function throwIt(err: any): unknown;
@@ -0,0 +1,2 @@
1
+ export declare function tryIt<T, D = T | null>(fn: () => T, defaultResult?: D): [error: null, result: T] | [error: any, result: D];
2
+ export declare function tryItAsync<T, D = T | null>(fn: () => Promise<T>, defaultResult?: D): Promise<[error: null, result: T] | [error: any, result: D]>;
package/package.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "@riim/fn-utils",
3
+ "version": "0.1.0",
4
+ "description": "",
5
+ "homepage": "https://github.com/Riim/fnUtils#readme",
6
+ "bugs": {
7
+ "url": "https://github.com/Riim/fnUtils/issues"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/Riim/fnUtils.git"
12
+ },
13
+ "license": "MIT",
14
+ "author": "Dmitry Vibe",
15
+ "type": "commonjs",
16
+ "main": "dist/fnUtils.js",
17
+ "typings": "dist/index.d.ts",
18
+ "scripts": {
19
+ "build": "rm -rf dist && npx tsc && rolldown -c && babel dist --out-dir dist",
20
+ "test": "rm -rf coverage && jest --coverage"
21
+ },
22
+ "devDependencies": {
23
+ "@babel/cli": "7.28.6",
24
+ "@babel/core": "7.29.0",
25
+ "@babel/plugin-transform-block-scoping": "7.28.6",
26
+ "@eslint/js": "9.39.3",
27
+ "@trivago/prettier-plugin-sort-imports": "6.0.2",
28
+ "@typescript-eslint/parser": "8.56.1",
29
+ "eslint": "9.39.3",
30
+ "eslint-plugin-github": "6.0.0",
31
+ "globals": "17.3.0",
32
+ "rolldown": "1.0.0-rc.6",
33
+ "typescript": "5.9.3",
34
+ "typescript-eslint": "8.56.1"
35
+ }
36
+ }