CodeSampleX

Beispiel

semver 7.8.5: sort and reverse sort semantic versions with sort, rsort, and compareBuild

Verifiziertes Beispiel für npm semver 7.8.5: sort and reverse sort semantic versions with sort, rsort, and compareBuild. Der Vertrag lief auf node 22 · linux…

sha256:26f645febc9c2deea5d00a0918ae7444800698ffcea15760bc7be42ee34375d2

Dieses Netzwerk bietet eine Sache: ein Sample, das baut. Es hat es in einer Sandbox ausgeführt und die signierte Quittung behalten. Es bewertet nichts und garantiert nichts — ob derselbe Code bei Ihnen baut, hat es nicht gemessen. Wie viele verschiedene Signaturschlüssel eine bestandene Vertragsquittung eingereicht haben. Einer ist der Autor allein; mehr als einer heißt, jemand anderes hat es auch gebaut. Ein Schlüssel wird selbst erzeugt und hat keine registrierte Identität dahinter — gezählt werden Schlüssel, nicht Personen. MIT-0

Ausführungsbelege

Die deklarierte Umgebung und die signierten Läufe stehen getrennt, damit Sie genau sehen, was dieses Sample ausgeführt hat und wo.

Beleggrundlage
Signierter Vertrag bestanden
Verifizierungsbelege
2
Signaturschlüssel, die es gebaut haben
2
Deklarierte Umgebung windows 11 x64 npm

Umgebungen der Verifizierungsläufe

Umgebung Contract Stufen Lauf
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

Fall

HOW
Ziel
sort and reverse sort semantic versions with sort, rsort, and compareBuild
Pakete
Symbole
  • compareBuild
  • rsort
  • sort
Erstellt
2026-08-19T10:55:07Z

Contract

  1. semver.sort sorts an array of version strings in ascending semver order
  2. semver.rsort sorts an array of version strings in descending semver order
  3. semver.compareBuild compares two versions distinguishing build metadata
  4. semver.sort and semver.rsort mutate arrays in place and return the sorted array

Dateien

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

Quellartefakt herunterladen (tar.gz)

Quelltext

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: sort and reverse sort semantic versions with sort, rsort, and compareBuild
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/semver@7.8.5
Demonstrate these symbols/APIs:
  - compareBuild
  - rsort
  - sort
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.
csx.json
{"case":{"caseId":"case:sha256:4cc337b6e3f229dece26820c20f90e46825da93e5d1da820aaa089c8d215236f","contract":["semver.sort sorts an array of version strings in ascending semver order","semver.rsort sorts an array of version strings in descending semver order","semver.compareBuild compares two versions distinguishing build metadata","semver.sort and semver.rsort mutate arrays in place and return the sorted array"],"goal":"sort and reverse sort semantic versions with sort, rsort, and compareBuild","kind":"HOW","packages":["pkg:npm/semver@7.8.5"],"schemaVersion":1,"symbols":["compareBuild","rsort","sort"]},"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","rsort","sort"],"verifierAdapter":"node-typescript@1"}
index.mjs
import semver from 'semver';

/**
 * Sorts an array of semantic version strings in ascending order.
 *
 * @param {string[]} list - Array of version strings to sort.
 * @param {object|boolean} [optionsOrLoose] - Options or loose flag.
 * @returns {string[]} The sorted array (mutated in place).
 */
export function sortVersions(list, optionsOrLoose) {
  return semver.sort(list, optionsOrLoose);
}

/**
 * Sorts an array of semantic version strings in descending order.
 *
 * @param {string[]} list - Array of version strings to sort.
 * @param {object|boolean} [optionsOrLoose] - Options or loose flag.
 * @returns {string[]} The reverse-sorted array (mutated in place).
 */
export function rsortVersions(list, optionsOrLoose) {
  return semver.rsort(list, optionsOrLoose);
}

/**
 * Compares two versions including build metadata.
 *
 * @param {string} v1 - First version string.
 * @param {string} v2 - Second version string.
 * @param {object|boolean} [optionsOrLoose] - Options or loose flag.
 * @returns {number} 0 if v1 == v2, 1 if v1 > v2, -1 if v1 < v2.
 */
export function compareWithBuild(v1, v2, optionsOrLoose) {
  return semver.compareBuild(v1, v2, optionsOrLoose);
}

export { semver };
package-lock.json
{
  "name": "sample-semver-sort",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-semver-sort",
      "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-sort",
  "version": "1.0.0",
  "type": "module",
  "private": true,
  "dependencies": {
    "semver": "7.8.5"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "sort and reverse sort semantic versions with sort, rsort, and compareBuild",
  "kind": "HOW",
  "packages": [
    "pkg:npm/semver@7.8.5"
  ],
  "symbols": [
    "compareBuild",
    "rsort",
    "sort"
  ],
  "constraints": {
    "executionContext": "node"
  },
  "runtimeConditions": {
    "ecosystem": "npm",
    "language": "javascript",
    "moduleSystem": "cjs",
    "packageManager": "npm@11.8.0",
    "runtime": "node@24.13.1"
  }
}
test/contract.mjs
import assert from 'node:assert/strict';
import semver from 'semver';
import { sortVersions, rsortVersions, compareWithBuild } from '../index.mjs';

// 1. semver.sort sorts an array of version strings in ascending semver order
const versionsAsc = ['1.2.3', '0.1.2', '1.3.0', '1.2.3-alpha'];
const sortedAsc = semver.sort([...versionsAsc]);
assert.deepStrictEqual(sortedAsc, ['0.1.2', '1.2.3-alpha', '1.2.3', '1.3.0']);
assert.deepStrictEqual(sortVersions([...versionsAsc]), ['0.1.2', '1.2.3-alpha', '1.2.3', '1.3.0']);

// 2. semver.rsort sorts an array of version strings in descending semver order
const versionsDesc = ['1.2.3', '0.1.2', '1.3.0', '1.2.3-alpha'];
const sortedDesc = semver.rsort([...versionsDesc]);
assert.deepStrictEqual(sortedDesc, ['1.3.0', '1.2.3', '1.2.3-alpha', '0.1.2']);
assert.deepStrictEqual(rsortVersions([...versionsDesc]), ['1.3.0', '1.2.3', '1.2.3-alpha', '0.1.2']);

// 3. semver.compareBuild compares two versions distinguishing build metadata
assert.strictEqual(semver.compare('1.0.0+build1', '1.0.0+build2'), 0);
assert.strictEqual(semver.compareBuild('1.0.0+build1', '1.0.0+build2'), -1);
assert.strictEqual(semver.compareBuild('1.0.0+build2', '1.0.0+build1'), 1);
assert.strictEqual(semver.compareBuild('1.0.0+build1', '1.0.0+build1'), 0);
assert.strictEqual(compareWithBuild('1.0.0+1', '1.0.0+2'), -1);
assert.strictEqual(semver.compareBuild('1.2.0', '1.1.0'), 1);
assert.strictEqual(semver.compareBuild('1.0.0-rc.1', '1.0.0'), -1);

// 4. semver.sort and semver.rsort mutate arrays in place and return the sorted array
const arr1 = ['2.0.0', '1.0.0'];
const ret1 = semver.sort(arr1);
assert.strictEqual(ret1, arr1);
assert.deepStrictEqual(arr1, ['1.0.0', '2.0.0']);

const arr2 = ['1.0.0', '2.0.0'];
const ret2 = semver.rsort(arr2);
assert.strictEqual(ret2, arr2);
assert.deepStrictEqual(arr2, ['2.0.0', '1.0.0']);

console.log('PASS: semver sort, rsort, and compareBuild contracts verified');

Ursprungs-Seeder

anonym