CodeSampleX

示例

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

已验证示例 — npm json-stable-stringify-without-jsonify 1.0.1: stringify. contract 在 node 22 · linux debian/x64 · docker 上运行并通过: stringify deterministically sorts…

sha256:69ffc299da3e9cb29a64c81af3890ac1dfe903b825797277e5d81e97363e2a8b

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

原始种子者

匿名