-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
101 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import Serialiser from './serialiser.js'; | ||
|
||
const serialisePreps = (preps = [], characters = {}) => { | ||
const serialiser = new Serialiser(characters); | ||
|
||
for (let i = 0; i < preps.length; i++) { | ||
serialiser.setString(preps[i]); | ||
serialiser.setUint8(); // 0x00 is the default value. | ||
} | ||
|
||
return serialiser.buffer; | ||
}; | ||
|
||
export default serialisePreps; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
const maxByteLength = 16384; // The largest resource is sprdata. | ||
|
||
class Serialiser { | ||
#view; | ||
#ptr = 0; | ||
#characters = {}; | ||
|
||
constructor(characters = {}) { | ||
const buffer = new ArrayBuffer(0, { maxByteLength }); | ||
this.#view = new DataView(buffer); | ||
// Swap keys for values. | ||
this.#characters = Object.fromEntries( | ||
Object.entries(characters).map(([key, value]) => [value, key]), | ||
); | ||
} | ||
|
||
setUint8(value = 0x00) { | ||
this.#view.buffer.resize(this.#ptr + 1); | ||
this.#view.setUint8(this.#ptr++, value); | ||
} | ||
|
||
setString(str) { | ||
// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/codePointAt#looping_with_codepointat | ||
for (let codePoint of str) { | ||
if (this.#characters[codePoint] !== undefined) { | ||
codePoint = this.#characters[codePoint]; | ||
} | ||
this.setUint8(codePoint.codePointAt(0)); | ||
} | ||
} | ||
|
||
get buffer() { | ||
return this.#view.buffer; | ||
} | ||
} | ||
|
||
export default Serialiser; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { describe, it } from 'node:test'; | ||
import assert from 'node:assert/strict'; | ||
import serialisePreps from '../src/lib/serialiser/serialisePreps.js'; | ||
|
||
describe('serialisePreps', () => { | ||
it('should return an instance of ArrayBuffer.', () => { | ||
const buffer = serialisePreps(); | ||
assert.ok(buffer instanceof ArrayBuffer); | ||
}); | ||
|
||
it('should serialise prepositions.', () => { | ||
const preps = ['a', 'b', 'c']; | ||
const buffer = serialisePreps(preps); | ||
const view = new DataView(buffer); | ||
|
||
assert.equal(view.getUint8(0), 0x61); | ||
assert.equal(view.getUint8(1), 0x00); | ||
assert.equal(view.getUint8(2), 0x62); | ||
assert.equal(view.getUint8(3), 0x00); | ||
assert.equal(view.getUint8(4), 0x63); | ||
assert.equal(view.getUint8(5), 0x00); | ||
}); | ||
|
||
it('should serialise characters according to the character mapping.', () => { | ||
const preps = ['0', '1', '2']; | ||
const characters = { a: '0', b: '1', c: '2' }; | ||
const buffer = serialisePreps(preps, characters); | ||
|
||
const view = new DataView(buffer); | ||
assert.equal(view.getUint8(0), 0x61); | ||
assert.equal(view.getUint8(1), 0x00); | ||
assert.equal(view.getUint8(2), 0x62); | ||
assert.equal(view.getUint8(3), 0x00); | ||
assert.equal(view.getUint8(4), 0x63); | ||
assert.equal(view.getUint8(5), 0x00); | ||
}); | ||
}); |