Painless string scanning.
A JavaScript/TypeScript rewrite of typst/unscanny
.
Feel free to open an issue or create a pull request!
npm install @retypejs/jscanny
jscanny
is published as a CommonJS (CJS) module that you can require()
as well as an ECMAScript module (ESM) that you can import
.
If you happen to encounter a "Could not find a declaration file for module '@retypejs/jscanny'. '/path/to/node_modules/@retypejs/jscanny/dist/cjs/src/index.js' implicitly has an 'any' type." error, try adding "moduleResolution": "nodenext"
in the tsconfig.json
file of your TypeScript project like so:
{
"compilerOptions": {
"moduleResolution": "nodenext"
}
}
const { Scanner, Char } = require('@retypejs/jscanny');
const { None, Some, Option } = require('@retypejs/jscanny');
import { Scanner, Char } from '@retypejs/jscanny';
import { None, Some, Option } from '@retypejs/jscanny';
Recognizing and parsing a simple comma-separated list of floats.
const s = new Scanner(' +12 , -15.3, 14.3 ');
let nums = [];
while (!s.done()) {
s.eatWhitespace();
let start = s.cursor;
s.eatIf(['+', '-']);
s.eatWhile(Char.isAsciiDigit);
s.eatIf('.');
s.eatWhile(Char.isAsciiDigit);
nums.push(parseFloat(s.from(start)));
s.eatWhitespace();
s.eatIf(',');
}
assert.deepEqual(nums, [12, -15.3, 14.3]);
This JavaScript/TypeScript rewrite of unscanny
passes the exact same tests as unscanny
(compare jscanny/tests/scanner.test.ts
to the tests defined in unscanny/src/lib.rs
).
git clone https://github.com/retypejs/jscanny.git # Clone this repository
npm install # Install dependencies
npm run build # Compile TypeScript to JavaScript
npm run test # Run all tests