vfile-reader 1.0.1
Install from the command line:
Learn more about npm packages
$ npm install @flex-development/vfile-reader@1.0.1
Install via package.json:
"@flex-development/vfile-reader": "1.0.1"
About this version
vfile utility to read characters from a file
This package implements an input reader that can be used to read characters from a file.
This package is useful when characters need to be read individually or by regex match, such as when building a parser or tokenizer.
This package is ESM only.
In Node.js (version 18+) with yarn:
yarn add @flex-development/vfile-reader
See Git - Protocols | Yarn for details regarding installing from Git.
In Deno with esm.sh
:
import { Reader } from 'https://esm.sh/@flex-development/vfile-reader'
In browsers with esm.sh
:
<script type="module">
import { Reader } from 'https://esm.sh/@flex-development/vfile-reader'
</script>
import { Reader } from '@flex-development/vfile-reader'
import { read } from 'to-vfile'
import type { VFile } from 'vfile'
const file: VFile = await read('hrt.ts')
const reader: Reader = new Reader(file)
while (!reader.eof) console.log(reader.read())
This package exports the identifier Reader
. There is no default export.
Create a new character reader.
Pass a start
point to make reader locations relative to a specific place. Any point or offset accessed will be
relative to the given point.
-
file
(Value
|VFile
) — file to read -
start
(Point
|null
|undefined
) — point before first character infile
(Character
) Current character or null
, with null
denoting end of file. Equivalent to
reader.peek(0)
.
(boolean
) Boolean indicating if reader has reached the end of file, with true
denoting end of file.
(Offset
) Index of current character.
Get the current point in the file.
(Point
) Current point in file, relative to reader#start
.
Get the next k
-th character from the file without changing the position of the reader, with null
denoting end of
file.
-
k
(number | undefined
) — difference between index of nextk
-th character and index of current character-
default:
1
-
default:
(Character
) Peeked character or null
.
Get the next match from the file without changing the position of the reader, with null
denoting no match.
-
test
(RegExp
) — character test
(CharacterMatch
) Peeked character match or null
.
(Character
) Previous character or null
, with null
denoting beginning or end of file. Equivalent to
reader.peek(-1)
.
Get the next k
-th character from the file, with null
denoting end of file.
Unlike peek
, this method changes the position of the reader.
-
k
(number | undefined
) — difference between index of nextk
-th character and index of current character-
default:
1
-
default:
(Character
) Next k
-th character or null
.
(Point
) Point before first character in file.
Match in a source file, with null
denoting no match (TypeScript type).
type CharacterMatch = RegExpExecArray | null
Character in a source file, with null
denoting the end of file (TypeScript type).
type Character = string | null
This package is fully typed with TypeScript.
See CONTRIBUTING.md
.
This project has a code of conduct. By interacting with this repository, organization, or community you agree to abide by its terms.