@chupacabral/textscanner v0.0.8
TextScanner
NOTE: NOT READY FOR ACTUAL USE YET\ I mean, it works as described, but the current functionality is notably less than what I'm intending to have by the end.
Simple, convenient, but customizable lexical scanner. Inspired by, but distinct from, Ruby's StringScanner.
How To Install
First, make sure you have Node.js and NPM ready to go on your machine.
Then, in the folder with your code, enter on a command line:
npm install @chupacabral/textscanner
To install it for use everywhere on your computer:
npm install -g @chupacabral/textscanner
How To Use
Instantiate a TextScanner object like so:
var TextScanner = require('@chupacabral/textscanner')
let scanner = new TextScanner('Hello, World!')
For the following code examples, assume they are after the previous block of code.
Methods
check ( ...options )
Checks to see if, at the current point for the scanner, there is any text coming up in the string that would match any of the given option strings.
Does not move the scanner position for the string.
Returns the matched text, otherwise null
.
console.log(scanner.scan('Goodbye')) // Outputs null
let greeting = scanner.check('Hello', 'Greetings', 'Salutations')
console.log(greeting) // Outputs "Hello"
console.log(scanner.pos) // Outputs 0
scan ( ...options )
Like check()
, looks to see if any of the given option strings come up next
in the string being scanner.
If there is a match, this method will adjust the scanner position forward.
Returns the matched text, otherwise null
.
console.log(scanner.scan('Goodbye')) // Outputs null
let greeting = scanner.scan('Hello', 'Greetings', 'Salutations')
console.log(greeting) // Outputs "Hello"
console.log(scanner.pos) // Outputs 5
movePointer ( n )
Adjusts the scan pointer n
characters.
The value for n
can be negative, and negative values will move the pointer
backwards along the scanned string.
If the pointer position would end up going out of bounds of the string, it will set itself to the beginning/end of the string.
scanner.movePointer(6)
console.log(scanner.pos) // Outputs 6
scanner.movePointer(100)
console.log(scanner.pos) // Outputs 13
peek ( n = 1 )
Returns the next n
characters after the scan pointer in the string being
scanned.
Peeking past the remaining length of the string will just return whatever's left.
let firstFive = scanner.peek(5)
console.log(firstFive) // Outputs "Hello"
scannedText ( )
Returns all the text before the scan pointer.
scanner.scan('Hello')
console.log(scanner.scannedText()) // Outputs "Hello"
unscannedText ( )
Returns all the text after the scan pointer.
scanner.scan('Hello')
console.log(scanner.unscannedText()) // Outputs ", World!"