Skip to content
This repository has been archived by the owner on Feb 17, 2021. It is now read-only.

Commit

Permalink
feat: rewritten in an immutable style
Browse files Browse the repository at this point in the history
  • Loading branch information
mg901 committed Feb 8, 2019
1 parent bf277c9 commit 983da8a
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions src/helpers/get-all-values-of.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
// @flow

import { isObject } from './is-object';

export const getAllValuesOf: (string, void | any[]) => (any) => any[] = (
target,
memo,
) => (obj) => {
const buffer = !Array.isArray(memo) ? [] : memo;

for (const key in obj) {
if (key === target) {
buffer.push(obj[key]);
} else if (Array.isArray(obj[key]) || isObject(obj[key])) {
getAllValuesOf(target, buffer)(obj[key]);
}
}

return buffer;
return Object.keys(Object(obj)).reduce(
(acc, key) =>
key === target
? [...acc, obj[key]]
: [...getAllValuesOf(target, acc)(obj[key])],
buffer,
);
};

0 comments on commit 983da8a

Please sign in to comment.