Skip to content

vfile-reader 2.0.0

Install from the command line:
Learn more about npm packages
$ npm install @flex-development/vfile-reader@2.0.0
Install via package.json:
"@flex-development/vfile-reader": "2.0.0"

About this version

vfile-reader

github release npm codecov module type: esm license conventional commits typescript vitest yarn

vfile utility to read from a file

Contents

What is this?

This package implements an input reader that can be used to read characters and code points from a file.

When should I use this?

This package is useful when characters or code points need to be processed individually or as a group, such as when building a parser or tokenizer.

Install

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 { CharacterReader, CodeReader } from 'https://esm.sh/@flex-development/vfile-reader'

In browsers with esm.sh:

<script type="module">
  import { CharacterReader, CodeReader } from 'https://esm.sh/@flex-development/vfile-reader'
</script>

Use

import { CharacterReader, CodeReader } from '@flex-development/vfile-reader'
import { read } from 'to-vfile'
import type { VFile } from 'vfile'

const file: VFile = await read('__fixtures__/emojis.txt') // πŸ˜πŸ‘πŸš€β‡οΈ

const chars: CharacterReader = new CharacterReader(file)
const codes: CodeReader = new CodeReader(file)

// for (const char of chars) console.dir({ char, now: chars.now() })
// for (const code of codes) console.dir({ code, now: codes.now() })

while (!chars.eof) {
  console.dir({
    char: chars.read(),
    code: codes.read(),
    now: codes.now()
  })
}

...yields

{ char: '😍', code: 128525, now: { column: 1, line: 1, offset: 0 } }
{ char: 'πŸ‘', code: 128077, now: { column: 2, line: 1, offset: 1 } }
{ char: 'πŸš€', code: 128640, now: { column: 3, line: 1, offset: 2 } }
{ char: '❇', code: 10055, now: { column: 4, line: 1, offset: 3 } }
{ char: '️', code: 65039, now: { column: 5, line: 1, offset: 4 } }
{ char: '\n', code: 10, now: { column: 6, line: 1, offset: 5 } }

API

This package exports the following identifiers:

There is no default export.

Reader(file[, start])

extends: Location
implements: ReaderIterator<T>

Create a new input 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.

Note: This is an abstract class and must be extended.

  • file (Value | VFile) β€” file to read
  • start (Point | null | undefined) β€” point before first reader value

Type Parameters

Reader#eof

(boolean) Boolean indicating if reader has reached the end of file, with true denoting end of file.

Reader#includes(value)

Check if the file contains the given search value, relative to the current reader position.

Parameters
  • value (string) β€” value to search for in file
Returns

(boolean) true if file contains search value.

Reader#index

(Offset) Index of current reader value.

Reader#next()

Get the next reader result.

Unlike peek, this method changes the position of the reader.

Returns

(ReaderIteratorResult<T>) Next reader result.

Reader#now()

Get the current point in the file.

Returns

(Point) Current point in file, relative to reader#start.

Reader#offset([point])

See Location#offset([point]).

Reader#output

(T) Current reader value or null, with null denoting end of file. Equivalent to reader.peek(0).

Reader#peek([k])

Get the next k-th reader value from the file without changing the position of the reader, with null denoting end of file.

Parameters
  • k (number | undefined) β€” difference between index of next k-th reader value and index of current value
    • default: 1
Returns

(T) Peeked reader value or null.

Reader#point([offset])

See Location#point([offset]).

Reader#previous

(T) Previous reader value or null, with null denoting beginning or end of file. Equivalent to reader.peek(-1).

Reader#read([k])

Get the next k-th reader value from the file, with null denoting end of file.

Unlike peek, this method changes the position of the reader.

Parameters
  • k (number | undefined) β€” difference between index of next k-th reader value and index of current value
    • default: 1
Returns

(T) Next k-th reader value or null.

Reader#reset()

Reset the position of the reader.

Returns

(this) The repositioned reader.

Reader#slice(m)

Get a slice of the most recent reader values, with the last value being the current reader value, without changing the position of the reader.

Parameters
  • m (number) β€” maximum number of reader values to include in slice
Returns

(NonNullable<T>[]) Reader values slice.

Reader#start

(Point) Point before first reader value in file.

CharacterReader(file[, start])

extends: Reader<Character>

Create a new character reader.

CharacterReader#peekMatch(test)

Get the next match from the file without changing the position of the reader, with null denoting no match.

Parameters
  • test (RegExp) β€” character test
Returns

(CharacterMatch) Peeked character match or null.

CodeReader(file[, start])

extends: Reader<Code>

Create a new code point reader.

CodeReader#stringify(...codes)

Convert the specified sequence of code points to a string.

Parameters
  • ...codes (Code[]) β€” code points sequence
Returns

(string) String created from code point sequence.

CharacterMatch

Match in a source file, with null denoting no match (TypeScript type).

type CharacterMatch = RegExpExecArray | null

Character

Character in a source file, with null denoting end of file (TypeScript type).

type Character = string | null

Code

An integer between 0 and 0x10FFFF (inclusive) representing a Unicode code point in a source file, with null denoting end of file (TypeScript type).

type Code = number | null

ReaderIterator<T>

Input reader iterator API (TypeScript interface).

interface ReaderIterator<T extends ReaderValue = ReaderValue> {
  [Symbol.iterator](): ReaderIterator<T>
  next(): ReaderIteratorResult<T>
}

ReaderIteratorResult

Union of iterator results (TypeScript type).

type ReaderIteratorResult<
  T extends ReaderValue = ReaderValue
> = IteratorReturnResult<T> | IteratorYieldResult<T>

ReaderValue

Character or code point in a source file, with null denoting the end of file (TypeScript type).

type ReaderValue = Character | Code

Types

This package is fully typed with TypeScript.

Contribute

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.

Details


Assets

  • vfile-reader-2.0.0.tgz

Download activity

  • Total downloads 4
  • Last 30 days 0
  • Last week 0
  • Today 0