Highly flexible JavaScript Quiz/Survey engine
$ npm install kwiz
const Kwiz = require('kwiz')
const quizDefinition = {
questions: [
{ message: 'Hey' },
{ message: 'What is your name?', answer: {type: 'string', hint: 'Really?', id: 'name'} },
{ message: 'Bye {{answers.name}}' }
]
}
const quiz = new Kwiz(quizDefinition)
quiz.start()
.then((reply) => {
return quiz.processMessage('John')
})
.then((reply) => {
return quiz.processMessage()
})
.then((reply) => {
console.log(quiz.getState())
})
There are some other examples.
Kwiz
Initialize new quiz.
Param | Type | Description |
---|---|---|
quizDefinition | Object |
Quiz definition |
state | Object |
Optional state |
handlers | Object |
Custom answer type handlers |
Add custom type handler.
Param | Type | Description |
---|---|---|
type | String |
Answer type |
handler | Function |
Handler function |
Start quiz.
Returns promise with engine reply.
Checks for quiz complete.
Process user reply.
Returns promise with engine reply.
Parameter | Type | Description |
---|---|---|
message | Any |
User reply |
Quiz defiunition is plain object with array of questions.
var quiz = {
questions: [
{ message: 'Hey' },
{
message: 'What is your name?',
answer: {type: 'string', hint: 'Really?', id: 'name' }
},
{ message: 'Wow', criteria: {'answers.age': {$lt: 21}}}
{ message: 'Bye {{answers.name}}'}
]
}
Question structure:
Field | Type | Description |
---|---|---|
message | 'String' | Handlebars template (must be empty for question groups) |
answer | 'Object' | Answer options (Optional) |
criteria | 'String' | Criteria (Optional) |
messages | 'Array' | Array of questons (Optional) |
attachment | 'Any' | Question attachment (Optional) |
Answer options structure:
Field | Type | Description |
---|---|---|
id | 'String' | Answer id. Will used as key for store user answer |
type | 'String' | Answer type |
items | 'Array' | Items for choise answer type. (Optional) |
There are some examples.
mongodb-like query.
Supported Operators:
- Array Operators (
$all
,$elemMatch
,$size
) - Comparisons Operators (
$gt
,$gte
,$lt
,$lte
,$ne
,$nin
,$in
) - Element Operators (
$exists
,$type
) - Evaluation Operators (
$regex
,$mod
,$where
) - Logical Operators (
$and
,$or
,$nor
,$not
)
For documentation on using query operators see mongodb
Engine reply structure:
Field | Type | Description |
---|---|---|
message | 'String' | Engine message (Optional) |
error | 'Bool' | Error flag (Optional) |
completed | 'Bool' | Is quiz completed (Optional) |
attachment | 'Any' | Question attachment (Optional) |
Supported answer types:
text
- any non-empty stringint
- any integer valuechoise
- list of predefined answerstruthy
- yes/no, yep/nope handler
Answer type handler used for checking/converting user answer.
function (question, answer) -> Promise
Parameter | Type | Description |
---|---|---|
question | Any |
Question definition |
answer | Any |
User answer |
The handler must reject promise when the type check failed otherwise will resolve with a message or anything you want. This value will be stored in quiz state.
quiz.addHandler('speed', function (question, answer) {
var matches = /(\d+) +mph/i.exec(answer)
return matches
? Promise.resolve(parseInt(matches[1], 10))
: Promise.reject(question.hint || 'Wrong speed value')
})
The MIT License (MIT)
Copyright (c) 2016 Vitaly Domnikov
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.