CodeSampleX

サンプル

json-stable-stringify-without-jsonify 1.0.1: stringify

検証済みサンプル — npm json-stable-stringify-without-jsonify 1.0.1: stringify. node 22 · linux debian/x64 · docker で contract を実行し、成功しました.

sha256:69ffc299da3e9cb29a64c81af3890ac1dfe903b825797277e5d81e97363e2a8b

このネットワークが提供するのは一つだけです。ビルドされるサンプル。サンドボックスで実行し、署名済みの受領証を保管します。等級はつけず、何も保証しません — 同じコードがあなたの環境でビルドされるかは測定していません。 合格した契約受領証を提出した異なる署名鍵の数です。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-01

ケース

HOW
ゴール
verify stringify in pkg:npm/json-stable-stringify-without-jsonify@1.0.1
パッケージ
シンボル
  • stringify
作成日
2026-09-01T19:51:48Z

コントラクト

  1. stringify deterministically sorts object keys in alphabetical order regardless of insertion order
  2. stringify supports custom indentation formatting using numeric and string space options
  3. stringify supports custom key comparator functions to define explicit sorting order
  4. stringify supports custom replacer functions to transform or filter object properties
  5. stringify safely handles circular references by returning a placeholder when cycles option is enabled
  6. stringify throws a TypeError on circular references when cycles option is disabled
  7. stringify respects custom toJSON serialization methods on objects
  8. stringify handles nested structures containing arrays, objects, and primitive values

ファイル

  • PROMPT.md
  • csx.json
  • package-lock.json
  • package.json
  • spec.json
  • src/index.mjs
  • 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 stringify in pkg:npm/json-stable-stringify-without-jsonify@1.0.1
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/json-stable-stringify-without-jsonify@1.0.1
Demonstrate these symbols/APIs:
  - stringify

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:a508428e5d204c21b9f1bb6bdbe463c8239eb056a6746c5434b02f4689d8fd9c","contract":["stringify deterministically sorts object keys in alphabetical order regardless of insertion order","stringify supports custom indentation formatting using numeric and string space options","stringify supports custom key comparator functions to define explicit sorting order","stringify supports custom replacer functions to transform or filter object properties","stringify safely handles circular references by returning a placeholder when cycles option is enabled","stringify throws a TypeError on circular references when cycles option is disabled","stringify respects custom toJSON serialization methods on objects","stringify handles nested structures containing arrays, objects, and primitive values"],"goal":"verify stringify in pkg:npm/json-stable-stringify-without-jsonify@1.0.1","kind":"HOW","packages":["pkg:npm/json-stable-stringify-without-jsonify@1.0.1"],"schemaVersion":1,"symbols":["stringify"]},"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/json-stable-stringify-without-jsonify@1.0.1"],"schemaVersion":1,"subject":"pkg:npm/json-stable-stringify-without-jsonify@1.0.1","symbols":["stringify"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "sample-json-stable-stringify-without-jsonify",
  "version": "1.0.1",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-json-stable-stringify-without-jsonify",
      "version": "1.0.1",
      "license": "MIT-0",
      "dependencies": {
        "json-stable-stringify-without-jsonify": "1.0.1"
      }
    },
    "node_modules/json-stable-stringify-without-jsonify": {
      "version": "1.0.1",
      "resolved": "https://registry.npmjs.org/json-stable-stringify-without-jsonify/-/json-stable-stringify-without-jsonify-1.0.1.tgz",
      "integrity": "sha512-Bdboy+l7tA3OGW6FjyFHWkP5LuByj1Tk33Ljyq0axyzdk9//JSi2u3fP1QSmd1KNwq6VOKYGlAu87CisVir6Pw==",
      "license": "MIT"
    }
  }
}
package.json
{
  "name": "sample-json-stable-stringify-without-jsonify",
  "version": "1.0.1",
  "type": "module",
  "private": true,
  "license": "MIT-0",
  "scripts": {
    "test": "node test/contract.mjs"
  },
  "dependencies": {
    "json-stable-stringify-without-jsonify": "1.0.1"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify stringify in pkg:npm/json-stable-stringify-without-jsonify@1.0.1",
  "kind": "HOW",
  "packages": [
    "pkg:npm/json-stable-stringify-without-jsonify@1.0.1"
  ],
  "symbols": [
    "stringify"
  ]
}
src/index.mjs
import stringify from 'json-stable-stringify-without-jsonify';

/**
 * Deterministically stringifies a JavaScript value into a stable JSON string.
 * @param {any} obj The value to serialize
 * @param {object|Function} [opts] Options object or comparison function
 * @returns {string|undefined} The serialized JSON string
 */
export function stableStringify(obj, opts) {
  return stringify(obj, opts);
}

export { stringify };
export default stringify;
test/contract.mjs
import assert from 'node:assert/strict';
import { stringify, stableStringify } from '../src/index.mjs';

// 1. stringify deterministically sorts object keys in alphabetical order regardless of insertion order
{
  const obj1 = { c: 3, a: 1, b: 2 };
  const obj2 = { b: 2, c: 3, a: 1 };
  const result1 = stringify(obj1);
  const result2 = stableStringify(obj2);

  assert.equal(result1, '{"a":1,"b":2,"c":3}');
  assert.equal(result2, '{"a":1,"b":2,"c":3}');
  assert.equal(result1, result2);
}

// 2. stringify supports custom indentation formatting using numeric and string space options
{
  const obj = { b: 2, a: 1 };
  const formattedWithNumber = stringify(obj, { space: 2 });
  assert.equal(formattedWithNumber, '{\n  "a": 1,\n  "b": 2\n}');

  const formattedWithString = stringify(obj, { space: '\t' });
  assert.equal(formattedWithString, '{\n\t"a": 1,\n\t"b": 2\n}');
}

// 3. stringify supports custom key comparator functions to define explicit sorting order
{
  const obj = { apple: 10, zebra: 20, banana: 30 };
  const customSorted = stringify(obj, {
    cmp: (a, b) => b.key.localeCompare(a.key)
  });
  assert.equal(customSorted, '{"zebra":20,"banana":30,"apple":10}');

  const cmpShorthand = stringify(obj, (a, b) => b.key.localeCompare(a.key));
  assert.equal(cmpShorthand, '{"zebra":20,"banana":30,"apple":10}');
}

// 4. stringify supports custom replacer functions to transform or filter object properties
{
  const obj = { id: 1, secret: 'password123', visible: true };
  const filtered = stringify(obj, {
    replacer: (key, value) => {
      if (key === 'secret') return undefined;
      if (typeof value === 'number') return value * 10;
      return value;
    }
  });
  assert.equal(filtered, '{"id":10,"visible":true}');
}

// 5. stringify safely handles circular references by returning a placeholder when cycles option is enabled
{
  const circularObj = { name: 'cycle-test' };
  circularObj.self = circularObj;

  const result = stringify(circularObj, { cycles: true });
  assert.equal(result, '{"name":"cycle-test","self":"__cycle__"}');
}

// 6. stringify throws a TypeError on circular references when cycles option is disabled
{
  const circularObj = { name: 'cycle-error' };
  circularObj.self = circularObj;

  assert.throws(
    () => stringify(circularObj),
    (err) => err instanceof TypeError && /Converting circular structure to JSON/.test(err.message)
  );
}

// 7. stringify respects custom toJSON serialization methods on objects
{
  const customObj = {
    x: 10,
    toJSON() {
      return { normalized: this.x * 2 };
    }
  };
  const result = stringify({ item: customObj });
  assert.equal(result, '{"item":{"normalized":20}}');
}

// 8. stringify handles nested structures containing arrays, objects, and primitive values
{
  const nested = {
    users: [
      { z: 'last', a: 'first' },
      { y: 2, b: 1 }
    ],
    count: 2,
    active: true,
    empty: null
  };
  const result = stringify(nested);
  assert.equal(
    result,
    '{"active":true,"count":2,"empty":null,"users":[{"a":"first","z":"last"},{"b":1,"y":2}]}'
  );
}

console.log('Contract assertions passed.');

オリジンシーダー

匿名