string

previously, we made a string term. we left a lot of its methods as the default. it's nice to be able to do this. but it's better to set some of those methods yourself. especially for terms you plan on using a lot, like "string", which can be used to parse words like "pasta".

yes that's right. it's "pasta" time again.

TRANSLATED:



let's recap...

we're making a term for matching strings, like "pasta". so it accepts a string as an ARGUMENT.

class StringTerm extends Term {
	constructor(string) {
		super()
		this.string = string
	}
}

we give it a name.

class StringTerm extends Term {
	//...
	name() {
		return `"${this.string}"`
	}
}

as always, it's easy to check.

class StringTerm extends Term {
	//...
	check(text) {
		return text.startsWith(this.string)
	}
}

it's easy to bite.

class StringTerm extends Term {
	//...
	bite(text) {
		if (text.startsWith(this.string)) {
			return this.string
		}
		return null
	}
}

it's easy to tail.

class StringTerm extends Term {
	//...
	tail(text) {
		if (text.startsWith(this.string)) {
			return text.slice(this.string.length)
		}
		return null
	}
}

it's easy to eat.

class StringTerm extends Term {
	//...
	eat(text) {
		if (text.startsWith(this.string)) {
			return [this.string, text.slice(this.string.length)]
		}
		return [null, null]
	}
}

it's easy to travel. this method is IMPORTANT but i haven't told you why yet.

class StringTerm extends Term {
	//...
	travel(text) {
		let i = 0
		while (i < text.length) {
			if (text[i] !== this.string[i]) {
				break
			}
			i++
		}
		return text.slice(0, i)
	}
}

no need to override the error method. the default message works fine. but it's good to get an APPROPRIATE size preview for it.

class StringTerm extends Term {
	//...
	preview(text) {
		return text.slice(0, this.string.length)
	}
}

and that's it. now you can make pasta (again).

const pastaTerm = new StringTerm("pasta")

function translate(text) {
	return pastaTerm.translate(text)
}

back to the dream