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

このネットワークが提供するのは一つだけです。ビルドされるサンプル。サンドボックスで実行し、署名済みの受領証を保管します。等級はつけず、何も保証しません — 同じコードがあなたの環境でビルドされるかは測定していません。 合格した契約受領証を提出した異なる署名鍵の数です。1 なら作者だけ、2 以上なら他の誰かもビルドしています。鍵は自己生成で背後に登録された身元がないため、数えているのは人ではなく鍵です。 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.');

オリジンシーダー

匿名