Skip to content

Commit

Permalink
Add ability to set custom answers
Browse files Browse the repository at this point in the history
  • Loading branch information
Alina-Lukianytsia committed Sep 19, 2021
1 parent 55b853b commit 2e4e50f
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 44 deletions.
26 changes: 16 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,31 @@ npm install jsmp-infra-alina_lukianytsia-magic_ball
#### ES6

```js
import { getAnswer } from 'jsmp-infra-alina_lukianytsia-magic_ball';

const answer = getAnswer(); // => answer = "It is decidedly so"
import { MagicBall } from 'jsmp-infra-alina_lukianytsia-magic_ball';

// with default values:
const magicBall = new MagicBall();
const answer = magicBall.getAnswer(); // => answer = "It is decidedly so"

// with set values:
const magicBall = new MagicBall(['yes', 'no']);
const answer = magicBall.getAnswer(); // => answer = "yes"
```

#### Node

```js
const { getAnswer } = require('jsmp-infra-alina_lukianytsia-magic_ball');
const { MagicBall } = require('jsmp-infra-alina_lukianytsia-magic_ball');

const answer = getAnswer(); // => answer = "It is decidedly so"
```
// with default values:
const magicBall = new MagicBall();
const answer = magicBall.getAnswer(); // => answer = "It is decidedly so"

#### Browser
// with set values:
const magicBall = new MagicBall(['yes', 'no']);
const answer = magicBall.getAnswer(); // => answer = "yes"

```html
<script>
jsmp-infra-alina_lukianytsia-magic_ball.getAnswer();
</script>
```

## Contributing
Expand Down
8 changes: 8 additions & 0 deletions __tests__/test-answers.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"TEST_ANSWERS": [
"Yes",
"No",
"I'm not ready to give an answer",
"Try again later"
]
}
29 changes: 22 additions & 7 deletions __tests__/test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
import { getAnswer } from '../dist';
import { MagicBall } from '../dist';
import { DEFAULT_ANSWERS } from '../src/answers.json';
import { TEST_ANSWERS } from './test-answers.json';

describe('getAnswer method:', () => {
test('type of received value', () => {
const answer = getAnswer();
describe('MagicBall instance with default values:', () => {
const magicBall = new MagicBall();

expect(typeof answer).toBe('string');
test('type of received value', () => {
expect(typeof magicBall.getAnswer()).toBe('string');
});

test('default answers contain received value', () => {
const answer = getAnswer();
expect(DEFAULT_ANSWERS.indexOf(magicBall.getAnswer())).toBeGreaterThanOrEqual(0);
});
});

describe('MagicBall instance with custom values:', () => {
const magicBall = new MagicBall(TEST_ANSWERS);

test('type of received value', () => {
expect(typeof magicBall.getAnswer()).toBe('string');
});

test('default answers does not contain received value', () => {
expect(DEFAULT_ANSWERS.indexOf(magicBall.getAnswer())).toBeLessThan(0);
});

expect(DEFAULT_ANSWERS.indexOf(answer)).toBeGreaterThanOrEqual(0);
test('custom answers contain received value', () => {
expect(TEST_ANSWERS.indexOf(magicBall.getAnswer())).toBeGreaterThanOrEqual(0);
});
});
6 changes: 3 additions & 3 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import getAnswer from './magic-ball';
export { getAnswer };
import MagicBall from './magic-ball';
export { MagicBall };
declare const _default: {
getAnswer: typeof getAnswer;
MagicBall: typeof MagicBall;
};
export default _default;
2 changes: 1 addition & 1 deletion dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 27 additions & 8 deletions dist/magic-ball.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,31 @@
/**
* @description
* Generates random answer.
* @classdesc
* Class creates random answers generator with custom or default answers
*
* @returns {string}
*
* @example
* const answer = getAnswer();
* // => answer='It is decidedly so'
**/
declare function getAnswer(): string;
export default getAnswer;
* - with default values:
* const magicBall = new MagicBall();
* const answer = magicBall.getAnswer(); // => answer='It is decidedly so'
*
* - with custom values:
* const magicBall = new MagicBall(['yes', 'no']);
* const answer = magicBall.getAnswer(); // => answer='yes'
*/
declare class MagicBall {
#private;
/**
* Create a MagicBall
* @param { string[] | undefined } answers - Sets the answers array, not mandatory.
* If is not passed - will be used DEFAULT_ANSWERS array.
*/
constructor(answers?: string[] | undefined);
/**
*@description
* Generates the random answer
*
* @return {string} Random answer.
*/
getAnswer: () => string;
}
export default MagicBall;
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jsmp-infra-alina_lukianytsia-magic_ball",
"version": "1.0.6",
"version": "2.0.0",
"description": "MagicBall - can give you an answer to any question except of alternative.",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import getAnswer from './magic-ball';
import MagicBall from './magic-ball';

export { getAnswer };
export { MagicBall };

export default { getAnswer };
export default { MagicBall };
40 changes: 30 additions & 10 deletions src/magic-ball.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,38 @@ import random from 'lodash.random/index.js';
import { DEFAULT_ANSWERS } from './answers.json';

/**
* @description
* Generates random answer.
* @classdesc
* Class creates random answers generator with custom or default answers
*
* @returns {string}
*
* @example
* const answer = getAnswer();
* // => answer='It is decidedly so'
**/
function getAnswer(): string {
return DEFAULT_ANSWERS[random(DEFAULT_ANSWERS.length - 1)];
}
* - with default values:
* const magicBall = new MagicBall();
* const answer = magicBall.getAnswer(); // => answer='It is decidedly so'
*
* - with custom values:
* const magicBall = new MagicBall(['yes', 'no']);
* const answer = magicBall.getAnswer(); // => answer='yes'
*/
class MagicBall {
#answers: string[];

export default getAnswer;
/**
* Create a MagicBall
* @param { string[] | undefined } answers - Sets the answers array, not mandatory.
* If is not passed - will be used DEFAULT_ANSWERS array.
*/
constructor(answers?: string[] | undefined) {
this.#answers = answers || DEFAULT_ANSWERS;
}

/**
*@description
* Generates the random answer
*
* @return {string} Random answer.
*/
getAnswer = ():string => this.#answers[random(this.#answers.length - 1)];
}

export default MagicBall;

0 comments on commit 2e4e50f

Please sign in to comment.