forked from nfriend/ts-key-enum
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
1,031 additions
and
2 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
const rp = require('request-promise'); | ||
const cheerio = require('cheerio'); | ||
const chalk = require('chalk'); | ||
const fs = require('fs'); | ||
const _ = require('lodash'); | ||
|
||
(async () => { | ||
const mdnUrl = `https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values#Speech_recognition_keys`; | ||
|
||
console.info(chalk.gray('Making GET request to MDN...')); | ||
const pageHtml = await rp(mdnUrl); | ||
console.info(chalk.gray('Successfully received response from MDN.')); | ||
|
||
console.info(chalk.gray('Scraping HTML for key names...')); | ||
const $ = cheerio.load(pageHtml); | ||
const keys = []; | ||
const keyValueRegex = /\"([a-z0-9_]+)\"/i; | ||
$('tr').each((index, element) => { | ||
const value = $(element) | ||
.find('td:nth-child(1) code') | ||
.text(); | ||
const description = $(element) | ||
.find('td:nth-child(2)') | ||
.text(); | ||
|
||
const matches = keyValueRegex.exec(value); | ||
const extractedValue = matches ? matches[1] : undefined; | ||
|
||
if (extractedValue && description) { | ||
keys.push({ value: extractedValue, description: description }); | ||
} | ||
}); | ||
|
||
// eliminate duplicate keys | ||
|
||
|
||
console.info( | ||
chalk.gray('Done scraping. Found the following keys:\n') + | ||
keys | ||
.map( | ||
k => | ||
` - ${chalk.blue(k.value)}: ${chalk.green( | ||
k.description, | ||
)}`, | ||
) | ||
.join('\n'), | ||
); | ||
|
||
let enumFile = `export enum Key {\n\n`; | ||
|
||
enumFile += keys.map(k => { | ||
return ` /** ${k.description} */\n ${k.value} = '${k.value}',\n\n`; | ||
}).join('') | ||
|
||
enumFile += '}'; | ||
|
||
fs.writeFileSync('./test.ts', enumFile); | ||
})(); |
Oops, something went wrong.