CodeSampleX

Ejemplo

semver 7.8.5: coerce strings into semver versions

Muestra verificada para npm semver 7.8.5: coerce strings into semver versions. El contrato se ejecutó en node 22 · linux alpine/x64 · docker y pasó.

sha256:a2843aa0100774bd3cdba69a3cf6c7ce597a8d73cb4f863cdbf684f887f93959

Esta red ofrece una sola cosa: una muestra que compila. La ejecutó en un sandbox y guardó el recibo firmado. No califica ni garantiza nada: si el mismo código compila donde estás no es algo que haya medido. Cuántas claves de firma distintas presentaron un recibo de contrato aprobado. Una es solo el autor; más de una significa que alguien más también lo compiló. Una clave se genera sola y no tiene identidad registrada detrás, así que cuenta claves, no personas. MIT-0

Evidencia de ejecución

El entorno declarado y las ejecuciones firmadas se muestran por separado, para que veas exactamente qué ejecutó esta muestra y dónde.

Base de evidencia
Contrato firmado aprobado
Recibos de verificación
2
Claves de firma que lo compilaron
2
Entorno declarado windows 11 x64 npm

Entornos de las ejecuciones de verificación

Entorno Contrato Etapas Ejecución
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

Caso

HOW
Objetivo
coerce strings into semver versions
Paquetes
Símbolos
  • coerce
Creado
2026-08-18T19:24:52Z

Contrato

  1. coerce basic integer string into major semver version
  2. coerce partial version string with minor and patch
  3. coerce version with prefix and surrounding text
  4. coerce strips excess numeric segments and prerelease tags
  5. coerce supports rtl option to match rightmost version
  6. coerce returns null when no valid version can be extracted

Archivos

  • PROMPT.md
  • csx.json
  • index.js
  • package-lock.json
  • package.json
  • spec.json
  • test/contract.js

Descargar el artefacto de código fuente (tar.gz)

Código fuente

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 strings into semver versions
Kind: HOW

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

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:bdf2dbdb7ddcc7ba5d945d8fcf04c80876a2b1c071cf122295c100ab8500cd2e","contract":["coerce basic integer string into major semver version","coerce partial version string with minor and patch","coerce version with prefix and surrounding text","coerce strips excess numeric segments and prerelease tags","coerce supports rtl option to match rightmost version","coerce returns null when no valid version can be extracted"],"goal":"coerce strings into semver versions","kind":"HOW","packages":["pkg:npm/semver@7.8.5"],"schemaVersion":1,"symbols":["coerce"]},"contractCommand":["node","test/contract.js"],"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":["coerce"],"verifierAdapter":"node-typescript@1"}
index.js
const semver = require('semver');

function coerceVersion(version, options) {
  return semver.coerce(version, options);
}

module.exports = {
  coerce: semver.coerce,
  coerceVersion
};
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==",
      "license": "ISC",
      "bin": {
        "semver": "bin/semver.js"
      },
      "engines": {
        "node": ">=10"
      }
    }
  }
}
package.json
{
  "name": "sample",
  "version": "1.0.0",
  "private": true,
  "main": "index.js",
  "dependencies": {
    "semver": "7.8.5"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "coerce strings into semver versions",
  "kind": "HOW",
  "packages": [
    "pkg:npm/semver@7.8.5"
  ],
  "symbols": [
    "coerce"
  ]
}
test/contract.js
const assert = require('assert');
const { coerce, coerceVersion } = require('../index.js');

// Assertion 1: coerce basic integer string into major semver version
const res1 = coerce('42');
assert(res1 !== null, 'expected valid semver instance');
assert.strictEqual(res1.version, '42.0.0');
assert.strictEqual(res1.major, 42);
assert.strictEqual(res1.minor, 0);
assert.strictEqual(res1.patch, 0);

// Assertion 2: coerce partial version string with minor and patch
const res2a = coerce('42.6');
assert(res2a !== null, 'expected valid semver instance');
assert.strictEqual(res2a.version, '42.6.0');
assert.strictEqual(res2a.minor, 6);

const res2b = coerceVersion('42.6.7');
assert(res2b !== null, 'expected valid semver instance');
assert.strictEqual(res2b.version, '42.6.7');
assert.strictEqual(res2b.patch, 7);

// Assertion 3: coerce version with prefix and surrounding text
const res3a = coerce('v1.2.3');
assert(res3a !== null, 'expected valid semver instance');
assert.strictEqual(res3a.version, '1.2.3');

const res3b = coerce('version: 2.0.1-build');
assert(res3b !== null, 'expected valid semver instance');
assert.strictEqual(res3b.version, '2.0.1');

// Assertion 4: coerce strips excess numeric segments and prerelease tags
const res4 = coerce('42.6.7.9.3-alpha');
assert(res4 !== null, 'expected valid semver instance');
assert.strictEqual(res4.version, '42.6.7');

// Assertion 5: coerce supports rtl option to match rightmost version
const res5Left = coerce('v1.2.3-1.2.4', { rtl: false });
assert(res5Left !== null, 'expected valid semver instance');
assert.strictEqual(res5Left.version, '1.2.3');

const res5Right = coerce('v1.2.3-1.2.4', { rtl: true });
assert(res5Right !== null, 'expected valid semver instance');
assert.strictEqual(res5Right.version, '1.2.4');

// Assertion 6: coerce returns null when no valid version can be extracted
assert.strictEqual(coerce('not-a-version'), null);
assert.strictEqual(coerce(''), null);
assert.strictEqual(coerceVersion('xyz'), null);

console.log('PASS: all contract assertions passed for semver.coerce');

Seeder de origen

anónimo