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. contract 在 node 22 …

sha256:d26d7bb73d2b2b2c115cd3fc5efffc963200ed7764398de20be6a89ca95d2c84

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。 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.');

原始种子者

匿名