Skip to content

Commit 2be4acf

Browse files
sodirayRay Epps
and
Ray Epps
authored
improve memo, partial, compose, and chain function types (#379)
* improve memo, partial, compose, and chain function types --------- Co-authored-by: Ray Epps <[email protected]>
1 parent caf1a86 commit 2be4acf

File tree

9 files changed

+577
-126
lines changed

9 files changed

+577
-126
lines changed

.github/workflows/test-on-master.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ jobs:
77
runs-on: ubuntu-latest
88
strategy:
99
matrix:
10-
node-version: [14.x, 16.x, 18.x]
10+
node-version: [14.x, 16.x, 18.17.1]
1111
steps:
1212
- uses: actions/checkout@v3
1313
- name: Use Node.js ${{ matrix.node-version }}

.github/workflows/test-on-pr.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ jobs:
77
runs-on: ubuntu-latest
88
strategy:
99
matrix:
10-
node-version: [14.x, 16.x, 18.x]
10+
node-version: [14.x, 16.x, 18.17.1]
1111
steps:
1212
- uses: actions/checkout@v3
1313
- name: Use Node.js ${{ matrix.node-version }}
@@ -22,7 +22,7 @@ jobs:
2222
- uses: actions/checkout@v3
2323
- uses: actions/setup-node@v3
2424
with:
25-
node-version: '16.x'
25+
node-version: '20.x'
2626
- run: yarn
2727
- run: yarn format:check
2828
verify-cdn-build-matches-src:
@@ -31,7 +31,7 @@ jobs:
3131
- uses: actions/checkout@v3
3232
- uses: actions/setup-node@v3
3333
with:
34-
node-version: '16.x'
34+
node-version: '20.x'
3535
- run: yarn
3636
- run: npm install -g checksum
3737
- name: Calc current cdn checksums

cdn/radash.esm.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -487,15 +487,15 @@ const guard = (func, shouldGuard) => {
487487
};
488488

489489
function chain(...funcs) {
490-
return function forInitialArg(initialArg) {
491-
return funcs.reduce((acc, fn) => fn(acc), initialArg);
490+
return (...args) => {
491+
return funcs.slice(1).reduce((acc, fn) => fn(acc), funcs[0](...args));
492492
};
493493
}
494-
const compose = (...funcs) => {
494+
function compose(...funcs) {
495495
return funcs.reverse().reduce((acc, fn) => fn(acc));
496-
};
496+
}
497497
const partial = (fn, ...args) => {
498-
return (...rest) => fn(...args, ...rest);
498+
return (...rest) => fn(...[...args, ...rest]);
499499
};
500500
const partob = (fn, argobj) => {
501501
return (restobj) => fn({
@@ -711,15 +711,15 @@ const omit = (obj, keys2) => {
711711
const get = (value, path, defaultValue) => {
712712
const segments = path.split(/[\.\[\]]/g);
713713
let current = value;
714-
for (let key of segments) {
714+
for (const key of segments) {
715715
if (current === null)
716716
return defaultValue;
717717
if (current === void 0)
718718
return defaultValue;
719-
key = key.replace(/['"]/g, "");
720-
if (key.trim() === "")
719+
const dequoted = key.replace(/['"]/g, "");
720+
if (dequoted.trim() === "")
721721
continue;
722-
current = current[key];
722+
current = current[dequoted];
723723
}
724724
if (current === void 0)
725725
return defaultValue;

cdn/radash.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -490,15 +490,15 @@ var radash = (function (exports) {
490490
};
491491

492492
function chain(...funcs) {
493-
return function forInitialArg(initialArg) {
494-
return funcs.reduce((acc, fn) => fn(acc), initialArg);
493+
return (...args) => {
494+
return funcs.slice(1).reduce((acc, fn) => fn(acc), funcs[0](...args));
495495
};
496496
}
497-
const compose = (...funcs) => {
497+
function compose(...funcs) {
498498
return funcs.reverse().reduce((acc, fn) => fn(acc));
499-
};
499+
}
500500
const partial = (fn, ...args) => {
501-
return (...rest) => fn(...args, ...rest);
501+
return (...rest) => fn(...[...args, ...rest]);
502502
};
503503
const partob = (fn, argobj) => {
504504
return (restobj) => fn({
@@ -714,15 +714,15 @@ var radash = (function (exports) {
714714
const get = (value, path, defaultValue) => {
715715
const segments = path.split(/[\.\[\]]/g);
716716
let current = value;
717-
for (let key of segments) {
717+
for (const key of segments) {
718718
if (current === null)
719719
return defaultValue;
720720
if (current === void 0)
721721
return defaultValue;
722-
key = key.replace(/['"]/g, "");
723-
if (key.trim() === "")
722+
const dequoted = key.replace(/['"]/g, "");
723+
if (dequoted.trim() === "")
724724
continue;
725-
current = current[key];
725+
current = current[dequoted];
726726
}
727727
if (current === void 0)
728728
return defaultValue;

cdn/radash.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/curry/chain.mdx

+19-18
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,29 @@ chained(0) // => 10
2121
chained(7) // => 24
2222
```
2323

24-
### More example
24+
### Example
2525
```ts
2626
import { chain } from 'radash'
2727

28-
type User = { id: number; name: string }
29-
const users: User[] = [
30-
{ id: 1, name: 'John Doe' },
31-
{ id: 2, name: 'John Smith' },
32-
{ id: 3, name: 'John Wick' }
33-
]
34-
function getName<T extends { name: string }>(item: T) {
35-
return item.name;
36-
}
37-
function upperCase(text: string): Uppercase<string> {
38-
return text.toUpperCase() as Uppercase<string>;
28+
type Deity = {
29+
name: string
30+
rank: number
3931
}
4032

41-
const getUpperName = chain<User, Uppercase<string>>(getName, upperCase)
42-
// ^ Use chain function here
33+
const gods: Deity[] = [
34+
{ rank: 8, name: 'Ra' },
35+
{ rank: 7, name: 'Zeus' },
36+
{ rank: 9, name: 'Loki' }
37+
]
38+
39+
const getName = (god: Deity) => item.name
40+
const upperCase = (text: string) => text.toUpperCase() as Uppercase<string>
41+
42+
const getUpperName = chain(
43+
getName,
44+
upperCase
45+
)
4346

44-
getUpperName(users[0]) // => 'JOHN DOE'
45-
users.map((user) => getUpperName(user)) // => ['JOHN DOE', 'JOHN SMITH', 'JOHN WICK']
46-
users.map(getUpperName) // => ['JOHN DOE', 'JOHN SMITH', 'JOHN WICK']
47-
// ^ use chained function as a Point-free
47+
getUpperName(gods[0]) // => 'RA'
48+
gods.map(getUpperName) // => ['RA', 'ZEUS', 'LOKI']
4849
```

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "radash",
3-
"version": "11.0.0",
3+
"version": "12.0.0",
44
"description": "Functional utility library - modern, simple, typed, powerful",
55
"main": "dist/cjs/index.cjs",
66
"module": "dist/esm/index.mjs",

0 commit comments

Comments
 (0)