Skip to content

Commit

Permalink
Merge pull request #438 from smartprocure/feature/fix-tree-paths
Browse files Browse the repository at this point in the history
Do not remove zeroes from tree paths
  • Loading branch information
stellarhoof authored Feb 7, 2024
2 parents 3142138 + 7ba3a58 commit 30e8529
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/big-plums-wave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'futil': patch
---

Do not remove zeroes from tree paths
4 changes: 3 additions & 1 deletion src/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ let last = _.takeRight(1)
* @signature joinString -> [string1, string2, ...stringN] -> string1 + joinString + string2 + joinString ... + stringN
* @typescript {(join: string, x: any[]) => string}
*/
export let compactJoin = _.curry((join, x) => _.compact(x).join(join))
export let compactJoin =
// TODO: (major) Do not use `_.compact` as it removes the number 0.
_.curry((join, x) => _.compact(x).join(join))

/**
* Compacts and joins an array with `.`
Expand Down
1 change: 1 addition & 0 deletions src/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export let insertAtIndex = _.curry((index, val, collection) =>
* @signature (fn, collection) -> collection
*/
export let compactMap = _.curry((fn, collection) =>
// TODO: (major) Do not use `_.compact` as it removes the number 0.
_.flow(_.map(fn), _.compact)(collection)
)

Expand Down
2 changes: 2 additions & 0 deletions src/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export const quote = wrap('"', '"')
* @signature 'asdf' -> '(asdf)'
*/
export const parens = wrap('(', ')')

// TODO: (major) Do not use `_.compact` as it removes the number 0.
export const concatStrings = _.flow(_.compact, _.map(_.trim), _.join(' '))

/**
Expand Down
11 changes: 7 additions & 4 deletions src/tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

import _ from 'lodash/fp'
import { findIndexed } from './conversion'
import { pushOn, dotEncoder, slashEncoder } from './array'
import { pushOn } from './array'
import { isBlank } from './lang'

/**
* A default check if something can be traversed - currently it is arrays and plain objects.
Expand Down Expand Up @@ -233,23 +234,25 @@ export let treeKeys = (x, i, xs, is) => [i, ...is]
*/
export let treeValues = (x, i, xs) => [x, ...xs]

let pathEncoder = (separator) => _.flow(_.remove(isBlank), _.join(separator))

/**
* Creates a path builder for use in `flattenTree`. By default, the builder will look use child indexes and a dotEncoder. Encoder can be an encoding function or a futil `encoder` (an object with encode and decode functions)
*
* @signature (build, encoder) -> treePathBuilderFunction
*/
export let treePath =
(build = treeKeys, encoder = dotEncoder) =>
(build = treeKeys, encoder = pathEncoder('.')) =>
(...args) =>
(encoder.encode || encoder)(build(...args).reverse())

/**
* Creates a path builder for use in `flattenTree`, using a slashEncoder and using the specified prop function as an iteratee on each node to determine the keys.
* Creates a path builder for use in `flattenTree`, using a pathEncoder and using the specified prop function as an iteratee on each node to determine the keys.
*
* @signature prop -> treePathBuilderFunction
*/
export let propTreePath = (prop) =>
treePath(_.flow(treeValues, _.map(prop)), slashEncoder)
treePath(_.flow(treeValues, _.map(prop)), pathEncoder('/'))

/**
* Creates a flat object with a property for each node, using `buildPath` to determine the keys. `buildPath` takes the same arguments as a tree walking iteratee. It will default to a dot tree path.
Expand Down

0 comments on commit 30e8529

Please sign in to comment.