CodeSampleX

Sample

semver 5.7.2: major

Verified sample for npm semver 5.7.2: major. The contract ran on node 22 · linux alpine/x64 · docker and passed: major returns integer major component for…

sha256:aa7cdb815f7800413330dcdebfbd1fe7452e6d0fc4f82496d8cf9e884f6eaf7c

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
1
Signing keys that built it
1
Declared environment node 22.23 linux 24 · ubuntu · glibc 2.39 x64 node 22.23 javascript npm 10

Verification-run environments

Environment Contract Stages Run
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-08-28

Case

HOW
Goal
verify semver.major in pkg:npm/semver@5.7.2
Packages
Symbols
  • semver.major
Environment
node 22.23.2
Created
2026-08-28T04:39:20Z

Contract

  1. major returns integer major component for standard semantic version strings
  2. major strips leading v prefix when extracting major version
  3. major extracts correct major version when prerelease identifiers and build metadata are present
  4. major throws TypeError for invalid version formats or non-string inputs
  5. major supports loose parsing mode to handle whitespace after v prefix

Files

  • PROMPT.md
  • csx.json
  • package-lock.json
  • package.json
  • spec.json
  • src/index.js
  • 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: verify semver.major in pkg:npm/semver@5.7.2
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/semver@5.7.2
Demonstrate these symbols/APIs:
  - semver.major

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:70a153538ef00d3003b2762aa53f5496ec9b95cc2b535db7304f602d3ca51a5b","contract":["major returns integer major component for standard semantic version strings","major strips leading v prefix when extracting major version","major extracts correct major version when prerelease identifiers and build metadata are present","major throws TypeError for invalid version formats or non-string inputs","major supports loose parsing mode to handle whitespace after v prefix"],"goal":"verify semver.major in pkg:npm/semver@5.7.2","kind":"HOW","packages":["pkg:npm/semver@5.7.2"],"schemaVersion":1,"symbols":["semver.major"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","language":"javascript","libc":"glibc","libcVersion":"2.39","moduleSystem":"cjs","os":"linux","osVersionBucket":"24","packageManager":"npm","packageManagerVersion":"10.9.8","runtime":"node","runtimeVersion":"22.23.2","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/semver@5.7.2"],"schemaVersion":1,"subject":"pkg:npm/semver@5.7.2","symbols":["semver.major"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "sample-semver-major",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-semver-major",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "semver": "5.7.2"
      }
    },
    "node_modules/semver": {
      "version": "5.7.2",
      "resolved": "https://registry.npmjs.org/semver/-/semver-5.7.2.tgz",
      "integrity": "sha512-cBznnQ9KjJqU67B52RMC65CMarK2600WFnbkcaiwWq3xy/5haFJlshgnpjovMVJ+Hff49d8GEn0b87C5pDQ10g==",
      "license": "ISC",
      "bin": {
        "semver": "bin/semver"
      }
    }
  }
}
package.json
{
  "name": "sample-semver-major",
  "version": "1.0.0",
  "private": true,
  "description": "Clean-room code sample for semver.major in semver@5.7.2",
  "main": "src/index.js",
  "license": "MIT-0",
  "dependencies": {
    "semver": "5.7.2"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify semver.major in pkg:npm/semver@5.7.2",
  "kind": "HOW",
  "packages": [
    "pkg:npm/semver@5.7.2"
  ],
  "symbols": [
    "semver.major"
  ]
}
src/index.js
const semver = require('semver');

/**
 * Extracts the major version number from a semantic version string.
 *
 * @param {string} version - The version string to extract major from.
 * @param {boolean|object} [loose=false] - Whether to allow loose parsing.
 * @returns {number} The integer major version.
 * @throws {TypeError} If the version string is invalid.
 */
function extractMajor(version, loose = false) {
  return semver.major(version, loose);
}

/**
 * Safely extracts the major version number without throwing.
 * Returns null if the version is invalid.
 *
 * @param {string} version - The version string to parse.
 * @param {boolean|object} [loose=false] - Whether to allow loose parsing.
 * @returns {number|null} The integer major version, or null if invalid.
 */
function safeExtractMajor(version, loose = false) {
  try {
    return semver.major(version, loose);
  } catch {
    return null;
  }
}

module.exports = {
  semver,
  major: semver.major,
  extractMajor,
  safeExtractMajor
};
test/contract.mjs
import assert from 'node:assert/strict';
import { createRequire } from 'node:module';

const require = createRequire(import.meta.url);
const {
  semver,
  major,
  extractMajor,
  safeExtractMajor
} = require('../src/index.js');

async function runContractTests() {
  // 1. Export verification
  assert.equal(typeof semver.major, 'function', 'semver.major must be a function');
  assert.equal(typeof major, 'function', 'major export must be a function');
  assert.equal(typeof extractMajor, 'function', 'extractMajor helper must be a function');
  assert.equal(typeof safeExtractMajor, 'function', 'safeExtractMajor helper must be a function');

  // 2. major returns integer major component for standard semantic version strings
  assert.equal(semver.major('1.2.3'), 1, 'major of 1.2.3 should be 1');
  assert.equal(major('2.4.10'), 2, 'major of 2.4.10 should be 2');
  assert.equal(extractMajor('0.1.0'), 0, 'extractMajor of 0.1.0 should be 0');
  assert.equal(extractMajor('10.20.30'), 10, 'extractMajor of 10.20.30 should be 10');
  assert.equal(safeExtractMajor('5.0.0'), 5, 'safeExtractMajor of 5.0.0 should be 5');

  // 3. major strips leading v prefix when extracting major version
  assert.equal(semver.major('v1.0.0'), 1, 'leading v should be stripped');
  assert.equal(major('v0.0.1'), 0, 'leading v should be stripped on 0.0.1');
  assert.equal(extractMajor('v3.14.159'), 3, 'leading v should be stripped in extractMajor');
  assert.equal(safeExtractMajor('v4.2.0'), 4, 'leading v should be stripped in safeExtractMajor');

  // 4. major extracts correct major version when prerelease identifiers and build metadata are present
  assert.equal(semver.major('3.4.5-alpha.1'), 3, 'prerelease tag should be ignored for major version');
  assert.equal(major('2.0.0-rc.2'), 2, 'rc prerelease tag should be ignored for major version');
  assert.equal(extractMajor('4.0.0+build.123'), 4, 'build metadata should be ignored for major version');
  assert.equal(extractMajor('1.2.3-beta.1+20130313144700'), 1, 'prerelease and build metadata ignored');
  assert.equal(safeExtractMajor('7.0.0-beta.5'), 7, 'prerelease tag handled in safeExtractMajor');

  // 5. major throws TypeError for invalid version formats or non-string inputs
  assert.throws(
    () => semver.major('not-a-version'),
    {
      name: 'TypeError',
      message: 'Invalid Version: not-a-version'
    },
    'invalid string should throw TypeError'
  );

  assert.throws(
    () => major('1.2'),
    {
      name: 'TypeError',
      message: 'Invalid Version: 1.2'
    },
    'incomplete two-part version should throw TypeError'
  );

  assert.throws(
    () => extractMajor(''),
    {
      name: 'TypeError',
      message: 'Invalid Version: '
    },
    'empty string should throw TypeError'
  );

  assert.throws(
    () => extractMajor(null),
    {
      name: 'TypeError',
      message: 'Invalid Version: null'
    },
    'null input should throw TypeError'
  );

  assert.throws(
    () => extractMajor(undefined),
    {
      name: 'TypeError',
      message: 'Invalid Version: undefined'
    },
    'undefined input should throw TypeError'
  );

  assert.throws(
    () => extractMajor(123),
    {
      name: 'TypeError',
      message: 'Invalid Version: 123'
    },
    'numeric input should throw TypeError'
  );

  assert.equal(safeExtractMajor('not-a-version'), null, 'safeExtractMajor should return null on invalid version');
  assert.equal(safeExtractMajor(null), null, 'safeExtractMajor should return null on null');

  // 6. major supports loose parsing mode to handle whitespace after v prefix
  assert.equal(semver.major('v 6.0.0', true), 6, 'loose mode should parse space after v');
  assert.equal(extractMajor('v 8.1.0', { loose: true }), 8, 'loose mode via options object should work');
  assert.equal(safeExtractMajor('v 9.0.0', true), 9, 'safeExtractMajor supports loose flag');

  assert.throws(
    () => semver.major('v 6.0.0', false),
    {
      name: 'TypeError',
      message: 'Invalid Version: v 6.0.0'
    },
    'strict mode should reject space after v'
  );

  assert.equal(safeExtractMajor('v 6.0.0', false), null, 'safeExtractMajor should return null for strict rejection');

  console.log('All semver.major contract tests passed successfully.');
}

runContractTests().catch((err) => {
  console.error('Contract test failure:', err);
  process.exit(1);
});

Origin Seeder

anonymous