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

quaire

v0.1.0

Published

a framework-agnostic library to create user-flows, surveys, and questionnaires

Downloads

34

Readme

quaire

a framework-agnostic library to create user-flows, surveys, and questionnaires

Why the name?

Because I needed one and Questionnaire is terrible to type. Let me know if you have a better one!

What use-case does it try to solve?

I have to implement more and more features that behave like questionnaires or surveys. Over the years I tried a couple of things to make it easier for me to implement those features.

Of course, the first approach I tried was to implement a static user flow, for example:

  • present first question on first page
  • user selects option a
  • navigate to the next question
  • etc.

That was ok as long as I didn't have to change the flow but when I had to change the flow, I found myself changing big parts of the implementation.

I also tried to use finite state machines, but I ended up with spaghetti state machines most of the time due to regular changes in the user-flow (re-arranging questions, adding questions, skip questions, etc.)

The solution I found to work best for me was a concept of game-development called decision-tree. That way the whole user-flow is based on a static data structure that can be changed without the need of changing the view or the behaviour. This pattern provides a nice separation of:

  • data
  • behaviour
  • presentation

Features?

How does it work?

Installation

npm i --save quaire

Define quaire data

First you need to define the data (decision tree) based on the QuaireItem interface. This can be static data in a JS/TS file, a JSON file that you load on demand or a dynamic JSON from a CMS or backend API.

The structure for questions looks as follows:

import { QuaireComponentType, QuaireItem, QuaireNavigationItem } from 'quaire';

export const items: QuaireItem[] = [
  {
    id: 1,
    resultProperty: 'foo', // property that is used to save the answer
    navigationItemId: 1, // assiciation with the navigation entry (optional)
    dependsOnResultProperties: [], // dependencies on answers from former questions, based on the result property - not questions IDs
    componentType: QuaireComponentType.SINGLE_SELECT, // component type as indication for custom presentation logic
    question: 'Question 1',
    description: 'Description 1',
    required: true,
    selectOptions: [], // options for select components (optional)
    rangeOption: {}, // option for range components (optional)
    inputOption: {}, // option for input components (optional)
    defaultValue: {}, // default value for any kind of component (optional)
    nextItemId: {}, // id of the follow up question (optional), usually you want to define this in selectOptions, rangeOption or inputOption
  },
  // ...
];

The structure for the navigation looks a follows:

export const navigationItems: QuaireNavigationItem[] = [
  {
    id: 1,
    parentId: null, // has no parent
    name: 'Category 1',
  },
  {
    id: 2,
    parentId: 1, // has a parent (works only for one level)
    name: 'Subcategory 1',
  },
  {
    id: 3,
    parentId: null,
    name: 'Category 2',
  },
];

Use quaire behaviour

To use the default behavior you need to initialize Quaire with the data you defined in the former step.

import { Quaire } from 'quaire';

const q = new Quaire({ items, navigationItems }); // (optional) you can pass an existing result to restore the questionnaire

Next you can get the first active Question and display it in any way you want

let activeQuestion = q.getActiveQuestion(); // first question
let navigation = q.getNavigation(); // initial navigation
let result = q.getResult(); // initial result

// display activeQuestion.question and the related component presentation logic
// Vue.js pseudo code example
<template>
    <div>
        <template v-if="activeQuestion.componentType === 'SINGLE_SELECT'">
            {{ activeQuestion.question }}
            // loop through activeQuestion.selectOptions, etc.
        <template>
    </div>
</template>

After the user selected an answer you can save the answer and get the next question. It's also a good idea to update the navigation and result

onSubmit(value: any) {
    q.saveAnswer(value);
    activeQuestion = q.getActiveQuestion(); // follow up question
    navigation = q.getNavigation(); // update navigation
    result = q.getResult(); // update result

    // ...
}

You need to identify the end of the user-flow on your own. One way to do it is via the question ID or you create some logic around the result object of the Questionnaire.

onSubmit(value: any) {
    // ...
    const isValid = q.isValid();

    // check if the questionnaire is valid
    if(!isValid) {
        return;
    }

    // via ID
    if(activeQuestion.id === 3) {
        // persist result to the backend, redirect to another page,
        // whatever you want after the questionnaire is filled out
    }

    // via result
    if(result.foo && result.bar && result.baz) {
        // persist result to the backend, redirect to another page,
        // whatever you want after the questionnaire is filled out
    }
}

Extend quaire

Examples

Contribute

Contributions are always welcome! Please read the contribution guidelines first.

Contact

License

MIT