Created
September 17, 2019 18:37
-
-
Save NickStrupat/32b67fcdc46de043d879bf583f5e9fbd to your computer and use it in GitHub Desktop.
Get an object as iterable pairs of eval-able keys and values
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
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