サンプル
semver 7.8.5: compare version numbers with semver.cmp
検証済みサンプル — npm semver 7.8.5: compare version numbers with semver.cmp. node 22 · linux alpine/x64 · docker で contract を実行し、成功しました.
sha256:206eb265ef78d9254422b731e0e5bf637c04d6a20ee046953e44937c3cd2e709
このネットワークが提供するのは一つだけです。ビルドされるサンプル。サンドボックスで実行し、署名済みの受領証を保管します。等級はつけず、何も保証しません — 同じコードがあなたの環境でビルドされるかは測定していません。
合格した契約受領証を提出した異なる署名鍵の数です。1 なら作者だけ、2 以上なら他の誰かもビルドしています。鍵は自己生成で背後に登録された身元がないため、数えているのは人ではなく鍵です。
MIT-0
実行証拠
宣言された環境と署名済みの実行を分けてあります。このサンプルが何をどこで実行したかをそのまま確認できます。
- 証拠の基準
- 署名済みコントラクト合格
- 検証レシート
- 2
- ビルドした署名鍵
- 2
宣言された環境
node 22 windows 11 x64 node 22 javascript npm 10
検証実行環境
| 環境 | コントラクト | ステージ | 実行日 |
|---|---|---|---|
| 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 version numbers with semver.cmp
- パッケージ
- シンボル
-
- cmp
- 環境
- node 22
- 作成日
- 2026-08-19T07:42:16Z
コントラクト
- semver.cmp correctly evaluates equality operators (=, ==, ===, !=, !==)
- semver.cmp correctly evaluates relational operators (<, <=, >, >=)
- semver.cmp correctly compares prerelease versions
- semver.cmp throws TypeError for invalid comparator operators
- compareVersions wrapper correctly compares versions using comparison operators
ファイル
- PROMPT.md
- csx.json
- index.js
- package-lock.json
- package.json
- spec.json
- test/contract.js
ソース
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 version numbers with semver.cmp
Kind: HOW
Use EXACTLY these public packages and versions:
- pkg:npm/semver@7.8.5
Demonstrate these symbols/APIs:
- cmp
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.
{"case":{"caseId":"case:sha256:32d5b739a4649eeebada11fdc9288b6ff99427265eb29691259601898a2013e8","constraints":{"executionContext":"node"},"contract":["semver.cmp correctly evaluates equality operators (=, ==, ===, !=, !==)","semver.cmp correctly evaluates relational operators (\u003c, \u003c=, \u003e, \u003e=)","semver.cmp correctly compares prerelease versions","semver.cmp throws TypeError for invalid comparator operators","compareVersions wrapper correctly compares versions using comparison operators"],"goal":"compare version numbers with semver.cmp","kind":"HOW","packages":["pkg:npm/semver@7.8.5"],"schemaVersion":1,"symbols":["cmp"]},"contractCommand":["node","test/contract.js"],"environment":{"arch":"x64","ecosystem":"npm","executionContext":"node","language":"javascript","moduleSystem":"cjs","os":"windows","osVersionBucket":"11","packageManager":"npm","packageManagerVersion":"10","runtime":"node","runtimeVersion":"22","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/semver@7.8.5"],"schemaVersion":1,"symbols":["cmp"],"verifierAdapter":"node-typescript@1"}
const semver = require('semver');
/**
* Compares two semantic version strings using a specified operator.
*
* @param {string} v1 - First semantic version
* @param {string} operator - Comparison operator ('<', '<=', '>', '>=', '==', '===', '!=', '!==')
* @param {string} v2 - Second semantic version
* @param {boolean|object} [optionsOrLoose] - Optional loose boolean or semver options
* @returns {boolean} Result of the comparison
*/
function compareVersions(v1, operator, v2, optionsOrLoose) {
return semver.cmp(v1, operator, v2, optionsOrLoose);
}
module.exports = {
compareVersions
};
{
"name": "sample-semver-cmp",
"version": "1.0.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "sample-semver-cmp",
"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"
}
}
}
}
{
"name": "sample-semver-cmp",
"version": "1.0.0",
"private": true,
"main": "index.js",
"dependencies": {
"semver": "7.8.5"
}
}
{
"schemaVersion": 1,
"goal": "compare version numbers with semver.cmp",
"kind": "HOW",
"packages": [
"pkg:npm/semver@7.8.5"
],
"symbols": [
"cmp"
],
"constraints": {
"executionContext": "node"
},
"runtimeConditions": {
"ecosystem": "npm",
"language": "javascript",
"moduleSystem": "cjs",
"packageManager": "npm@11.8.0",
"runtime": "node@24.13.1"
}
}
const assert = require('assert');
const semver = require('semver');
const { compareVersions } = require('../index');
// 1. semver.cmp correctly evaluates equality operators (=, ==, ===, !=, !==)
assert.strictEqual(
semver.cmp('1.2.3', '===', '1.2.3'),
true,
'1.2.3 === 1.2.3 should be true'
);
assert.strictEqual(
semver.cmp('1.2.3', '===', '1.2.4'),
false,
'1.2.3 === 1.2.4 should be false'
);
assert.strictEqual(
semver.cmp('1.2.3', '==', '1.2.3'),
true,
'1.2.3 == 1.2.3 should be true'
);
assert.strictEqual(
semver.cmp('1.2.3', '=', '1.2.3'),
true,
'1.2.3 = 1.2.3 should be true'
);
assert.strictEqual(
semver.cmp('1.2.3', '!==', '1.2.4'),
true,
'1.2.3 !== 1.2.4 should be true'
);
assert.strictEqual(
semver.cmp('1.2.3', '!==', '1.2.3'),
false,
'1.2.3 !== 1.2.3 should be false'
);
assert.strictEqual(
semver.cmp('1.2.3', '!=', '1.2.4'),
true,
'1.2.3 != 1.2.4 should be true'
);
// 2. semver.cmp correctly evaluates relational operators (<, <=, >, >=)
assert.strictEqual(
semver.cmp('1.0.0', '<', '2.0.0'),
true,
'1.0.0 < 2.0.0 should be true'
);
assert.strictEqual(
semver.cmp('2.0.0', '<', '1.0.0'),
false,
'2.0.0 < 1.0.0 should be false'
);
assert.strictEqual(
semver.cmp('1.0.0', '<=', '1.0.0'),
true,
'1.0.0 <= 1.0.0 should be true'
);
assert.strictEqual(
semver.cmp('1.0.0', '<=', '2.0.0'),
true,
'1.0.0 <= 2.0.0 should be true'
);
assert.strictEqual(
semver.cmp('2.0.0', '>', '1.0.0'),
true,
'2.0.0 > 1.0.0 should be true'
);
assert.strictEqual(
semver.cmp('1.0.0', '>', '2.0.0'),
false,
'1.0.0 > 2.0.0 should be false'
);
assert.strictEqual(
semver.cmp('1.0.0', '>=', '1.0.0'),
true,
'1.0.0 >= 1.0.0 should be true'
);
assert.strictEqual(
semver.cmp('2.0.0', '>=', '1.0.0'),
true,
'2.0.0 >= 1.0.0 should be true'
);
assert.strictEqual(
semver.cmp('1.0.0', '>=', '2.0.0'),
false,
'1.0.0 >= 2.0.0 should be false'
);
// 3. semver.cmp correctly compares prerelease versions
assert.strictEqual(
semver.cmp('1.0.0-alpha.1', '<', '1.0.0-alpha.2'),
true,
'1.0.0-alpha.1 < 1.0.0-alpha.2 should be true'
);
assert.strictEqual(
semver.cmp('1.0.0-alpha', '<', '1.0.0'),
true,
'1.0.0-alpha < 1.0.0 should be true'
);
assert.strictEqual(
semver.cmp('1.0.0-beta', '>', '1.0.0-alpha'),
true,
'1.0.0-beta > 1.0.0-alpha should be true'
);
// 4. semver.cmp throws TypeError for invalid comparator operators
assert.throws(
() => {
semver.cmp('1.0.0', 'invalid', '2.0.0');
},
TypeError,
'semver.cmp should throw TypeError for invalid operator'
);
// 5. compareVersions wrapper correctly compares versions using comparison operators
assert.strictEqual(
compareVersions('1.5.0', '<', '2.0.0'),
true,
'compareVersions returns true when 1.5.0 < 2.0.0'
);
assert.strictEqual(
compareVersions('2.0.0', '===', '2.0.0'),
true,
'compareVersions returns true when 2.0.0 === 2.0.0'
);
assert.strictEqual(
compareVersions('1.0.0', '>', '2.0.0'),
false,
'compareVersions returns false when 1.0.0 > 2.0.0'
);
console.log('All contract tests passed!');
オリジンシーダー
匿名