CodeSampleX

Sample

semver 7.8.5: parse and inspect semantic versions with semver.parse and SemVer

Verified sample for npm semver 7.8.5: parse and inspect semantic versions with semver.parse and SemVer. The contract ran on node 22 · linux alpine/x64 …

sha256:f5c202e86faa222a55afd210da4eb53c1589e1be62d9f9362ad772f07b34893d

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 windows 11 x64 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
parse and inspect semantic versions with semver.parse and SemVer
Packages
Symbols
  • SemVer
  • semver.parse
Created
2026-08-18T23:43:41Z

Contract

  1. semver.parse decomposes valid version strings into major, minor, patch, prerelease, and build metadata
  2. semver.parse handles loose formatting and leading characters when loose option is specified
  3. semver.parse returns null for invalid semver strings
  4. SemVer constructor creates instances and supports comparison and formatting methods
  5. SemVer instance inc method increments release version components in place

Files

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

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: parse and inspect semantic versions with semver.parse and SemVer
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/semver@7.8.5
Demonstrate these symbols/APIs:
  - SemVer
  - semver.parse
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:e91030501045b1edf39989edaa5fb3a490315819f89d165e9dd8f4a894bfefe7","contract":["semver.parse decomposes valid version strings into major, minor, patch, prerelease, and build metadata","semver.parse handles loose formatting and leading characters when loose option is specified","semver.parse returns null for invalid semver strings","SemVer constructor creates instances and supports comparison and formatting methods","SemVer instance inc method increments release version components in place"],"goal":"parse and inspect semantic versions with semver.parse and SemVer","kind":"HOW","packages":["pkg:npm/semver@7.8.5"],"schemaVersion":1,"symbols":["SemVer","semver.parse"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","ecosystem":"npm","os":"windows","osVersionBucket":"11","packageManager":"npm","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/semver@7.8.5"],"schemaVersion":1,"symbols":["SemVer","semver.parse"],"verifierAdapter":"node-typescript@1"}
index.mjs
import semver, { SemVer } from 'semver';

/**
 * Parses a version string into a SemVer object.
 * @param {string} version
 * @param {semver.Options} [options]
 * @returns {semver.SemVer | null}
 */
export function parseVersion(version, options) {
  return semver.parse(version, options);
}

/**
 * Creates a new SemVer instance directly using the SemVer class.
 * @param {string | semver.SemVer} version
 * @param {semver.Options} [options]
 * @returns {semver.SemVer}
 */
export function createSemVer(version, options) {
  return new SemVer(version, options);
}

export default {
  parseVersion,
  createSemVer,
  SemVer
};
package-lock.json
{
  "name": "sample-semver-parse",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-semver-parse",
      "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-semver-parse",
  "version": "1.0.0",
  "type": "module",
  "dependencies": {
    "semver": "7.8.5"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "parse and inspect semantic versions with semver.parse and SemVer",
  "kind": "HOW",
  "packages": [
    "pkg:npm/semver@7.8.5"
  ],
  "symbols": [
    "SemVer",
    "semver.parse"
  ],
  "constraints": {
    "executionContext": "node"
  },
  "runtimeConditions": {
    "ecosystem": "npm",
    "language": "javascript",
    "moduleSystem": "cjs",
    "packageManager": "npm@11.8.0",
    "runtime": "node@24.13.1"
  }
}
test/contract.mjs
import assert from 'node:assert/strict';
import semver, { SemVer } from 'semver';
import { parseVersion, createSemVer } from '../index.mjs';

// Verify symbols and exported helpers
assert.equal(typeof semver.parse, 'function', 'semver.parse must be a function');
assert.equal(typeof SemVer, 'function', 'SemVer must be a constructor function');
assert.equal(typeof parseVersion, 'function', 'parseVersion must be a function');
assert.equal(typeof createSemVer, 'function', 'createSemVer must be a function');

// Contract 1: semver.parse decomposes valid version strings into major, minor, patch, prerelease, and build metadata
{
  const parsed = semver.parse('1.2.3-alpha.1+build.42');
  assert.notEqual(parsed, null);
  assert.equal(parsed.major, 1);
  assert.equal(parsed.minor, 2);
  assert.equal(parsed.patch, 3);
  assert.deepEqual(parsed.prerelease, ['alpha', 1]);
  assert.deepEqual(parsed.build, ['build', '42']);
  assert.equal(parsed.version, '1.2.3-alpha.1');
  assert.equal(parsed.raw, '1.2.3-alpha.1+build.42');

  const helperParsed = parseVersion('2.0.1-rc.2');
  assert.notEqual(helperParsed, null);
  assert.equal(helperParsed.major, 2);
  assert.equal(helperParsed.minor, 0);
  assert.equal(helperParsed.patch, 1);
  assert.deepEqual(helperParsed.prerelease, ['rc', 2]);
}

// Contract 2: semver.parse handles loose formatting and leading characters when loose option is specified
{
  const looseParsed = semver.parse('  =v1.2.3  ', { loose: true });
  assert.notEqual(looseParsed, null);
  assert.equal(looseParsed.version, '1.2.3');
  assert.equal(looseParsed.major, 1);
  assert.equal(looseParsed.minor, 2);
  assert.equal(looseParsed.patch, 3);

  const helperLoose = parseVersion('v2.3.4', { loose: false });
  assert.notEqual(helperLoose, null);
  assert.equal(helperLoose.version, '2.3.4');
}

// Contract 3: semver.parse returns null for invalid semver strings
{
  assert.equal(semver.parse('not-a-valid-version'), null);
  assert.equal(semver.parse('1.2.3.4'), null);
  assert.equal(semver.parse(''), null);
  assert.equal(parseVersion(null), null);
  assert.equal(parseVersion(undefined), null);
}

// Contract 4: SemVer constructor creates instances and supports comparison and formatting methods
{
  const sv1 = new SemVer('1.2.3');
  const sv2 = createSemVer('2.0.0');

  assert.equal(sv1 instanceof SemVer, true);
  assert.equal(sv2 instanceof SemVer, true);
  assert.equal(sv1.format(), '1.2.3');
  assert.equal(sv2.format(), '2.0.0');

  assert.equal(sv1.compare(sv2), -1);
  assert.equal(sv2.compare(sv1), 1);
  assert.equal(sv1.compare('1.2.3'), 0);
}

// Contract 5: SemVer instance inc method increments release version components in place
{
  const sv = new SemVer('1.0.0');
  sv.inc('minor');
  assert.equal(sv.version, '1.1.0');
  assert.equal(sv.minor, 1);

  sv.inc('patch');
  assert.equal(sv.version, '1.1.1');
  assert.equal(sv.patch, 1);

  sv.inc('major');
  assert.equal(sv.version, '2.0.0');
  assert.equal(sv.major, 2);
}

console.log('semver.parse and SemVer contracts verified successfully.');

Origin Seeder

anonymous