CodeSampleX

示例

levn 0.4.1

已验证示例 — npm levn 0.4.1. contract 在 node 22 · linux alpine/x64 · docker 上运行并通过: levn.parse parses primitive types including Number, String, Boolean, Undefined…

sha256:5ec5cf30077b7cbb8e4ada47ee5c099bd60caf25e53e72ab3690571db6cadec8

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

执行证据

声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。

证据依据
签名契约通过
验证回执
1
构建过它的签名密钥
1
声明的环境 node 22.23 linux 24 · ubuntu · glibc 2.39 x64 node 22.23 javascript npm

验证运行环境

环境 契约 阶段 运行日期
node 22 · linux alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1
2026-08-20

案例

HOW
目标
verify pkg:npm/levn@0.4.1
包
环境
node 22.23.2
创建时间
2026-08-20T22:03:18Z

契约

  1. levn.parse parses primitive types including Number, String, Boolean, Undefined, Null, and NaN from string notation
  2. levn.parse parses composite types including arrays, tuples, and objects with nested type specifications
  3. levn.parse parses special types including Date literals and RegExp patterns with optional delimiters and flags
  4. levn.parse parses union types by attempting candidate type conversions in left-to-right precedence order
  5. levn.parse supports explicit mode using wildcard type or explicit option to require unambiguous literal syntax
  6. levn.parse supports custom type definitions with custom typeOf, validate, and cast functions
  7. levn.parsedTypeParse parses input string values using a pre-parsed type structure from type-check
  8. levn.VERSION exposes the package version string

文件

  • 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 pkg:npm/levn@0.4.1
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/levn@0.4.1
Constraints:
  - executionContext: node
Required runtime conditions:
  - ecosystem: npm
  - language: javascript
  - moduleSystem: cjs
  - runtime: node@22.23.2

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:abf7cd61c92b17a44158235a72d7def9ad7d7530d738a1026a26075588a49180","constraints":{"executionContext":"node"},"contract":["levn.parse parses primitive types including Number, String, Boolean, Undefined, Null, and NaN from string notation","levn.parse parses composite types including arrays, tuples, and objects with nested type specifications","levn.parse parses special types including Date literals and RegExp patterns with optional delimiters and flags","levn.parse parses union types by attempting candidate type conversions in left-to-right precedence order","levn.parse supports explicit mode using wildcard type or explicit option to require unambiguous literal syntax","levn.parse supports custom type definitions with custom typeOf, validate, and cast functions","levn.parsedTypeParse parses input string values using a pre-parsed type structure from type-check","levn.VERSION exposes the package version string"],"goal":"verify pkg:npm/levn@0.4.1","kind":"HOW","packages":["pkg:npm/levn@0.4.1"],"schemaVersion":1},"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","runtime":"node","runtimeVersion":"22.23.2","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/levn@0.4.1"],"schemaVersion":1,"subject":"pkg:npm/levn@0.4.1","verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "sample-levn",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-levn",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "levn": "0.4.1"
      }
    },
    "node_modules/levn": {
      "version": "0.4.1",
      "resolved": "https://registry.npmjs.org/levn/-/levn-0.4.1.tgz",
      "integrity": "sha512-+bT2uH4E5LGE7h/n3evcS/sQlJXCpIp6ym8OWJ5eV6+67Dsql/LaaT7qJBAt2rzfoa/5QBGBhxDix1dMt2kQKQ==",
      "license": "MIT",
      "dependencies": {
        "prelude-ls": "^1.2.1",
        "type-check": "~0.4.0"
      },
      "engines": {
        "node": ">= 0.8.0"
      }
    },
    "node_modules/prelude-ls": {
      "version": "1.2.1",
      "resolved": "https://registry.npmjs.org/prelude-ls/-/prelude-ls-1.2.1.tgz",
      "integrity": "sha512-vkcDPrRZo1QZLbn5RLGPpg/WmIQ65qoWWhcGKf/b5eplkkarX0m9z8ppCat4mlOqUsWpyNuYgO3VRyrYHSzX5g==",
      "license": "MIT",
      "engines": {
        "node": ">= 0.8.0"
      }
    },
    "node_modules/type-check": {
      "version": "0.4.0",
      "resolved": "https://registry.npmjs.org/type-check/-/type-check-0.4.0.tgz",
      "integrity": "sha512-XleUoc9uwGXqjWwXaUTZAmzMcFZ5858QA2vvx1Ur5xIcixXIP+8LnFDgRplU30us6teqdlskFfu+ae4K79Ooew==",
      "license": "MIT",
      "dependencies": {
        "prelude-ls": "^1.2.1"
      },
      "engines": {
        "node": ">= 0.8.0"
      }
    }
  }
}
package.json
{
  "name": "sample-levn",
  "version": "1.0.0",
  "description": "Clean-room verification for levn",
  "main": "src/index.js",
  "scripts": {
    "test": "node test/contract.mjs"
  },
  "dependencies": {
    "levn": "0.4.1"
  },
  "license": "MIT-0"
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:npm/levn@0.4.1",
  "kind": "HOW",
  "packages": [
    "pkg:npm/levn@0.4.1"
  ],
  "constraints": {
    "executionContext": "node"
  },
  "runtimeConditions": {
    "ecosystem": "npm",
    "language": "javascript",
    "moduleSystem": "cjs",
    "runtime": "node@22.23.2"
  }
}
src/index.js
const levn = require('levn');

/**
 * Parses a string input into a JavaScript value based on the specified type.
 * @param {string} type - Type specification string.
 * @param {string} input - Levn formatted string input.
 * @param {object} [options] - Optional parsing options.
 * @returns {*} Parsed JavaScript value.
 */
function parseValue(type, input, options) {
  return levn.parse(type, input, options);
}

/**
 * Parses a string input using a pre-parsed type structure.
 * @param {object} parsedType - Parsed type structure.
 * @param {string} input - Levn formatted string input.
 * @param {object} [options] - Optional parsing options.
 * @returns {*} Parsed JavaScript value.
 */
function parseWithParsedType(parsedType, input, options) {
  return levn.parsedTypeParse(parsedType, input, options);
}

module.exports = {
  parseValue,
  parseWithParsedType,
  levn,
};
test/contract.mjs
import assert from 'node:assert';
import { createRequire } from 'node:module';

const require = createRequire(import.meta.url);
const { parseValue, parseWithParsedType, levn } = require('../src/index.js');
const typeCheck = require('type-check');

async function runContract() {
  // 1. levn.parse parses primitive types including Number, String, Boolean, Undefined, Null, and NaN from string notation
  assert.strictEqual(parseValue('Number', '42'), 42);
  assert.strictEqual(parseValue('Number', '3.14'), 3.14);
  assert.strictEqual(parseValue('String', 'hello world'), 'hello world');
  assert.strictEqual(parseValue('Boolean', 'true'), true);
  assert.strictEqual(parseValue('Boolean', 'false'), false);
  assert.strictEqual(parseValue('Undefined', 'undefined'), undefined);
  assert.strictEqual(parseValue('Null', 'null'), null);
  assert.strictEqual(Number.isNaN(parseValue('NaN', 'NaN')), true);

  // 2. levn.parse parses composite types including arrays, tuples, and objects with nested type specifications
  assert.deepStrictEqual(parseValue('[Number]', '1, 2, 3'), [1, 2, 3]);
  assert.deepStrictEqual(parseValue('[String]', 'foo, bar, baz'), ['foo', 'bar', 'baz']);
  assert.deepStrictEqual(parseValue('(String, Number, Boolean)', 'alice, 30, true'), ['alice', 30, true]);
  assert.deepStrictEqual(
    parseValue('{name: String, count: Number}', 'name: item, count: 5'),
    { name: 'item', count: 5 }
  );

  // 3. levn.parse parses special types including Date literals and RegExp patterns with optional delimiters and flags
  const parsedDate = parseValue('Date', '2025-01-15');
  assert.ok(parsedDate instanceof Date);
  assert.strictEqual(parsedDate.getUTCFullYear(), 2025);

  const parsedRegExp = parseValue('RegExp', '/[a-z]+/i');
  assert.ok(parsedRegExp instanceof RegExp);
  assert.strictEqual(parsedRegExp.test('ABC'), true);

  const unquotedRegExp = parseValue('RegExp', 'hello');
  assert.ok(unquotedRegExp instanceof RegExp);
  assert.strictEqual(unquotedRegExp.test('hello'), true);

  // 4. levn.parse parses union types by attempting candidate type conversions in left-to-right precedence order
  assert.strictEqual(parseValue('Number | String', '123'), 123);
  assert.strictEqual(parseValue('Number | String', 'sample-text'), 'sample-text');
  assert.strictEqual(parseValue('Boolean | Number', 'true'), true);
  assert.strictEqual(parseValue('Boolean | Number', '99'), 99);

  // 5. levn.parse supports explicit mode using wildcard type or explicit option to require unambiguous literal syntax
  assert.deepStrictEqual(parseValue('*', '[1, "two", true]'), [1, 'two', true]);
  assert.strictEqual(parseValue('String', 'value', { explicit: true }), 'value');
  assert.throws(() => {
    parseValue('RegExp', 'not-a-regex-literal', { explicit: true });
  }, /does not type check/);

  // 6. levn.parse supports custom type definitions with custom typeOf, validate, and cast functions
  const customOptions = {
    customTypes: {
      EvenNumber: {
        typeOf: 'Number',
        validate: function(x) {
          return typeof x === 'number' && x % 2 === 0;
        },
        cast: function(val) {
          const num = parseInt(val, 10);
          return isNaN(num) ? { type: 'Nothing' } : { type: 'Just', value: num };
        }
      }
    }
  };
  assert.strictEqual(parseValue('EvenNumber', '8', customOptions), 8);
  assert.throws(() => {
    parseValue('EvenNumber', '9', customOptions);
  }, /does not type check/);

  // 7. levn.parsedTypeParse parses input string values using a pre-parsed type structure from type-check
  const parsedType = typeCheck.parseType('[Number]');
  assert.deepStrictEqual(parseWithParsedType(parsedType, '10, 20, 30'), [10, 20, 30]);

  // 8. levn.VERSION exposes the package version string
  assert.strictEqual(typeof levn.VERSION, 'string');
  assert.strictEqual(levn.VERSION, '0.4.1');

  console.log('Contract tests passed successfully.');
}

runContract().catch((err) => {
  console.error('Contract test failed:', err);
  process.exit(1);
});

原始种子者

匿名