Skip to content

Commit

Permalink
feat: [CodeReader] .serialize
Browse files Browse the repository at this point in the history
Signed-off-by: Lexus Drumgold <[email protected]>
  • Loading branch information
unicornware committed Jun 14, 2024
1 parent c949bdc commit 4567f01
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
- [`CharacterReader(file[, start])`](#characterreaderfile-start)
- [`CharacterReader#peekMatch(test)`](#characterreaderpeekmatchtest)
- [`CodeReader(file[, start])`](#codereaderfile-start)
- [`CodeReader#serialize(...codes)`](#codereaderserializecodes)
- [`CodeReader.serialize(...codes)`](#codereaderserializecodes)
- [`CodeReader#serialize(...codes)`](#codereaderserializecodes-1)
- [`chars`](#chars)
- [`codes`](#codes)
- [`CharacterMatch`](#charactermatch)
Expand Down Expand Up @@ -301,7 +302,7 @@ Get the next match from the file without changing the position of the reader, wi
Create a new character code reader.
#### `CodeReader#serialize(...codes)`
#### `CodeReader.serialize(...codes)`
Convert the specified sequence of character codes to a string.
Expand All @@ -313,6 +314,10 @@ 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
26 changes: 23 additions & 3 deletions src/__tests__/code.reader.functional.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,26 @@ describe('functional:CodeReader', () => {
subject = new TestSubject(file)
})

describe('.serialize', () => {
let spy: MockInstance<(typeof String)['fromCodePoint']>

beforeEach(() => {
spy = vi.spyOn(String, 'fromCodePoint')
})

it('should call String.fromCodePoint', () => {
// Arrange
const codes: Code[] = [10, 13]

// Act
TestSubject.serialize(...codes)

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

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

Expand Down Expand Up @@ -60,13 +80,13 @@ describe('functional:CodeReader', () => {
})

describe('#serialize', () => {
let spy: MockInstance<(typeof String)['fromCodePoint']>
let spy: MockInstance<TestSubject['serialize']>

beforeEach(() => {
spy = vi.spyOn(String, 'fromCodePoint')
spy = vi.spyOn(TestSubject, 'serialize')
})

it('should call String.fromCodePoint', () => {
it('should call CodeReader.serialize', () => {
// Arrange
const codes: Code[] = [subject.peek(0), subject.peek(1), subject.peek(13)]

Expand Down
18 changes: 17 additions & 1 deletion src/code.reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,22 @@ class CodeReader extends Reader<Code> {
})
}

/**
* Convert the specified sequence of character codes to a string.
*
* @see {@linkcode Code}
*
* @public
* @static
* @instance
*
* @param {Code[]} codes - Character code sequence
* @return {string} String created from character code sequence
*/
public static serialize(...codes: Code[]): string {
return String.fromCodePoint(...(<NonNullable<Code>[]>codes))
}

/**
* Get the current character code without changing the position of the reader,
* with `null` denoting end of file.
Expand Down Expand Up @@ -136,7 +152,7 @@ class CodeReader extends Reader<Code> {
* @return {string} String created from character code sequence
*/
public serialize(...codes: Code[]): string {
return String.fromCodePoint(...(<NonNullable<Code>[]>codes))
return CodeReader.serialize(...codes)
}

/**
Expand Down

0 comments on commit 4567f01

Please sign in to comment.