Created
May 24, 2016 18:51
-
-
Save goldhand/a4999ff05619386db7df21d9c20bae05 to your computer and use it in GitHub Desktop.
New javascript generator example
This file contains hidden or 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
/** | |
* Helpers for breaking objects into arrays | |
*/ | |
// returns a generator of the objects keys | |
export function *generateKeys(obj) { | |
let prop; | |
for (prop of obj) { | |
yield prop; | |
} | |
} | |
// returns a generator of the objects values | |
export function *generateValues(obj) { | |
let prop; | |
for (prop in obj) { | |
yield obj[prop]; | |
} | |
} | |
// returns an array of the objects keys | |
export function objKeys(obj) { | |
return Array.from(generateKeys(obj)); | |
} | |
// returns an array of the objects values | |
export function objValues(obj) { | |
return Array.from(generateValues(obj)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment