Skip to content

Instantly share code, notes, and snippets.

@goldhand
Created May 24, 2016 18:51
Show Gist options
  • Save goldhand/a4999ff05619386db7df21d9c20bae05 to your computer and use it in GitHub Desktop.
Save goldhand/a4999ff05619386db7df21d9c20bae05 to your computer and use it in GitHub Desktop.
New javascript generator example
/**
* 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