Skip to content

Commit

Permalink
Do not remove zeroes from tree paths
Browse files Browse the repository at this point in the history
  • Loading branch information
Alejandro Hernandez committed Feb 7, 2024
1 parent 3142138 commit 6cfceb5
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 4 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
22 changes: 21 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 removes the number 0.
_.curry((join, x) => _.compact(x).join(join))

/**
* Compacts and joins an array with `.`
Expand Down Expand Up @@ -171,6 +173,24 @@ export let dotEncoder = encoder('.')
*/
export let slashEncoder = encoder('/')

// The name "pack" is tentative. It's intended to behave as `_.compact` without
// removing the number 0.
let pack = _.remove(
(x) => x === undefined || x === null || x === false || x === ''
)

// Internal use until we release next major
let __internalEncoder = (separator) => ({
encode: _.flow(pack, _.join(separator)),
decode: _.split(separator),
})

// Internal use until we release next major
export let __internalDotEncoder = __internalEncoder('.')

// Internal use until we release next major
export let __internalSlashEncoder = __internalEncoder('/')

/**
* Takes a predicate function and an array, and returns an array of arrays where each element has one or more elements of the original array. Similar to Haskell's [groupBy](http://zvon.org/other/haskell/Outputlist/groupBy_f.html).
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 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 removes the number 0.
export const concatStrings = _.flow(_.compact, _.map(_.trim), _.join(' '))

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

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

/**
* A default check if something can be traversed - currently it is arrays and plain objects.
Expand Down Expand Up @@ -239,7 +239,7 @@ export let treeValues = (x, i, xs) => [x, ...xs]
* @signature (build, encoder) -> treePathBuilderFunction
*/
export let treePath =
(build = treeKeys, encoder = dotEncoder) =>
(build = treeKeys, encoder = __internalDotEncoder) =>
(...args) =>
(encoder.encode || encoder)(build(...args).reverse())

Expand All @@ -249,7 +249,7 @@ export let treePath =
* @signature prop -> treePathBuilderFunction
*/
export let propTreePath = (prop) =>
treePath(_.flow(treeValues, _.map(prop)), slashEncoder)
treePath(_.flow(treeValues, _.map(prop)), __internalSlashEncoder)

/**
* 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
6 changes: 6 additions & 0 deletions test/tree.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ describe('Tree Functions', () => {
}
expect(F.traverse(x)).to.deep.equal(x)
})
describe('treePath', () => {
it('should not remove zeroes', () => {
let path = F.treePath()(null, 'first', [], ['second', 0, 'third'])
expect(path).to.equal('third.0.second.first')
})
})
describe('walk', () => {
let x = {
a: 1,
Expand Down

0 comments on commit 6cfceb5

Please sign in to comment.