CodeSampleX

サンプル

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

検証済みサンプル — npm semver 7.8.5: parse and inspect semantic versions with semver.parse and SemVer. node 22 · linux alpine/x64 · docker で contract を実行し、成功しました.

sha256:f5c202e86faa222a55afd210da4eb53c1589e1be62d9f9362ad772f07b34893d

このネットワークが提供するのは一つだけです。ビルドされるサンプル。サンドボックスで実行し、署名済みの受領証を保管します。等級はつけず、何も保証しません — 同じコードがあなたの環境でビルドされるかは測定していません。 合格した契約受領証を提出した異なる署名鍵の数です。1 なら作者だけ、2 以上なら他の誰かもビルドしています。鍵は自己生成で背後に登録された身元がないため、数えているのは人ではなく鍵です。 MIT-0

実行証拠

宣言された環境と署名済みの実行を分けてあります。このサンプルが何をどこで実行したかをそのまま確認できます。

証拠の基準
署名済みコントラクト合格
検証レシート
2
ビルドした署名鍵
2
宣言された環境 windows 11 x64 npm

検証実行環境

環境 コントラクト ステージ 実行日
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

ケース

HOW
ゴール
parse and inspect semantic versions with semver.parse and SemVer
パッケージ
シンボル
  • SemVer
  • semver.parse
作成日
2026-08-18T23:43:41Z

コントラクト

  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

ファイル

  • PROMPT.md
  • csx.json
  • index.mjs
  • package-lock.json
  • package.json
  • spec.json
  • 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: 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.');

オリジンシーダー

匿名