Skip to content

Commit b8fe7e3

Browse files
committedSep 15, 2020
Add forceFloat option for integers
1 parent 2bbe3a9 commit b8fe7e3

File tree

4 files changed

+43
-2
lines changed

4 files changed

+43
-2
lines changed
 

‎README.md

+1
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ maxDepth | number | `100`
118118
initialBufferSize | number | `2048`
119119
sortKeys | boolean | false
120120
forceFloat32 | boolean | false
121+
forceFloat | boolean | false
121122
ignoreUndefined | boolean | false
122123
context | user-defined | -
123124

‎src/Encoder.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export class Encoder<ContextType> {
2020
private readonly sortKeys = false,
2121
private readonly forceFloat32 = false,
2222
private readonly ignoreUndefined = false,
23+
private readonly forceFloat = false,
2324
) {}
2425

2526
private getUint8Array(): Uint8Array {
@@ -85,7 +86,7 @@ export class Encoder<ContextType> {
8586
}
8687
}
8788
private encodeNumber(object: number) {
88-
if (Number.isSafeInteger(object)) {
89+
if (Number.isSafeInteger(object) && !this.forceFloat) {
8990
if (object >= 0) {
9091
if (object < 0x80) {
9192
// positive fixint

‎src/encode.ts

+9
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ export type EncodeOptions<ContextType = undefined> = Partial<
2323
* The default is `false`. Note that it needs more time to encode.
2424
*/
2525
ignoreUndefined: boolean;
26+
27+
/**
28+
* If `true`, integer numbers are encoded as floating point,
29+
* with the `forceFloat32` option taken into account.
30+
*
31+
* The default is `false`.
32+
*/
33+
forceFloat: boolean;
2634
}>
2735
> &
2836
ContextOf<ContextType>;
@@ -47,6 +55,7 @@ export function encode<ContextType>(
4755
options.sortKeys,
4856
options.forceFloat32,
4957
options.ignoreUndefined,
58+
options.forceFloat,
5059
);
5160
return encoder.encode(value);
5261
}

‎test/encode.test.ts

+31-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ describe("encode", () => {
1313
assert.deepStrictEqual(encode(3.14), Uint8Array.from([0xcb, 0x40, 0x9, 0x1e, 0xb8, 0x51, 0xeb, 0x85, 0x1f]));
1414
});
1515

16-
it("encodes numbers in float32 when forceFloate32=true", () => {
16+
it("encodes numbers in float32 when forceFloat32=true", () => {
1717
assert.deepStrictEqual(encode(3.14, { forceFloat32: true }), Uint8Array.from([0xca, 0x40, 0x48, 0xf5, 0xc3]));
1818
});
1919

@@ -25,6 +25,36 @@ describe("encode", () => {
2525
});
2626
});
2727

28+
context("forceFloat", () => {
29+
it("encodes integers as integers without forceFloat", () => {
30+
assert.deepStrictEqual(encode(3), Uint8Array.from([0x3]));
31+
});
32+
33+
it("encodes integers as floating point when forceFloat=true", () => {
34+
assert.deepStrictEqual(
35+
encode(3, { forceFloat: true }),
36+
Uint8Array.from([0xcb, 0x40, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]),
37+
);
38+
});
39+
40+
it("encodes integers as floating point when forceFloat=true", () => {
41+
assert.deepStrictEqual(
42+
encode(3, { forceFloat: true }),
43+
Uint8Array.from([0xcb, 0x40, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]),
44+
);
45+
});
46+
47+
it("encodes integers as float32 when forceFloat=true and forceFloat32=true", () => {
48+
assert.deepStrictEqual(
49+
encode(3, { forceFloat: true, forceFloat32: true }),
50+
Uint8Array.from([0xca, 0x40, 0x40, 0x00, 0x00]));
51+
});
52+
53+
it("encodes integers as integers when forceFloat=false", () => {
54+
assert.deepStrictEqual(encode(3, { forceFloat: false }), Uint8Array.from([0x3]));
55+
});
56+
});
57+
2858
context("ignoreUndefined", () => {
2959
it("encodes { foo: undefined } as is by default", () => {
3060
assert.deepStrictEqual(decode(encode({ foo: undefined, bar: 42 })), { foo: null, bar: 42 });

0 commit comments

Comments
 (0)
Failed to load comments.