Skip to content

Commit

Permalink
feat: Change get to return default value if it's explicitly set and…
Browse files Browse the repository at this point in the history
… the result isNothing (null, undefined, NaN).
  • Loading branch information
andreidmt committed Oct 23, 2019
1 parent fc1900d commit 2aedfac
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/get/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ import { pipe } from "../pipe/pipe"
* // => undefined
*/
const get = (path, defaultValue) => source => {
let result = undefined

if (is(source) && typeof source === "object") {
return pipe(
result = pipe(
reduce(
(acc, item) =>
is(acc) && typeof acc === "object" ? acc[item] : undefined,
Expand All @@ -45,7 +47,7 @@ const get = (path, defaultValue) => source => {
)(Array.isArray(path) ? path : [path])
}

return undefined
return isNothing(result) && is(defaultValue) ? defaultValue : result
}

export { get }
6 changes: 6 additions & 0 deletions src/get/get.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ test("get", t => {
"Get existing prop from object that is also NaN with default value"
)

t.equal(
get(["a", "b"], "default value")({}),
"default value",
"Get existing prop from object that is also NaN with default value"
)

t.ok(
Number.isNaN(get(["a"])({ a: NaN })),
"Get existing prop from object that is also NaN"
Expand Down

0 comments on commit 2aedfac

Please sign in to comment.