npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

kwiz

v3.0.0

Published

Quiz/Survey engine

Downloads

18

Readme

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.

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