npm.io
0.1.0 • Published 21h ago

@varykit/core

Licence
MIT
Version
0.1.0
Deps
0
Size
8 kB
Vulns
0
Weekly
0

@varykit/core

Framework-agnostic multivariate testing core. Zero dependencies.

This package contains the pure bucketing logic shared by all VaryKit framework packages. It has no dependencies and no peer dependencies — it is safely importable from Node, the browser, Vue, or any other environment without pulling in anything extra.

The segment model

VaryKit assigns every visitor to a single global segment (e.g. control or treatment). That segment is reused consistently across every page and element test — a visitor is never in different segments for different tests.

Install

pnpm add @varykit/core

API

resolveSegment(options)

Builds a weight table (explicit distributions + auto-split remainder), resolves a visitor's segment (sticky via existingSegment, otherwise weighted random), and resolves the segment's cookie duration.

import { resolveSegment } from '@varykit/core'

const { segment, duration } = resolveSegment({
  segments: {
    control: { distribution: 0.5 },
    treatment: { distribution: 0.5 }
  },
  existingSegment: undefined, // cookie value, if any
  defaultDuration: 60 * 60 * 24 * 30
})
validateSegments(segments)

Validates that the global segment distributions are coherent, throwing a descriptive Error on invalid configurations.

import { validateSegments } from '@varykit/core'

validateSegments({
  control: { distribution: 0.5 },
  treatment: { distribution: 0.5 }
})
Types
  • VarySegmentConfig{ distribution?: number; duration?: number }
  • VarySegments{ [segment: string]: VarySegmentConfig }
  • ResolvedSegment{ segment: string; duration: number }
Constants
  • DEFAULT_DURATION60 * 60 * 24 * 30 (30 days)

Scope

This package owns only the bucketing math and validation. Persistence (cookies) and reactivity are the responsibility of each framework package (@varykit/vue, @varykit/nuxt).

License

MIT

Keywords