diff --git a/README.md b/README.md index 1d5f247..b603830 100644 --- a/README.md +++ b/README.md @@ -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 - ``` ## Contributing diff --git a/__tests__/test-answers.json b/__tests__/test-answers.json new file mode 100644 index 0000000..0b9fce7 --- /dev/null +++ b/__tests__/test-answers.json @@ -0,0 +1,8 @@ +{ + "TEST_ANSWERS": [ + "Yes", + "No", + "I'm not ready to give an answer", + "Try again later" + ] +} diff --git a/__tests__/test.ts b/__tests__/test.ts index f729fbc..40774e2 100644 --- a/__tests__/test.ts +++ b/__tests__/test.ts @@ -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); }); }); diff --git a/dist/index.d.ts b/dist/index.d.ts index 7b2e2e5..ece12b3 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -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; diff --git a/dist/index.js b/dist/index.js index 02badf7..4103694 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1 +1 @@ -!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define("jsmp-infra-alina_lukianytsia-magic_ball",[],e):"object"==typeof exports?exports["jsmp-infra-alina_lukianytsia-magic_ball"]=e():t["jsmp-infra-alina_lukianytsia-magic_ball"]=e()}("undefined"==typeof self?this:self,(function(){return t={820:function(t,e,n){"use strict";var o=this&&this.__importDefault||function(t){return t&&t.__esModule?t:{default:t}};Object.defineProperty(e,"__esModule",{value:!0}),e.getAnswer=void 0;const r=o(n(294));e.getAnswer=r.default,e.default={getAnswer:r.default}},294:function(t,e,n){"use strict";var o=this&&this.__importDefault||function(t){return t&&t.__esModule?t:{default:t}};Object.defineProperty(e,"__esModule",{value:!0});const r=o(n(611)),i=n(383);e.default=function(){return i.DEFAULT_ANSWERS[(0,r.default)(i.DEFAULT_ANSWERS.length-1)]}},611:t=>{var e=9007199254740991,n=/^\s+|\s+$/g,o=/^[-+]0x[0-9a-f]+$/i,r=/^0b[01]+$/i,i=/^0o[0-7]+$/i,u=/^(?:0|[1-9]\d*)$/,a=parseFloat,f=parseInt,l=Object.prototype.toString,s=Math.floor,c=Math.min,p=Math.random;function d(t){var e=typeof t;return!!t&&("object"==e||"function"==e)}function y(t){return t?Infinity===(t=function(t){if("number"==typeof t)return t;if(function(t){return"symbol"==typeof t||function(t){return!!t&&"object"==typeof t}(t)&&"[object Symbol]"==l.call(t)}(t))return NaN;if(d(t)){var e="function"==typeof t.valueOf?t.valueOf():t;t=d(e)?e+"":e}if("string"!=typeof t)return 0===t?t:+t;t=t.replace(n,"");var u=r.test(t);return u||i.test(t)?f(t.slice(2),u?2:8):o.test(t)?NaN:+t}(t))||t===-1/0?17976931348623157e292*(t<0?-1:1):t==t?t:0:0===t?t:0}t.exports=function(t,n,o){if(o&&"boolean"!=typeof o&&function(t,n,o){if(!d(o))return!1;var r=typeof n;return!!("number"==r?function(t){return null!=t&&function(t){return"number"==typeof t&&t>-1&&t%1==0&&t<=e}(t.length)&&!function(t){var e=d(t)?l.call(t):"";return"[object Function]"==e||"[object GeneratorFunction]"==e}(t)}(o)&&function(t,n){return!!(n=null==n?e:n)&&("number"==typeof t||u.test(t))&&t>-1&&t%1==0&&tn){var r=t;t=n,n=r}if(o||t%1||n%1){var i=p();return c(t+i*(n-t+a("1e-"+((i+"").length-1))),n)}return function(t,e){return t+s(p()*(e-t+1))}(t,n)}},383:t=>{"use strict";t.exports=JSON.parse('{"DEFAULT_ANSWERS":["As I see it, yes","It is certain","It is decidedly so","Most likely","Outlook good","Signs point to yes","Without a doubt","Yes","Yes - definitely","You may rely on it","Reply hazy, try again","Ask again later","Better not tell you now","Cannot predict now","Concentrate and ask again","Don`t count on it","My reply is no","My sources say no","Outlook not so good","Very doubtful"]}')}},e={},function n(o){var r=e[o];if(void 0!==r)return r.exports;var i=e[o]={exports:{}};return t[o].call(i.exports,i,i.exports,n),i.exports}(820).default;var t,e})); \ No newline at end of file +!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define("jsmp-infra-alina_lukianytsia-magic_ball",[],e):"object"==typeof exports?exports["jsmp-infra-alina_lukianytsia-magic_ball"]=e():t["jsmp-infra-alina_lukianytsia-magic_ball"]=e()}("undefined"==typeof self?this:self,(function(){return t={820:function(t,e,n){"use strict";var o=this&&this.__importDefault||function(t){return t&&t.__esModule?t:{default:t}};Object.defineProperty(e,"__esModule",{value:!0}),e.MagicBall=void 0;const r=o(n(294));e.MagicBall=r.default,e.default={MagicBall:r.default}},294:function(t,e,n){"use strict";var o,r=this&&this.__classPrivateFieldSet||function(t,e,n,o,r){if("m"===o)throw new TypeError("Private method is not writable");if("a"===o&&!r)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof e?t!==e||!r:!e.has(t))throw new TypeError("Cannot write private member to an object whose class did not declare it");return"a"===o?r.call(t,n):r?r.value=n:e.set(t,n),n},i=this&&this.__classPrivateFieldGet||function(t,e,n,o){if("a"===n&&!o)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof e?t!==e||!o:!e.has(t))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===n?o:"a"===n?o.call(t):o?o.value:e.get(t)},a=this&&this.__importDefault||function(t){return t&&t.__esModule?t:{default:t}};Object.defineProperty(e,"__esModule",{value:!0});const s=a(n(611)),u=n(383);o=new WeakMap,e.default=class{constructor(t){o.set(this,void 0),this.getAnswer=()=>i(this,o,"f")[(0,s.default)(i(this,o,"f").length-1)],r(this,o,t||u.DEFAULT_ANSWERS,"f")}}},611:t=>{var e=9007199254740991,n=/^\s+|\s+$/g,o=/^[-+]0x[0-9a-f]+$/i,r=/^0b[01]+$/i,i=/^0o[0-7]+$/i,a=/^(?:0|[1-9]\d*)$/,s=parseFloat,u=parseInt,f=Object.prototype.toString,l=Math.floor,c=Math.min,d=Math.random;function p(t){var e=typeof t;return!!t&&("object"==e||"function"==e)}function y(t){return t?Infinity===(t=function(t){if("number"==typeof t)return t;if(function(t){return"symbol"==typeof t||function(t){return!!t&&"object"==typeof t}(t)&&"[object Symbol]"==f.call(t)}(t))return NaN;if(p(t)){var e="function"==typeof t.valueOf?t.valueOf():t;t=p(e)?e+"":e}if("string"!=typeof t)return 0===t?t:+t;t=t.replace(n,"");var a=r.test(t);return a||i.test(t)?u(t.slice(2),a?2:8):o.test(t)?NaN:+t}(t))||t===-1/0?17976931348623157e292*(t<0?-1:1):t==t?t:0:0===t?t:0}t.exports=function(t,n,o){if(o&&"boolean"!=typeof o&&function(t,n,o){if(!p(o))return!1;var r=typeof n;return!!("number"==r?function(t){return null!=t&&function(t){return"number"==typeof t&&t>-1&&t%1==0&&t<=e}(t.length)&&!function(t){var e=p(t)?f.call(t):"";return"[object Function]"==e||"[object GeneratorFunction]"==e}(t)}(o)&&function(t,n){return!!(n=null==n?e:n)&&("number"==typeof t||a.test(t))&&t>-1&&t%1==0&&tn){var r=t;t=n,n=r}if(o||t%1||n%1){var i=d();return c(t+i*(n-t+s("1e-"+((i+"").length-1))),n)}return function(t,e){return t+l(d()*(e-t+1))}(t,n)}},383:t=>{"use strict";t.exports=JSON.parse('{"DEFAULT_ANSWERS":["As I see it, yes","It is certain","It is decidedly so","Most likely","Outlook good","Signs point to yes","Without a doubt","Yes","Yes - definitely","You may rely on it","Reply hazy, try again","Ask again later","Better not tell you now","Cannot predict now","Concentrate and ask again","Don`t count on it","My reply is no","My sources say no","Outlook not so good","Very doubtful"]}')}},e={},function n(o){var r=e[o];if(void 0!==r)return r.exports;var i=e[o]={exports:{}};return t[o].call(i.exports,i,i.exports,n),i.exports}(820).default;var t,e})); \ No newline at end of file diff --git a/dist/magic-ball.d.ts b/dist/magic-ball.d.ts index 81d2cb3..af5f8ea 100644 --- a/dist/magic-ball.d.ts +++ b/dist/magic-ball.d.ts @@ -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; diff --git a/package-lock.json b/package-lock.json index 03ea275..10dd986 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "jsmp-infra-alina_lukianytsia-magic_ball", - "version": "1.0.0", + "version": "1.0.6", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index bda508a..1f62f96 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/index.ts b/src/index.ts index 6a8477c..67f10dd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,5 @@ -import getAnswer from './magic-ball'; +import MagicBall from './magic-ball'; -export { getAnswer }; +export { MagicBall }; -export default { getAnswer }; +export default { MagicBall }; diff --git a/src/magic-ball.ts b/src/magic-ball.ts index 17c00df..17ac98a 100644 --- a/src/magic-ball.ts +++ b/src/magic-ball.ts @@ -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;