CodeSampleX

示例

semver 7.8.5: compare versions including build metadata with semver.compareBuild

已验证示例 — npm semver 7.8.5: compare versions including build metadata with semver.compareBuild. contract 在 node 22 · linux alpine/x64 · docker 上运行并通过.

sha256:89761a13ea0a6acf75bfb216aacb98301ac7af23a5cc0a17da684ecf7bc97d11

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。 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
目标
compare versions including build metadata with semver.compareBuild
包
符号
  • compareBuild
创建时间
2026-08-19T11:10:44Z

契约

  1. semver.compareBuild('1.0.0+1', '1.0.0+2') === -1
  2. semver.compareBuild('1.0.0+2', '1.0.0+1') === 1
  3. semver.compareBuild('1.0.0+build', '1.0.0+build') === 0
  4. semver.compareBuild('1.0.0', '1.0.0+build') === -1
  5. semver.compareBuild('1.0.0+build', '1.0.0') === 1
  6. semver.compareBuild('1.0.0', '2.0.0') === -1
  7. semver.compareBuild('1.0.0-alpha', '1.0.0-beta') === -1

文件

  • PROMPT.md
  • csx.json
  • package-lock.json
  • package.json
  • spec.json
  • src/index.mjs
  • 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: compare versions including build metadata with semver.compareBuild
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/semver@7.8.5
Demonstrate these symbols/APIs:
  - compareBuild

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:4bb581c36a828265dc803ceca6b1314322fd3ab877100076b488880981ebda93","contract":["semver.compareBuild('1.0.0+1', '1.0.0+2') === -1","semver.compareBuild('1.0.0+2', '1.0.0+1') === 1","semver.compareBuild('1.0.0+build', '1.0.0+build') === 0","semver.compareBuild('1.0.0', '1.0.0+build') === -1","semver.compareBuild('1.0.0+build', '1.0.0') === 1","semver.compareBuild('1.0.0', '2.0.0') === -1","semver.compareBuild('1.0.0-alpha', '1.0.0-beta') === -1"],"goal":"compare versions including build metadata with semver.compareBuild","kind":"HOW","packages":["pkg:npm/semver@7.8.5"],"schemaVersion":1,"symbols":["compareBuild"]},"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":["compareBuild"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "sample-semver-compare-build",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-semver-compare-build",
      "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-compare-build",
  "version": "1.0.0",
  "type": "module",
  "private": true,
  "dependencies": {
    "semver": "7.8.5"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "compare versions including build metadata with semver.compareBuild",
  "kind": "HOW",
  "packages": [
    "pkg:npm/semver@7.8.5"
  ],
  "symbols": [
    "compareBuild"
  ]
}
src/index.mjs
import semver from 'semver';

export function compareWithBuild(v1, v2) {
  return semver.compareBuild(v1, v2);
}
test/contract.mjs
import assert from 'node:assert/strict';
import semver from 'semver';
import { compareWithBuild } from '../src/index.mjs';

// Exact symbol assertion: semver.compareBuild
assert.equal(typeof semver.compareBuild, 'function');

// Contract: compareBuild differentiates versions with different build metadata
assert.equal(semver.compareBuild('1.0.0+1', '1.0.0+2'), -1);
assert.equal(semver.compareBuild('1.0.0+2', '1.0.0+1'), 1);
assert.equal(semver.compareBuild('1.0.0+build', '1.0.0+build'), 0);

// Contract: compareBuild treats version without build metadata as lower than version with build metadata
assert.equal(semver.compareBuild('1.0.0', '1.0.0+build'), -1);
assert.equal(semver.compareBuild('1.0.0+build', '1.0.0'), 1);

// Contract: compareBuild respects standard major, minor, patch, and prerelease precedence
assert.equal(semver.compareBuild('1.0.0', '2.0.0'), -1);
assert.equal(semver.compareBuild('2.0.0', '1.0.0'), 1);
assert.equal(semver.compareBuild('1.0.0', '1.0.0'), 0);
assert.equal(semver.compareBuild('1.0.0-alpha', '1.0.0-beta'), -1);

// Contract: helper function wraps semver.compareBuild
assert.equal(compareWithBuild('1.0.0+1', '1.0.0+2'), -1);
assert.equal(compareWithBuild('1.0.0+build', '1.0.0+build'), 0);

console.log('Contract passed');

原始种子者

匿名