CodeSampleX

Exemple

ansi-regex 6.3.0: ansiRegex

Échantillon vérifié pour npm ansi-regex 6.3.0: ansiRegex. Le contrat s'est exécuté sur node 22 · linux alpine/x64 · docker et a réussi.

sha256:5d93b0bd0c9c8a9316d975582e7110e149bb52fd8fd4dfd3e94e1ce3f1d4348f

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é
1
Environnement déclaré node 22.23 linux 24 · ubuntu · glibc 2.39 x64 node 22.23 javascript npm 10

Environnements des exécutions de vérification

Environnement Contrat Étapes Exécution
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-08-26
node 22 · linux debian/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1node:22@sha256:8a34c4ab3ea2…
2026-09-07

Cas

HOW
Objectif
verify ansiRegex in pkg:npm/ansi-regex@6.3.0
Paquets
Symboles
  • ansiRegex
Environnement
node 22.23.2
Créé
2026-08-26T04:44:29Z

Contrat

  1. ansiRegex returns a RegExp instance for matching ANSI escape codes
  2. ansiRegex with default options returns a global regular expression
  3. ansiRegex with onlyFirst set to true returns a non-global regular expression
  4. ansiRegex matches standard text formatting and color escape sequences
  5. ansiRegex matches 256-color and 24-bit truecolor escape sequences
  6. stripAnsi removes all ANSI escape codes to return plain text
  7. hasAnsi detects whether a string contains ANSI escape codes
  8. extractAnsi collects all ANSI escape codes found in a string

Fichiers

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

Télécharger l’artefact source (tar.gz)

Code source

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: verify ansiRegex in pkg:npm/ansi-regex@6.3.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/ansi-regex@6.3.0
Demonstrate these symbols/APIs:
  - ansiRegex

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:0457022621187a42a0968f8df4eb90c34eb67be7169b894a3528013e1662fc27","contract":["ansiRegex returns a RegExp instance for matching ANSI escape codes","ansiRegex with default options returns a global regular expression","ansiRegex with onlyFirst set to true returns a non-global regular expression","ansiRegex matches standard text formatting and color escape sequences","ansiRegex matches 256-color and 24-bit truecolor escape sequences","stripAnsi removes all ANSI escape codes to return plain text","hasAnsi detects whether a string contains ANSI escape codes","extractAnsi collects all ANSI escape codes found in a string"],"goal":"verify ansiRegex in pkg:npm/ansi-regex@6.3.0","kind":"HOW","packages":["pkg:npm/ansi-regex@6.3.0"],"schemaVersion":1,"symbols":["ansiRegex"]},"contractCommand":["node","test/contract.js"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","executionContext":"node","language":"javascript","libc":"glibc","libcVersion":"2.39","moduleSystem":"esm","os":"linux","osVersionBucket":"24","packageManager":"npm","packageManagerVersion":"10.9.8","runtime":"node","runtimeVersion":"22.23.2","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/ansi-regex@6.3.0"],"schemaVersion":1,"subject":"pkg:npm/ansi-regex@6.3.0","symbols":["ansiRegex"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "sample-ansi-regex",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-ansi-regex",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "ansi-regex": "6.3.0"
      }
    },
    "node_modules/ansi-regex": {
      "version": "6.3.0",
      "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-6.3.0.tgz",
      "integrity": "sha512-WpDfL7NO6j7tH88IDBNVdUJxDh9nmCteAVW9dsep846XdwF4naCBK+/tGLX3KJgcpgMRXCFlTM2hKGoK9FsdrQ==",
      "license": "MIT",
      "engines": {
        "node": ">=12"
      },
      "funding": {
        "url": "https://github.com/chalk/ansi-regex?sponsor=1"
      }
    }
  }
}
package.json
{
  "name": "sample-ansi-regex",
  "version": "1.0.0",
  "private": true,
  "type": "module",
  "description": "Clean-room verification sample for ansi-regex",
  "main": "src/index.js",
  "scripts": {
    "test": "node test/contract.js"
  },
  "dependencies": {
    "ansi-regex": "6.3.0"
  },
  "license": "MIT-0"
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify ansiRegex in pkg:npm/ansi-regex@6.3.0",
  "kind": "HOW",
  "packages": [
    "pkg:npm/ansi-regex@6.3.0"
  ],
  "symbols": [
    "ansiRegex"
  ]
}
src/index.js
import ansiRegex from 'ansi-regex';

/**
 * Returns a regular expression matching ANSI escape codes.
 * @param {Object} [options]
 * @param {boolean} [options.onlyFirst=false] - Match only the first occurrence.
 * @returns {RegExp}
 */
export function getAnsiRegex(options) {
  return ansiRegex(options);
}

/**
 * Checks if a string contains any ANSI escape codes.
 * @param {string} text
 * @returns {boolean}
 */
export function hasAnsi(text) {
  if (typeof text !== 'string') {
    return false;
  }
  return ansiRegex({ onlyFirst: true }).test(text);
}

/**
 * Strips all ANSI escape codes from a string.
 * @param {string} text
 * @returns {string}
 */
export function stripAnsi(text) {
  if (typeof text !== 'string') {
    return '';
  }
  return text.replace(ansiRegex(), '');
}

/**
 * Extracts all ANSI escape sequences from a string.
 * @param {string} text
 * @returns {string[]}
 */
export function extractAnsi(text) {
  if (typeof text !== 'string') {
    return [];
  }
  const matches = text.match(ansiRegex());
  return matches ? Array.from(matches) : [];
}

export { ansiRegex };
export default ansiRegex;
test/contract.js
import assert from 'node:assert';
import ansiRegex, { getAnsiRegex, hasAnsi, stripAnsi, extractAnsi } from '../src/index.js';

// 1. ansiRegex returns a RegExp instance for matching ANSI escape codes
const regex = ansiRegex();
assert.ok(regex instanceof RegExp, 'ansiRegex() must return a RegExp instance');
assert.strictEqual(typeof ansiRegex, 'function', 'ansiRegex must be a function');

// 2. ansiRegex with default options returns a global regular expression
assert.strictEqual(regex.global, true, 'ansiRegex() regex must have global flag');
assert.strictEqual(getAnsiRegex().global, true, 'getAnsiRegex() regex must have global flag');

// 3. ansiRegex with onlyFirst set to true returns a non-global regular expression
const firstRegex = ansiRegex({ onlyFirst: true });
assert.ok(firstRegex instanceof RegExp, 'ansiRegex({ onlyFirst: true }) must return a RegExp instance');
assert.strictEqual(firstRegex.global, false, 'ansiRegex({ onlyFirst: true }) must not have global flag');
assert.strictEqual(getAnsiRegex({ onlyFirst: true }).global, false, 'getAnsiRegex({ onlyFirst: true }) must not have global flag');

// 4. ansiRegex matches standard text formatting and color escape sequences
const colorString = '\x1b[31mRed text\x1b[39m and \x1b[1mBold text\x1b[22m';
assert.strictEqual(firstRegex.test(colorString), true, 'firstRegex must match standard color escape sequence');
const colorMatches = colorString.match(ansiRegex());
assert.ok(colorMatches !== null, 'color matches must not be null');
assert.strictEqual(colorMatches.length, 4, 'must match 4 escape codes in colorString');
assert.strictEqual(colorMatches[0], '\x1b[31m');
assert.strictEqual(colorMatches[1], '\x1b[39m');
assert.strictEqual(colorMatches[2], '\x1b[1m');
assert.strictEqual(colorMatches[3], '\x1b[22m');

// 5. ansiRegex matches 256-color and 24-bit truecolor escape sequences
const advancedString = '\x1b[38;5;196m256-color\x1b[39m \x1b[38;2;255;100;50mTruecolor\x1b[39m';
assert.strictEqual(hasAnsi(advancedString), true, 'hasAnsi must return true for advanced ANSI codes');
const advancedMatches = extractAnsi(advancedString);
assert.strictEqual(advancedMatches.length, 4, 'must match 4 advanced escape codes');
assert.strictEqual(advancedMatches[0], '\x1b[38;5;196m');
assert.strictEqual(advancedMatches[1], '\x1b[39m');
assert.strictEqual(advancedMatches[2], '\x1b[38;2;255;100;50m');
assert.strictEqual(advancedMatches[3], '\x1b[39m');

// 6. stripAnsi removes all ANSI escape codes to return plain text
const mixedString = '\x1b[32m\x1b[4mClean Text\x1b[24m\x1b[39m';
const plain = stripAnsi(mixedString);
assert.strictEqual(plain, 'Clean Text', 'stripAnsi must remove all ANSI codes');
assert.strictEqual(stripAnsi('Plain text without ANSI'), 'Plain text without ANSI', 'stripAnsi should leave plain text intact');
assert.strictEqual(stripAnsi(null), '', 'stripAnsi handles non-string input safely');

// 7. hasAnsi detects whether a string contains ANSI escape codes
assert.strictEqual(hasAnsi('\x1b[33mWarning\x1b[39m'), true, 'hasAnsi must return true for styled text');
assert.strictEqual(hasAnsi('Unstyled normal text'), false, 'hasAnsi must return false for plain text');
assert.strictEqual(hasAnsi(''), false, 'hasAnsi must return false for empty string');
assert.strictEqual(hasAnsi(undefined), false, 'hasAnsi handles non-string input safely');

// 8. extractAnsi collects all ANSI escape codes found in a string
const extracted = extractAnsi('\x1b[41mAlert\x1b[49m');
assert.deepStrictEqual(extracted, ['\x1b[41m', '\x1b[49m'], 'extractAnsi must return all matched ANSI sequences');
assert.deepStrictEqual(extractAnsi('No ansi'), [], 'extractAnsi must return empty array when no ANSI exists');
assert.deepStrictEqual(extractAnsi(123), [], 'extractAnsi handles non-string input safely');

console.log('Contract tests passed successfully.');

Seeder d'origine

anonyme