Skip to content

Commit

Permalink
feat: version 1.1.5
Browse files Browse the repository at this point in the history
  • Loading branch information
santi100a committed Jun 29, 2023
1 parent 1714034 commit f9e5dd1
Show file tree
Hide file tree
Showing 92 changed files with 2,111 additions and 753 deletions.
11 changes: 8 additions & 3 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@
"node": true,
"jest/globals": true
},
"extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended", "plugin:jest/recommended"],
"overrides": [],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:jest/recommended"
],
"overrides": [],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": "latest"
Expand All @@ -16,6 +20,7 @@
"rules": {
"indent": ["error", "tab"],
"quotes": ["error", "single"],
"semi": ["error", "always"]
"semi": ["error", "always"],
"@typescript-eslint/no-var-requires": "off"
}
}
4 changes: 4 additions & 0 deletions .github/workflows/test.yml → .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ jobs:
run: yarn
- name: Build code
run: yarn build
- name: Build UMD Version
run: yarn build:umd
- name: Set authentication token
run: |
npm set //registry.npmjs.org/:_authToken ${{ secrets.NPM_AUTH_TOKEN }}
Expand All @@ -88,6 +90,8 @@ jobs:
run: yarn
- name: Build code
run: yarn build
- name: Build UMD Version
run: yarn build:umd
- name: Set authentication token
run: |
npm set //npm.pkg.github.com/:_authToken ${{ secrets.GPR_AUTH_TOKEN }}
Expand Down
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
cjs/
coverage/
node_modules/
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,14 @@
- Added `randomLower`, `randomUpper`, `randomLetter`, `randomLowers`, `randomUppers` and `randomLetters`.

## Version 1.1.4

- Fixed a bug in `randomFromArray` which would let floats through as an `amount`.
- Added `randomDate` and `randomDates`.
- Exported many internal constants and types.
- Improved documentation.
- Improved documentation.

## Version 1.1.5

- Split the library into different files (under `cjs/`) to allow partial loading of required features.
- Added new randomizers: `randomBoolean`, `randomHexColor`, `randomPhrase` and `randomUUID`.
- Added a UMD build.
133 changes: 84 additions & 49 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Santi's Random Number Library

[![Build Status](https://github.com/santi100a/random-lib/actions/workflows/test.yml/badge.svg)](https://github.com/santi100a/random-lib/actions)
[![Build Status](https://github.com/santi100a/random-lib/actions/workflows/ci.yml/badge.svg)](https://github.com/santi100a/random-lib/actions)
[![npm homepage](https://img.shields.io/npm/v/@santi100/random-lib)](https://npmjs.org/package/@santi100/random-lib)
[![GitHub stars](https://img.shields.io/github/stars/santi100a/random-lib.svg)](https://github.com/santi100a/random-lib)
[![License](https://img.shields.io/github/license/santi100a/random-lib.svg)](https://github.com/santi100a/random-lib)
Expand All @@ -14,51 +14,49 @@ There's no warranty, and be aware there might be bugs in my code. Pull requests

- `function random(max: number, min: number = 0): number;`
Generate a random integer between `min` (0 by default) and `max`.
|Name | Type | Description | Optional? |
|Name | Type | Description | Optional? |
|------|----------|-----------------------------------------|-----------|
|`max` | `number` | The maximum value. | No |
|`min?`| `number` | The minimum value. Defaults to 0. | Yes |
|`max` | `number` | The maximum value. | No |
|`min?`| `number` | The minimum value. Defaults to 0. | Yes |

Returns a pseudo-random integer between `min` and `max`.

- `function randomFloat(max: number, min: number = 0): number;`
Generate a random floating-point number between `min` (0 by default) and `max`.
Generate a random integer between `min` (0 by default) and `max`.
|Name | Type | Description | Optional? | Default |
Generate a random integer between `min` (0 by default) and `max`.
|Name | Type | Description | Optional? | Default |
|------|----------|-----------------------------------------|-----------|---------|
|`max` | `number` | The maximum value. | No | *N/A* |
|`min?`| `number` | The minimum value. Defaults to 0. | Yes | 0 |
|`max` | `number` | The maximum value. | No | _N/A_ |
|`min?`| `number` | The minimum value. Defaults to 0. | Yes | 0 |

Returns a pseudo-random integer between `min` and `max`.

- `function randomFromArray<T = unknown>(array: T[]): T;`
Returns a random item of `array`.
| Name | Type | Description | Optional? | Default |
| Name | Type | Description | Optional? | Default |
|---------|--------------|-----------------------------------------|-----------|-----------|
| `array` | `number` | The maximum value. | No | *N/A* |
| `T` | *type param* | The minimum value. Defaults to 0. | Yes | `unknown` |
| `array` | `number` | The maximum value. | No | _N/A_ |
| `T` | _type param_ | The minimum value. Defaults to 0. | Yes | `unknown` |

- `function randomIntegers(amount?: number, opts?: RandomArraysOptions): number[];`
Returns an array with `amount` random integers.
| Name | Type | Description | Optional? | Default |
| Name | Type | Description | Optional? | Default |
|---------------|-----------------------|-----------------------------------------|-------------|-----------|
| `amount` | `number` | The amount of random integers to fill the array with. | Yes | 4 |
| `opts` | `RandomArraysOptions` |The shape of the options passed to `randomIntegers` and `randomFloats`. | Yes | `{}` |
| `opts.max?` | `number` | The maximum value. | Yes | 300 |
| `opts.min?` | `number` | The minimum value. | Yes | 0 |

| `amount` | `number` | The amount of random integers to fill the array with. | Yes | 4 |
| `opts` | `RandomArraysOptions` |The shape of the options passed to `randomIntegers` and `randomFloats`. | Yes | `{}` |
| `opts.max?` | `number` | The maximum value. | Yes | 300 |
| `opts.min?` | `number` | The minimum value. | Yes | 0 |

- `function randomFloats(amount?: number, opts?: RandomArraysOptions): number[];`
Returns an array with `amount` random floating-point numbers.
| Name | Type | Description | Optional? | Default |
| Name | Type | Description | Optional? | Default |
|---------------|-----------------------|-----------------------------------------|-------------|-----------|
| `amount` | `number` | The amount of random floating-point numbers to fill the array with. | Yes | 4 |
| `opts` | `RandomArraysOptions` |The shape of the options passed to `randomIntegers` and `randomFloats`. | Yes | `{}` |
| `opts.max?` | `number` | The maximum value. | Yes | 300 |
| `opts.min?` | `number` | The minimum value. | Yes | 0 |
| `amount` | `number` | The amount of random floating-point numbers to fill the array with. | Yes | 4 |
| `opts` | `RandomArraysOptions` |The shape of the options passed to `randomIntegers` and `randomFloats`. | Yes | `{}` |
| `opts.max?` | `number` | The maximum value. | Yes | 300 |
| `opts.min?` | `number` | The minimum value. | Yes | 0 |
- `function randomLower(): string;`
Returns a random lowercase letter.

- `function randomUpper(): string;`
Returns a random uppercase letter.

Expand All @@ -67,49 +65,86 @@ There's no warranty, and be aware there might be bugs in my code. Pull requests

- `function randomLowers(amount: number): string[];`
Returns `amount` random lowercase letters.
| Name | Type | Description | Optional? | Default |
|---------------|-----------------------|-----------------------------------------|-------------|-----------|
| `amount` | `number` | How many random lowercase letters to return. | No | *N/A* |

| Name | Type | Description | Optional? | Default |
| -------- | -------- | -------------------------------------------- | --------- | ------- |
| `amount` | `number` | How many random lowercase letters to return. | No | _N/A_ |

- `function randomUppers(amount: number): string[];`
Returns `amount` random uppercase letters.
| Name | Type | Description | Optional? | Default |
| Name | Type | Description | Optional? | Default |
|---------------|-----------------------|-----------------------------------------|-------------|-----------|
| `amount` | `number` | How many random uppercase letters to return. | No | *N/A* |
| `amount` | `number` | How many random uppercase letters to return. | No | _N/A_ |
- `function randomLetters(amount: number): string[];`
Returns `amount` random letters.
| Name | Type | Description | Optional? | Default |
| Name | Type | Description | Optional? | Default |
|---------------|-----------------------|-----------------------------------------|-------------|-----------|
| `amount` | `number` | How many random letters to return. | No | *N/A* |
| `amount` | `number` | How many random letters to return. | No | _N/A_ |
- `function randomDate(minDate: Date, maxDate: Date): Date;`
Generates a random date between `minDate` and `maxDate`.
| Name | Type | Description | Optional? | Default |
| Name | Type | Description | Optional? | Default |
|---------------|-----------------------|-----------------------------------------|-------------|-----------|
| `minDate` | `Date` | The minimum date to generate a random date from. | No | *N/A* |
| `maxDate` | `Date` | The maximum date to generate a random date from. | No | *N/A* |
| `minDate` | `Date` | The minimum date to generate a random date from. | No | _N/A_ |
| `maxDate` | `Date` | The maximum date to generate a random date from. | No | _N/A_ |
- `function randomDates(minDate: Date, maxDate: Date, amount: number): Date[];`

Generates `amount` random dates between `minDate` and `maxDate`.
| Name | Type | Description | Optional? | Default |
|---------------|-----------------------|-----------------------------------------|-------------|-----------|
| `minDate` | `Date` | The minimum date to generate a random date from. | No | *N/A* |
| `maxDate` | `Date` | The maximum date to generate a random date from. | No | *N/A* |
| `amount` | `number` | The amount of dates to generate. | No | *N/A* |

| Name | Type | Description | Optional? | Default |
| --------- | -------- | ------------------------------------------------ | --------- | ------- |
| `minDate` | `Date` | The minimum date to generate a random date from. | No | _N/A_ |
| `maxDate` | `Date` | The maximum date to generate a random date from. | No | _N/A_ |
| `amount` | `number` | The amount of dates to generate. | No | _N/A_ |

- `function randomBoolean(): boolean;` **NEW! (since 1.1.5)**

Generates a random boolean value.

- `function randomHexColor(): string;` **NEW! (since 1.1.5)**

Generates a random hex colorcode.

- `function randomHexColor(shorthand: boolean): string;` **NEW! (since 1.1.5)**

Generates a random hex colorcode.

| Name | Type | Description | Optional? | Default |
|------|------|-------------|-----------|---------|
|`shorthand` | `boolean` | Whether or not use a 3-character code instead of a 6-character one. | No | _N/A_ |

- `function randomPhrase(): string;` **NEW! (since 1.1.5)**
Generate a random 6-word phrase.

**Keep in mind these phrases have NO GRAMATICAL sense and are generated from a list of random words.**

- `function randomPhrase(wordCount: number): string;` **NEW! (since 1.1.5)**

Generate a random phrase with `wordCount` words.

**Keep in mind these phrases have NO GRAMATICAL sense and are generated from a list of random words.**

| Name | Type | Description | Optional? | Default |
|------|------|-------------|-----------|---------|
|`wordCount` | `number` | The amount of words to use in the phrase. | No | _N/A_ |

- `function randomUUID(): string;` **NEW! (since 1.1.5)**

Generates a pseudo-random UUID v4.

## Usage

```typescript
import {
random,
randomFromArray,
randomIntegers,
randomFloats,
random,
randomFromArray,
randomIntegers,
randomFloats,
} from '@santi100/random-lib'; // ESM
const {
random,
randomFromArray,
randomIntegers,
randomFloats,
random,
randomFromArray,
randomIntegers,
randomFloats,
} = require('@santi100/random-lib'); // CJS

// Generate a random integer between 1 and 10
Expand Down
32 changes: 32 additions & 0 deletions cjs/core.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Utility type that enforces there's at least one element in the array.
*/
export type AtLeastOneElement<T> = [T, ...T[]];
/**
* Default maximum value for `randomIntegers` and `randomFloats`.
*/
export declare const DEFAULT_RANDOM_NUMBERS_MAX = 300;
/**
* Wordlist for `randomPhrase`.
*/
export declare const WORDLIST: string[];
/**
* The shape of the options passed to `randomIntegers` and `randomFloats`.
* @since 1.1.1
*/
export interface RandomArraysOptions {
max?: number;
min?: number;
}
/**
* An array containing all 26 English lowercase letters.
*/
export declare const LOWERS: AtLeastOneElement<string>;
/**
* An array containing all 26 English uppercase letters.
*/
export declare const UPPERS: AtLeastOneElement<string>;
/**
* An array containing all 52 English letters (uppercase and lowercase).
*/
export declare const LETTERS: AtLeastOneElement<string>;
97 changes: 97 additions & 0 deletions cjs/core.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
"use strict";
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
exports.__esModule = true;
exports.LETTERS = exports.UPPERS = exports.LOWERS = exports.WORDLIST = exports.DEFAULT_RANDOM_NUMBERS_MAX = void 0;
/**
* Default maximum value for `randomIntegers` and `randomFloats`.
*/
exports.DEFAULT_RANDOM_NUMBERS_MAX = 300;
/**
* Wordlist for `randomPhrase`.
*/
exports.WORDLIST = [
'pizza',
'garbage',
'fog',
'merge',
'actor',
'replace',
'naive',
'quiz',
'huge',
'ivory',
'island',
'road',
'mercy',
'swallow',
'diary',
'puppy',
'burden',
'track',
'scissors',
'club',
'noodle',
'reason',
'garment',
'baby',
'wide',
'uncover',
'vital',
'advance',
'vacant',
'loud',
'cupboard',
'green',
'banner',
'skate',
'author',
'damage',
'man',
'ordinary',
'joke',
'close',
'person',
'love',
'artist',
'trap',
'shaft',
'crack',
'learn',
'delay',
'romance',
'december',
'share',
'reason',
'entry',
'emotion',
'ocean',
'layer',
'nature',
'draft',
'loop',
'zone',
'speak',
'desert',
'bubble',
'common',
];
/**
* An array containing all 26 English lowercase letters.
*/
exports.LOWERS = 'abcdefghijklmnopqrstuvwxyz'.split('');
/**
* An array containing all 26 English uppercase letters.
*/
exports.UPPERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('');
/**
* An array containing all 52 English letters (uppercase and lowercase).
*/
exports.LETTERS = __spreadArray(__spreadArray([], exports.LOWERS, true), exports.UPPERS, true);
Loading

0 comments on commit f9e5dd1

Please sign in to comment.