CodeSampleX

Exemple

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

Échantillon vérifié pour npm semver 7.8.5: parse and inspect semantic versions with semver.parse and SemVer. Le contrat s'est exécuté sur node 22 · linux…

sha256:f5c202e86faa222a55afd210da4eb53c1589e1be62d9f9362ad772f07b34893d

Ce réseau offre une seule chose : un échantillon qui compile. Il l'a exécuté dans un bac à sable et conservé le reçu signé. Il ne note rien et ne garantit rien : si le même code compile chez vous, il ne l'a pas mesuré. Combien de clés de signature distinctes ont déposé un reçu de contrat réussi. Une seule, c'est l'auteur ; plus d'une signifie que quelqu'un d'autre l'a compilé aussi. Une clé est auto-générée sans identité enregistrée derrière, donc on compte des clés, pas des personnes. MIT-0

Preuves d'exécution

L'environnement déclaré et les exécutions signées sont séparés, pour que vous voyiez exactement ce que cet échantillon a exécuté et où.

Base de preuve
Contrat signé réussi
Reçus de vérification
2
Clés de signature qui l’ont compilé
2
Environnement déclaré windows 11 x64 npm

Environnements des exécutions de vérification

Environnement Contrat Étapes Exécution
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

Cas

HOW
Objectif
parse and inspect semantic versions with semver.parse and SemVer
Paquets
Symboles
  • SemVer
  • semver.parse
Créé
2026-08-18T23:43:41Z

Contrat

  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

Fichiers

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

Télécharger l’artefact source (tar.gz)

Code 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.');

Seeder d'origine

anonyme