From 37f75aa25aa2426cadf014334f97945ba59934cb Mon Sep 17 00:00:00 2001 From: Srikar Parsi Date: Wed, 17 Aug 2022 16:32:39 -0400 Subject: [PATCH 01/14] method to randomly generate 64 bit int as string --- src/CONST.js | 3 +++ src/libs/NumberUtils.js | 22 ++++++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 src/libs/NumberUtils.js diff --git a/src/CONST.js b/src/CONST.js index 0f90121754ea..a08dec10a02c 100755 --- a/src/CONST.js +++ b/src/CONST.js @@ -790,6 +790,9 @@ const CONST = { INVITE: 'invite', LEAVE_ROOM: 'leaveRoom', }, + + MAX_64BIT_TOP_HALF: 9223372036, + MAX_64BIT_BOTTOM_HALF: 854775807, }; export default CONST; diff --git a/src/libs/NumberUtils.js b/src/libs/NumberUtils.js new file mode 100644 index 000000000000..75d6d753c66f --- /dev/null +++ b/src/libs/NumberUtils.js @@ -0,0 +1,22 @@ +import CONST from "../CONST"; + +function rand64() { + // Max 64-bit signed: + // 9,223,372,036,854,775,807 + // The top part of the max 64-bit number *+1* because we're flooring it + let top = Math.floor(Math.random() * CONST.MAX_64BIT_TOP_HALF + 1); + + // The bottom part of the max 64-bit number *+1* for the same reason + let rest = Math.floor(Math.random() * CONST.MAX_64BIT_BOTTOM_HALF + 1); + + // If the top is any number but the highest one, we can actually have any value for the rest + if (top != CONST.​​MAX_64BIT_TOP_HALF) { + rest = Math.floor(Math.random() * 1000000000); + } + + // Pad the bottom with zeros + let restString = rest.toString().padStart(9, '0'); + + return top + restString; + } + \ No newline at end of file From 9cdc571ef65d69072043fd3533a48f3ef8130e70 Mon Sep 17 00:00:00 2001 From: Srikar Parsi Date: Thu, 18 Aug 2022 11:37:30 -0400 Subject: [PATCH 02/14] js doc --- src/libs/NumberUtils.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/libs/NumberUtils.js b/src/libs/NumberUtils.js index 75d6d753c66f..dca75bc41c60 100644 --- a/src/libs/NumberUtils.js +++ b/src/libs/NumberUtils.js @@ -1,5 +1,9 @@ import CONST from "../CONST"; +/** + * Generates a random positive 64 bit integer by randomly generating the left half and right half and concatenating them + * @returns {string} randomly generated 64 bit string + */ function rand64() { // Max 64-bit signed: // 9,223,372,036,854,775,807 From b8408f5b0e5db5aee4e4a4c8a28d542ab047c405 Mon Sep 17 00:00:00 2001 From: Srikar Parsi <48188732+srikarparsi@users.noreply.github.com> Date: Thu, 18 Aug 2022 13:30:10 -0400 Subject: [PATCH 03/14] Update java docs Co-authored-by: Carlos Martins --- src/libs/NumberUtils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/NumberUtils.js b/src/libs/NumberUtils.js index dca75bc41c60..bce68e1468dd 100644 --- a/src/libs/NumberUtils.js +++ b/src/libs/NumberUtils.js @@ -1,7 +1,7 @@ import CONST from "../CONST"; /** - * Generates a random positive 64 bit integer by randomly generating the left half and right half and concatenating them + * Generates a random positive 64 bit numeric string by randomly generating the left half and right half and concatenating them. Used to generate client-side ids. * @returns {string} randomly generated 64 bit string */ function rand64() { From 229b778acb7caa9ae247ff1d88c64944a32b06e1 Mon Sep 17 00:00:00 2001 From: Srikar Parsi Date: Thu, 18 Aug 2022 13:34:25 -0400 Subject: [PATCH 04/14] change from top and bottom to left and right --- src/CONST.js | 4 ++-- src/libs/NumberUtils.js | 14 +++++++------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/CONST.js b/src/CONST.js index a08dec10a02c..75d3a2205c20 100755 --- a/src/CONST.js +++ b/src/CONST.js @@ -791,8 +791,8 @@ const CONST = { LEAVE_ROOM: 'leaveRoom', }, - MAX_64BIT_TOP_HALF: 9223372036, - MAX_64BIT_BOTTOM_HALF: 854775807, + MAX_64BIT_LEFT_HALF: 9223372036, + MAX_64BIT_RIGHT_HALF: 854775807, }; export default CONST; diff --git a/src/libs/NumberUtils.js b/src/libs/NumberUtils.js index dca75bc41c60..897f4b272eb8 100644 --- a/src/libs/NumberUtils.js +++ b/src/libs/NumberUtils.js @@ -7,20 +7,20 @@ import CONST from "../CONST"; function rand64() { // Max 64-bit signed: // 9,223,372,036,854,775,807 - // The top part of the max 64-bit number *+1* because we're flooring it - let top = Math.floor(Math.random() * CONST.MAX_64BIT_TOP_HALF + 1); + // The left part of the max 64-bit number *+1* because we're flooring it + let left = Math.floor(Math.random() * CONST.MAX_64BIT_LEFT_HALF + 1); - // The bottom part of the max 64-bit number *+1* for the same reason - let rest = Math.floor(Math.random() * CONST.MAX_64BIT_BOTTOM_HALF + 1); + // The right part of the max 64-bit number *+1* for the same reason + let rest = Math.floor(Math.random() * CONST.MAX_64BIT_RIGHT_HALF + 1); // If the top is any number but the highest one, we can actually have any value for the rest - if (top != CONST.​​MAX_64BIT_TOP_HALF) { + if (left != CONST.MAX_64BIT_LEFT_HALF) { rest = Math.floor(Math.random() * 1000000000); } - // Pad the bottom with zeros + // Pad the right with zeros let restString = rest.toString().padStart(9, '0'); - return top + restString; + return left + restString; } \ No newline at end of file From 8a72dccd1ff830b4de49afbe3ceff859872438b0 Mon Sep 17 00:00:00 2001 From: Srikar Parsi Date: Thu, 18 Aug 2022 13:41:22 -0400 Subject: [PATCH 05/14] variable names --- src/libs/NumberUtils.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/libs/NumberUtils.js b/src/libs/NumberUtils.js index f2a98a7214d8..1b02f991096b 100644 --- a/src/libs/NumberUtils.js +++ b/src/libs/NumberUtils.js @@ -11,16 +11,18 @@ function rand64() { let left = Math.floor(Math.random() * CONST.MAX_64BIT_LEFT_HALF + 1); // The right part of the max 64-bit number *+1* for the same reason - let rest = Math.floor(Math.random() * CONST.MAX_64BIT_RIGHT_HALF + 1); + let right; // If the top is any number but the highest one, we can actually have any value for the rest if (left != CONST.MAX_64BIT_LEFT_HALF) { - rest = Math.floor(Math.random() * 1000000000); + right = Math.floor(Math.random() * 1000000000); + } else { + right = Math.floor(Math.random() * CONST.MAX_64BIT_RIGHT_HALF + 1); } // Pad the right with zeros - let restString = rest.toString().padStart(9, '0'); + let rightString = right.toString().padStart(9, '0'); - return left + restString; + return left + rightString; } \ No newline at end of file From 4f76cbddb369685eda86d06600391bd1bfb96176 Mon Sep 17 00:00:00 2001 From: Srikar Parsi Date: Thu, 18 Aug 2022 14:47:16 -0400 Subject: [PATCH 06/14] style fixes --- src/libs/NumberUtils.js | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/libs/NumberUtils.js b/src/libs/NumberUtils.js index 1b02f991096b..f91478163f69 100644 --- a/src/libs/NumberUtils.js +++ b/src/libs/NumberUtils.js @@ -1,4 +1,4 @@ -import CONST from "../CONST"; +import CONST from '../CONST'; /** * Generates a random positive 64 bit numeric string by randomly generating the left half and right half and concatenating them. Used to generate client-side ids. @@ -8,21 +8,20 @@ function rand64() { // Max 64-bit signed: // 9,223,372,036,854,775,807 // The left part of the max 64-bit number *+1* because we're flooring it - let left = Math.floor(Math.random() * CONST.MAX_64BIT_LEFT_HALF + 1); - + const left = Math.floor(Math.random() * CONST.MAX_64BIT_LEFT_HALF + 1); + // The right part of the max 64-bit number *+1* for the same reason let right; - + // If the top is any number but the highest one, we can actually have any value for the rest - if (left != CONST.MAX_64BIT_LEFT_HALF) { + if (left !== CONST.MAX_64BIT_LEFT_HALF) { right = Math.floor(Math.random() * 1000000000); } else { right = Math.floor(Math.random() * CONST.MAX_64BIT_RIGHT_HALF + 1); } - + // Pad the right with zeros - let rightString = right.toString().padStart(9, '0'); + const rightString = right.toString().padStart(9, '0'); return left + rightString; - } - \ No newline at end of file +} From 6b2e5bb5af8efb350b31a664d7856b5734550ba7 Mon Sep 17 00:00:00 2001 From: Srikar Parsi Date: Thu, 18 Aug 2022 14:59:04 -0400 Subject: [PATCH 07/14] trailing space, local lint fixers isn't working --- src/libs/NumberUtils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/NumberUtils.js b/src/libs/NumberUtils.js index f91478163f69..3e487749be19 100644 --- a/src/libs/NumberUtils.js +++ b/src/libs/NumberUtils.js @@ -22,6 +22,6 @@ function rand64() { // Pad the right with zeros const rightString = right.toString().padStart(9, '0'); - + return left + rightString; } From f4d25b974d30bfc637b7aaf0f76a2fe4bcd9220f Mon Sep 17 00:00:00 2001 From: Srikar Parsi Date: Tue, 23 Aug 2022 02:28:54 -0400 Subject: [PATCH 08/14] bounds and unused var --- src/libs/NumberUtils.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/libs/NumberUtils.js b/src/libs/NumberUtils.js index 3e487749be19..722b744bccb4 100644 --- a/src/libs/NumberUtils.js +++ b/src/libs/NumberUtils.js @@ -4,11 +4,12 @@ import CONST from '../CONST'; * Generates a random positive 64 bit numeric string by randomly generating the left half and right half and concatenating them. Used to generate client-side ids. * @returns {string} randomly generated 64 bit string */ +/* eslint-disable no-unused-vars */ function rand64() { // Max 64-bit signed: // 9,223,372,036,854,775,807 // The left part of the max 64-bit number *+1* because we're flooring it - const left = Math.floor(Math.random() * CONST.MAX_64BIT_LEFT_HALF + 1); + const left = Math.floor(Math.random() * (CONST.MAX_64BIT_LEFT_HALF + 1)); // The right part of the max 64-bit number *+1* for the same reason let right; @@ -17,7 +18,7 @@ function rand64() { if (left !== CONST.MAX_64BIT_LEFT_HALF) { right = Math.floor(Math.random() * 1000000000); } else { - right = Math.floor(Math.random() * CONST.MAX_64BIT_RIGHT_HALF + 1); + right = Math.floor(Math.random() * (CONST.MAX_64BIT_RIGHT_HALF + 1)); } // Pad the right with zeros From 291acf321a84b7b68952f39f7eb65e03950d1d1f Mon Sep 17 00:00:00 2001 From: Srikar Parsi Date: Tue, 23 Aug 2022 22:41:37 -0400 Subject: [PATCH 09/14] added tests --- tests/unit/NumberUtilsTest.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 tests/unit/NumberUtilsTest.js diff --git a/tests/unit/NumberUtilsTest.js b/tests/unit/NumberUtilsTest.js new file mode 100644 index 000000000000..5325cfc00682 --- /dev/null +++ b/tests/unit/NumberUtilsTest.js @@ -0,0 +1,14 @@ +const NumberUtils = require('../../src/libs/NumberUtils'); + +const result = NumberUtils.rand64(); + +const max64 = 9223372036854775807; + +describe('libs/NumberUtils', () => { + it('should generate a random 64-bit numeric string', () => { + const id = NumberUtils.rand64(); + expect(isNaN(result)).toBe(false); + expect(result > max64).toBe(false); + }); +}); + From a070bd33460a3cc9a2f7f7d7893d65d6f3087d19 Mon Sep 17 00:00:00 2001 From: Brandon Stites Date: Wed, 24 Aug 2022 16:42:27 +0200 Subject: [PATCH 10/14] Update comment and make numbers more readable --- src/CONST.js | 4 ++-- src/libs/NumberUtils.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/CONST.js b/src/CONST.js index a6b7586c217b..80f20c5f4f53 100755 --- a/src/CONST.js +++ b/src/CONST.js @@ -790,8 +790,8 @@ const CONST = { INVITE: 'invite', LEAVE_ROOM: 'leaveRoom', }, - MAX_64BIT_LEFT_HALF: 9223372036, - MAX_64BIT_RIGHT_HALF: 854775807, + MAX_64BIT_LEFT_HALF: 9_223_372_036, + MAX_64BIT_RIGHT_HALF: 854_775_807, IOS_KEYBOARD_SPACE_OFFSET: -30, }; diff --git a/src/libs/NumberUtils.js b/src/libs/NumberUtils.js index 722b744bccb4..48c7bb6dd447 100644 --- a/src/libs/NumberUtils.js +++ b/src/libs/NumberUtils.js @@ -2,7 +2,7 @@ import CONST from '../CONST'; /** * Generates a random positive 64 bit numeric string by randomly generating the left half and right half and concatenating them. Used to generate client-side ids. - * @returns {string} randomly generated 64 bit string + * @returns {String} string representation of a randomly generated 64 bit signed integer */ /* eslint-disable no-unused-vars */ function rand64() { @@ -16,7 +16,7 @@ function rand64() { // If the top is any number but the highest one, we can actually have any value for the rest if (left !== CONST.MAX_64BIT_LEFT_HALF) { - right = Math.floor(Math.random() * 1000000000); + right = Math.floor(Math.random() * 1_000_000_000); } else { right = Math.floor(Math.random() * (CONST.MAX_64BIT_RIGHT_HALF + 1)); } From c4ba24edc82532e8450f3131c664d4bd4f793554 Mon Sep 17 00:00:00 2001 From: Brandon Stites Date: Wed, 24 Aug 2022 16:51:48 +0200 Subject: [PATCH 11/14] Update test to use bigint and check for greater than 0 --- tests/unit/NumberUtilsTest.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/tests/unit/NumberUtilsTest.js b/tests/unit/NumberUtilsTest.js index 5325cfc00682..7b925bf76f9a 100644 --- a/tests/unit/NumberUtilsTest.js +++ b/tests/unit/NumberUtilsTest.js @@ -1,14 +1,13 @@ const NumberUtils = require('../../src/libs/NumberUtils'); -const result = NumberUtils.rand64(); - -const max64 = 9223372036854775807; - describe('libs/NumberUtils', () => { it('should generate a random 64-bit numeric string', () => { const id = NumberUtils.rand64(); - expect(isNaN(result)).toBe(false); - expect(result > max64).toBe(false); + expect(Number.isNaN(id)); + // eslint-disable-next-line no-undef + expect(BigInt(id)).toBeLessThan(BigInt(9223372036854775807)); + // eslint-disable-next-line no-undef + expect(BigInt(id)).toBeGreaterThan(0); }); }); From 2cdbd9631e5cbcf0549d84b911b89456f4d77ee4 Mon Sep 17 00:00:00 2001 From: Brandon Stites Date: Wed, 24 Aug 2022 16:56:57 +0200 Subject: [PATCH 12/14] Check for the type instead of whether it is a number or not --- tests/unit/NumberUtilsTest.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/NumberUtilsTest.js b/tests/unit/NumberUtilsTest.js index 7b925bf76f9a..0200d607374f 100644 --- a/tests/unit/NumberUtilsTest.js +++ b/tests/unit/NumberUtilsTest.js @@ -3,7 +3,7 @@ const NumberUtils = require('../../src/libs/NumberUtils'); describe('libs/NumberUtils', () => { it('should generate a random 64-bit numeric string', () => { const id = NumberUtils.rand64(); - expect(Number.isNaN(id)); + expect(typeof id).toBe('string'); // eslint-disable-next-line no-undef expect(BigInt(id)).toBeLessThan(BigInt(9223372036854775807)); // eslint-disable-next-line no-undef From 177a0f74ea8f3b011bdfa7979c7cf9d9ee4c846a Mon Sep 17 00:00:00 2001 From: Brandon Stites Date: Wed, 24 Aug 2022 17:00:02 +0200 Subject: [PATCH 13/14] Add or equal --- tests/unit/NumberUtilsTest.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/unit/NumberUtilsTest.js b/tests/unit/NumberUtilsTest.js index 0200d607374f..76cb991f3756 100644 --- a/tests/unit/NumberUtilsTest.js +++ b/tests/unit/NumberUtilsTest.js @@ -5,9 +5,9 @@ describe('libs/NumberUtils', () => { const id = NumberUtils.rand64(); expect(typeof id).toBe('string'); // eslint-disable-next-line no-undef - expect(BigInt(id)).toBeLessThan(BigInt(9223372036854775807)); + expect(BigInt(id)).toBeLessThanOrEqual(BigInt(9223372036854775807)); // eslint-disable-next-line no-undef - expect(BigInt(id)).toBeGreaterThan(0); + expect(BigInt(id)).toBeGreaterThanOrEqual(0); }); }); From 3992780c4715d9b291af5743bb06d7afcd921f4b Mon Sep 17 00:00:00 2001 From: Brandon Stites Date: Wed, 24 Aug 2022 17:02:57 +0200 Subject: [PATCH 14/14] Export rand64 --- src/libs/NumberUtils.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/libs/NumberUtils.js b/src/libs/NumberUtils.js index 48c7bb6dd447..d9c980b11cc0 100644 --- a/src/libs/NumberUtils.js +++ b/src/libs/NumberUtils.js @@ -26,3 +26,7 @@ function rand64() { return left + rightString; } + +module.exports = { + rand64, +};