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

free-survey-core

v1.3.7

Published

A free survey/questionnaire library supporting you build your own implementation of form builder or parser.

Downloads

53

Readme

Free Survey Core

Free Survey Core是一个免费、开源的调查问卷库,它提供一个实现问卷功能的抽象类库和一个默认的实现,通过它你可以创建自己的问卷设计工具或是问卷功能实现而无需自己再进行问卷概念的抽象。

npm NPM GitHub top language npm

安装

npm install free-survey-core

用法

import { Survey } from "free-survey-core";
import { AbstractPage } from "free-survey-core";
import { Page } from "free-survey-core";
import { QuestionGroup } from "free-survey-core";
import { SingleTextQuestion } from "free-survey-core";

const survey = new Survey();
const page = new Page();
survey.pages.push(page);
const pages: Array<AbstractPage> = survey.pages;
const questionGroup = new QuestionGroup();
const singleTextQuestion = new SingleTextQuestion((options = { title: "a single text question" }));
questionGroup.questions.push(singleTextQuestion);
page.elements.push(questionGroup);

const answer = survey.getAnswer();
const answerFlattened = survey.getAnswerFlattened();

设计思路

free-survey-core 将问卷设计为可包含多个页面、页面又可包含不同元素的形式。即每个问卷(Survey)包含多个页面(Page),每个页面包含多个题组(QuestionGroup)或问题(AbstractQuestion),每个题组也可包含多个问题。

问卷中的问题通常有许多类型,如单项选择、下拉选择、多项选择、文本问答、时间选择等类型,free-survey-core 将各种问题抽象为 AbstractQuestion 存放在页面或是题组中,具体的实现则是各种具体的问题类(如SingleTextQuestion)。

free-survey-core 支持的所有问题类型均注册在 QuestionType 中,你可以使用以下命令导入:

import QuestionType from "free-survey-core";

free-survey-core 中的主要类均被抽象为 AbstractElement ,元素类型包括有问卷(survey)、页面(page)、题组(questionGroup)、问题(question),其中题组和问题又被归类为页面元素(AbstractPageElement),你可以通过以下代码获取元素类型和页面元素类型:

import { ElementType, PageElementType } from "free-survey-core";

free-survey-core 主要的责任在于对调查问卷的抽象和数据的存储而非功能实现,因而其包含的功能较为有限。

扩展

若需要对题型进行扩展,继承 AbstractQuestion 并实现即可。此外,若要支持导入导出功能,需要实例化 QuestionParserFactory,调用 registerParser 方法,提供解析器即可。解析问卷时,会逐一遍历所有元素,根据元素类型和问题类型调用对应的解析器进行解析,此时会把识别到的对应元素传入解析器,解析器需要根据元素对象实例化元素并返回,随后,实例化的元素会被插入到对应的位置。解析器定义如下:

export type Parser = (obj: any) => AbstractQuestion;

支持的问题类型

| 问题类型 | 是否支持 | 是否规划 | | :------------------------: | :------: | :------: | | 单项选择(RadioGroup) | ✅ | | | 多项选择(CheckBox) | ✅ | | | 文本问答(SingleText) | ✅ | | | 下拉选择(Dropdown) | ✅ | | | 时间选择(TimePicker) | ✅ | | | 时间段选择(TimeSpanPicker) | ✅ | | | 分割线(Splitter) | ✅ | | | 文件(File) | ❌ | ✅ |

注意

  1. 默认的ID生成器(ULID)是一个简单的自增ID生成器,在创建问题时只需要将ID设置为null就会采用默认ID生成器。如果你需要更复杂的ID生成器,可以自行实现生成算法,并在添加问题时将生成的ID传入构造函数中,但生成的ID必须在同一问卷中保证唯一性。
  2. 为了在Node环境中使用ULID,我们启用了不安全的 Math.random 模块,这是因为我们因为我们不需要生成的ID作为加密算法的一部分,而只是需要一个唯一标识符。如果你需要更安全的ID生成器,可以自行实现ID生成器并在创建问题时生成ID传入构造函数中。
  3. 此库仅提供了基础的问卷设计功能,不包含任何UI组件,你可以根据自己的需求实现UI组件。我们提供了一个基于Vue3的问卷设计生成组件 free-survey-form-builder 和一个同样基于Vue3的问卷填写组件 free-survey-form

反馈建议

请在 free-survey-core 的 Github仓库 提起issue以便进行反馈和建议。如有使用问题也可提出issue。

项目未来规划

free-survey-core 目前仍处于非常早期的版本,主要专注于基础实现,因此对于一些基础功能以外的建设目前暂不考虑,例如国际化、文档补充、测试等。如果你对这些有兴趣,欢迎你参与建设。

交流与沟通

如果你有沟通和交流的意愿,欢迎你发送邮件至 [email protected]

开源协议

Apache 2.0 © HHao