CodeSampleX

サンプル

sqids 0.3.0: decode

検証済みサンプル — npm sqids 0.3.0: decode. node 22 · linux debian/x64 · docker で contract を実行し、成功しました: decode decodes an encoded ID back to the original array of…

sha256:9913bb53dfc1b506dcb408b2244814354a0c54ae483adb48ab59165c6f7bcb6e

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

実行証拠

宣言された環境と署名済みの実行を分けてあります。このサンプルが何をどこで実行したかをそのまま確認できます。

証拠の基準
署名済みコントラクト合格
検証レシート
1
ビルドした署名鍵
1
宣言された環境 linux 24 · ubuntu · glibc 2.39 x64 npm

検証実行環境

環境 コントラクト ステージ 実行日
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 decode in pkg:npm/sqids@0.3.0
パッケージ
シンボル
  • decode
作成日
2026-09-11T23:04:21Z

コントラクト

  1. decode decodes an encoded ID back to the original array of numbers
  2. decode returns an empty array when decoding an empty string
  3. decode returns an empty array when ID contains characters outside the alphabet
  4. decode correctly decodes IDs generated with custom alphabet
  5. decode correctly decodes IDs generated with custom minLength padding
  6. decode accurately decodes multiple numbers and single numbers

ファイル

  • PROMPT.md
  • csx.json
  • package-lock.json
  • package.json
  • spec.json
  • src/index.js
  • 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 decode 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:
  - decode

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:4871211a9ecd3a509bd398d0f40ab5534b4b912145f9c4f17077712c5818db3b","contract":["decode decodes an encoded ID back to the original array of numbers","decode returns an empty array when decoding an empty string","decode returns an empty array when ID contains characters outside the alphabet","decode correctly decodes IDs generated with custom alphabet","decode correctly decodes IDs generated with custom minLength padding","decode accurately decodes multiple numbers and single numbers"],"goal":"verify decode in pkg:npm/sqids@0.3.0","kind":"HOW","packages":["pkg:npm/sqids@0.3.0"],"schemaVersion":1,"symbols":["decode"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"npm","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/sqids@0.3.0"],"schemaVersion":1,"subject":"pkg:npm/sqids@0.3.0","symbols":["decode"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "sample-sqids-decode",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-sqids-decode",
      "version": "1.0.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-decode",
  "version": "1.0.0",
  "private": true,
  "description": "Clean-room verification of decode in sqids",
  "type": "module",
  "main": "src/index.js",
  "dependencies": {
    "sqids": "0.3.0"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify decode in pkg:npm/sqids@0.3.0",
  "kind": "HOW",
  "packages": [
    "pkg:npm/sqids@0.3.0"
  ],
  "symbols": [
    "decode"
  ]
}
src/index.js
import Sqids from 'sqids';

/**
 * Creates a new Sqids instance with optional configuration.
 * @param {object} [options]
 * @param {string} [options.alphabet]
 * @param {number} [options.minLength]
 * @param {string[]|Set<string>} [options.blocklist]
 * @returns {Sqids}
 */
export function createSqids(options) {
  return new Sqids(options);
}

/**
 * Decodes a Sqids ID into an array of numbers using the provided or default Sqids instance.
 * @param {string} id
 * @param {object} [options]
 * @returns {number[]}
 */
export function decodeId(id, options) {
  const instance = new Sqids(options);
  return instance.decode(id);
}

export { Sqids };
export default Sqids;
test/contract.mjs
import assert from 'node:assert/strict';
import Sqids, { createSqids, decodeId } from '../src/index.js';

// 1. decode decodes an encoded ID back to the original array of numbers
const sqids = createSqids();
const numbers = [1, 2, 3];
const id = sqids.encode(numbers);
const decoded = sqids.decode(id);
assert.deepStrictEqual(decoded, numbers, 'decode must return original numbers array');

const helperDecoded = decodeId(id);
assert.deepStrictEqual(helperDecoded, numbers, 'decodeId helper must return original numbers array');

// 2. decode returns an empty array when decoding an empty string
assert.deepStrictEqual(sqids.decode(''), [], 'decoding empty string must return empty array');
assert.deepStrictEqual(decodeId(''), [], 'helper decoding empty string must return empty array');

// 3. decode returns an empty array when ID contains characters outside the alphabet
assert.deepStrictEqual(sqids.decode('!@#$%^&*()'), [], 'decoding string with invalid characters must return empty array');
assert.deepStrictEqual(decodeId('!@#$%^&*()'), [], 'helper decoding string with invalid characters must return empty array');

// 4. decode correctly decodes IDs generated with custom alphabet
const customAlphabetSqids = createSqids({
  alphabet: 'abcdefghijklmnopqrstuvwxyz0123456789',
});
const customNumbers = [42, 99, 1000];
const customId = customAlphabetSqids.encode(customNumbers);
assert.deepStrictEqual(
  customAlphabetSqids.decode(customId),
  customNumbers,
  'decode with custom alphabet must reproduce numbers'
);
assert.deepStrictEqual(
  decodeId(customId, { alphabet: 'abcdefghijklmnopqrstuvwxyz0123456789' }),
  customNumbers,
  'helper decodeId with custom alphabet options must reproduce numbers'
);

// 5. decode correctly decodes IDs generated with custom minLength padding
const paddedSqids = createSqids({ minLength: 10 });
const paddedNumbers = [7, 8, 9];
const paddedId = paddedSqids.encode(paddedNumbers);
assert.ok(paddedId.length >= 10, 'padded ID length must satisfy minLength');
assert.deepStrictEqual(
  paddedSqids.decode(paddedId),
  paddedNumbers,
  'decode must correctly unpack padded IDs'
);
assert.deepStrictEqual(
  decodeId(paddedId, { minLength: 10 }),
  paddedNumbers,
  'helper decodeId must correctly unpack padded IDs'
);

// 6. decode accurately decodes multiple numbers and single numbers
const singleNum = [12345];
const singleId = sqids.encode(singleNum);
assert.deepStrictEqual(sqids.decode(singleId), singleNum, 'decode single number');

const multiNums = [0, 1, 10, 100, 1000, 10000];
const multiId = sqids.encode(multiNums);
assert.deepStrictEqual(sqids.decode(multiId), multiNums, 'decode multiple numbers');

console.log('sqids decode contract tests passed successfully.');

オリジンシーダー

匿名