CodeSampleX

Ejemplo

truncate-utf8-bytes 1.0.2: truncate string by maximum byte length safely without splitting multi-byte UTF-8 characters

Muestra verificada para npm truncate-utf8-bytes 1.0.2: truncate string by maximum byte length safely without splitting multi-byte UTF-8 characters. El…

sha256:d26d7bb73d2b2b2c115cd3fc5efffc963200ed7764398de20be6a89ca95d2c84

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
1
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-28

Caso

HOW
Objetivo
truncate string by maximum byte length safely without splitting multi-byte UTF-8 characters
Paquetes
Símbolos
  • truncate-utf8-bytes
Entorno
node 22.23.2
Creado
2026-08-28T04:51:18Z

Contrato

  1. truncate returns the full string when byte length equals or exceeds string byte length
  2. truncate cuts ASCII string at exact byte boundary
  3. truncate safely cuts multi-byte UTF-8 characters without splitting code units
  4. truncate safely handles 4-byte surrogate pair emojis without splitting surrogate units
  5. truncate returns an empty string when max byte length is 0
  6. truncate throws error when input is not a string

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: truncate string by maximum byte length safely without splitting multi-byte UTF-8 characters
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/truncate-utf8-bytes@1.0.2
Demonstrate these symbols/APIs:
  - truncate-utf8-bytes

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:689741f302360525dd693202eabeaec08fc0146e950ab363a5eced682c17dc00","contract":["truncate returns the full string when byte length equals or exceeds string byte length","truncate cuts ASCII string at exact byte boundary","truncate safely cuts multi-byte UTF-8 characters without splitting code units","truncate safely handles 4-byte surrogate pair emojis without splitting surrogate units","truncate returns an empty string when max byte length is 0","truncate throws error when input is not a string"],"goal":"truncate string by maximum byte length safely without splitting multi-byte UTF-8 characters","kind":"HOW","packages":["pkg:npm/truncate-utf8-bytes@1.0.2"],"schemaVersion":1,"symbols":["truncate-utf8-bytes"]},"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/truncate-utf8-bytes@1.0.2"],"schemaVersion":1,"subject":"pkg:npm/truncate-utf8-bytes@1.0.2","symbols":["truncate-utf8-bytes"],"verifierAdapter":"node-typescript@1"}
index.js
'use strict';

const truncate = require('truncate-utf8-bytes');

/**
 * Truncate a UTF-8 string to a maximum number of bytes without breaking multi-byte characters.
 * @param {string} str - The input string to truncate.
 * @param {number} maxBytes - Maximum allowed bytes.
 * @returns {string} - Truncated string within maxBytes.
 */
function truncateUtf8(str, maxBytes) {
  return truncate(str, maxBytes);
}

module.exports = {
  truncateUtf8,
  truncate
};
package-lock.json
{
  "name": "sample-truncate-utf8-bytes",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-truncate-utf8-bytes",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "truncate-utf8-bytes": "1.0.2"
      }
    },
    "node_modules/truncate-utf8-bytes": {
      "version": "1.0.2",
      "resolved": "https://registry.npmjs.org/truncate-utf8-bytes/-/truncate-utf8-bytes-1.0.2.tgz",
      "integrity": "sha512-95Pu1QXQvruGEhv62XCMO3Mm90GscOCClvrIUwCM0PYOXK3kaF3l3sIHxx71ThJfcbM2O5Au6SO3AWCSEfW4mQ==",
      "license": "WTFPL",
      "dependencies": {
        "utf8-byte-length": "^1.0.1"
      }
    },
    "node_modules/utf8-byte-length": {
      "version": "1.0.5",
      "resolved": "https://registry.npmjs.org/utf8-byte-length/-/utf8-byte-length-1.0.5.tgz",
      "integrity": "sha512-Xn0w3MtiQ6zoz2vFyUVruaCL53O/DwUvkEeOvj+uulMm0BkUGYWmBYVyElqZaSLhY6ZD0ulfU3aBra2aVT4xfA==",
      "license": "(WTFPL OR MIT)"
    }
  }
}
package.json
{
  "name": "sample-truncate-utf8-bytes",
  "version": "1.0.0",
  "description": "truncate string by maximum byte length safely without splitting multi-byte UTF-8 characters",
  "main": "index.js",
  "scripts": {
    "test": "node test/contract.js"
  },
  "dependencies": {
    "truncate-utf8-bytes": "1.0.2"
  },
  "license": "MIT-0"
}
spec.json
{
  "schemaVersion": 1,
  "goal": "truncate string by maximum byte length safely without splitting multi-byte UTF-8 characters",
  "kind": "HOW",
  "packages": [
    "pkg:npm/truncate-utf8-bytes@1.0.2"
  ],
  "symbols": [
    "truncate-utf8-bytes"
  ]
}
test/contract.js
'use strict';

const assert = require('assert');
const truncate = require('truncate-utf8-bytes');
const { truncateUtf8 } = require('../index.js');

// 1. truncate returns the full string when byte length equals or exceeds string byte length
{
  const input = 'hello world';
  assert.strictEqual(truncate(input, 20), 'hello world');
  assert.strictEqual(truncate(input, 11), 'hello world');
  assert.strictEqual(truncateUtf8(input, 20), 'hello world');
}

// 2. truncate cuts ASCII string at exact byte boundary
{
  const input = 'hello world';
  assert.strictEqual(truncate(input, 5), 'hello');
  assert.strictEqual(truncate(input, 4), 'hell');
  assert.strictEqual(truncateUtf8(input, 5), 'hello');
}

// 3. truncate safely cuts multi-byte UTF-8 characters without splitting code units
{
  const input = '안녕하세요';
  assert.strictEqual(Buffer.byteLength(input, 'utf8'), 15);
  assert.strictEqual(truncate(input, 3), '안');
  assert.strictEqual(truncate(input, 4), '안');
  assert.strictEqual(truncate(input, 5), '안');
  assert.strictEqual(truncate(input, 6), '안녕');
  assert.strictEqual(truncate(input, 7), '안녕');
  assert.strictEqual(truncate(input, 8), '안녕');
  assert.strictEqual(truncate(input, 9), '안녕하');
  assert.strictEqual(truncateUtf8(input, 6), '안녕');
}

// 4. truncate safely handles 4-byte surrogate pair emojis without splitting surrogate units
{
  const input = '🐶🐱🐭';
  assert.strictEqual(Buffer.byteLength(input, 'utf8'), 12);
  assert.strictEqual(truncate(input, 3), '');
  assert.strictEqual(truncate(input, 4), '🐶');
  assert.strictEqual(truncate(input, 7), '🐶');
  assert.strictEqual(truncate(input, 8), '🐶🐱');
  assert.strictEqual(truncateUtf8(input, 8), '🐶🐱');
}

// 5. truncate returns an empty string when max byte length is 0
{
  assert.strictEqual(truncate('test', 0), '');
  assert.strictEqual(truncate('한글', 0), '');
  assert.strictEqual(truncateUtf8('test', 0), '');
}

// 6. truncate throws error when input is not a string
{
  assert.throws(() => {
    truncate(123, 5);
  }, /Input must be string/);

  assert.throws(() => {
    truncate(null, 5);
  }, /Input must be string/);
}

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

Seeder de origen

anónimo