Skip to content

Painless string scanning (a JavaScript/TypeScript rewrite of @typst/unscanny).

License

Notifications You must be signed in to change notification settings

retypejs/jscanny

Repository files navigation

jscanny

latest release latest tag license

Painless string scanning.

A JavaScript/TypeScript rewrite of typst/unscanny.

Contributing

Feel free to open an issue or create a pull request!

Installation

npm install @retypejs/jscanny

Usage

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"
    }
}

CommonJS

const { Scanner, Char } = require('@retypejs/jscanny');
const { None, Some, Option } = require('@retypejs/jscanny');

ESM

import { Scanner, Char } from '@retypejs/jscanny';
import { None, Some, Option } from '@retypejs/jscanny';

Example

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]);

Testing

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

License

MIT © Bastien Voirin