Skip to content

Commit

Permalink
feat(boolean): add randomBool (#55)
Browse files Browse the repository at this point in the history
* feat: implement randomBool()

* test: add tests for randomBool()

* chore: export boolean functions

* docs: add random-bool page

* docs: add boolean page directory

* fix: add file extension to import for ESM support

Co-authored-by: Alireza Safaierad <[email protected]>

* fix: add file extension to import for ESM support

Co-authored-by: Alireza Safaierad <[email protected]>

* fix wrong import path to randomInt

---------

Co-authored-by: Alireza Safaierad <[email protected]>
  • Loading branch information
AmirHosseinKarimi and ASafaeirad authored Oct 30, 2023
1 parent c98a343 commit 0c6a933
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 0 deletions.
6 changes: 6 additions & 0 deletions docs/pages/boolean.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Boolean

import meta from './boolean/_meta.json';
import { TOC } from '../components/TOC.tsx';

<TOC base="/boolean" meta={meta} />
3 changes: 3 additions & 0 deletions docs/pages/boolean/_meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"random-bool": "randomBool"
}
21 changes: 21 additions & 0 deletions docs/pages/boolean/random-bool.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# randomBool

Generate a random boolean. (true or false)

### Import

```typescript copy
import { randomBool } from '@fullstacksjs/toolbox';
```

### Signature

```typescript copy
function randomBool(): boolean {}
```

### Examples

```typescript copy
randomBool() // true | false
```
1 change: 1 addition & 0 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './src/array/index.ts';
export * from './src/boolean/index.ts';
export * from './src/error/index.ts';
export * from './src/function/index.ts';
export * from './src/node/index.ts';
Expand Down
1 change: 1 addition & 0 deletions src/boolean/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { randomBool } from './randomBool.ts';
29 changes: 29 additions & 0 deletions src/boolean/randomBool.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { randomBool } from './randomBool';

describe('randomBool', () => {
it('should return an boolean', () => {
for (let i = 0; i < 100; i++) {
const boolean = randomBool();

expect(boolean).toBeTypeOf('boolean');
}
});

it('should select the first item', () => {
let i = 0;

// eslint-disable-next-line jest/no-conditional-in-test
for (; i < 100; i++) if (randomBool()) break;

expect(i).toBeLessThan(100);
});

it('should select the last item', () => {
let i = 0;

// eslint-disable-next-line jest/no-conditional-in-test
for (; i < 100; i++) if (!randomBool()) break;

expect(i).toBeLessThan(100);
});
});
14 changes: 14 additions & 0 deletions src/boolean/randomBool.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { randomInt } from '../number/randomInt.ts';

/**
* Generate a random boolean.
*
* @returns {boolean} random boolean
*
* @example
*
* randomBool() // Returns true or false
*/
export function randomBool(): boolean {
return !!randomInt(0, 1);
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './array/index.ts';
export * from './boolean/index.ts';
export * from './error/index.ts';
export * from './function/index.ts';
export * from './guards/index.ts';
Expand Down

0 comments on commit 0c6a933

Please sign in to comment.