Created
July 28, 2015 13:34
-
-
Save dignifiedquire/f8ac257da72d58d56bac to your computer and use it in GitHub Desktop.
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
❯ babel-node parser.js | |
The CPU consists of integrated circuits in the microproces>sor of the computer. The CPU is responsible for processing information and controlling the computer. |
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
import tokenizer from 'string-tokenizer' | |
import chalk from 'chalk' | |
function tokenize (text) { | |
return tokenizer() | |
.input(text) | |
.token('blank', /<<<.+?(?=>>>)/, values => values[0].replace(/^<<</, '')) | |
.token('text', /.+?(?=<<<|$)/, values => values[0].replace(/^>>>/, '')) | |
.resolve() | |
} | |
function formatBlank (blank) { | |
return chalk.red.bold(blank) | |
} | |
function format ({text, blank}) { | |
let result = '' | |
const length = Math.max(text.length, blank.length) | |
for (let i = 0; i < length; i++) { | |
result += text[i] || '' | |
result += formatBlank(blank[i] || '') | |
} | |
return result | |
} | |
function run (text) { | |
const tokens = tokenize(text) | |
console.log(format(tokens)) | |
} | |
run('The CPU consists of integrated circuits in the <<<microproces>sor>>> of the computer. The CPU is responsible for <<<processing>>> information and controlling the computer.') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment