Skip to content

Commit

Permalink
Add random spinner (#47)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <[email protected]>
  • Loading branch information
sbalay and sindresorhus authored Jul 18, 2020
1 parent 1e7d53c commit 8e14994
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
5 changes: 5 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ console.log(cliSpinners.dots);
declare const cliSpinners: {
readonly [spinnerName in cliSpinners.SpinnerName]: cliSpinners.Spinner;
} & {
/**
Returns a random spinner each time it's called.
*/
readonly random: cliSpinners.Spinner;

// TODO: Remove this for the next major release
default: typeof cliSpinners;
};
Expand Down
10 changes: 10 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

const spinners = Object.assign({}, require('./spinners.json'));

const spinnersList = Object.keys(spinners);

Object.defineProperty(spinners, 'random', {
get() {
const randomIndex = Math.floor(Math.random() * spinnersList.length);
const spinnerName = spinnersList[randomIndex];
return spinners[spinnerName];
}
});

module.exports = spinners;
// TODO: Remove this for the next major release
module.exports.default = spinners;
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ Each spinner comes with a recommended `interval` and an array of `frames`.

[See the spinners.](spinners.json)

`cliSpinners.random` will return a random spinner each time it's called.

## Related

- [ora](https://github.com/sindresorhus/ora) - Elegant terminal spinner
Expand Down
33 changes: 33 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,41 @@
import test from 'ava';
import cliSpinners from '.';

function mockMathRandom(fixedResult) {
unMockMathRandom();
const originalImplementation = Math.random;
Math.random = () => fixedResult;
Math.random.originalImplementation = originalImplementation;
}

function unMockMathRandom() {
if (Math.random.originalImplementation) {
Math.random = Math.random.originalImplementation;
}
}

test('main', t => {
t.is(typeof cliSpinners, 'object');
t.is(cliSpinners.dots.interval, 80);
t.true(Array.isArray(cliSpinners.dots.frames));
});

test('random getter', t => {
const spinnersList = Object.keys(cliSpinners)
// TODO: Remove this filter when "module.exports.default = spinners" is removed from index.js.
.filter(key => key !== 'default')
.map(key => cliSpinners[key]);

// Should always return an item from the spinners list.
t.true(spinnersList.includes(cliSpinners.random));

// Should return the first spinner when `Math.random()` is the minimum value.
mockMathRandom(0);
t.is(cliSpinners.random, spinnersList[0]);

mockMathRandom(0.99);
// Should return the last spinner when `Math.random()` is the maximum value.
t.is(cliSpinners.random, spinnersList[spinnersList.length - 1]);

unMockMathRandom();
});

0 comments on commit 8e14994

Please sign in to comment.