it's time to rewrite EVERYTHING from scratch.
let's get SERIOUS this time. we'll put a jsconfig.json file in
the root of our project to turn on type checking.
{
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"strict": true,
"noImplicitAny": true,
"noImplicitOverride": true,
"noImplicitThis": true,
"noImplicitReturns": true,
"noUncheckedIndexedAccess": true
}
}
then we'll move our code to a script.js file so that we get
squiggly lines if we do something wrong in it.
<script type="module" src="script.js"></script>
this time, we'll create all of our terms as PROPERTIES of the
Term object. let's start by making that object.
const Term = {}
and let's define our Term type. we'll start with just the
bite method this time. and we're changing bite a
little bit! instead of spitting out a single bite, it now spits out an array
of bites.
/**
* @typedef {object} Term
* @property {(text: string) => string[]} bite
*/
let's make a term called "Pasta". it bites off "pasta". REMEMBER to write its type so that you get squiggly lines if you do something wrong.
/** @type {Term} */
Term.Pasta = {
bite(text) {
return text.startsWith(string) ? [string] : []
}
}
to use a term, assign it to Root.
Term.Root = Term.Pasta
then use it.
function translate(text) {
return Term.Root.bite(text)
}
back to the dream