CodeSampleX

샘플

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

검증된 샘플 — npm truncate-utf8-bytes 1.0.2: truncate string by maximum byte length safely without splitting multi-byte UTF-8 characters. node 22 · linux…

sha256:d26d7bb73d2b2b2c115cd3fc5efffc963200ed7764398de20be6a89ca95d2c84

이 네트워크가 제공하는 것은 하나입니다. 빌드되는 샘플. 샌드박스에서 돌리고 서명된 영수증을 보관합니다. 등급을 매기지 않고 무엇도 보증하지 않습니다 — 같은 코드가 당신 환경에서 빌드되는지는 측정한 적이 없습니다. 통과한 계약 영수증을 낸 서로 다른 서명 키의 수입니다. 하나면 작성자 혼자이고, 둘 이상이면 다른 사람도 빌드했다는 뜻입니다. 키는 스스로 만드는 것이고 뒤에 등록된 신원이 없으므로, 세는 것은 사람이 아니라 키입니다. MIT-0

실행 증거

선언된 환경과 서명된 실행을 분리해 두었습니다. 이 샘플이 무엇을 어디서 실행했는지 그대로 볼 수 있습니다.

증거 기준
서명된 컨트랙트 통과
검증 영수증
1
빌드한 서명 키
1
선언된 환경 node 22.23 linux 24 · ubuntu · glibc 2.39 x64 node 22.23 javascript npm 10

검증 실행 환경

환경 컨트랙트 단계 실행일
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

케이스

HOW
목표
truncate string by maximum byte length safely without splitting multi-byte UTF-8 characters
패키지
심벌
  • truncate-utf8-bytes
환경
node 22.23.2
생성일
2026-08-28T04:51:18Z

컨트랙트

  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

파일

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

소스 아티팩트 내려받기 (tar.gz)

소스

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.');

오리진 시더

익명