Skip to content

Commit

Permalink
Updated docs and added test with objects
Browse files Browse the repository at this point in the history
  • Loading branch information
Balastrong committed Aug 10, 2022
1 parent f2a6d86 commit c6fba99
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 8 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ _Hint:_ The [test.ts](./src/__tests__/test.ts) file contains all possible exampl

### RandomPicker object

You can use the RandomPicker object to pick one or more random elements from a weighted array.
You can use the RandomPicker object to pick one or more random elements from a weighted array. The array can be any type, objects are fine.

The best usage here is if you need to pick many items at different moments and you don't want to recreate the object every time.

Expand Down Expand Up @@ -98,10 +98,12 @@ const result = pick(items);

## Roadmap

- [x] RandomPicker class
- RandomPicker class
- [ ] Multiple picks with option for duplicates
- [x] Standalone methods
- Standalone methods
- [x] pick
- [x] pickMany
- [x] flatten
- [ ] Allow for custom random function
- Extra features
- [ ] Allow for custom random function
- [ ] Add badges to the documentation
10 changes: 6 additions & 4 deletions deno_lib/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ _Hint:_ The [test.ts](./src/__tests__/test.ts) file contains all possible exampl

### RandomPicker object

You can use the RandomPicker object to pick one or more random elements from a weighted array.
You can use the RandomPicker object to pick one or more random elements from a weighted array. The array can be any type, objects are fine.

The best usage here is if you need to pick many items at different moments and you don't want to recreate the object every time.

Expand Down Expand Up @@ -98,10 +98,12 @@ const result = pick(items);

## Roadmap

- [x] RandomPicker class
- RandomPicker class
- [ ] Multiple picks with option for duplicates
- [x] Standalone methods
- Standalone methods
- [x] pick
- [x] pickMany
- [x] flatten
- [ ] Allow for custom random function
- Extra features
- [ ] Allow for custom random function
- [ ] Add badges to the documentation
15 changes: 15 additions & 0 deletions deno_lib/__tests__/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,21 @@ describe("RandomPicker", () => {
picked.every((p) => items.some((i) => i.original === p))
).toBeTruthy();
});

it("works with objects", () => {
const objectItems = [
{ original: { name: "Bronze" }, weight: 20 },
{ original: { name: "Silver" }, weight: 10 },
{ original: { name: "Gold" }, weight: 3 },
{ original: { name: "Platinum" }, weight: 1 },
];
const picker = new RandomPicker(objectItems);

const pickedItem = picker.pick();
expect(
objectItems.some((item) => item.original.name === pickedItem.name)
).toBeTruthy();
});
});

describe("pick", () => {
Expand Down
15 changes: 15 additions & 0 deletions src/__tests__/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,21 @@ describe("RandomPicker", () => {
picked.every((p) => items.some((i) => i.original === p))
).toBeTruthy();
});

it("works with objects", () => {
const objectItems = [
{ original: { name: "Bronze" }, weight: 20 },
{ original: { name: "Silver" }, weight: 10 },
{ original: { name: "Gold" }, weight: 3 },
{ original: { name: "Platinum" }, weight: 1 },
];
const picker = new RandomPicker(objectItems);

const pickedItem = picker.pick();
expect(
objectItems.some((item) => item.original.name === pickedItem.name)
).toBeTruthy();
});
});

describe("pick", () => {
Expand Down

0 comments on commit c6fba99

Please sign in to comment.