CodeSampleX

示例

sqids 0.3.0: encode

已验证示例 — npm sqids 0.3.0: encode. contract 在 node 22 · linux debian/x64 · docker 上运行并通过: assert sqids.encode encodes array of numbers into a unique string ID

sha256:9288fcf5ed7a48059a1bdf7302a7475c66f67fb74cc9b8bddd5067538644a0bf

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 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 debian/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1node:22@sha256:8a34c4ab3ea2…
2026-09-11

案例

HOW
目标
verify encode in pkg:npm/sqids@0.3.0
包
符号
  • encode
环境
node 22.23.2
创建时间
2026-09-11T23:07:04Z

契约

  1. assert sqids.encode encodes array of numbers into a unique string ID
  2. assert sqids.encode produces distinct string IDs for different number arrays
  3. assert sqids.encode encodes empty array to empty string
  4. assert sqids.encode satisfies minimum length when minLength option is configured
  5. assert sqids.encode restricts output characters to provided custom alphabet
  6. assert sqids.encode throws error when negative numbers are provided

文件

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

下载源代码构件 (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: verify encode in pkg:npm/sqids@0.3.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/sqids@0.3.0
Demonstrate these symbols/APIs:
  - encode

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:0488ea56fa111e66247cf05cb5d9b8a9c0a9b781b1a6236fa2ce0389d8535a4b","contract":["assert sqids.encode encodes array of numbers into a unique string ID","assert sqids.encode produces distinct string IDs for different number arrays","assert sqids.encode encodes empty array to empty string","assert sqids.encode satisfies minimum length when minLength option is configured","assert sqids.encode restricts output characters to provided custom alphabet","assert sqids.encode throws error when negative numbers are provided"],"goal":"verify encode in pkg:npm/sqids@0.3.0","kind":"HOW","packages":["pkg:npm/sqids@0.3.0"],"schemaVersion":1,"symbols":["encode"]},"contractCommand":["node","test/contract.mjs"],"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/sqids@0.3.0"],"schemaVersion":1,"subject":"pkg:npm/sqids@0.3.0","symbols":["encode"],"verifierAdapter":"node-typescript@1"}
index.js
const sqidsModule = require('sqids');
const Sqids = sqidsModule.default || sqidsModule;

/**
 * Encodes an array of numbers into a unique string ID using Sqids.
 *
 * @param {number[]} numbers - Array of non-negative integers to encode.
 * @param {Object} [options] - Optional Sqids configuration options.
 * @returns {string} The encoded string ID.
 */
function encodeNumbers(numbers, options) {
  const instance = options ? new Sqids(options) : new Sqids();
  return instance.encode(numbers);
}

module.exports = {
  encodeNumbers,
  Sqids,
};
package-lock.json
{
  "name": "sample-sqids-encode",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-sqids-encode",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "sqids": "0.3.0"
      }
    },
    "node_modules/sqids": {
      "version": "0.3.0",
      "resolved": "https://registry.npmjs.org/sqids/-/sqids-0.3.0.tgz",
      "integrity": "sha512-lOQK1ucVg+W6n3FhRwwSeUijxe93b51Bfz5PMRMihVf1iVkl82ePQG7V5vwrhzB11v0NtsR25PSZRGiSomJaJw==",
      "license": "MIT"
    }
  }
}
package.json
{
  "name": "sample-sqids-encode",
  "version": "1.0.0",
  "description": "Clean-room sample verifying encode in pkg:npm/sqids@0.3.0",
  "main": "index.js",
  "license": "MIT-0",
  "dependencies": {
    "sqids": "0.3.0"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify encode in pkg:npm/sqids@0.3.0",
  "kind": "HOW",
  "packages": [
    "pkg:npm/sqids@0.3.0"
  ],
  "symbols": [
    "encode"
  ]
}
test/contract.mjs
import assert from 'node:assert';
import Sqids from 'sqids';
import { encodeNumbers } from '../index.js';

// 1. Basic encoding: encodes array of numbers to a unique string
const defaultSqids = new Sqids();
const encoded1 = defaultSqids.encode([1, 2, 3]);
assert.strictEqual(typeof encoded1, 'string');
assert.strictEqual(encoded1, '86Rf07', 'sqids.encode must encode [1, 2, 3] to 86Rf07');
assert.deepStrictEqual(
  defaultSqids.decode(encoded1),
  [1, 2, 3],
  'sqids.decode must decode back to the original array'
);

const encodedHelper = encodeNumbers([1, 2, 3]);
assert.strictEqual(encodedHelper, '86Rf07', 'encodeNumbers must produce identical result to sqids.encode');

// 2. Distinct inputs produce distinct strings
const encoded2 = defaultSqids.encode([1, 2, 4]);
assert.notStrictEqual(encoded1, encoded2, 'sqids.encode must produce different IDs for different inputs');

// 3. Empty array encoding: returns empty string
const encodedEmpty = defaultSqids.encode([]);
assert.strictEqual(encodedEmpty, '', 'sqids.encode must return empty string for empty array');

// 4. Custom minLength option: ensures minimum length of generated ID
const sqidsMin = new Sqids({ minLength: 10 });
const encodedMin = sqidsMin.encode([1, 2, 3]);
assert(encodedMin.length >= 10, 'sqids.encode must satisfy minLength requirement');
assert.strictEqual(encodedMin, '86Rf07xd4z', 'sqids.encode with minLength 10 matches expected output');
assert.deepStrictEqual(
  sqidsMin.decode(encodedMin),
  [1, 2, 3],
  'sqids.decode must decode minLength-padded IDs correctly'
);

// 5. Custom alphabet option: uses characters from the provided alphabet
const customAlphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
const sqidsCustom = new Sqids({ alphabet: customAlphabet });
const encodedCustom = sqidsCustom.encode([1, 2, 3]);
assert.strictEqual(encodedCustom, 'UOYbqf', 'sqids.encode with custom alphabet matches expected output');
for (const char of encodedCustom) {
  assert(customAlphabet.includes(char), `character ${char} must be within custom alphabet`);
}
assert.deepStrictEqual(
  sqidsCustom.decode(encodedCustom),
  [1, 2, 3],
  'sqids.decode must decode custom alphabet IDs correctly'
);

// 6. Error handling: throws error on negative numbers
assert.throws(
  () => {
    defaultSqids.encode([-1]);
  },
  {
    message: /Encoding supports numbers between 0 and/,
  },
  'sqids.encode must throw error when negative number is passed'
);

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

原始种子者

匿名