CodeSampleX

샘플

semver 5.7.2: patch

검증된 샘플 — npm semver 5.7.2: patch. node 22 · linux alpine/x64 · docker에서 contract를 실행해 통과했습니다: patch returns integer patch number for valid semantic version…

sha256:7c7794f75e4ce921fd5aff40cd4555fb28ef726deb61a0e9abf17617b595aeed

이 네트워크가 제공하는 것은 하나입니다. 빌드되는 샘플. 샌드박스에서 돌리고 서명된 영수증을 보관합니다. 등급을 매기지 않고 무엇도 보증하지 않습니다 — 같은 코드가 당신 환경에서 빌드되는지는 측정한 적이 없습니다. 통과한 계약 영수증을 낸 서로 다른 서명 키의 수입니다. 하나면 작성자 혼자이고, 둘 이상이면 다른 사람도 빌드했다는 뜻입니다. 키는 스스로 만드는 것이고 뒤에 등록된 신원이 없으므로, 세는 것은 사람이 아니라 키입니다. MIT-0

실행 증거

선언된 환경과 서명된 실행을 분리해 두었습니다. 이 샘플이 무엇을 어디서 실행했는지 그대로 볼 수 있습니다.

증거 기준
서명된 컨트랙트 통과
검증 영수증
1
빌드한 서명 키
1
선언된 환경 node 22.23 linux 24 · ubuntu · glibc 2.39 x64 node 22.23 javascript npm 10

검증 실행 환경

환경 컨트랙트 단계 실행일
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

케이스

HOW
목표
verify semver.patch in pkg:npm/semver@5.7.2
패키지
심벌
  • semver.patch
환경
node 22.23.2
생성일
2026-08-28T04:43:25Z

컨트랙트

  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

파일

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

소스 아티팩트 내려받기 (tar.gz)

소스

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);
});

오리진 시더

익명