Skip to content

Instantly share code, notes, and snippets.

@Balastrong
Created August 17, 2022 08:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Balastrong/e0aef8b0889616c6bdcc3fe749e219ce to your computer and use it in GitHub Desktop.
Save Balastrong/e0aef8b0889616c6bdcc3fe749e219ce to your computer and use it in GitHub Desktop.
wrand - randomGenerator validate
private validate(items: WeightedItem<T>[]) {
if (items.length === 0) {
throw new Error("Items list is empty!");
}
const set = new Set();
for (const item of items) {
if (item.weight <= 0) {
throw new Error(
`All weights must be positive! ${item.original} has weight ${item.weight}`
);
}
if (set.has(item.original)) {
throw new Error(`Items must be unique! ${item.original} is duplicate!`);
}
set.add(item.original);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment