CodeSampleX

Ejemplo

ansi-styles 6.2.3: foregroundColorNames

Muestra verificada para npm ansi-styles 6.2.3: foregroundColorNames. El contrato se ejecutó en node 22 · linux alpine/x64 · docker y pasó.

sha256:ee5c514d5b7934bb61b901a95f92749b002f98365590db252043ccf7dc6bbaff

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
1
Entorno declarado node 22.23 linux 24 · ubuntu · glibc 2.39 x64 node 22.23 javascript npm 10

Entornos de las ejecuciones de verificación

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

Caso

HOW
Objetivo
verify ansi-styles.foregroundColorNames in pkg:npm/ansi-styles@6.2.3
Paquetes
Símbolos
  • ansi-styles.foregroundColorNames
Entorno
node 22.23.2
Creado
2026-08-23T05:39:49Z

Contrato

  1. foregroundColorNames is an array containing all standard and bright foreground color names
  2. every name in foregroundColorNames corresponds to a valid style in ansiStyles with matching open and close codes
  3. foregroundColorNames includes standard foreground colors and bright color aliases
  4. isValidForegroundColor verifies whether a given name exists in foregroundColorNames
  5. applyForegroundColor wraps text in the ANSI escape codes for the specified foreground color name

Archivos

  • PROMPT.md
  • csx.json
  • package-lock.json
  • package.json
  • spec.json
  • src/index.js
  • 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: verify ansi-styles.foregroundColorNames in pkg:npm/ansi-styles@6.2.3
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/ansi-styles@6.2.3
Demonstrate these symbols/APIs:
  - ansi-styles.foregroundColorNames

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:5787e2d550df7307cf9d572aa3e5912da99615a185c6f18587cf3dcae0d7dcb7","contract":["foregroundColorNames is an array containing all standard and bright foreground color names","every name in foregroundColorNames corresponds to a valid style in ansiStyles with matching open and close codes","foregroundColorNames includes standard foreground colors and bright color aliases","isValidForegroundColor verifies whether a given name exists in foregroundColorNames","applyForegroundColor wraps text in the ANSI escape codes for the specified foreground color name"],"goal":"verify ansi-styles.foregroundColorNames in pkg:npm/ansi-styles@6.2.3","kind":"HOW","packages":["pkg:npm/ansi-styles@6.2.3"],"schemaVersion":1,"symbols":["ansi-styles.foregroundColorNames"]},"contractCommand":["node","test/contract.js"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","executionContext":"node","language":"javascript","libc":"glibc","libcVersion":"2.39","moduleSystem":"cjs","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-styles@6.2.3"],"schemaVersion":1,"subject":"pkg:npm/ansi-styles@6.2.3","symbols":["ansi-styles.foregroundColorNames"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "sample-ansi-styles-fg-names",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-ansi-styles-fg-names",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "ansi-styles": "6.2.3"
      }
    },
    "node_modules/ansi-styles": {
      "version": "6.2.3",
      "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-6.2.3.tgz",
      "integrity": "sha512-4Dj6M28JB+oAH8kFkTLUo+a2jwOFkuqb3yucU0CANcRRUbxS0cP0nZYCGjcc3BNXwRIsUVmDGgzawme7zvJHvg==",
      "license": "MIT",
      "engines": {
        "node": ">=12"
      },
      "funding": {
        "url": "https://github.com/chalk/ansi-styles?sponsor=1"
      }
    }
  }
}
package.json
{
  "name": "sample-ansi-styles-fg-names",
  "version": "1.0.0",
  "private": true,
  "description": "Clean-room verification for ansi-styles foregroundColorNames",
  "main": "src/index.js",
  "scripts": {
    "test": "node test/contract.js"
  },
  "dependencies": {
    "ansi-styles": "6.2.3"
  },
  "license": "MIT-0"
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify ansi-styles.foregroundColorNames in pkg:npm/ansi-styles@6.2.3",
  "kind": "HOW",
  "packages": [
    "pkg:npm/ansi-styles@6.2.3"
  ],
  "symbols": [
    "ansi-styles.foregroundColorNames"
  ]
}
src/index.js
const ansiStylesModule = require('ansi-styles');

const ansiStyles = ansiStylesModule.default || ansiStylesModule;
const { foregroundColorNames } = ansiStylesModule;

/**
 * Returns the list of all supported foreground color names.
 * @returns {string[]}
 */
function getForegroundColorNames() {
  return foregroundColorNames;
}

/**
 * Checks if a given string is a valid foreground color name in ansi-styles.
 * @param {string} name
 * @returns {boolean}
 */
function isValidForegroundColor(name) {
  return foregroundColorNames.includes(name);
}

/**
 * Retrieves the open and close ANSI escape sequences for a foreground color name.
 * @param {string} name
 * @returns {{ open: string, close: string }}
 */
function getForegroundColorCodes(name) {
  if (!isValidForegroundColor(name)) {
    throw new Error(`Invalid foreground color name: ${name}`);
  }
  const style = (ansiStyles.color && ansiStyles.color[name]) || ansiStyles[name];
  if (!style) {
    throw new Error(`Style not found for foreground color: ${name}`);
  }
  return {
    open: style.open,
    close: style.close,
  };
}

/**
 * Wraps text in the specified foreground color's ANSI escape codes.
 * @param {string} text
 * @param {string} name
 * @returns {string}
 */
function applyForegroundColor(text, name) {
  const { open, close } = getForegroundColorCodes(name);
  return `${open}${text}${close}`;
}

module.exports = {
  ansiStyles,
  foregroundColorNames,
  getForegroundColorNames,
  isValidForegroundColor,
  getForegroundColorCodes,
  applyForegroundColor,
};
test/contract.js
const assert = require('node:assert');
const {
  ansiStyles,
  foregroundColorNames,
  getForegroundColorNames,
  isValidForegroundColor,
  getForegroundColorCodes,
  applyForegroundColor,
} = require('../src/index.js');

// 1. foregroundColorNames is an array containing all standard and bright foreground color names
assert(Array.isArray(foregroundColorNames), 'foregroundColorNames must be an Array');
assert.strictEqual(foregroundColorNames.length, 18, 'foregroundColorNames must have 18 color names');
assert.deepStrictEqual(getForegroundColorNames(), foregroundColorNames);

// 2. every name in foregroundColorNames corresponds to a valid style in ansiStyles with matching open and close codes
for (const name of foregroundColorNames) {
  const codes = getForegroundColorCodes(name);
  assert(typeof codes.open === 'string' && codes.open.startsWith('\u001B['), `${name} open code must start with ANSI escape`);
  assert(codes.open.endsWith('m'), `${name} open code must end with 'm'`);
  assert.strictEqual(codes.close, '\u001B[39m', `${name} close code must be default foreground close`);

  // Direct access on ansiStyles.color and ansiStyles root
  const directStyle = ansiStyles.color ? ansiStyles.color[name] : ansiStyles[name];
  assert.strictEqual(directStyle.open, codes.open);
  assert.strictEqual(directStyle.close, codes.close);
}

// 3. foregroundColorNames includes standard foreground colors and bright color aliases
const expectedStandard = ['black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white'];
const expectedBright = [
  'blackBright', 'gray', 'grey',
  'redBright', 'greenBright', 'yellowBright',
  'blueBright', 'magentaBright', 'cyanBright', 'whiteBright'
];

for (const name of expectedStandard) {
  assert(foregroundColorNames.includes(name), `Missing standard color ${name}`);
}
for (const name of expectedBright) {
  assert(foregroundColorNames.includes(name), `Missing bright color ${name}`);
}

// Test specific open code mappings
assert.strictEqual(ansiStyles.color.black.open, '\u001B[30m');
assert.strictEqual(ansiStyles.color.red.open, '\u001B[31m');
assert.strictEqual(ansiStyles.color.green.open, '\u001B[32m');
assert.strictEqual(ansiStyles.color.yellow.open, '\u001B[33m');
assert.strictEqual(ansiStyles.color.blue.open, '\u001B[34m');
assert.strictEqual(ansiStyles.color.magenta.open, '\u001B[35m');
assert.strictEqual(ansiStyles.color.cyan.open, '\u001B[36m');
assert.strictEqual(ansiStyles.color.white.open, '\u001B[37m');
assert.strictEqual(ansiStyles.color.blackBright.open, '\u001B[90m');
assert.strictEqual(ansiStyles.color.redBright.open, '\u001B[91m');
assert.strictEqual(ansiStyles.color.greenBright.open, '\u001B[92m');
assert.strictEqual(ansiStyles.color.yellowBright.open, '\u001B[93m');
assert.strictEqual(ansiStyles.color.blueBright.open, '\u001B[94m');
assert.strictEqual(ansiStyles.color.magentaBright.open, '\u001B[95m');
assert.strictEqual(ansiStyles.color.cyanBright.open, '\u001B[96m');
assert.strictEqual(ansiStyles.color.whiteBright.open, '\u001B[97m');
assert.deepStrictEqual(ansiStyles.color.gray, ansiStyles.color.blackBright);
assert.deepStrictEqual(ansiStyles.color.grey, ansiStyles.color.blackBright);

// 4. isValidForegroundColor verifies whether a given name exists in foregroundColorNames
assert.strictEqual(isValidForegroundColor('blue'), true);
assert.strictEqual(isValidForegroundColor('magentaBright'), true);
assert.strictEqual(isValidForegroundColor('bgRed'), false);
assert.strictEqual(isValidForegroundColor('nonExistentColor'), false);
assert.strictEqual(isValidForegroundColor(''), false);

// 5. applyForegroundColor wraps text in the ANSI escape codes for the specified foreground color name
const text = 'Hello CodeSampleX';
assert.strictEqual(applyForegroundColor(text, 'blue'), `\u001B[34m${text}\u001B[39m`);
assert.strictEqual(applyForegroundColor(text, 'redBright'), `\u001B[91m${text}\u001B[39m`);
assert.strictEqual(applyForegroundColor(text, 'green'), `\u001B[32m${text}\u001B[39m`);

assert.throws(() => {
  applyForegroundColor(text, 'invalidColor');
}, /Invalid foreground color name: invalidColor/);

console.log('All contract assertions passed successfully.');

Seeder de origen

anónimo