3.0.0 • Published 8 years ago

kwiz v3.0.0

Weekly downloads
3
License
MIT
Repository
github
Last release
8 years ago

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: 'Buy {{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.

ParamTypeDescription
quizDefinitionObjectQuiz definition
stateObjectOptional state
handlersObjectCustom answer type handlers

Kwiz.addHandler(type, handler)

Add custom type handler.

ParamTypeDescription
typeStringAnswer type
handlerFunctionHandler 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.

ParameterTypeDescription
messageAnyUser 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: 'Buy {{answers.name}}'}
  ]
}

Question structure:

FieldTypeDescription
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:

FieldTypeDescription
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:

FieldTypeDescription
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

ParameterTypeDescription
questionAnyQuestion definition
answerAnyUser 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.

3.0.0

8 years ago

2.0.0

8 years ago

1.2.0

8 years ago

1.1.0

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago

0.3.1

8 years ago

0.3.0

8 years ago

0.2.0

8 years ago

0.0.1

8 years ago

0.0.0

8 years ago