CodeSampleX

示例

@csstools/css-tokenizer 4.0.0: tokenize CSS string into tokens with @csstools/css-tokenizer

已验证示例 — npm @csstools/css-tokenizer 4.0.0: tokenize CSS string into tokens with @csstools/css-tokenizer. contract 在 node 22 · linux alpine/x64 · docker…

sha256:9ba4cfafda73ef248e0f1d2d38ab25a1799478143493ba46e506bb5d0fdc71ff

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

案例

HOW
目标
tokenize CSS string into tokens with @csstools/css-tokenizer
包
符号
  • tokenize
环境
node 22.23.2
创建时间
2026-08-28T10:10:19Z

契约

  1. tokenize is exported as a function from @csstools/css-tokenizer
  2. tokenize decomposes standard CSS declarations into CSSToken tuples containing type, raw representation, start and end offsets
  3. tokenize extracts structured token values for identifiers and dimensions with parsed units and numeric types
  4. tokenize with onParseError callback captures parser errors without throwing an unhandled exception
  5. stringify reconstructs valid CSS text from an array of tokens produced by tokenize

文件

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

下载源代码构件 (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: tokenize CSS string into tokens with @csstools/css-tokenizer
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/%40csstools/css-tokenizer@4.0.0
Demonstrate these symbols/APIs:
  - tokenize

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:a1757b70ebf19c36608b47fd8a858dc97956e6494bbbc4537bc781292fbcf9f0","contract":["tokenize is exported as a function from @csstools/css-tokenizer","tokenize decomposes standard CSS declarations into CSSToken tuples containing type, raw representation, start and end offsets","tokenize extracts structured token values for identifiers and dimensions with parsed units and numeric types","tokenize with onParseError callback captures parser errors without throwing an unhandled exception","stringify reconstructs valid CSS text from an array of tokens produced by tokenize"],"goal":"tokenize CSS string into tokens with @csstools/css-tokenizer","kind":"HOW","packages":["pkg:npm/%40csstools/css-tokenizer@4.0.0"],"schemaVersion":1,"symbols":["tokenize"]},"contractCommand":["node","test/contract.js"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","executionContext":"node","language":"javascript","libc":"glibc","libcVersion":"2.39","moduleSystem":"esm","os":"linux","osVersionBucket":"24","packageManager":"npm","packageManagerVersion":"10.9.8","runtime":"node","runtimeVersion":"22.23.2","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/%40csstools/css-tokenizer@4.0.0"],"schemaVersion":1,"subject":"pkg:npm/%40csstools/css-tokenizer@4.0.0","symbols":["tokenize"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "sample-csstools-css-tokenizer",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-csstools-css-tokenizer",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "@csstools/css-tokenizer": "4.0.0"
      }
    },
    "node_modules/@csstools/css-tokenizer": {
      "version": "4.0.0",
      "resolved": "https://registry.npmjs.org/@csstools/css-tokenizer/-/css-tokenizer-4.0.0.tgz",
      "integrity": "sha512-QxULHAm7cNu72w97JUNCBFODFaXpbDg+dP8b/oWFAZ2MTRppA3U00Y2L1HqaS4J6yBqxwa/Y3nMBaxVKbB/NsA==",
      "license": "MIT",
      "engines": {
        "node": ">=20.19.0"
      }
    }
  }
}
package.json
{
  "name": "sample-csstools-css-tokenizer",
  "version": "1.0.0",
  "private": true,
  "type": "module",
  "description": "Clean-room verification for @csstools/css-tokenizer tokenize",
  "main": "src/index.js",
  "scripts": {
    "test": "node test/contract.js"
  },
  "dependencies": {
    "@csstools/css-tokenizer": "4.0.0"
  },
  "license": "MIT-0"
}
spec.json
{
  "schemaVersion": 1,
  "goal": "tokenize CSS string into tokens with @csstools/css-tokenizer",
  "kind": "HOW",
  "packages": [
    "pkg:npm/%40csstools/css-tokenizer@4.0.0"
  ],
  "symbols": [
    "tokenize"
  ]
}
src/index.js
import {
  tokenize,
  tokenizer,
  TokenType,
  isToken,
  isTokenIdent,
  isTokenDimension,
  stringify,
  ParseError
} from '@csstools/css-tokenizer';

/**
 * Tokenizes a CSS string into an array of CSSToken items.
 * @param {string} css
 * @param {object} [options]
 * @returns {Array}
 */
export function tokenizeCSS(css, options = {}) {
  return tokenize({ css, unicodeRangesAllowed: options.unicodeRangesAllowed }, options);
}

/**
 * Filters an array of tokens by token type.
 * @param {Array} tokens
 * @param {string} type
 * @returns {Array}
 */
export function filterTokensByType(tokens, type) {
  return tokens.filter((token) => isToken(token) && token[0] === type);
}

/**
 * Tokenizes CSS while collecting any parsing errors encountered.
 * @param {string} css
 * @returns {{ tokens: Array, errors: Array<ParseError> }}
 */
export function tokenizeWithErrors(css) {
  const errors = [];
  const tokens = tokenize(
    { css },
    {
      onParseError: (err) => {
        errors.push(err);
      }
    }
  );
  return { tokens, errors };
}

/**
 * Reconstructs CSS string representation from an array of tokens.
 * @param {Array} tokens
 * @returns {string}
 */
export function stringifyTokens(tokens) {
  return stringify(...tokens);
}

export {
  tokenize,
  tokenizer,
  TokenType,
  isToken,
  isTokenIdent,
  isTokenDimension,
  stringify,
  ParseError
};
test/contract.js
import assert from 'node:assert/strict';
import {
  tokenize,
  TokenType,
  isToken,
  isTokenIdent,
  isTokenDimension,
  stringify,
  tokenizeCSS,
  filterTokensByType,
  tokenizeWithErrors,
  stringifyTokens
} from '../src/index.js';

// 1. tokenize is exported as a function from @csstools/css-tokenizer
assert.equal(typeof tokenize, 'function', 'tokenize must be a function');
assert.equal(typeof tokenizeCSS, 'function', 'tokenizeCSS helper must be a function');

// 2. tokenize decomposes standard CSS declarations into CSSToken tuples containing type, raw representation, start and end offsets
const cssSample = 'color: red; margin: 10px;';
const tokens = tokenizeCSS(cssSample);

assert.ok(Array.isArray(tokens), 'tokens must be an array');
assert.ok(tokens.length > 0, 'tokens array must not be empty');

for (const token of tokens) {
  assert.ok(isToken(token), 'each element must satisfy isToken');
  assert.equal(typeof token[0], 'string', 'token[0] (type) must be string');
  assert.equal(typeof token[1], 'string', 'token[1] (raw) must be string');
  assert.equal(typeof token[2], 'number', 'token[2] (start) must be number');
  assert.equal(typeof token[3], 'number', 'token[3] (end) must be number');
}

const firstToken = tokens[0];
assert.equal(firstToken[0], TokenType.Ident);
assert.equal(firstToken[1], 'color');
assert.equal(firstToken[2], 0);
assert.equal(firstToken[3], 4);
assert.deepEqual(firstToken[4], { value: 'color' });
assert.ok(isTokenIdent(firstToken), 'first token must satisfy isTokenIdent');

const lastToken = tokens[tokens.length - 1];
assert.equal(lastToken[0], TokenType.EOF);

// 3. tokenize extracts structured token values for identifiers and dimensions with parsed units and numeric types
const dimensionTokens = filterTokensByType(tokens, TokenType.Dimension);
assert.equal(dimensionTokens.length, 1, 'must find exactly one dimension token');

const dimToken = dimensionTokens[0];
assert.ok(isTokenDimension(dimToken), 'must satisfy isTokenDimension');
assert.equal(dimToken[1], '10px');
assert.equal(dimToken[4].value, 10);
assert.equal(dimToken[4].unit, 'px');
assert.equal(dimToken[4].type, 'integer');

const identTokens = filterTokensByType(tokens, TokenType.Ident);
const identValues = identTokens.map((t) => t[4].value);
assert.deepEqual(identValues, ['color', 'red', 'margin']);

// 4. tokenize with onParseError callback captures parser errors without throwing an unhandled exception
const malformedCSS = '"unclosed string\n';
const { tokens: errTokens, errors } = tokenizeWithErrors(malformedCSS);

assert.ok(Array.isArray(errTokens), 'errTokens must be an array');
assert.ok(Array.isArray(errors), 'errors must be an array');
assert.ok(errors.length > 0, 'must report at least one parse error for unclosed newline string');
assert.ok(errors[0].message.includes('string'), 'error message must indicate string parse error');
assert.equal(typeof errors[0].sourceStart, 'number', 'error must contain sourceStart offset');
assert.equal(typeof errors[0].sourceEnd, 'number', 'error must contain sourceEnd offset');

// 5. stringify reconstructs valid CSS text from an array of tokens produced by tokenize
const reconstructedCSS = stringifyTokens(tokens);
assert.equal(reconstructedCSS, cssSample, 'stringifyTokens must reconstruct original CSS string');

const directStringify = stringify(...tokens);
assert.equal(directStringify, cssSample, 'direct stringify must match original CSS string');

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

原始种子者

匿名