Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make random() iterable #24

Merged
merged 5 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ jobs:
matrix:
node-version:
- 14
- 12
Copy link
Contributor Author

@Richienb Richienb Feb 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updating tsd past the version that supports Node.js 12 fixes a problem, which for me, causes it to find and display type errors in @types/node

steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
Expand Down
9 changes: 9 additions & 0 deletions assert-in-range.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import chalk from 'chalk';
import inRange from 'in-range';

export default function assertInRange(t, value, {start = 0, end}) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

t.true(
inRange(value, {start, end}),
`${start} ${start <= value ? '≤' : chalk.red('≰')} ${chalk.yellow(value)} ${value <= end ? '≤' : chalk.red('≰')} ${end}`
);
}
20 changes: 18 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
Generate random numbers that are consecutively unique.

@returns A function, that when called, will return a random number that is never the same as the previous.
@returns Returns a function, that when called, will return a random number that is never the same as the previous. The returned function is also an iterable which consumes from the same source as the function.

@example
```
Expand All @@ -12,5 +12,21 @@ const random = uniqueRandom(1, 10);
console.log(random(), random(), random());
//=> 5 2 6
```

@example
```
import uniqueRandom from 'unique-random';

const random = uniqueRandom(1, 10);

for (const number of random) {
console.log(number);

// The unique numbers will be iterated over infinitely
if (stopCondition) {
break;
}
}
```
*/
export default function uniqueRandom(minimum: number, maximum: number): () => number;
export default function uniqueRandom(minimum: number, maximum: number): (() => number) & {[Symbol.iterator](): Iterator<number>};
10 changes: 9 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
export default function uniqueRandom(minimum, maximum) {
let previousValue;

return function random() {
function random() {
const number = Math.floor(
(Math.random() * (maximum - minimum + 1)) + minimum
);

previousValue = number === previousValue && minimum !== maximum ? random() : number;

return previousValue;
}

random[Symbol.iterator] = function * () {
while (true) {
yield random();
}
};

return random;
}
8 changes: 6 additions & 2 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import {expectType} from 'tsd';
import {expectAssignable, expectType} from 'tsd';
import uniqueRandom from './index.js';

const random = uniqueRandom(1, 10);

expectType<() => number>(random);
expectAssignable<() => number>(random);
expectType<number>(random());

for (const number of random) {
expectType<number>(number);
}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
],
"devDependencies": {
"ava": "^3.15.0",
"tsd": "^0.14.0",
"chalk": "^5.3.0",
"in-range": "^3.0.0",
"tsd": "^0.30.7",
"xo": "^0.38.2"
}
}
17 changes: 16 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,22 @@ console.log(random(), random(), random());

### uniqueRandom(minimum, maximum)

Returns a function, that when called, will return a random number that is never the same as the previous.
Returns a function, that when called, will return a random number that is never the same as the previous. The returned function is also an iterable which consumes from the same source as the function.

```js
import uniqueRandom from 'unique-random';

const random = uniqueRandom(1, 10);

for (const number of random) {
console.log(number);

// The unique numbers will be iterated over infinitely
if (stopCondition) {
break;
}
}
```

## Related

Expand Down
26 changes: 20 additions & 6 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import test from 'ava';
import assertInRange from './assert-in-range.js';
import uniqueRandom from './index.js';

test('main', t => {
Expand All @@ -10,16 +11,29 @@ test('main', t => {
while (--count > 0) {
currentValue = random();

if (
currentValue === previousValue ||
currentValue > 10 ||
currentValue < 1
) {
t.fail();
assertInRange(t, currentValue, {start: 1, end: 10});
if (previousValue !== undefined) {
t.not(currentValue, previousValue);
}

previousValue = currentValue;
}

t.pass();
});

test('iterator', t => {
t.plan(3); // In case the for-of loop doesn't run

const random = uniqueRandom(1, 10);

for (const number of random) { // eslint-disable-line no-unreachable-loop
assertInRange(t, number, {start: 1, end: 10});
break;
}

const {value, done} = random[Symbol.iterator]().next();

assertInRange(t, value, {start: 1, end: 10});
t.false(done);
});
Loading