Skip to content

Commit

Permalink
Improvemed function naming (#8)
Browse files Browse the repository at this point in the history
* bumped version number

* leafNodeMap -> mapLeafNodes
  • Loading branch information
taylorsweetman authored Jun 5, 2022
1 parent aa08c2c commit 71c90dd
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 27 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,22 @@ For example:

```typescript
const addOne = (num) => num + 1
const curried = leafNodeMap(addOne)(1)
const uncurried = _leafNodeMap(addOne, 1)
const curried = mapLeafNodes(addOne)(1)
const uncurried = _mapLeafNodes(addOne, 1)
const sameResult = curried === uncurried
console.log(sameResult)
// true
```

## Highlights

### [leafNodeMap](https://github.com/taylorsweetman/taydash/blob/main/src/functions/leafNodeMap.ts)
### [mapLeafNodes](https://github.com/taylorsweetman/taydash/blob/main/src/functions/mapLeafNodes.ts)

- Curried function which iterates over a `JSONValue` (JSON style object, array, or primitive) and applies a passed function to each leaf node of the value.

```typescript
const addOne = (num) => num + 1
const result = leafNodeMap(addOne)({ num: 0, arr: [1, 2, 3] })
const result = mapLeafNodes(addOne)({ num: 0, arr: [1, 2, 3] })
console.log(result)
// { num: 1, arr: [ 2, 3, 4 ] }
```
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "taydash",
"version": "1.1.0",
"version": "2.0.0",
"description": "Helpful utility functions",
"keywords": [
"utility functions",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { JSONArray, JSONLeaf, JSONObject, JSONValue } from '../types'
import { identity } from 'lodash/fp'
import { _leafNodeMap, leafNodeMap } from './leafNodeMap'
import { _mapLeafNodes, mapLeafNodes } from './mapLeafNodes'

describe('leafNodeMap', () => {
describe('mapLeafNodes', () => {
describe('with identity function', () => {
it.each([
[1, 1],
Expand All @@ -12,8 +12,8 @@ describe('leafNodeMap', () => {
])(
'should apply function on leaf iterables correctly: %s -> %s',
(iter: JSONLeaf, expected: JSONLeaf) => {
expect(leafNodeMap(identity)(iter)).toBe(expected)
expect(_leafNodeMap(identity, iter)).toBe(expected)
expect(mapLeafNodes(identity)(iter)).toBe(expected)
expect(_mapLeafNodes(identity, iter)).toBe(expected)
}
)

Expand All @@ -23,8 +23,8 @@ describe('leafNodeMap', () => {
])(
'should return empty object or list when receiving empty object or list: %s -> %s',
(iter: JSONObject | JSONArray, expected: JSONObject | JSONArray) => {
expect(leafNodeMap(identity)(iter)).toEqual(expected)
expect(_leafNodeMap(identity, iter)).toEqual(expected)
expect(mapLeafNodes(identity)(iter)).toEqual(expected)
expect(_mapLeafNodes(identity, iter)).toEqual(expected)
}
)

Expand All @@ -40,8 +40,8 @@ describe('leafNodeMap', () => {
])(
'should correctly use applied functions on deeply nested JSONValue: %j -> %j',
(iter: JSONValue, expected: JSONValue) => {
expect(leafNodeMap(identity)(iter)).toEqual(expected)
expect(_leafNodeMap(identity, iter)).toEqual(expected)
expect(mapLeafNodes(identity)(iter)).toEqual(expected)
expect(_mapLeafNodes(identity, iter)).toEqual(expected)
}
)
})
Expand All @@ -57,8 +57,8 @@ describe('leafNodeMap', () => {
])(
'should apply function on leaf iterables correctly: %s -> %s',
(iter: JSONLeaf, expected: JSONLeaf) => {
expect(leafNodeMap(toOne)(iter)).toBe(expected)
expect(_leafNodeMap(toOne, iter)).toBe(expected)
expect(mapLeafNodes(toOne)(iter)).toBe(expected)
expect(_mapLeafNodes(toOne, iter)).toBe(expected)
}
)

Expand All @@ -68,8 +68,8 @@ describe('leafNodeMap', () => {
])(
'should return empty object or list when receiving empty object or list: %s -> %s',
(iter: JSONObject | JSONArray, expected: JSONObject | JSONArray) => {
expect(leafNodeMap(toOne)(iter)).toEqual(expected)
expect(_leafNodeMap(toOne, iter)).toEqual(expected)
expect(mapLeafNodes(toOne)(iter)).toEqual(expected)
expect(_mapLeafNodes(toOne, iter)).toEqual(expected)
}
)

Expand All @@ -82,8 +82,8 @@ describe('leafNodeMap', () => {
])(
'should correctly use applied functions on deeply nested JSONValue: %j -> %j',
(iter: JSONValue, expected: JSONValue) => {
expect(leafNodeMap(toOne)(iter)).toEqual(expected)
expect(_leafNodeMap(toOne, iter)).toEqual(expected)
expect(mapLeafNodes(toOne)(iter)).toEqual(expected)
expect(_mapLeafNodes(toOne, iter)).toEqual(expected)
}
)
})
Expand Down
10 changes: 5 additions & 5 deletions src/functions/leafNodeMap.ts → src/functions/mapLeafNodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ import { isPlainObject, isArray, entries, pipe, reduce, map } from 'lodash/fp'
const mapArray =
(func: (a: JSONValue) => JSONValue) =>
(iterable: JSONArray): JSONArray =>
map(leafNodeMap(func))(iterable)
map(mapLeafNodes(func))(iterable)

const mapObject =
(func: (a: JSONValue) => JSONValue) =>
(iterable: JSONObject): JSONObject =>
pipe(
entries,
reduce(
(accum, [key, val]) => ({ ...accum, [key]: leafNodeMap(func)(val) }),
(accum, [key, val]) => ({ ...accum, [key]: mapLeafNodes(func)(val) }),
{} as JSONObject
)
)(iterable)

export const leafNodeMap =
export const mapLeafNodes =
(func: (a: JSONValue) => JSONValue) =>
(iterable: JSONValue): JSONValue => {
if (isLeaf(iterable)) return func(iterable)
Expand All @@ -29,7 +29,7 @@ export const leafNodeMap =
return null
}

export const _leafNodeMap = (
export const _mapLeafNodes = (
func: (a: JSONValue) => JSONValue,
iterable: JSONValue
): JSONValue => leafNodeMap(func)(iterable)
): JSONValue => mapLeafNodes(func)(iterable)
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { leafNodeMap, _leafNodeMap } from './functions/leafNodeMap'
export { mapLeafNodes, _mapLeafNodes } from './functions/mapLeafNodes'
export {
type JSONArray,
type JSONLeaf,
Expand Down

0 comments on commit 71c90dd

Please sign in to comment.