Skip to content

Instantly share code, notes, and snippets.

@NickStrupat
Created September 17, 2019 18:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NickStrupat/32b67fcdc46de043d879bf583f5e9fbd to your computer and use it in GitHub Desktop.
Save NickStrupat/32b67fcdc46de043d879bf583f5e9fbd to your computer and use it in GitHub Desktop.
Get an object as iterable pairs of eval-able keys and values
function * getAllKeyValuePairs(o: any, key: string): IterableIterator<[string, string]> {
if (o instanceof Map) {
for (let element of o.entries())
yield * getAllKeyValuePairs(element[1], `${key}.get("${element[0]}")`);
}
else if (o instanceof Date) {
for (let languageCode of ['en', 'fr'])
yield * getAllKeyValuePairs(o.toLocaleString(languageCode), `${key}.toLocaleString("${languageCode}")`);
}
else if (o instanceof Array) {
for (let i in o)
yield * getAllKeyValuePairs(o[i], `${key}[${i}]`);
}
else if (typeof o === "object") {
for (let propertyName of getPropertyNames(o))
yield * getAllKeyValuePairs(o[propertyName], `${key}.${propertyName}`);
}
else if (typeof o === "string")
yield [key, o];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment