CodeSampleX

示例

semver 7.8.5: Check version satisfaction against ranges, coerce strings into versions, determine range minimum versions, test boundary relations with gtr, ltr, and outside, and extract prerelease components with semver

已验证示例 — npm semver 7.8.5: Check version satisfaction against ranges, coerce strings into versions, determine range minimum versions, test boundary relations…

sha256:b7cfd82b032190b07bc561320642a4e4d9ad350b26cdc8f208b45aac52ee2b99

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。 MIT-0

执行证据

声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。

证据依据
签名契约通过
验证回执
2
构建过它的签名密钥
2
声明的环境 node 22.22 linux 24 · ubuntu · glibc 2.39 x64 node 22.22 javascript npm 11 wsl

验证运行环境

环境 契约 阶段 运行日期
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 debian/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1node:22@sha256:8a34c4ab3ea2…
2026-09-07

案例

HOW
目标
Check version satisfaction against ranges, coerce strings into versions, determine range minimum versions, test boundary relations with gtr, ltr, and outside, and extract prerelease components with semver
包
符号
  • coerce
  • gtr
  • ltr
  • minVersion
  • outside
  • prerelease
  • satisfies
环境
node 22.22.2
创建时间
2026-08-19T08:23:36Z

契约

  1. assert semver.satisfies verifies whether a version satisfies a range expression, including prerelease options
  2. assert semver.coerce parses loose or dirty strings into SemVer objects with standard and right-to-left scanning
  3. assert semver.minVersion determines the lowest version that can satisfy a given range comparator
  4. assert semver.gtr and semver.ltr test whether a version is strictly greater than or less than all versions in a range
  5. assert semver.outside determines whether a version lies above or below range bounds
  6. assert semver.prerelease extracts array components of prerelease identifiers or returns null for release versions

文件

  • 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: Check version satisfaction against ranges, coerce strings into versions, determine range minimum versions, test boundary relations with gtr, ltr, and outside, and extract prerelease components with semver
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/semver@7.8.5
Demonstrate these symbols/APIs:
  - coerce
  - gtr
  - ltr
  - minVersion
  - outside
  - prerelease
  - satisfies
Constraints:
  - executionContext: node
Required runtime conditions:
  - ecosystem: npm
  - language: javascript
  - moduleSystem: cjs
  - packageManager: npm@11.8.0
  - runtime: node@22.22.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:058e569ea1b7f6b145be5ca91a0e132d81bd231b90f3280d7fe03eb7ae82db8c","constraints":{"executionContext":"node"},"contract":["assert semver.satisfies verifies whether a version satisfies a range expression, including prerelease options","assert semver.coerce parses loose or dirty strings into SemVer objects with standard and right-to-left scanning","assert semver.minVersion determines the lowest version that can satisfy a given range comparator","assert semver.gtr and semver.ltr test whether a version is strictly greater than or less than all versions in a range","assert semver.outside determines whether a version lies above or below range bounds","assert semver.prerelease extracts array components of prerelease identifiers or returns null for release versions"],"goal":"Check version satisfaction against ranges, coerce strings into versions, determine range minimum versions, test boundary relations with gtr, ltr, and outside, and extract prerelease components with semver","kind":"HOW","packages":["pkg:npm/semver@7.8.5"],"schemaVersion":1,"symbols":["coerce","gtr","ltr","minVersion","outside","prerelease","satisfies"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","executionContext":"node","language":"javascript","libc":"glibc","libcVersion":"2.39","moduleSystem":"esm","os":"linux","osVersionBucket":"24","packageManager":"npm","packageManagerVersion":"11.8.0","runtime":"node","runtimeVersion":"22.22.2","schemaVersion":1,"virtualization":"wsl"},"license":"MIT-0","packages":["pkg:npm/semver@7.8.5"],"schemaVersion":1,"symbols":["coerce","gtr","ltr","minVersion","outside","prerelease","satisfies"],"verifierAdapter":"node-typescript@1"}
index.mjs
import semver from 'semver';

/**
 * Check if a version satisfies a SemVer range.
 */
export function checkSatisfies(version, range, options = {}) {
  return semver.satisfies(version, range, options);
}

/**
 * Coerce an arbitrary string into a SemVer object.
 */
export function coerceVersion(input, options = {}) {
  return semver.coerce(input, options);
}

/**
 * Find the minimum version that satisfies the given range.
 */
export function getMinVersion(range, options = {}) {
  return semver.minVersion(range, options);
}

/**
 * Check if a version is greater than every version in the range.
 */
export function isGreaterThanRange(version, range, options = {}) {
  return semver.gtr(version, range, options);
}

/**
 * Check if a version is less than every version in the range.
 */
export function isLessThanRange(version, range, options = {}) {
  return semver.ltr(version, range, options);
}

/**
 * Check if a version is outside a range on the given side ('>' or '<').
 */
export function isOutsideRange(version, range, hilo, options = {}) {
  return semver.outside(version, range, hilo, options);
}

/**
 * Extract prerelease components as an array or null.
 */
export function getPrerelease(version, options = {}) {
  return semver.prerelease(version, options);
}
package-lock.json
{
  "name": "sample",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample",
      "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=="
    }
  }
}
package.json
{
  "name": "sample",
  "version": "1.0.0",
  "type": "module",
  "dependencies": {
    "semver": "7.8.5"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "Check version satisfaction against ranges, coerce strings into versions, determine range minimum versions, test boundary relations with gtr, ltr, and outside, and extract prerelease components with semver",
  "kind": "HOW",
  "packages": [
    "pkg:npm/semver@7.8.5"
  ],
  "symbols": [
    "coerce",
    "gtr",
    "ltr",
    "minVersion",
    "outside",
    "prerelease",
    "satisfies"
  ],
  "constraints": {
    "executionContext": "node"
  },
  "runtimeConditions": {
    "ecosystem": "npm",
    "language": "javascript",
    "moduleSystem": "cjs",
    "packageManager": "npm@11.8.0",
    "runtime": "node@22.22.2"
  }
}
test/contract.mjs
import assert from 'node:assert/strict';
import semver from 'semver';
import {
  checkSatisfies,
  coerceVersion,
  getMinVersion,
  isGreaterThanRange,
  isLessThanRange,
  isOutsideRange,
  getPrerelease
} from '../index.mjs';

// 1. assert semver.satisfies verifies whether a version satisfies a range expression, including prerelease options
{
  assert.equal(checkSatisfies('1.2.3', '^1.2.0'), true);
  assert.equal(checkSatisfies('1.3.0', '~1.2.0'), false);
  assert.equal(checkSatisfies('2.0.0', '>=1.0.0 <3.0.0'), true);
  assert.equal(checkSatisfies('1.2.3-alpha.1', '^1.2.0'), false);
  assert.equal(checkSatisfies('1.2.3-alpha.1', '^1.2.0', { includePrerelease: true }), true);
  assert.equal(checkSatisfies('invalid', '^1.0.0'), false);
}

// 2. assert semver.coerce parses loose or dirty strings into SemVer objects with standard and right-to-left scanning
{
  const standard = coerceVersion('v2');
  assert.ok(standard instanceof semver.SemVer);
  assert.equal(standard.version, '2.0.0');

  const dirty = coerceVersion('package 42.6.7.9-rc.1');
  assert.equal(dirty.version, '42.6.7');

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

  assert.equal(coerceVersion('no-version-here'), null);
}

// 3. assert semver.minVersion determines the lowest version that can satisfy a given range comparator
{
  const minGte = getMinVersion('>=1.2.3 <2.0.0');
  assert.ok(minGte instanceof semver.SemVer);
  assert.equal(minGte.version, '1.2.3');

  const minGt = getMinVersion('>1.2.3');
  assert.equal(minGt.version, '1.2.4');

  const minCaret = getMinVersion('^2.1.0');
  assert.equal(minCaret.version, '2.1.0');

  const minLessThan = getMinVersion('<1.0.0');
  assert.equal(minLessThan.version, '0.0.0');

  assert.equal(getMinVersion('>1.0.0 <1.0.0'), null);
}

// 4. assert semver.gtr and semver.ltr test whether a version is strictly greater than or less than all versions in a range
{
  assert.equal(isGreaterThanRange('2.0.0', '^1.0.0'), true);
  assert.equal(isGreaterThanRange('1.5.0', '^1.0.0'), false);
  assert.equal(isGreaterThanRange('0.9.0', '^1.0.0'), false);

  assert.equal(isLessThanRange('0.9.0', '^1.0.0'), true);
  assert.equal(isLessThanRange('1.5.0', '^1.0.0'), false);
  assert.equal(isLessThanRange('2.0.0', '^1.0.0'), false);
}

// 5. assert semver.outside determines whether a version lies above or below range bounds
{
  assert.equal(isOutsideRange('2.0.0', '^1.0.0', '>'), true);
  assert.equal(isOutsideRange('2.0.0', '^1.0.0', '<'), false);
  assert.equal(isOutsideRange('0.5.0', '^1.0.0', '<'), true);
  assert.equal(isOutsideRange('0.5.0', '^1.0.0', '>'), false);
  assert.equal(isOutsideRange('1.5.0', '^1.0.0', '>'), false);
  assert.equal(isOutsideRange('1.5.0', '^1.0.0', '<'), false);
}

// 6. assert semver.prerelease extracts array components of prerelease identifiers or returns null for release versions
{
  assert.deepEqual(getPrerelease('1.2.3-alpha.1'), ['alpha', 1]);
  assert.deepEqual(getPrerelease('2.0.0-beta'), ['beta']);
  assert.deepEqual(getPrerelease('3.0.0-rc.2.exp.4'), ['rc', 2, 'exp', 4]);
  assert.equal(getPrerelease('1.2.3'), null);
  assert.equal(getPrerelease('1.2.3+build.123'), null);
  assert.equal(getPrerelease('invalid'), null);
}

console.log('PASS: all contract assertions satisfied');

原始种子者

匿名