Exemple
semver 7.8.5: compare version numbers with semver.cmp
Échantillon vérifié pour npm semver 7.8.5: compare version numbers with semver.cmp. Le contrat s'est exécuté sur node 22 · linux alpine/x64 · docker et a…
sha256:206eb265ef78d9254422b731e0e5bf637c04d6a20ee046953e44937c3cd2e709
Ce réseau offre une seule chose : un échantillon qui compile. Il l'a exécuté dans un bac à sable et conservé le reçu signé. Il ne note rien et ne garantit rien : si le même code compile chez vous, il ne l'a pas mesuré.
Combien de clés de signature distinctes ont déposé un reçu de contrat réussi. Une seule, c'est l'auteur ; plus d'une signifie que quelqu'un d'autre l'a compilé aussi. Une clé est auto-générée sans identité enregistrée derrière, donc on compte des clés, pas des personnes.
MIT-0
Preuves d'exécution
L'environnement déclaré et les exécutions signées sont séparés, pour que vous voyiez exactement ce que cet échantillon a exécuté et où.
- Base de preuve
- Contrat signé réussi
- Reçus de vérification
- 2
- Clés de signature qui l’ont compilé
- 2
Environnement déclaré
node 22 windows 11 x64 node 22 javascript npm 10
Environnements des exécutions de vérification
| Environnement | Contrat | Étapes | Exécution |
|---|---|---|---|
| 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 |
Cas
HOW- Objectif
- compare version numbers with semver.cmp
- Paquets
- Symboles
-
- cmp
- Environnement
- node 22
- Créé
- 2026-08-19T07:42:16Z
Contrat
- 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
Fichiers
- PROMPT.md
- csx.json
- index.js
- package-lock.json
- package.json
- spec.json
- test/contract.js
Code source
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!');
Seeder d'origine
anonyme