From 6b87ca1c4d87f5c3cb9915b87d2ef75b1aebaae0 Mon Sep 17 00:00:00 2001 From: Danish Arora <35004822+danisharora099@users.noreply.github.com> Date: Mon, 27 Feb 2023 01:01:55 +0530 Subject: [PATCH] chore: move `random_subset.ts` to `@waku/utils` (#1191) --- .../src/lib/predefined_bootstrap_nodes.ts | 2 +- packages/dns-discovery/src/index.spec.ts | 3 +- packages/dns-discovery/src/random_subset.ts | 30 ------------------- packages/utils/src/common/index.ts | 1 + .../lib => utils/src/common}/random_subset.ts | 0 packages/utils/src/index.ts | 1 + 6 files changed, 4 insertions(+), 33 deletions(-) delete mode 100644 packages/dns-discovery/src/random_subset.ts create mode 100644 packages/utils/src/common/index.ts rename packages/{core/src/lib => utils/src/common}/random_subset.ts (100%) diff --git a/packages/core/src/lib/predefined_bootstrap_nodes.ts b/packages/core/src/lib/predefined_bootstrap_nodes.ts index 69e7f8ad44..3606297f21 100644 --- a/packages/core/src/lib/predefined_bootstrap_nodes.ts +++ b/packages/core/src/lib/predefined_bootstrap_nodes.ts @@ -1,4 +1,4 @@ -import { getPseudoRandomSubset } from "./random_subset.js"; +import { getPseudoRandomSubset } from "@waku/utils"; export const DefaultWantedNumber = 1; diff --git a/packages/dns-discovery/src/index.spec.ts b/packages/dns-discovery/src/index.spec.ts index f237a3cbe8..92cf61b70e 100644 --- a/packages/dns-discovery/src/index.spec.ts +++ b/packages/dns-discovery/src/index.spec.ts @@ -1,7 +1,6 @@ +import { getPseudoRandomSubset } from "@waku/utils"; import { expect } from "chai"; -import { getPseudoRandomSubset } from "./random_subset.js"; - describe("Discovery", () => { it("returns all values when wanted number matches available values", function () { const values = ["a", "b", "c"]; diff --git a/packages/dns-discovery/src/random_subset.ts b/packages/dns-discovery/src/random_subset.ts deleted file mode 100644 index 20d0ca83ce..0000000000 --- a/packages/dns-discovery/src/random_subset.ts +++ /dev/null @@ -1,30 +0,0 @@ -/** - * Return pseudo random subset of the input. - */ -export function getPseudoRandomSubset( - values: T[], - wantedNumber: number -): T[] { - if (values.length <= wantedNumber || values.length <= 1) { - return values; - } - - return shuffle(values).slice(0, wantedNumber); -} - -function shuffle(arr: T[]): T[] { - if (arr.length <= 1) { - return arr; - } - const randInt = (): number => { - return Math.floor(Math.random() * Math.floor(arr.length)); - }; - - for (let i = 0; i < arr.length; i++) { - const j = randInt(); - const tmp = arr[i]; - arr[i] = arr[j]; - arr[j] = tmp; - } - return arr; -} diff --git a/packages/utils/src/common/index.ts b/packages/utils/src/common/index.ts new file mode 100644 index 0000000000..5f3d35f3c9 --- /dev/null +++ b/packages/utils/src/common/index.ts @@ -0,0 +1 @@ +export * from "./random_subset.js"; diff --git a/packages/core/src/lib/random_subset.ts b/packages/utils/src/common/random_subset.ts similarity index 100% rename from packages/core/src/lib/random_subset.ts rename to packages/utils/src/common/random_subset.ts diff --git a/packages/utils/src/index.ts b/packages/utils/src/index.ts index 4737706c59..33c0548d02 100644 --- a/packages/utils/src/index.ts +++ b/packages/utils/src/index.ts @@ -1,2 +1,3 @@ export * from "./bytes/index.js"; export * from "./libp2p/index.js"; +export * from "./common/index.js";