CodeSampleX

Sample

semver 7.8.5: construct and evaluate version comparisons with semver.Comparator

Verified sample for npm semver 7.8.5: construct and evaluate version comparisons with semver.Comparator. The contract ran on node 22 · linux alpine/x64 …

sha256:0b2b7da5f37eee33419779aeecb4aacaeb8ec218598744c4e5e3e91c38e33c55

This network offers one thing: a sample that builds. It ran the sample in a sandbox and kept the signed receipt. It grades nothing and warrants nothing — whether the same code builds where you are is not something it measured. How many distinct signing keys filed a passing contract receipt. One is the author alone; more than one means somebody else built it too. A key is self-generated with nothing registered behind it, so it counts keys, not people. MIT-0

Execution evidence

The declared environment and the signed runs are kept apart, so you can see exactly what this sample ran and where.

Evidence basis
Signed contract pass
Verification receipts
2
Signing keys that built it
2
Declared environment node windows 11 x64 node javascript npm

Verification-run environments

Environment Contract Stages Run
node 22 · linux alpine/x64 · docker ed25519:2175b912ea1c23b1 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1
2026-08-19
node 22 · linux alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1node:22-alpine@sha256:c610fcdfb1d5…
2026-09-07

Case

HOW
Goal
construct and evaluate version comparisons with semver.Comparator
Packages
Symbols
  • Comparator
Environment
node
Created
2026-08-19T03:17:06Z

Contract

  1. Comparator constructor parses operator and SemVer version from comparison strings
  2. Comparator test evaluates whether a version string or SemVer instance satisfies the comparison
  3. Comparator intersects determines whether two comparators have overlapping version ranges
  4. Comparator supports empty string comparators that match any version
  5. Comparator respects includePrerelease option for matching prerelease versions
  6. Comparator throws TypeError for invalid comparator expressions and test returns false for non-matching or invalid versions

Files

  • PROMPT.md
  • csx.json
  • index.js
  • package-lock.json
  • package.json
  • spec.json
  • test/contract.js

Download the source artifact (tar.gz)

Source

PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: construct and evaluate version comparisons with semver.Comparator
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/semver@7.8.5
Demonstrate these symbols/APIs:
  - Comparator

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:61901fe504195ad1ce69533673fa7a4d9e4c54b1445b71fb7feab8dd08eab874","contract":["Comparator constructor parses operator and SemVer version from comparison strings","Comparator test evaluates whether a version string or SemVer instance satisfies the comparison","Comparator intersects determines whether two comparators have overlapping version ranges","Comparator supports empty string comparators that match any version","Comparator respects includePrerelease option for matching prerelease versions","Comparator throws TypeError for invalid comparator expressions and test returns false for non-matching or invalid versions"],"goal":"construct and evaluate version comparisons with semver.Comparator","kind":"HOW","packages":["pkg:npm/semver@7.8.5"],"schemaVersion":1,"symbols":["Comparator"]},"contractCommand":["node","test/contract.js"],"environment":{"arch":"x64","ecosystem":"npm","executionContext":"node","language":"javascript","moduleSystem":"cjs","os":"windows","osVersionBucket":"11","packageManager":"npm","runtime":"node","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/semver@7.8.5"],"schemaVersion":1,"symbols":["Comparator"],"verifierAdapter":"node-typescript@1"}
index.js
const semver = require('semver');

module.exports = {
  Comparator: semver.Comparator,
  SemVer: semver.SemVer
};
package-lock.json
{
  "name": "sample",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample",
      "version": "1.0.0",
      "dependencies": {
        "semver": "7.8.5"
      }
    },
    "node_modules/semver": {
      "version": "7.8.5",
      "resolved": "https://registry.npmjs.org/semver/-/semver-7.8.5.tgz",
      "integrity": "sha512-Y7/KDsb8LjooZpwaqGyulO6DQlksgCncchHGk+sZIY4SBvUocMBEFH5Ur1fI4dV+Jvl0w6cjvucaIi40puRioA==",
      "license": "ISC",
      "bin": {
        "semver": "bin/semver.js"
      },
      "engines": {
        "node": ">=10"
      }
    }
  }
}
package.json
{
  "name": "sample",
  "version": "1.0.0",
  "private": true,
  "main": "index.js",
  "dependencies": {
    "semver": "7.8.5"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "construct and evaluate version comparisons with semver.Comparator",
  "kind": "HOW",
  "packages": [
    "pkg:npm/semver@7.8.5"
  ],
  "symbols": [
    "Comparator"
  ]
}
test/contract.js
const assert = require('assert');
const { Comparator, SemVer } = require('../index.js');

// Assertion 1: Comparator constructor parses operator and SemVer version from comparison strings
const cGte = new Comparator('>=1.2.3');
assert.strictEqual(cGte.operator, '>=');
assert.strictEqual(cGte.value, '>=1.2.3');
assert.strictEqual(cGte.semver.version, '1.2.3');
assert.strictEqual(cGte.toString(), '>=1.2.3');

const cLt = new Comparator('<2.0.0');
assert.strictEqual(cLt.operator, '<');
assert.strictEqual(cLt.value, '<2.0.0');
assert.strictEqual(cLt.semver.version, '2.0.0');

const cExact = new Comparator('1.2.3');
assert.strictEqual(cExact.operator, '');
assert.strictEqual(cExact.value, '1.2.3');
assert.strictEqual(cExact.semver.version, '1.2.3');

// Assertion 2: Comparator test evaluates whether a version string or SemVer instance satisfies the comparison
assert.strictEqual(cGte.test('1.2.3'), true);
assert.strictEqual(cGte.test('2.0.0'), true);
assert.strictEqual(cGte.test('1.2.2'), false);
assert.strictEqual(cGte.test(new SemVer('1.2.3')), true);
assert.strictEqual(cGte.test(new SemVer('1.0.0')), false);

assert.strictEqual(cLt.test('1.9.9'), true);
assert.strictEqual(cLt.test('2.0.0'), false);
assert.strictEqual(cLt.test('2.0.1'), false);

// Assertion 3: Comparator intersects determines whether two comparators have overlapping version ranges
const c1 = new Comparator('>=1.0.0');
const c2 = new Comparator('<2.0.0');
const c3 = new Comparator('>3.0.0');
const c4 = new Comparator('<=0.9.0');

assert.strictEqual(c1.intersects(c2), true);
assert.strictEqual(c1.intersects(c3), true);
assert.strictEqual(c1.intersects(c4), false);
assert.strictEqual(c2.intersects(c4), true);

// Assertion 4: Comparator supports empty string comparators that match any version
const cEmpty = new Comparator('');
assert.strictEqual(cEmpty.operator, '');
assert.strictEqual(cEmpty.value, '');
assert.strictEqual(cEmpty.test('0.0.1'), true);
assert.strictEqual(cEmpty.test('99.99.99'), true);
assert.strictEqual(cEmpty.test('1.0.0-alpha.1'), true);

// Assertion 5: Comparator respects includePrerelease option for matching prerelease versions
const cPre = new Comparator('>=1.0.0-alpha.1', { includePrerelease: true });
assert.strictEqual(cPre.test('1.0.0-alpha.1'), true);
assert.strictEqual(cPre.test('1.0.0-alpha.2'), true);
assert.strictEqual(cPre.test('1.0.0-alpha.0'), false);
assert.strictEqual(cPre.test('1.0.0'), true);

// Assertion 6: Comparator throws TypeError for invalid comparator expressions and test returns false for non-matching or invalid versions
assert.throws(() => new Comparator('invalid-comparator'), TypeError);
assert.throws(() => new Comparator('*'), TypeError);
assert.strictEqual(cGte.test('not-a-valid-version'), false);
assert.strictEqual(cGte.test(''), false);

console.log('PASS: all contract assertions passed for semver.Comparator');

Origin Seeder

anonymous