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

@3dsource/business-rules

v0.0.2-dev.0

Published

Business rules for 3dsource applications

Readme

@3dsource/business-rules

Business rules of the 3dsource modular configurator: the rule set format, the operations that change it, and the evaluation that tells which component types are required or optional.

The core is pure functions. It does no network or storage calls, gets everything as arguments and never modifies its inputs. The rule set belongs to the core: other code does not parse or change it by hand but goes through these functions.

Used by:

  • the Metabox configurator editor — shows and changes the rules;
  • the Stream configurator frontend — applies the rules while a user builds a configuration;
  • the backend — through its adapter, which parses the input and decides which issues to show.

Installation

npm install @3dsource/business-rules

Version 1

  • Rule set format RuleSetV1 (schemaVersion: 1).
  • One rule type, componentOptionality: it makes a component type optional and sets whether a component of it is selected at the start.
  • One condition, always.
  • One target, a component type. A rule applies to every component of the type, never to a single component.
{
  "schemaVersion": 1,
  "rules": [
    {
      "id": "88888888-8888-4888-8888-888888888888",
      "type": "componentOptionality",
      "condition": { "type": "always" },
      "result": {
        "target": {
          "type": "componentType",
          "id": "33333333-3333-4333-8333-333333333333"
        },
        "defaultState": "unselected"
      }
    }
  ]
}

Results and issues

Every function except createRuleSet returns either the value or { issues }. A wrong input is never thrown: an exception means a bug in the library.

Each issue has a literal code for code, a path to the input field it is about (e.g. position, rule.result.target, ruleSet.rules[2].id) and a message for diagnostics, not for users.

  • Operations and evaluation return every issue found, in parameter order.
  • JSON parsing returns the first issue and stops.

Applying the rules

Parse the stored set, evaluate it against the configurator, then read each component type.

import type { Configurator } from '@3dsource/business-rules';
import { createRuleSet, createRuleSetFromJson, evaluateRules, resolveOptionality } from '@3dsource/business-rules';

// `stored` is the parsed JSON of the set; `null` when the configurator has no rules yet.
function optionalities(stored: unknown, configurator: Configurator) {
  const parsed = stored === null ? { ruleSet: createRuleSet() } : createRuleSetFromJson(stored);
  if ('issues' in parsed) return parsed;

  const evaluated = evaluateRules({ ruleSet: parsed.ruleSet, configurator });
  if ('issues' in evaluated) return evaluated;

  // Each: { kind: 'required' } or { kind: 'optional', defaultState, ruleId }
  return {
    types: configurator.componentTypes.map((type) => ({
      type,
      optionality: resolveOptionality(type, evaluated.rules),
    })),
  };
}
  • The root type is always required.
  • A type with an effective componentOptionality rule is optional; defaultState is its initial state.
  • Any other type is required, while its parent is present.
  • When parsing or evaluation returns issues, apply no rules: a partly valid set is never used.

In V1 the result does not depend on the user's selection. Evaluate once and again only when the set or the configurator changes.

Changing the set

import { addRule, removeRule, replaceRule, toChoice } from '@3dsource/business-rules';

const choice = toChoice(optionality); // 'required' | 'optionalSelected' | 'optionalUnselected'

const result = addRule({
  ruleSet,
  ruleId: crypto.randomUUID(),
  schemaVersion: ruleSet.schemaVersion,
  position: null, // append; or a 1-based position
  rule: {
    type: 'componentOptionality',
    condition: { type: 'always' },
    result: {
      target: { type: 'componentType', id: typeId },
      defaultState: 'selected',
    },
  },
  configurator,
});
if ('issues' in result) report(result.issues);
else save(result.ruleSet);

| Editor change | Operation | | ------------------------ | --------------------------- | | Required → Optional | addRule | | One Optional → the other | replaceRule with ruleId | | Optional → Required | removeRule with ruleId |

Operations check the new rule against the configurator, but not the other rules of the set. A set with a stale rule can still be changed and fixed.

Data from outside — a rule a client sent, a set from storage or GraphQL — goes through createRuleFromJson or createRuleSetFromJson first. Both take a parsed JSON value, not a string.

When the configurator changes

Rules are not removed on their own. Call removeTarget with the configurator after the change:

  • a component type was deleted, including when its last component was deleted — with the deleted type;
  • the root type changed — with the new root.
const result = removeTarget({
  ruleSet,
  target: { type: 'componentType', id: deletedTypeId },
  configurator,
});

Deleting a single component while the type keeps others changes no rules.

A rule left on a missing type or on the root makes evaluateRules return unknown_target or forbidden_target, and then no rules apply.

API reference

API reference — every function, type and issue, generated from the TSDoc.

| Function | Purpose | | ----------------------- | ------------------------------------------------ | | createRuleSet | An empty set of the current version | | createRuleSetFromJson | Parse a stored set | | createRuleFromJson | Parse a rule body | | addRule | Add a rule at a position | | replaceRule | Replace a rule body, keeping its id and position | | removeRule | Remove one rule | | removeTarget | Remove every rule of a target | | evaluateRules | The effective rules for a configurator | | resolveOptionality | Required or optional for one component type | | toChoice | The editor choice for an optionality | | evaluateCondition | Whether one condition holds |

Repository development

Run commands from the repository root:

pnpm business-rules:build
pnpm business-rules:docs
pnpm business-rules:test
pnpm business-rules:lint
pnpm business-rules:release:patch
  • business-rules:docs regenerates the API reference into api/ from the TSDoc of src/public-api.ts. The folder is gitignored; the package build regenerates it, so the published reference always matches the code.
  • The release command performs verification, changes the package version and publishes the verified tarball to npm.