Skip to content

telegraf/kwiz

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

42 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

kwiz

Build Status NPM Version

Highly flexible JavaScript Quiz/Survey engine

Installation

$ npm install kwiz

Example

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.

Api


Kwiz.new(quizDefinition, [state], [handlers])

Initialize new quiz.

Param Type Description
quizDefinition Object Quiz definition
state Object Optional state
handlers Object Custom answer type handlers

Kwiz.addHandler(type, handler)

Add custom type handler.

Param Type Description
type String Answer type
handler Function Handler function

Kwiz.start() -> Promise

Start quiz.

Returns promise with engine reply.


Kwiz.isCompleted() -> Bool

Checks for quiz complete.


Kwiz.processMessage(message) -> Promise

Process user reply.

Returns promise with engine reply.

Parameter Type Description
message Any User reply

Quiz definition

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.

Criteria

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

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)

Answer types

Supported answer types:

  • text - any non-empty string
  • int - any integer value
  • choise - list of predefined answers
  • truthy - yes/no, yep/nope handler

Custom answer type handlers

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.

Example

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')
})

License

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.

About

Highly flexible JavaScript quiz engine

Resources

License

Stars

Watchers

Forks

Packages

No packages published