Skip to content

Commit

Permalink
improve memo, partial, compose, and chain function types (#379)
Browse files Browse the repository at this point in the history
* improve memo, partial, compose, and chain function types

---------

Co-authored-by: Ray Epps <[email protected]>
  • Loading branch information
sodiray and Ray Epps authored Feb 21, 2024
1 parent caf1a86 commit 2be4acf
Show file tree
Hide file tree
Showing 9 changed files with 577 additions and 126 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test-on-master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [14.x, 16.x, 18.x]
node-version: [14.x, 16.x, 18.17.1]
steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/test-on-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [14.x, 16.x, 18.x]
node-version: [14.x, 16.x, 18.17.1]
steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
Expand All @@ -22,7 +22,7 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: '16.x'
node-version: '20.x'
- run: yarn
- run: yarn format:check
verify-cdn-build-matches-src:
Expand All @@ -31,7 +31,7 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: '16.x'
node-version: '20.x'
- run: yarn
- run: npm install -g checksum
- name: Calc current cdn checksums
Expand Down
18 changes: 9 additions & 9 deletions cdn/radash.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -487,15 +487,15 @@ const guard = (func, shouldGuard) => {
};

function chain(...funcs) {
return function forInitialArg(initialArg) {
return funcs.reduce((acc, fn) => fn(acc), initialArg);
return (...args) => {
return funcs.slice(1).reduce((acc, fn) => fn(acc), funcs[0](...args));
};
}
const compose = (...funcs) => {
function compose(...funcs) {
return funcs.reverse().reduce((acc, fn) => fn(acc));
};
}
const partial = (fn, ...args) => {
return (...rest) => fn(...args, ...rest);
return (...rest) => fn(...[...args, ...rest]);
};
const partob = (fn, argobj) => {
return (restobj) => fn({
Expand Down Expand Up @@ -711,15 +711,15 @@ const omit = (obj, keys2) => {
const get = (value, path, defaultValue) => {
const segments = path.split(/[\.\[\]]/g);
let current = value;
for (let key of segments) {
for (const key of segments) {
if (current === null)
return defaultValue;
if (current === void 0)
return defaultValue;
key = key.replace(/['"]/g, "");
if (key.trim() === "")
const dequoted = key.replace(/['"]/g, "");
if (dequoted.trim() === "")
continue;
current = current[key];
current = current[dequoted];
}
if (current === void 0)
return defaultValue;
Expand Down
18 changes: 9 additions & 9 deletions cdn/radash.js
Original file line number Diff line number Diff line change
Expand Up @@ -490,15 +490,15 @@ var radash = (function (exports) {
};

function chain(...funcs) {
return function forInitialArg(initialArg) {
return funcs.reduce((acc, fn) => fn(acc), initialArg);
return (...args) => {
return funcs.slice(1).reduce((acc, fn) => fn(acc), funcs[0](...args));
};
}
const compose = (...funcs) => {
function compose(...funcs) {
return funcs.reverse().reduce((acc, fn) => fn(acc));
};
}
const partial = (fn, ...args) => {
return (...rest) => fn(...args, ...rest);
return (...rest) => fn(...[...args, ...rest]);
};
const partob = (fn, argobj) => {
return (restobj) => fn({
Expand Down Expand Up @@ -714,15 +714,15 @@ var radash = (function (exports) {
const get = (value, path, defaultValue) => {
const segments = path.split(/[\.\[\]]/g);
let current = value;
for (let key of segments) {
for (const key of segments) {
if (current === null)
return defaultValue;
if (current === void 0)
return defaultValue;
key = key.replace(/['"]/g, "");
if (key.trim() === "")
const dequoted = key.replace(/['"]/g, "");
if (dequoted.trim() === "")
continue;
current = current[key];
current = current[dequoted];
}
if (current === void 0)
return defaultValue;
Expand Down
2 changes: 1 addition & 1 deletion cdn/radash.min.js

Large diffs are not rendered by default.

37 changes: 19 additions & 18 deletions docs/curry/chain.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,29 @@ chained(0) // => 10
chained(7) // => 24
```

### More example
### Example
```ts
import { chain } from 'radash'

type User = { id: number; name: string }
const users: User[] = [
{ id: 1, name: 'John Doe' },
{ id: 2, name: 'John Smith' },
{ id: 3, name: 'John Wick' }
]
function getName<T extends { name: string }>(item: T) {
return item.name;
}
function upperCase(text: string): Uppercase<string> {
return text.toUpperCase() as Uppercase<string>;
type Deity = {
name: string
rank: number
}

const getUpperName = chain<User, Uppercase<string>>(getName, upperCase)
// ^ Use chain function here
const gods: Deity[] = [
{ rank: 8, name: 'Ra' },
{ rank: 7, name: 'Zeus' },
{ rank: 9, name: 'Loki' }
]

const getName = (god: Deity) => item.name
const upperCase = (text: string) => text.toUpperCase() as Uppercase<string>

const getUpperName = chain(
getName,
upperCase
)

getUpperName(users[0]) // => 'JOHN DOE'
users.map((user) => getUpperName(user)) // => ['JOHN DOE', 'JOHN SMITH', 'JOHN WICK']
users.map(getUpperName) // => ['JOHN DOE', 'JOHN SMITH', 'JOHN WICK']
// ^ use chained function as a Point-free
getUpperName(gods[0]) // => 'RA'
gods.map(getUpperName) // => ['RA', 'ZEUS', 'LOKI']
```
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "radash",
"version": "11.0.0",
"version": "12.0.0",
"description": "Functional utility library - modern, simple, typed, powerful",
"main": "dist/cjs/index.cjs",
"module": "dist/esm/index.mjs",
Expand Down
Loading

0 comments on commit 2be4acf

Please sign in to comment.