Skip to content

Commit

Permalink
feat!: Reader#sliceSerialize
Browse files Browse the repository at this point in the history
Signed-off-by: Lexus Drumgold <[email protected]>
  • Loading branch information
unicornware committed Jun 15, 2024
1 parent e740f3a commit f205a94
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 5 deletions.
31 changes: 26 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@
- [`Reader#previous`](#readerprevious)
- [`Reader#read([k])`](#readerreadk)
- [`Reader#reset()`](#readerreset)
- [`Reader#serialize(...values)`](#readerserializevalues)
- [`Reader#slice(range)`](#readerslicerange)
- [`Reader#sliceSerialize(range)`](#readersliceserializerange)
- [`Reader#start`](#readerstart)
- [`CharacterReader(file[, start])`](#characterreaderfile-start)
- [`CharacterReader#peekMatch(test)`](#characterreaderpeekmatchtest)
- [`CodeReader(file[, start])`](#codereaderfile-start)
- [`CodeReader.serialize(...codes)`](#codereaderserializecodes)
- [`CodeReader#serialize(...codes)`](#codereaderserializecodes-1)
- [`chars`](#chars)
- [`codes`](#codes)
- [`CharacterMatch`](#charactermatch)
Expand Down Expand Up @@ -267,6 +268,18 @@ Reset the position of the reader.
(`this`) The repositioned reader.
#### `Reader#serialize(...values)`
Convert the specified sequence of reader values to a string.
##### `Parameters`
- `...values` (`ReaderSlice<T> | T[]`) &mdash; reader value sequence
##### `Returns`
(`string`) String created from reader value sequence.
#### `Reader#slice(range)`
Get the values spanning `range` without changing the position of the reader.
Expand All @@ -279,6 +292,18 @@ Get the values spanning `range` without changing the position of the reader.
([`ReaderSlice<T>`](#readerslicet)) Reader value slice.
#### `Reader#sliceSerialize(range)`
Get the text spanning `range` without changing the position of the reader.
##### `Parameters`
- `range` ([`Range`](#range)) &mdash; slice position
##### `Returns`
(`string`) Serialized slice.
#### `Reader#start`
([`Point`][point]) Point before first reader value in file.
Expand Down Expand Up @@ -319,10 +344,6 @@ Convert the specified sequence of character codes to a string.
(`string`) String created from character code sequence.
#### `CodeReader#serialize(...codes)`
Instance method equivalent of [`CodeReader.serialize(...codes)`](#codereaderserializecodes).
### `chars`
Character dictionary.
Expand Down
39 changes: 39 additions & 0 deletions src/__tests__/abstract.reader.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
*/

import type { ReaderValue, ReaderValues } from '#src/types'
import type { MockInstance } from '#tests/interfaces'
import type { Offset } from '@flex-development/unist-util-types'
import type { Point } from '@flex-development/vfile-location'
import { read } from 'to-vfile'
import type { VFile, Value } from 'vfile'
Expand Down Expand Up @@ -42,6 +44,19 @@ describe('unit:Reader', () => {
super(file, start)
this.init([...this.source])
}

/**
* Convert the specified sequence of reader values to a string.
*
* @public
* @instance
*
* @param {ReaderValue[]} values - Reader value sequence
* @return {string} String created from reader value sequence
*/
public serialize(...values: ReaderValue[]): string {
return values.join('')
}
}

file = await read('__fixtures__/hrt.ts')
Expand Down Expand Up @@ -185,4 +200,28 @@ describe('unit:Reader', () => {
expect(subject.slice([start.offset, end.offset])).to.eql(slice)
})
})

describe('#sliceSerialize', () => {
let range: [Offset, Offset]
let serialize: MockInstance<TestSubject['serialize']>
let slice: MockInstance<TestSubject['slice']>

beforeEach(() => {
serialize = vi.spyOn(subject, 'serialize')
slice = vi.spyOn(subject, 'slice')

subject.sliceSerialize(range = [112, 114])
})

it('should call #serialize', () => {
expect(serialize).toHaveBeenCalledOnce()
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
expect(serialize).toHaveBeenCalledWith(...slice.mock.results[0]!.value)
})

it('should call #slice', () => {
expect(slice).toHaveBeenCalledOnce()
expect(slice).toHaveBeenCalledWith(range)
})
})
})
17 changes: 17 additions & 0 deletions src/__tests__/character.reader.functional.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,23 @@ describe('functional:CharacterReader', () => {
})
})

describe('#serialize', () => {
let spy: MockInstance<Character[]['join']>

beforeEach(() => {
spy = vi.spyOn(Array.prototype, 'join')
})

it('should call Array.prototype.join', () => {
// Act
subject.serialize()

// Expect
expect(spy).toHaveBeenCalledOnce()
expect(spy).toHaveBeenCalledWith('')
})
})

describe('#slice', () => {
let spy: MockInstance<Reader['slice']>

Expand Down
29 changes: 29 additions & 0 deletions src/abstract.reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,20 @@ abstract class Reader<
return this.#position = null, this
}

/**
* Convert the specified sequence of reader values to a string.
*
* @see {@linkcode ReaderSlice}
*
* @public
* @abstract
* @instance
*
* @param {ReaderSlice<T> | T[]} values - Reader value sequence
* @return {string} String created from reader value sequence
*/
public abstract serialize(...values: ReaderSlice<T> | T[]): string

/**
* Get the values spanning `range` without changing the position of the
* reader.
Expand All @@ -278,6 +292,21 @@ abstract class Reader<

return <never>this.values.slice(h, typeof x === 'number' ? x++ : undefined)
}

/**
* Get the text spanning `range` without changing the position of the reader.
*
* @see {@linkcode Range}
*
* @public
* @instance
*
* @param {Range} range - Slice position
* @return {string} Serialized slice
*/
public sliceSerialize(range: Range): string {
return this.serialize(...this.slice(range))
}
}

export default Reader
15 changes: 15 additions & 0 deletions src/character.reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,21 @@ class CharacterReader extends Reader<Character> {
return super.read(k)
}

/**
* Convert the specified sequence of characters to a string.
*
* @see {@linkcode Character}
*
* @public
* @instance
*
* @param {Character[]} chars - Character sequence
* @return {string} String created from character sequence
*/
public serialize(...chars: Character[]): string {
return chars.join('')
}

/**
* Get the characters spanning `range` without changing the position of the
* reader.
Expand Down

0 comments on commit f205a94

Please sign in to comment.