CodeSampleX

サンプル

semver 7.8.5: coerce loose version strings into valid semver with semver.coerce

検証済みサンプル — npm semver 7.8.5: coerce loose version strings into valid semver with semver.coerce. node 22 · linux alpine/x64 · docker で contract を実行し、成功しました.

sha256:e12c401ab9754a0e13f9dd7e06ce765eb0ec770294e2d37692c7bde116e07b8f

このネットワークが提供するのは一つだけです。ビルドされるサンプル。サンドボックスで実行し、署名済みの受領証を保管します。等級はつけず、何も保証しません — 同じコードがあなたの環境でビルドされるかは測定していません。 合格した契約受領証を提出した異なる署名鍵の数です。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
ゴール
coerce loose version strings into valid semver with semver.coerce
パッケージ
シンボル
  • semver.coerce
作成日
2026-08-18T22:37:09Z

コントラクト

  1. semver.coerce parses short or partial version strings into valid SemVer objects with zero-filled minor and patch
  2. semver.coerce extracts valid SemVer versions from strings containing surrounding text and multiple version segments
  3. semver.coerce supports right-to-left parsing with the rtl option
  4. semver.coerce preserves prerelease tags when includePrerelease option is true
  5. semver.coerce returns null for non-version strings that contain no valid numeric version pattern

ファイル

  • 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: coerce loose version strings into valid semver with semver.coerce
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/semver@7.8.5
Demonstrate these symbols/APIs:
  - semver.coerce
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:afbf314f3cac02c3147715d2bcc1090bf6b613ff44f03b15fe035384f2b70e06","constraints":{"executionContext":"node"},"contract":["semver.coerce parses short or partial version strings into valid SemVer objects with zero-filled minor and patch","semver.coerce extracts valid SemVer versions from strings containing surrounding text and multiple version segments","semver.coerce supports right-to-left parsing with the rtl option","semver.coerce preserves prerelease tags when includePrerelease option is true","semver.coerce returns null for non-version strings that contain no valid numeric version pattern"],"goal":"coerce loose version strings into valid semver with semver.coerce","kind":"HOW","packages":["pkg:npm/semver@7.8.5"],"schemaVersion":1,"symbols":["semver.coerce"]},"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.coerce"],"verifierAdapter":"node-typescript@1"}
index.mjs
import semver from 'semver';

/**
 * Coerces a string or number into a SemVer object if possible.
 * @param {string | number} input
 * @param {semver.CoerceOptions} [options]
 * @returns {semver.SemVer | null}
 */
export function coerceVersion(input, options) {
  return semver.coerce(input, options);
}

export default {
  coerceVersion
};
package-lock.json
{
  "name": "sample-semver-coerce",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-semver-coerce",
      "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-coerce",
  "version": "1.0.0",
  "type": "module",
  "dependencies": {
    "semver": "7.8.5"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "coerce loose version strings into valid semver with semver.coerce",
  "kind": "HOW",
  "packages": [
    "pkg:npm/semver@7.8.5"
  ],
  "symbols": [
    "semver.coerce"
  ],
  "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 from 'semver';
import { coerceVersion } from '../index.mjs';

// Verify semver.coerce function contract
assert.equal(typeof semver.coerce, 'function', 'semver.coerce must be a function');
assert.equal(typeof coerceVersion, 'function', 'coerceVersion must be a function');

// Contract 1: semver.coerce parses short or partial version strings into valid SemVer objects with zero-filled minor and patch
{
  const coercedV2 = semver.coerce('v2');
  assert.notEqual(coercedV2, null);
  assert.equal(coercedV2.version, '2.0.0');
  assert.equal(coercedV2.major, 2);
  assert.equal(coercedV2.minor, 0);
  assert.equal(coercedV2.patch, 0);

  const coercedMinor = semver.coerce('1.2');
  assert.notEqual(coercedMinor, null);
  assert.equal(coercedMinor.version, '1.2.0');

  const coercedNum = semver.coerce(42);
  assert.notEqual(coercedNum, null);
  assert.equal(coercedNum.version, '42.0.0');

  const helperResult = coerceVersion('v3.5');
  assert.notEqual(helperResult, null);
  assert.equal(helperResult.version, '3.5.0');
}

// Contract 2: semver.coerce extracts valid SemVer versions from strings containing surrounding text and multiple version segments
{
  const coercedPrefix = semver.coerce('release-2.4.1.8');
  assert.notEqual(coercedPrefix, null);
  assert.equal(coercedPrefix.version, '2.4.1');

  const coercedText = semver.coerce('app version 3.14.15 build 99');
  assert.notEqual(coercedText, null);
  assert.equal(coercedText.version, '3.14.15');

  const helperText = coerceVersion('build-v1.0.4-rc1');
  assert.notEqual(helperText, null);
  assert.equal(helperText.version, '1.0.4');
}

// Contract 3: semver.coerce supports right-to-left parsing with the rtl option
{
  const standardLtr = semver.coerce('1.2.3.4');
  assert.notEqual(standardLtr, null);
  assert.equal(standardLtr.version, '1.2.3');

  const parsedRtl = semver.coerce('1.2.3.4', { rtl: true });
  assert.notEqual(parsedRtl, null);
  assert.equal(parsedRtl.version, '2.3.4');

  const helperRtl = coerceVersion('release-1.0.0-final-2.0.0', { rtl: true });
  assert.notEqual(helperRtl, null);
  assert.equal(helperRtl.version, '2.0.0');
}

// Contract 4: semver.coerce preserves prerelease tags when includePrerelease option is true
{
  const strippedPrerelease = semver.coerce('1.2.3-alpha.1');
  assert.notEqual(strippedPrerelease, null);
  assert.equal(strippedPrerelease.version, '1.2.3');
  assert.deepEqual(strippedPrerelease.prerelease, []);

  const preservedPrerelease = semver.coerce('1.2.3-alpha.1', { includePrerelease: true });
  assert.notEqual(preservedPrerelease, null);
  assert.equal(preservedPrerelease.raw, '1.2.3-alpha.1');
  assert.deepEqual(preservedPrerelease.prerelease, ['alpha', 1]);

  const helperPrerelease = coerceVersion('2.0.0-beta.2', { includePrerelease: true });
  assert.notEqual(helperPrerelease, null);
  assert.equal(helperPrerelease.raw, '2.0.0-beta.2');
}

// Contract 5: semver.coerce returns null for non-version strings that contain no valid numeric version pattern
{
  assert.equal(semver.coerce('invalid-string'), null);
  assert.equal(semver.coerce(''), null);
  assert.equal(semver.coerce('no-digits-here'), null);
  assert.equal(coerceVersion('xyz'), null);
}

console.log('semver.coerce contract verified successfully.');

オリジンシーダー

匿名