From 023eba3c98f55130fa1ca3d0a072fa9bd973bb56 Mon Sep 17 00:00:00 2001 From: Haifeng Xue Date: Thu, 8 Aug 2024 16:40:12 +0200 Subject: [PATCH 1/3] Fixed, bit operation fails in case BigInt is not supported in decode Signed-off-by: Haifeng Xue --- javascript/index.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/javascript/index.js b/javascript/index.js index d0fcca9..eb0904e 100644 --- a/javascript/index.js +++ b/javascript/index.js @@ -77,11 +77,12 @@ function decodeUnsignedValues(encoded) { let result = Num(0); let shift = Num(0); const resList = []; + const mask = Num(0x20); encoded.split('').forEach((char) => { const value = Num(decodeChar(char)); - result |= (value & Num(0x1F)) << shift; - if ((value & Num(0x20)) === Num(0)) { + result += (value % Num(32)) * (Num(1) << shift); + if ((value % (mask * Num(2))) < mask) { resList.push(result); result = Num(0); shift = Num(0); @@ -111,11 +112,11 @@ function decodeHeader(version, encodedHeader) { function toSigned(val) { // Decode the sign from an unsigned value let res = val; - if (res & Num(1)) { - res = ~res; + if (res % Num(2)) { + res = (res + Num(1)) * Num(-1); } - res >>= Num(1); - return +res.toString(); + res = res / Num(2); + return Math.floor(+res.toString()); } function encode({ precision = DEFAULT_PRECISION, thirdDim = ABSENT, thirdDimPrecision = 0, polyline }) { From fc545374418c566c11d21a459a262b4009748ca3 Mon Sep 17 00:00:00 2001 From: Haifeng Xue Date: Mon, 12 Aug 2024 17:12:40 +0200 Subject: [PATCH 2/3] Updated, flexpolyline encode supports type Number by arithmetic operation Signed-off-by: Haifeng Xue --- javascript/index.js | 17 +++++++++++------ javascript/package.json | 2 +- javascript/test/test.js | 2 ++ 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/javascript/index.js b/javascript/index.js index eb0904e..63510e2 100644 --- a/javascript/index.js +++ b/javascript/index.js @@ -25,7 +25,7 @@ const ELEVATION = 3; const CUSTOM1 = 6; const CUSTOM2 = 7; -const Num = typeof BigInt !== "undefined" ? BigInt : Number; +const Num = (typeof BigInt === "undefined" || (typeof process === "object" ? process : {}).env?.FORCE_NUMBER_TYPE) ? Number : BigInt; function decode(encoded) { const decoder = decodeUnsignedValues(encoded); @@ -81,7 +81,7 @@ function decodeUnsignedValues(encoded) { encoded.split('').forEach((char) => { const value = Num(decodeChar(char)); - result += (value % Num(32)) * (Num(1) << shift); + result += (value % Num(32)) * (Num(2) ** shift); if ((value % (mask * Num(2))) < mask) { resList.push(result); result = Num(0); @@ -172,10 +172,15 @@ function encodeUnsignedNumber(val) { // Uses variable integer encoding to encode an unsigned integer. Returns the encoded string. let res = ''; let numVal = Num(val); + const bigIntSupported = typeof numVal === 'bigint'; while (numVal > 0x1F) { - const pos = (numVal & Num(0x1F)) | Num(0x20); + const pos = (numVal % Num(32)) + Num(32); res += ENCODING_TABLE[pos]; - numVal >>= Num(5); + if (bigIntSupported) { + numVal >>= Num(5); + } else { + numVal = parseInt(numVal / Num(32)); + } } return res + ENCODING_TABLE[numVal]; } @@ -185,9 +190,9 @@ function encodeScaledValue(value) { // `appender` is a callable where the produced chars will land to let numVal = Num(value); const negative = numVal < 0; - numVal <<= Num(1); + numVal = numVal * Num(2); if (negative) { - numVal = ~numVal; + numVal = numVal * Num(-1) - Num(1); } return encodeUnsignedNumber(numVal); diff --git a/javascript/package.json b/javascript/package.json index b4b366a..a18ce6e 100644 --- a/javascript/package.json +++ b/javascript/package.json @@ -6,7 +6,7 @@ "types": "index.d.ts", "scripts": { "prepack": "cp ../LICENSE .", - "test": "node test/test.js", + "test": "node test/test.js && FORCE_NUMBER_TYPE=true node test/test.js", "release": "./release.sh" }, "keywords": [ diff --git a/javascript/test/test.js b/javascript/test/test.js index 5441e28..1700e01 100644 --- a/javascript/test/test.js +++ b/javascript/test/test.js @@ -42,7 +42,9 @@ function runTests() { }); } +console.time('test'); runTests(); +console.timeEnd('test'); function parseLine(line) { // Strip off all spaces, curly braces, square brackets and trailing comma From 8eb1e37c7417ff0afddab5473618dda4f557f07f Mon Sep 17 00:00:00 2001 From: Haifeng Xue Date: Mon, 12 Aug 2024 17:16:09 +0200 Subject: [PATCH 3/3] small cleanup in test Signed-off-by: Haifeng Xue --- javascript/test/test.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/javascript/test/test.js b/javascript/test/test.js index 1700e01..5441e28 100644 --- a/javascript/test/test.js +++ b/javascript/test/test.js @@ -42,9 +42,7 @@ function runTests() { }); } -console.time('test'); runTests(); -console.timeEnd('test'); function parseLine(line) { // Strip off all spaces, curly braces, square brackets and trailing comma