CodeSampleX

Exemple

jest-regex-util 29.6.3: Escape special regex characters in strings using jest-regex-util escapeStrForRegex

Échantillon vérifié pour npm jest-regex-util 29.6.3: Escape special regex characters in strings using jest-regex-util escapeStrForRegex. Le contrat s'est…

sha256:b920b7181cf55760cfae70012ddb33c4d30bb56d99e6ceb84f9b64e268887bf6

Ce réseau offre une seule chose : un échantillon qui compile. Il l'a exécuté dans un bac à sable et conservé le reçu signé. Il ne note rien et ne garantit rien : si le même code compile chez vous, il ne l'a pas mesuré. Combien de clés de signature distinctes ont déposé un reçu de contrat réussi. Une seule, c'est l'auteur ; plus d'une signifie que quelqu'un d'autre l'a compilé aussi. Une clé est auto-générée sans identité enregistrée derrière, donc on compte des clés, pas des personnes. MIT-0

Preuves d'exécution

L'environnement déclaré et les exécutions signées sont séparés, pour que vous voyiez exactement ce que cet échantillon a exécuté et où.

Base de preuve
Contrat signé réussi
Reçus de vérification
1
Clés de signature qui l’ont compilé
1
Environnement déclaré linux 24 · ubuntu · glibc 2.39 x64 npm

Environnements des exécutions de vérification

Environnement Contrat Étapes Exécution
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-27

Cas

HOW
Objectif
Escape special regex characters in strings using jest-regex-util escapeStrForRegex
Paquets
Symboles
  • escapeStrForRegex
Créé
2026-08-27T12:16:13Z

Contrat

  1. escapeStrForRegex escapes all regular expression metacharacters with backslashes
  2. escapeStrForRegex escapes metacharacters within complex strings
  3. RegExp constructed from escaped string matches original string verbatim
  4. escapeStrForRegex leaves strings without metacharacters unchanged

Fichiers

  • NOTES.md
  • PROMPT.md
  • csx.json
  • package-lock.json
  • package.json
  • spec.json
  • test/contract.mjs

Télécharger l’artefact source (tar.gz)

Code source

NOTES.md
This contract measures the behavior of `escapeStrForRegex` from `jest-regex-util@29.6.3`.

It asserts that all regular expression metacharacters (`[`, `]`, `{`, `}`, `(`, `)`, `*`, `+`, `?`, `.`, `\`, `^`, `$`, `|`) are escaped with a preceding backslash, ensuring that a `RegExp` constructed from the escaped string matches the input literally and that strings without metacharacters remain unaltered.
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: Escape special regex characters in strings using jest-regex-util escapeStrForRegex
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/jest-regex-util@29.6.3

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:69989df27f1634f0c0746fd2266584db02eed2d70cd465620d2bcdeb21b0d90a","contract":["escapeStrForRegex escapes all regular expression metacharacters with backslashes","escapeStrForRegex escapes metacharacters within complex strings","RegExp constructed from escaped string matches original string verbatim","escapeStrForRegex leaves strings without metacharacters unchanged"],"goal":"Escape special regex characters in strings using jest-regex-util escapeStrForRegex","kind":"HOW","packages":["pkg:npm/jest-regex-util@29.6.3"],"schemaVersion":1,"symbols":["escapeStrForRegex"]},"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/jest-regex-util@29.6.3"],"schemaVersion":1,"subject":"pkg:npm/jest-regex-util@29.6.3","symbols":["escapeStrForRegex"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "sample-jest-regex-util",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-jest-regex-util",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "jest-regex-util": "29.6.3"
      }
    },
    "node_modules/jest-regex-util": {
      "version": "29.6.3",
      "resolved": "https://registry.npmjs.org/jest-regex-util/-/jest-regex-util-29.6.3.tgz",
      "integrity": "sha512-KJJBsRCyyLNWCNBOvZyRDnAIfUiRJ8v+hOBQYGn8gDyF3UegwiP4gwRR3/SDa42g1YbVycTidUF3rKjyLFDWbg==",
      "license": "MIT",
      "engines": {
        "node": "^14.15.0 || ^16.10.0 || >=18.0.0"
      }
    }
  }
}
package.json
{
  "name": "sample-jest-regex-util",
  "version": "1.0.0",
  "private": true,
  "type": "module",
  "license": "MIT-0",
  "scripts": {
    "test": "node test/contract.mjs"
  },
  "dependencies": {
    "jest-regex-util": "29.6.3"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "Escape special regex characters in strings using jest-regex-util escapeStrForRegex",
  "kind": "HOW",
  "packages": [
    "pkg:npm/jest-regex-util@29.6.3"
  ]
}
test/contract.mjs
import assert from 'node:assert/strict';
import { escapeStrForRegex } from 'jest-regex-util';

// 1. Assert each special regex character is escaped with a backslash
const metacharacters = ['[', ']', '{', '}', '(', ')', '*', '+', '?', '.', '\\', '^', '$', '|'];
for (const char of metacharacters) {
  assert.equal(
    escapeStrForRegex(char),
    `\\${char}`,
    `escapeStrForRegex should escape regular expression character: ${char}`
  );
}

// 2. Assert escapeStrForRegex escapes all metacharacters within a complex string
const complexInput = 'resource (v1.0) [dev] *+?^$|{}';
const escaped = escapeStrForRegex(complexInput);
assert.equal(
  escaped,
  'resource \\(v1\\.0\\) \\[dev\\] \\*\\+\\?\\^\\$\\|\\{\\}',
  'escapeStrForRegex should escape all regex metacharacters in a complex string'
);

// 3. Assert RegExp constructed from escaped string matches the original string literally
const pattern = new RegExp(`^${escaped}$`);
assert.ok(
  pattern.test(complexInput),
  'RegExp compiled from escaped string must match original string verbatim'
);

// 4. Assert strings without regex metacharacters are unchanged
const alphanumeric = 'standard_filename-123';
assert.equal(
  escapeStrForRegex(alphanumeric),
  alphanumeric,
  'escapeStrForRegex should not modify strings without regex metacharacters'
);

Seeder d'origine

anonyme