Created
August 14, 2018 01:19
-
-
Save Piterden/baa9ceaab30e295dabda9c7d6485a04b to your computer and use it in GitHub Desktop.
Tg API page passport part parser and generator tool
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
| let el = document.getElementById('telegram-passport').parentElement | |
| const code = [] | |
| let cls = '' | |
| let comment = '' | |
| while (el.nextElementSibling && el.nextElementSibling.tagName !== 'H3') { | |
| if (el.nextElementSibling.tagName === 'H4') { | |
| cls = `${cls}export type ${el.nextElementSibling.innerText}` | |
| } | |
| if (el.nextElementSibling.tagName === 'P') { | |
| comment = comment + `/**${el.nextElementSibling.innerText.replace(/(.{1,72}\s)\s*?/g, '\n * $1')}\n */\n` | |
| comment = comment.replace(/\n \*\/\n\/\*\*/g, '\n *') | |
| } | |
| if (el.nextElementSibling.tagName === 'TABLE') { | |
| const keys = Array.from(el.nextElementSibling.querySelectorAll('tr:first-child td')) | |
| .map((td) => td.innerText) | |
| const values = Array.from(el.nextElementSibling.querySelectorAll('tr:not(:first-child) td')) | |
| .map((td, idx) => [keys[idx % keys.length].toLowerCase(), td.innerText]) | |
| .reduce((acc, cur, idx) => { | |
| acc[parseInt(idx / keys.length)] = acc[parseInt(idx / keys.length)] || {} | |
| acc[parseInt(idx / keys.length)][cur[0]] = cur[1] | |
| return acc | |
| }, []) | |
| cls += '\n{\n\n' + values.map((prop) => ` /**${prop.description.replace(/“|”/g, '`').replace(/(.{1,72}\s)\s*?/g, '\n * $1')}\n */\n ${prop.parameters || prop.field}: ${prop.type.replace(/Array of (\w+)/, '$1[]')};\n`).join('\n') | |
| code.push(`${comment}${cls}\n}\n`) | |
| cls = '' | |
| comment = '' | |
| } | |
| el = el.nextElementSibling | |
| } | |
| console.log(code.join('\n')) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
tg_passport.mp4