Skip to content

Commit

Permalink
feat: (u)int64
Browse files Browse the repository at this point in the history
  • Loading branch information
cha0s committed Nov 27, 2024
1 parent c0c6ed2 commit 40ea2c5
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 21 deletions.
5 changes: 5 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
/** @type {import('eslint').Linter.Config} */
module.exports = {
globals: {
BigInt: false,
BigInt64Array: false,
BigUint64Array: false,
},
root: true,
parserOptions: {
ecmaVersion: 'latest',
Expand Down
41 changes: 20 additions & 21 deletions README.md

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions src/codecs/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export function typeToElementClass(type) {
case 'uint32': return Uint32Array;
case 'float32': return Float32Array;
case 'float64': return Float64Array;
case 'bigint64': return BigInt64Array;
case 'biguint64': return BigUint64Array;
}
return undefined;
}
Expand Down
24 changes: 24 additions & 0 deletions src/codecs/array.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import Uint32Codec from './uint32.js';
import Float32Codec from './float32.js';
import Float64Codec from './float64.js';
import StringCodec from './string.js';
import Int64Codec from './int64.js';
import Uint64Codec from './uint64.js';

Codecs.uint8 = Uint8Codec;
Codecs.int8 = Int8Codec;
Expand All @@ -22,6 +24,8 @@ Codecs.uint32 = Uint32Codec;
Codecs.float32 = Float32Codec;
Codecs.float64 = Float64Codec;
Codecs.string = StringCodec;
Codecs.int64 = Int64Codec;
Codecs.uint64 = Uint64Codec;

for (const numberType of [
'int8',
Expand All @@ -47,6 +51,26 @@ for (const numberType of [
});
}

test('int64 array', async () => {
const codec = new Codec({
element: {type: 'int64'},
});
const value = [1n, -2n, 3n, -4n];
const view = new DataView(new ArrayBuffer(codec.size(value)));
expect(codec.encode(value, view, 0)).to.equal(36);
expect(codec.decode(view, {byteOffset: 0})).to.deep.equal(value);
});

test('uint64 array', async () => {
const codec = new Codec({
element: {type: 'uint64'},
});
const value = [1n, 2n, 3n, 4n];
const view = new DataView(new ArrayBuffer(codec.size(value)));
expect(codec.encode(value, view, 0)).to.equal(36);
expect(codec.decode(view, {byteOffset: 0})).to.deep.equal(value);
});

test('string array', async () => {
const codec = new Codec({
element: {type: 'string'},
Expand Down
16 changes: 16 additions & 0 deletions src/codecs/int64.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Int64Codec {
decode(view, target) {
const value = view.getBigInt64(target.byteOffset);
target.byteOffset += 8;
return value;
}
encode(value, view, byteOffset) {
view.setBigInt64(byteOffset, value);
return 8;
}
size() {
return 8;
}
}

export default Int64Codec;
11 changes: 11 additions & 0 deletions src/codecs/int64.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {expect, test} from 'vitest';

import Codec from './int64.js';

test('int64', async () => {
const codec = new Codec();
const value = -32n;
const view = new DataView(new ArrayBuffer(codec.size(value)));
expect(codec.encode(value, view, 0)).to.equal(8);
expect(codec.decode(view, {byteOffset: 0})).to.deep.equal(value);
});
16 changes: 16 additions & 0 deletions src/codecs/uint64.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Uint64Codec {
decode(view, target) {
const value = view.getBigUint64(target.byteOffset);
target.byteOffset += 8;
return value;
}
encode(value, view, byteOffset) {
view.setBigUint64(byteOffset, value);
return 8;
}
size() {
return 8;
}
}

export default Uint64Codec;
11 changes: 11 additions & 0 deletions src/codecs/uint64.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {expect, test} from 'vitest';

import Codec from './uint64.js';

test('uint64', async () => {
const codec = new Codec();
const value = 32n;
const view = new DataView(new ArrayBuffer(codec.size(value)));
expect(codec.encode(value, view, 0)).to.equal(8);
expect(codec.decode(view, {byteOffset: 0})).to.deep.equal(value);
});

0 comments on commit 40ea2c5

Please sign in to comment.