Skip to content

Commit af97dcb

Browse files
committed
feat: Introduced options property for including wrong answers
1 parent e7381f6 commit af97dcb

File tree

14 files changed

+234
-89
lines changed

14 files changed

+234
-89
lines changed

.eslintignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
/dist/*
2-
/questions/*
3-
/test/*
2+
/node_modules/*
3+
/questions/*

.eslintrc.cjs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,14 @@ module.exports = {
1818
'linebreak-style': 'off',
1919
'no-unused-vars': 'off',
2020
'no-param-reassign': 'off',
21+
'no-underscore-dangle': 'off',
2122
},
23+
overrides: [
24+
{
25+
files: ['test/*'],
26+
rules: {
27+
'no-unused-expressions': 'off',
28+
},
29+
},
30+
],
2231
};

.github/workflows/testing.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
name: Testing
22

33
on:
4+
workflow_dispatch:
45
pull_request:
56
types: [opened, synchronize]
67

CHANGELOG.md

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,31 @@
1-
## [1.0.4](https://github.com/MilosPaunovic/questionnaire/compare/1.0.3...1.0.4)
1+
# Changelog
2+
3+
## [2.0.0](https://github.com/MilosPaunovic/questionnaire/compare/1.0.4...2.0.0) - 2022-09-30
4+
5+
### Breaking changes
6+
7+
This major release introduces a breaking change as the method `questions()` now expects an object parameter, instead of number, like in the previous versions.
8+
9+
### Added
10+
11+
* Allowed options argument to be passed for recieving wrong answers, as well as the correct one.
12+
13+
## [1.0.4](https://github.com/MilosPaunovic/questionnaire/compare/1.0.3...1.0.4) - 2022-09-28
14+
15+
### Fixed
216

317
* Included question data files into the final bundle.
418

5-
## [1.0.3](https://github.com/MilosPaunovic/questionnaire/compare/1.0.2...1.0.3)
19+
## [1.0.3](https://github.com/MilosPaunovic/questionnaire/compare/1.0.2...1.0.3) - 2022-09-28
20+
21+
### Changed
622

7-
* Trimmed down list of questions following the [advice](https://www.linkedin.com/feed/update/urn:li:activity:6980753295447330816?commentUrn=urn%3Ali%3Acomment%3A%28activity%3A6980753295447330816%2C6980847167380803586%29) of the initial answers creator.
823
* Improved the way of library bundling.
924

10-
## [1.0.2](https://github.com/MilosPaunovic/questionnaire/commits/1.0.2)
25+
### Removed
26+
27+
* Trimmed down list of questions following the [advice](https://www.linkedin.com/feed/update/urn:li:activity:6980753295447330816?commentUrn=urn%3Ali%3Acomment%3A%28activity%3A6980753295447330816%2C6980847167380803586%29) of the initial answers creator.
28+
29+
## [1.0.2](https://github.com/MilosPaunovic/questionnaire/commits/1.0.2) - 2022-09-28
1130

12-
* Initial release with two variations of questions in Serbian (Latin and Cyrillic) and two methods, question and questions.
31+
_Initial release._

README.md

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,22 +42,31 @@ Currently available country codes and variations:
4242

4343
#### Question
4444

45-
Returns random question from the list in the object form, containing both question and answer properties.
45+
Returns random question from the list in the object form, containing question and answer properties.
4646

4747
```js
48-
QUESTIONNAIRE.question();
48+
QUESTIONNAIRE.question(); // => { question: 'QUESTION', answer: 'ANSWER' }
49+
```
50+
51+
Returns random question from the list in the object form, containing question, answer and wrong (containg number of wrong answers you've specified, in range from 1 to 30) properties.
4952

50-
// => { question: 'QUESTION', answer: 'ANSWER' }
53+
```js
54+
QUESTIONNAIRE.question({ wrong: 3}); // => { question: 'QUESTION', answer: 'ANSWER', wrong: ['WRONG', 'WRONG', 'WRONG'] }
5155
```
5256

57+
5358
#### Questions
5459

55-
Returns array of question objects from the list, defaults to 10, accepted values between 1 and 10000
60+
Returns an array of 10 question objects from the list.
5661

5762
```js
58-
QUESTIONNAIRE.questions(howMany = 10)
63+
QUESTIONNAIRE.questions() // => [{ question: 'QUESTION', answer: 'ANSWER' }, { question: 'QUESTION', answer: 'ANSWER' }...]
64+
```
65+
66+
Returns an array of question objects with wrong answers (containg number of wrong answers you've specified, in range from 1 to 30) from the list (number of items defaults to 10, accepted range from 1 to 10000).
5967

60-
// => [{ question: 'QUESTION', answer: 'ANSWER' }, { question: 'QUESTION', answer: 'ANSWER' }...]
68+
```js
69+
QUESTIONNAIRE.questions({ howMany: 7, wrong: 1 }) // => [{ question: 'QUESTION', answer: 'ANSWER', wrong: ['WRONG'] }, { question: 'QUESTION', answer: 'ANSWER', wrong: ['WRONG'] }...]
6170
```
6271

6372
## Contributing

dist/questionnaire.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@paunovic/questionnaire",
3-
"version": "1.0.4",
3+
"version": "2.0.0",
44
"description": "Serving random questions in multiple languages has never been this easy.",
55
"author": "Milos Paunovic <paun992@hotmail.com>",
66
"license": "MIT",

questions/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Every questions entry should be simple JavaScript file which exports only one co
66

77
## Naming conventions
88

9-
Every questions file should be placed in folder named in the format of 2 digit ISO Country code, and the file name itself should be an IETF primary language tag, with optional IETF language subtag from other standards such as ISO 639, ISO 15924, ISO 3166-1 and UN M.49, if necessary. Example would be `rs/sr-Cyrl` for Serbian written in Cyrillic script.
9+
Every questions file should be placed in folder named in the format of 2 letter ISO Country code, and the file name itself should be an IETF primary language tag, with optional IETF language subtag from other standards such as ISO 639, ISO 15924, ISO 3166-1 and UN M.49, if necessary. Example would be `rs/sr-Cyrl` for Serbian written in Cyrillic script.
1010

1111
Find out more at [Country Code](https://countrycode.org/) and [Wikipedia](https://en.wikipedia.org/wiki/IETF_language_tag).
1212

src/helpers/general.js

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,54 @@
11
/**
2-
* Function which returns random number for range between 1 and supplied maximum.
3-
*
4-
* @param {number} maximum - Parameter which tells the upper limit of generated random number
5-
* @return {number} Random number between 1 and suplied maximum
2+
* Returns random number in range between 1 and supplied maximum.
63
*
4+
* @param {number} maximum Upper limit of generated random number.
5+
* @return {number} Random number between 1 and suplied maximum.
76
*/
8-
97
export const randomNumber = (maximum) => {
10-
if (!maximum) throw new Error('Missing parameter maximum.');
11-
if (!parseInt(maximum, 10)) throw new Error('Parameter maximum must be convertible to number.');
8+
if (!maximum) throw new Error('Parameter maximum is missing.');
9+
if (!parseInt(maximum, 10)) throw new Error('Parameter maximum must be castable to integer.');
1210

1311
return Math.floor(Math.random() * parseInt(maximum, 10)) + 1;
1412
};
13+
14+
/**
15+
* Returns an array of random numbers in range between 1 and supplied maximum.
16+
*
17+
* @param {number} items Number of array items to be returned.
18+
* @param {number} maximum Upper limit of generated random number.
19+
* @return {number} Array of random numbers between 1 and suplied maximum.
20+
*/
21+
export const randomNumbers = (items, maximum) => {
22+
if (!items) throw new Error('Parameter items is missing.');
23+
if (!parseInt(items, 10)) throw new Error('Parameter items must be castable to integer.');
24+
25+
if (!maximum) throw new Error('Parameter maximum is missing.');
26+
if (!parseInt(maximum, 10)) throw new Error('Parameter maximum must be castable to integer.');
27+
28+
const result = new Set();
29+
30+
while (result.size < items) {
31+
result.add(randomNumber(maximum));
32+
}
33+
34+
return Array.from(result);
35+
};
36+
37+
/**
38+
* Checks if the parameter can be casted to integer and, if so, limits the
39+
* value to supplied minimum and maximum.
40+
*
41+
* @param {any} value Parameter value itself.
42+
* @param {string} name Name of the parameter for error message.
43+
* @param {number} minimum Lower limit of value.
44+
* @param {number} maximum Upper limit of value.
45+
* @return {number} Properly casted and limited value.
46+
*/
47+
export const parameterLimitter = (value, name, minimum, maximum) => {
48+
if (!parseInt(value, 10)) throw new Error(`Parameter ${name} must be castable to integer.`);
49+
50+
if (parseInt(value, 10) < minimum) value = minimum;
51+
if (parseInt(value, 10) > maximum) value = maximum;
52+
53+
return value;
54+
};

0 commit comments

Comments
 (0)