Skip to content

Commit

Permalink
fix: unicode support
Browse files Browse the repository at this point in the history
  • Loading branch information
unicornware committed Jun 8, 2024
1 parent 5d04761 commit 8968007
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,22 @@ 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 file: VFile = await read('regex.txt')
const reader: Reader = new Reader(file)

while (!reader.eof) console.log(reader.read())
while (!reader.eof) console.dir(reader.read())
```

...yields

```sh
'👍'
'🚀'
''
''
'/'
'\n'
null
```

## API
Expand Down
1 change: 1 addition & 0 deletions __fixtures__/regex.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/👍🚀❇️/
21 changes: 16 additions & 5 deletions src/reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ import type { Character, CharacterMatch } from './types'
* @extends {Location}
*/
class Reader extends Location {
/**
* Characters in file.
*
* @protected
* @readonly
* @instance
* @member {ReadonlyArray<string>} characters
*/
protected readonly characters!: readonly string[]

/**
* Current position in file.
*
Expand Down Expand Up @@ -50,11 +60,12 @@ class Reader extends Location {
* @param {(Point | null)?} [start] - Point before first character in `file`
*/
constructor(file: Value | VFile, start?: Point | null) {
super(file, start)
super(file = String(file), start)

Object.defineProperties(this, {
characters: { enumerable: false, value: [...file] },
position: { enumerable: false, value: 0 },
source: { enumerable: false, value: String(file) }
source: { enumerable: false, value: file }
})
}

Expand Down Expand Up @@ -82,7 +93,7 @@ class Reader extends Location {
* @return {boolean} `true` if at end of file, `false` otherwise
*/
public get eof(): boolean {
return this.index >= this.source.length
return this.index >= this.characters.length
}

/**
Expand Down Expand Up @@ -142,7 +153,7 @@ class Reader extends Location {
* @return {Character} Peeked character or `null`
*/
public peek(k: number = 1): Character {
return this.source[this.index + k] ?? null
return this.characters[this.index + k] ?? null
}

/**
Expand Down Expand Up @@ -178,7 +189,7 @@ class Reader extends Location {
* @return {Character} Next `k`-th character or `null`
*/
public read(k: number = 1): Character {
return this.source[this.position += k] ?? null
return this.characters[this.position += k] ?? null
}
}

Expand Down

0 comments on commit 8968007

Please sign in to comment.