CodeSampleX

Beispiel

semver 5.7.2: patch

Verifiziertes Beispiel für npm semver 5.7.2: patch. Der Vertrag lief auf node 22 · linux alpine/x64 · docker und bestand: patch returns integer patch number…

sha256:7c7794f75e4ce921fd5aff40cd4555fb28ef726deb61a0e9abf17617b595aeed

Dieses Netzwerk bietet eine Sache: ein Sample, das baut. Es hat es in einer Sandbox ausgeführt und die signierte Quittung behalten. Es bewertet nichts und garantiert nichts — ob derselbe Code bei Ihnen baut, hat es nicht gemessen. Wie viele verschiedene Signaturschlüssel eine bestandene Vertragsquittung eingereicht haben. Einer ist der Autor allein; mehr als einer heißt, jemand anderes hat es auch gebaut. Ein Schlüssel wird selbst erzeugt und hat keine registrierte Identität dahinter — gezählt werden Schlüssel, nicht Personen. MIT-0

Ausführungsbelege

Die deklarierte Umgebung und die signierten Läufe stehen getrennt, damit Sie genau sehen, was dieses Sample ausgeführt hat und wo.

Beleggrundlage
Signierter Vertrag bestanden
Verifizierungsbelege
1
Signaturschlüssel, die es gebaut haben
1
Deklarierte Umgebung node 22.23 linux 24 · ubuntu · glibc 2.39 x64 node 22.23 javascript npm 10

Umgebungen der Verifizierungsläufe

Umgebung Contract Stufen Lauf
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

Fall

HOW
Ziel
verify semver.patch in pkg:npm/semver@5.7.2
Pakete
Symbole
  • semver.patch
Umgebung
node 22.23.2
Erstellt
2026-08-28T04:43:25Z

Contract

  1. patch returns integer patch number for valid semantic version strings
  2. patch handles leading v prefix and prerelease or build metadata
  3. patch throws TypeError for invalid version formats in strict mode
  4. patch parses loose version strings when loose mode is enabled
  5. patch accepts SemVer instance objects directly

Dateien

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

Quellartefakt herunterladen (tar.gz)

Quelltext

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.patch 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.patch
Constraints:
  - executionContext: node
Required runtime conditions:
  - ecosystem: npm
  - language: javascript
  - moduleSystem: cjs
  - packageManager: npm@10.9.8
  - runtime: node@22.23.2

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:ee7d070d9a6a0d592391d21e6e418d5f088b10c2c8b69440fd29ecf16f831cf4","constraints":{"executionContext":"node"},"contract":["patch returns integer patch number for valid semantic version strings","patch handles leading v prefix and prerelease or build metadata","patch throws TypeError for invalid version formats in strict mode","patch parses loose version strings when loose mode is enabled","patch accepts SemVer instance objects directly"],"goal":"verify semver.patch in pkg:npm/semver@5.7.2","kind":"HOW","packages":["pkg:npm/semver@5.7.2"],"schemaVersion":1,"symbols":["semver.patch"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","executionContext":"node","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.patch"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "sample-semver-patch",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-semver-patch",
      "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-patch",
  "version": "1.0.0",
  "private": true,
  "description": "Clean-room code sample for semver.patch in semver@5.7.2",
  "main": "src/index.js",
  "license": "MIT-0",
  "dependencies": {
    "semver": "5.7.2"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify semver.patch in pkg:npm/semver@5.7.2",
  "kind": "HOW",
  "packages": [
    "pkg:npm/semver@5.7.2"
  ],
  "symbols": [
    "semver.patch"
  ],
  "constraints": {
    "executionContext": "node"
  },
  "runtimeConditions": {
    "ecosystem": "npm",
    "language": "javascript",
    "moduleSystem": "cjs",
    "packageManager": "npm@10.9.8",
    "runtime": "node@22.23.2"
  }
}
src/index.js
const semver = require('semver');

/**
 * Returns the patch version number for a valid semantic version string or SemVer instance.
 * Throws a TypeError if the version string is invalid.
 *
 * @param {string|semver.SemVer} version - Version string or SemVer instance.
 * @param {boolean|object} [loose=false] - Whether to allow loose parsing.
 * @returns {number} The patch version integer.
 */
function extractPatch(version, loose = false) {
  return semver.patch(version, loose);
}

/**
 * Safely extracts the patch version number, returning null instead of throwing on invalid versions.
 *
 * @param {string|semver.SemVer} version - Version string or SemVer instance.
 * @param {boolean|object} [loose=false] - Whether to allow loose parsing.
 * @returns {number|null} The patch version number or null if invalid.
 */
function getPatchSafe(version, loose = false) {
  try {
    return semver.patch(version, loose);
  } catch {
    return null;
  }
}

/**
 * Checks whether a given version has a specific patch version number.
 *
 * @param {string|semver.SemVer} version - Version string or SemVer instance.
 * @param {number} expectedPatch - The expected patch version.
 * @param {boolean|object} [loose=false] - Whether to allow loose parsing.
 * @returns {boolean} True if the patch version matches expectedPatch, false otherwise.
 */
function isPatchEqual(version, expectedPatch, loose = false) {
  const patchNum = getPatchSafe(version, loose);
  return patchNum !== null && patchNum === expectedPatch;
}

module.exports = {
  semver,
  patch: semver.patch,
  extractPatch,
  getPatchSafe,
  isPatchEqual
};
test/contract.mjs
import assert from 'node:assert/strict';
import { createRequire } from 'node:module';

const require = createRequire(import.meta.url);
const {
  semver,
  patch,
  extractPatch,
  getPatchSafe,
  isPatchEqual
} = require('../src/index.js');

async function runContractTests() {
  // 1. patch returns integer patch number for valid semantic version strings
  assert.equal(typeof semver.patch, 'function', 'semver.patch must be a function');
  assert.equal(typeof patch, 'function', 'patch must be a function');
  assert.equal(patch('1.2.3'), 3, 'patch of 1.2.3 should be 3');
  assert.equal(patch('0.0.0'), 0, 'patch of 0.0.0 should be 0');
  assert.equal(patch('10.20.30'), 30, 'patch of 10.20.30 should be 30');
  assert.equal(extractPatch('2.4.10'), 10, 'extractPatch helper should return 10');
  assert.equal(getPatchSafe('1.0.5'), 5, 'getPatchSafe should return 5 for valid version');
  assert.equal(isPatchEqual('1.0.5', 5), true, 'isPatchEqual should return true for matching patch');
  assert.equal(isPatchEqual('1.0.5', 4), false, 'isPatchEqual should return false for non-matching patch');

  // 2. patch handles leading v prefix and prerelease or build metadata
  assert.equal(patch('v1.2.3'), 3, 'leading v prefix should return patch number 3');
  assert.equal(patch('v0.0.1'), 1, 'leading v prefix on 0.0.1 should return patch number 1');
  assert.equal(patch('1.2.3-alpha.1'), 3, 'prerelease version should return patch number 3');
  assert.equal(patch('2.0.0-rc.2'), 0, 'prerelease version 2.0.0-rc.2 should return patch number 0');
  assert.equal(patch('1.2.3+build.456'), 3, 'build metadata should return patch number 3');
  assert.equal(patch('1.2.3-beta.1+20130313144700'), 3, 'prerelease and build metadata should return patch number 3');

  // 3. patch throws TypeError for invalid version formats in strict mode
  assert.throws(() => patch('not-a-version'), TypeError, 'invalid string should throw TypeError');
  assert.throws(() => patch('1.2'), TypeError, 'incomplete version 1.2 should throw TypeError');
  assert.throws(() => patch('1.2.3.4'), TypeError, 'four-part version should throw TypeError');
  assert.throws(() => patch(''), TypeError, 'empty string should throw TypeError');
  assert.throws(() => patch(null), TypeError, 'null input should throw TypeError');
  assert.throws(() => patch(undefined), TypeError, 'undefined input should throw TypeError');
  assert.throws(() => patch(123), TypeError, 'numeric input should throw TypeError');
  assert.equal(getPatchSafe('invalid'), null, 'getPatchSafe should return null for invalid string');
  assert.equal(isPatchEqual('invalid', 0), false, 'isPatchEqual should return false for invalid string');

  // 4. patch parses loose version strings when loose mode is enabled
  assert.equal(patch('v 1.2.3', true), 3, 'loose mode should handle space after v');
  assert.throws(() => patch('v 1.2.3', false), TypeError, 'strict mode should throw for space after v');
  assert.equal(patch('v 2.0.4', { loose: true }), 4, 'loose mode via options object should return 4');
  assert.equal(extractPatch('v 3.0.7', true), 7, 'extractPatch helper should pass loose flag');
  assert.equal(getPatchSafe('v 1.0.8', true), 8, 'getPatchSafe helper should support loose flag');
  assert.equal(isPatchEqual('v 1.0.8', 8, true), true, 'isPatchEqual helper should support loose flag');

  // 5. patch accepts SemVer instance objects directly
  const semverObj = new semver.SemVer('4.5.6');
  assert.equal(patch(semverObj), 6, 'patch should accept SemVer instance and return 6');
  assert.equal(getPatchSafe(semverObj), 6, 'getPatchSafe should accept SemVer instance and return 6');

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

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

Ursprungs-Seeder

anonym