CodeSampleX

Sample

semver 7.8.5: compare versions with semver.cmp

Verified sample for npm semver 7.8.5: compare versions with semver.cmp. The contract ran on node 22 · linux alpine/x64 · docker and passed.

sha256:7d75d056ba45c0de70ba39b7c13cb5e54abe68d67512e25ce175c191ce34eaec

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
compare versions with semver.cmp
Packages
Symbols
  • semver.cmp
Environment
node
Created
2026-08-19T05:47:00Z

Contract

  1. cmp evaluates greater than and greater than or equal comparisons between semantic versions
  2. cmp evaluates less than and less than or equal comparisons between semantic versions
  3. cmp evaluates equality and inequality operators correctly
  4. cmp evaluates strict string identity with triple equals operator
  5. cmp evaluates prerelease version precedence correctly
  6. cmp throws TypeError when an invalid comparison operator is provided

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: compare versions with semver.cmp
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/semver@7.8.5
Demonstrate these symbols/APIs:
  - semver.cmp
Constraints:
  - executionContext: node
Required runtime conditions:
  - ecosystem: npm
  - language: javascript
  - moduleSystem: cjs
  - packageManager: npm@11.8.0
  - runtime: node@24.13.1

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:a5f3387091b5d72d82db09eaf78fa3662f488ce6e3670d0afdc2c5958ed8ec8f","constraints":{"executionContext":"node"},"contract":["cmp evaluates greater than and greater than or equal comparisons between semantic versions","cmp evaluates less than and less than or equal comparisons between semantic versions","cmp evaluates equality and inequality operators correctly","cmp evaluates strict string identity with triple equals operator","cmp evaluates prerelease version precedence correctly","cmp throws TypeError when an invalid comparison operator is provided"],"goal":"compare versions with semver.cmp","kind":"HOW","packages":["pkg:npm/semver@7.8.5"],"schemaVersion":1,"symbols":["semver.cmp"]},"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":["semver.cmp"],"verifierAdapter":"node-typescript@1"}
index.js
const semver = require('semver');

module.exports = {
  cmp: semver.cmp
};
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": "compare versions with semver.cmp",
  "kind": "HOW",
  "packages": [
    "pkg:npm/semver@7.8.5"
  ],
  "symbols": [
    "semver.cmp"
  ],
  "constraints": {
    "executionContext": "node"
  },
  "runtimeConditions": {
    "ecosystem": "npm",
    "language": "javascript",
    "moduleSystem": "cjs",
    "packageManager": "npm@11.8.0",
    "runtime": "node@24.13.1"
  }
}
test/contract.js
const assert = require('assert');
const { cmp } = require('../index.js');

// cmp evaluates greater than and greater than or equal comparisons between semantic versions
assert.strictEqual(cmp('1.5.0', '>', '1.2.0'), true);
assert.strictEqual(cmp('1.2.0', '>', '1.5.0'), false);
assert.strictEqual(cmp('1.5.0', '>=', '1.5.0'), true);
assert.strictEqual(cmp('1.5.0', '>=', '1.2.0'), true);

// cmp evaluates less than and less than or equal comparisons between semantic versions
assert.strictEqual(cmp('1.2.0', '<', '1.5.0'), true);
assert.strictEqual(cmp('1.5.0', '<', '1.2.0'), false);
assert.strictEqual(cmp('1.2.0', '<=', '1.2.0'), true);
assert.strictEqual(cmp('1.2.0', '<=', '1.5.0'), true);

// cmp evaluates equality and inequality operators correctly
assert.strictEqual(cmp('1.2.3', '==', 'v1.2.3'), true);
assert.strictEqual(cmp('1.2.3', '=', '1.2.3'), true);
assert.strictEqual(cmp('1.2.3', '!=', '1.2.4'), true);
assert.strictEqual(cmp('1.2.3', '!=', '1.2.3'), false);
assert.strictEqual(cmp('1.2.3', '==', '1.2.3+build123'), true);

// cmp evaluates strict string identity with triple equals operator
assert.strictEqual(cmp('1.2.3', '===', '1.2.3'), true);
assert.strictEqual(cmp('1.2.3', '===', 'v1.2.3'), false);
assert.strictEqual(cmp('1.2.3', '!==', 'v1.2.3'), true);

// cmp evaluates prerelease version precedence correctly
assert.strictEqual(cmp('1.0.0-alpha', '<', '1.0.0'), true);
assert.strictEqual(cmp('1.0.0-beta', '>', '1.0.0-alpha'), true);
assert.strictEqual(cmp('1.0.0-alpha.1', '<', '1.0.0-alpha.2'), true);

// cmp throws TypeError when an invalid comparison operator is provided
assert.throws(() => {
  cmp('1.0.0', 'invalid', '1.0.0');
}, TypeError);

console.log('PASS: semver.cmp contract verified');

Origin Seeder

anonymous