-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Sean Hamilton
committed
Nov 30, 2018
1 parent
9e5d65e
commit deed825
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// o | ||
import is from './is'; | ||
import empty from './empty'; | ||
import flattenKeys from './flattenKeys'; | ||
|
||
/** | ||
* Get an array of values from the specified object | ||
* | ||
* @param {object} object The object to get the values from | ||
* @param {boolean} [follow=false] Whether to follow objects | ||
* @returns {Array} | ||
*/ | ||
function values(object, follow) { | ||
// check if object is an object | ||
if (is(object) && !empty(object)) { | ||
// create an empty array for the result | ||
const result = []; | ||
|
||
// if follow is true flatten the object keys so | ||
// its easy to get the path to get the value | ||
// if follow is false it will just be the base | ||
// object therefore it will only need to base keys | ||
const keysObject = follow | ||
? flattenKeys(object) | ||
: object; | ||
|
||
// loop over the keys of the object | ||
Object.keys(keysObject).forEach((key) => { | ||
// get the current key value | ||
const value = keysObject[key]; | ||
|
||
// add it to the result array | ||
result.push(value); | ||
}); | ||
|
||
// return the result | ||
return result; | ||
} | ||
|
||
// if the object isn't an object or is empty return | ||
// an empty array because it won't contain any values | ||
return []; | ||
} | ||
|
||
export default values; |