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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@quizerjs/svelte

v0.0.7

Published

quizerjs Svelte 集成包

Readme

@quizerjs/svelte

quizerjs 的 Svelte 集成包,提供 Svelte 组件和 Stores,用于在 Svelte 应用中集成 quizerjs。

安装

npm install @quizerjs/svelte @quizerjs/dsl
# 或
pnpm add @quizerjs/svelte @quizerjs/dsl
# 或
yarn add @quizerjs/svelte @quizerjs/dsl

使用

基础用法

<script>
  import { QuizComponent } from '@quizerjs/svelte';
  import type { QuizDSL } from '@quizerjs/dsl';

  const dsl: QuizDSL = {
    quiz: {
      title: '示例测验',
      questions: [
        {
          id: 'q1',
          type: 'single_choice',
          text: '这是单选题?',
          options: [
            { id: 'opt1', text: '选项 A', isCorrect: true },
            { id: 'opt2', text: '选项 B', isCorrect: false },
          ],
        },
      ],
    },
  };
</script>

<QuizComponent {dsl} />

使用 Store

<script>
  import { useQuiz } from '@quizerjs/svelte';
  import type { QuizDSL } from '@quizerjs/dsl';

  const dsl: QuizDSL = { /* ... */ };

  const {
    answers,
    submitted,
    score,
    setAnswer,
    submit,
    reset,
  } = useQuiz({
    dsl,
    onSubmit: (answers, score) => {
      console.log('提交答案:', answers);
      console.log('得分:', score);
    },
  });
</script>

<div>
  <!-- 自定义 UI -->
  <button on:click={submit}>提交</button>
  <button on:click={reset}>重置</button>
</div>

API

QuizComponent

Svelte 组件,用于渲染完整的测验界面。

Props

  • dsl: QuizDSL - 测验 DSL 数据(必需)
  • disabled?: boolean - 是否禁用交互(默认:false
  • showResults?: boolean - 是否显示结果(默认:true
  • onSubmit?: (answers: Record<string, any>) => void - 提交回调
  • onAnswerChange?: (questionId: string, answer: any) => void - 答案变化回调

useQuiz

Svelte Store,提供测验的状态管理和逻辑。

参数

interface UseQuizOptions {
  dsl?: QuizDSL;
  autoValidate?: boolean;
  onSubmit?: (answers: Record<string, any>, score: number) => void;
}

返回值

interface UseQuizReturn {
  dsl: Writable<QuizDSL | null>;
  answers: Writable<Record<string, any>>;
  submitted: Writable<boolean>;
  score: Writable<number | null>;
  loadDSL: (dsl: QuizDSL) => void;
  setAnswer: (questionId: string, answer: any) => void;
  submit: () => void;
  reset: () => void;
  isAnswerCorrect: (question: Question, answer: any) => boolean;
}

支持的题型

  • 单选题 (single_choice)
  • 多选题 (multiple_choice)
  • 文本输入题 (text_input)
  • 判断题 (true_false)

样式

组件包含默认样式,可以直接使用。如果需要自定义样式,可以通过 CSS 覆盖类名。

License

MIT