Created
July 23, 2017 20:51
-
-
Save alextanhongpin/99b2e414cc271188bbf52610baa34070 to your computer and use it in GitHub Desktop.
Simple object to README.md
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
const data = { | |
hello: 'world', | |
someInteger: 1, | |
aFloat: 1.40, | |
isTrue: false, | |
items: [1, 2, 3] | |
} | |
function toReadme(data) { | |
const keys = Object.keys(data) | |
const values = Object.values(data) | |
const maxLength = Math.max(...values.map((it) => JSON.stringify(it).length), ...keys.map(it => it.length)) | |
const keysPadded = keys.map((key) => key.padEnd(maxLength)) | |
const valuesPadded = values.map((value) => JSON.stringify(value).padEnd(maxLength)) | |
let readme = `|${keysPadded.join("|")}|\n` | |
readme += `|${Array(keysPadded.length).fill(Array(maxLength).fill("-").join("")).join("|")}|\n` | |
readme += `|${valuesPadded.join("|")}|\n` | |
return readme | |
} | |
console.log(toReadme(data)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment