CodeSampleX

示例

emoji-regex 9.2.2

已验证示例 — npm emoji-regex 9.2.2. contract 在 node 22 · linux alpine/x64 · docker 上运行并通过: emojiRegex exports a factory function returning a regular expression

sha256:6b22911f11525e526ae40eb644c6939a897de04842e56083889de1b8b81efdf2

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

执行证据

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

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

验证运行环境

环境 契约 阶段 运行日期
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-23

案例

HOW
目标
verify pkg:npm/emoji-regex@9.2.2
包
创建时间
2026-08-23T13:41:20Z

契约

  1. emojiRegex exports a factory function returning a regular expression
  2. hasEmoji returns true when string contains standard emojis and false for plain text
  3. extractEmojis extracts all emoji occurrences from text
  4. extractEmojis correctly extracts complex emoji sequences including modifiers and zero-width joiners
  5. replaceEmojis replaces emojis in text with a replacement string or custom replacer function
  6. countEmojis returns the number of emojis found in text
  7. RGI_Emoji subpath exports a factory function returning regex for RGI emoji sequences
  8. text subpath exports a factory function returning regex for text presentation emojis

文件

  • 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/emoji-regex@9.2.2
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/emoji-regex@9.2.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:2d3a60100aec5f5437ba91b77666fdcfa607a3d10dc5d833574db67d527a7ffe","contract":["emojiRegex exports a factory function returning a regular expression","hasEmoji returns true when string contains standard emojis and false for plain text","extractEmojis extracts all emoji occurrences from text","extractEmojis correctly extracts complex emoji sequences including modifiers and zero-width joiners","replaceEmojis replaces emojis in text with a replacement string or custom replacer function","countEmojis returns the number of emojis found in text","RGI_Emoji subpath exports a factory function returning regex for RGI emoji sequences","text subpath exports a factory function returning regex for text presentation emojis"],"goal":"verify pkg:npm/emoji-regex@9.2.2","kind":"HOW","packages":["pkg:npm/emoji-regex@9.2.2"],"schemaVersion":1},"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/emoji-regex@9.2.2"],"schemaVersion":1,"subject":"pkg:npm/emoji-regex@9.2.2","verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "sample-emoji-regex",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-emoji-regex",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "emoji-regex": "9.2.2"
      }
    },
    "node_modules/emoji-regex": {
      "version": "9.2.2",
      "resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-9.2.2.tgz",
      "integrity": "sha512-L18DaJsXSUk2+42pv8mLs5jJT2hqFkFE4j21wOmgbUqsZ2hL72NsUU785g9RXgo3s0ZNgVl42TiHp3ZtOv/Vyg==",
      "license": "MIT"
    }
  }
}
package.json
{
  "name": "sample-emoji-regex",
  "version": "1.0.0",
  "private": true,
  "description": "Clean-room code sample for emoji-regex@9.2.2",
  "main": "src/index.js",
  "license": "MIT-0",
  "dependencies": {
    "emoji-regex": "9.2.2"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:npm/emoji-regex@9.2.2",
  "kind": "HOW",
  "packages": [
    "pkg:npm/emoji-regex@9.2.2"
  ]
}
src/index.js
const emojiRegex = require('emoji-regex');
const rgiEmojiRegex = require('emoji-regex/RGI_Emoji.js');
const textEmojiRegex = require('emoji-regex/text.js');

/**
 * Creates a regular expression that matches all standard emoji symbols.
 *
 * @returns {RegExp} A new regular expression instance matching emojis.
 */
function createEmojiRegex() {
  return emojiRegex();
}

/**
 * Creates a regular expression that matches RGI (Recommended for General Interchange) emoji sequences.
 *
 * @returns {RegExp} A regular expression instance matching RGI emojis.
 */
function createRgiEmojiRegex() {
  return rgiEmojiRegex();
}

/**
 * Creates a regular expression that matches emojis in text presentation.
 *
 * @returns {RegExp} A regular expression instance matching text-presentation emojis.
 */
function createTextEmojiRegex() {
  return textEmojiRegex();
}

/**
 * Determines whether a string contains any emoji character.
 *
 * @param {string} text - The input string to check.
 * @returns {boolean} True if the string contains at least one emoji, otherwise false.
 */
function hasEmoji(text) {
  if (typeof text !== 'string') return false;
  return createEmojiRegex().test(text);
}

/**
 * Extracts all emoji characters and sequences found in the string.
 *
 * @param {string} text - The input string to extract emojis from.
 * @returns {string[]} An array of matched emoji strings.
 */
function extractEmojis(text) {
  if (typeof text !== 'string') return [];
  const regex = createEmojiRegex();
  const matches = [];
  for (const match of text.matchAll(regex)) {
    matches.push(match[0]);
  }
  return matches;
}

/**
 * Replaces all emoji occurrences in a string with a replacement string or callback.
 *
 * @param {string} text - The input string.
 * @param {string|Function} replacement - The replacement string or function.
 * @returns {string} The modified string with emojis replaced.
 */
function replaceEmojis(text, replacement = '') {
  if (typeof text !== 'string') return '';
  const regex = createEmojiRegex();
  return text.replace(regex, replacement);
}

/**
 * Counts the total number of emoji occurrences in the given text.
 *
 * @param {string} text - The input string.
 * @returns {number} Total count of emojis found.
 */
function countEmojis(text) {
  return extractEmojis(text).length;
}

module.exports = {
  emojiRegex,
  rgiEmojiRegex,
  textEmojiRegex,
  createEmojiRegex,
  createRgiEmojiRegex,
  createTextEmojiRegex,
  hasEmoji,
  extractEmojis,
  replaceEmojis,
  countEmojis
};
test/contract.mjs
import assert from 'node:assert/strict';
import { createRequire } from 'node:module';

const require = createRequire(import.meta.url);
const {
  emojiRegex,
  rgiEmojiRegex,
  textEmojiRegex,
  createEmojiRegex,
  createRgiEmojiRegex,
  createTextEmojiRegex,
  hasEmoji,
  extractEmojis,
  replaceEmojis,
  countEmojis
} = require('../src/index.js');

async function testAll() {
  // 1. emojiRegex exports a factory function returning a regular expression
  assert.equal(typeof emojiRegex, 'function', 'emojiRegex default export must be a function');
  const standardRegex = createEmojiRegex();
  assert.ok(standardRegex instanceof RegExp, 'createEmojiRegex must return a RegExp instance');
  assert.ok(standardRegex.global, 'emoji regex must have the global flag enabled');

  // 2. hasEmoji returns true when string contains standard emojis and false for plain text
  assert.equal(hasEmoji('Hello 😊 World'), true, 'should detect standard emoji in text');
  assert.equal(hasEmoji('Hello World'), false, 'should return false for plain ascii text');
  assert.equal(hasEmoji('Numbers 12345 and punctuation !@#$'), false, 'should return false for symbols and numbers');
  assert.equal(hasEmoji(''), false, 'should return false for empty string');
  assert.equal(hasEmoji(null), false, 'should handle non-string values gracefully');

  // 3. extractEmojis extracts all emoji occurrences from text
  const simpleText = '🚀 Launching rocket to 🌙 moon!';
  const simpleExtracted = extractEmojis(simpleText);
  assert.deepEqual(simpleExtracted, ['🚀', '🌙'], 'should extract individual emojis in order');

  // 4. extractEmojis correctly extracts complex emoji sequences including modifiers and zero-width joiners
  const complexText = 'Doctor: 👩‍⚕️, rainbow flag: 🏳️‍🌈, dark skin tone: 👨🏿';
  const complexExtracted = extractEmojis(complexText);
  assert.deepEqual(
    complexExtracted,
    ['👩‍⚕️', '🏳️‍🌈', '👨🏿'],
    'should extract complex emoji sequences with ZWJ and modifier characters intact'
  );

  // 5. replaceEmojis replaces emojis in text with a replacement string or custom replacer function
  const replacedWithTag = replaceEmojis('Hello 👋 World 🌍!', '[EMOJI]');
  assert.equal(replacedWithTag, 'Hello [EMOJI] World [EMOJI]!', 'should replace emojis with placeholder string');

  const stripped = replaceEmojis('Hello 🚀 World 🌟!');
  assert.equal(stripped, 'Hello  World !', 'should remove emojis when replacement defaults to empty string');

  const customReplaced = replaceEmojis('Rating: ⭐⭐⭐', (match) => `(${match})`);
  assert.equal(customReplaced, 'Rating: (⭐)(⭐)(⭐)', 'should support replacer function');

  // 6. countEmojis returns the number of emojis found in text
  assert.equal(countEmojis('🎉 Congratulations 🥳 on your achievement! 👏'), 3, 'should count 3 emojis correctly');
  assert.equal(countEmojis('No emojis here.'), 0, 'should return 0 when no emojis are present');
  assert.equal(countEmojis('👩‍👩‍👧‍👦'), 1, 'should count multi-codepoint family emoji as 1 emoji');

  // 7. RGI_Emoji subpath exports a factory function returning regex for RGI emoji sequences
  assert.equal(typeof rgiEmojiRegex, 'function', 'RGI_Emoji export must be a function');
  const rgiRegex = createRgiEmojiRegex();
  assert.ok(rgiRegex instanceof RegExp, 'createRgiEmojiRegex must return a RegExp instance');
  assert.ok(rgiRegex.test('🧑‍🚀'), 'RGI regex should match valid astronaut emoji');

  // 8. text subpath exports a factory function returning regex for text presentation emojis
  assert.equal(typeof textEmojiRegex, 'function', 'text export must be a function');
  const textRegex = createTextEmojiRegex();
  assert.ok(textRegex instanceof RegExp, 'createTextEmojiRegex must return a RegExp instance');

  console.log('All emoji-regex contract assertions passed successfully.');
}

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

原始种子者

匿名