find-similar-functions

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Similar Function Finder

相似函数查找工具使用指南

Use this skill before writing new JavaScript or TypeScript code that might overlap with existing helpers. The goal is to discover nearby functions, methods, constants, types, and interfaces early enough to reuse or extend them instead of creating duplicate behavior.
truffler
is a fuzzy symbol search tool. Treat it as a discovery layer: it points you to likely symbols, but you still need to inspect the code before deciding whether something is reusable.
在编写可能与现有辅助函数重叠的JavaScript或TypeScript代码前,请使用此技能。目标是尽早发现相关的函数、方法、常量、类型和接口,以便复用或扩展它们,而非创建重复的功能。
truffler
是一款模糊符号搜索工具。将其视为发现层:它会指向可能匹配的符号,但在决定是否可复用之前,你仍需要检查代码细节。

Workflow

工作流程

  1. State the behavior you are about to implement in a few words.
  2. Derive 3-6 search queries from the intended behavior:
    • the proposed function or class name
    • domain nouns, such as
      symbol
      ,
      path
      ,
      file
      ,
      route
      ,
      token
      , or
      config
    • verbs, such as
      parse
      ,
      normalize
      ,
      discover
      ,
      scan
      ,
      format
      ,
      rank
      ,
      score
      ,
      resolve
      , or
      validate
    • common abbreviations and synonyms, such as
      btn
      for
      button
      or
      cfg
      for
      config
  3. Run
    truffler
    against the narrowest useful root first, then broaden if needed.
  4. Inspect the top matches with normal code-reading tools before editing.
  5. Prefer reusing, extending, or moving existing code when the behavior substantially matches. Create new code only after the search shows there is not a suitable existing symbol.
  6. In your final response, briefly mention what you found and whether you reused something or intentionally added a new implementation.
  1. 用几句话描述你即将实现的功能。
  2. 根据预期功能生成3-6个搜索查询词:
    • 拟命名的函数或类名称
    • 领域名词,例如
      symbol
      path
      file
      route
      token
      config
    • 动词,例如
      parse
      normalize
      discover
      scan
      format
      rank
      score
      resolve
      validate
    • 常见缩写和同义词,例如用
      btn
      指代
      button
      ,用
      cfg
      指代
      config
  3. 先在最精准的根目录下运行
    truffler
    ,若需要再扩大范围。
  4. 使用常规代码阅读工具检查排名靠前的匹配结果,再进行编辑。
  5. 当现有符号的功能与需求高度匹配时,优先选择复用、扩展或迁移现有代码。仅当搜索结果显示没有合适的现有符号时,再创建新代码。
  6. 在最终回复中,简要说明你发现了什么,以及是复用了现有代码还是有意添加了新实现。

Command Recipes

命令示例

When
truffler
is installed in the target project:
bash
truffler "normalize path" src --kind function,method,constant,type --limit 20
truffler "score" src --kind function,method --format json --limit 15
When working inside this
truffler
repository:
bash
bun src/cli.ts "discover file" src --kind function,method,constant,type --limit 20
bun src/cli.ts "format result" src --kind function,method --format json --limit 15
When the project has no installed binary and you should not add dependencies, use
bunx
if network/package execution is acceptable for the environment:
bash
bunx @rayhanadev/truffler "parse config" src --kind function,method,type --limit 20
If
truffler
cannot be run, say so and fall back to the repository's available search tools. Still follow the same deduplication intent: search before implementing.
当目标项目中已安装
truffler
时:
bash
truffler "normalize path" src --kind function,method,constant,type --limit 20
truffler "score" src --kind function,method --format json --limit 15
truffler
自身代码库中工作时:
bash
bun src/cli.ts "discover file" src --kind function,method,constant,type --limit 20
bun src/cli.ts "format result" src --kind function,method --format json --limit 15
当项目未安装二进制文件且不应添加依赖时,如果环境允许网络/包执行,可使用
bunx
bash
bunx @rayhanadev/truffler "parse config" src --kind function,method,type --limit 20
若无法运行
truffler
,请说明情况并改用代码库中可用的搜索工具。仍需遵循相同的去重原则:先搜索再实现。

Search Strategy

搜索策略

Start with symbols most likely to represent reusable behavior:
bash
truffler "<query>" <root> --kind function,method,constant,type,interface --limit 20
Use JSON output when you need structured fields for ranking, locations, signatures, or automation:
bash
truffler "<query>" <root> --format json --limit 20
Broaden deliberately:
  • If a precise function name has no match, search the domain noun and verb separately.
  • If a utility may live outside
    src/
    , try known library, package, app, or test roots.
  • If there are many matches, restrict
    --kind
    and reduce the root rather than skimming unrelated output.
  • If top matches look close, read the implementation and nearby tests or call sites before deciding.
从最可能代表可复用功能的符号开始搜索:
bash
truffler "<query>" <root> --kind function,method,constant,type,interface --limit 20
当需要用于排序、定位、签名或自动化的结构化字段时,使用JSON输出:
bash
truffler "<query>" <root> --format json --limit 20
有策略地扩大搜索范围:
  • 如果精确的函数名称没有匹配结果,分别搜索领域名词和动词。
  • 如果工具类可能位于
    src/
    之外,尝试已知的库、包、应用或测试根目录。
  • 如果匹配结果过多,限制
    --kind
    参数并缩小根目录范围,而非浏览无关输出。
  • 如果排名靠前的结果看起来相近,在做决定前先查看实现代码及相关测试或调用位置。

Reuse Decision Guide

复用决策指南

Consider an existing symbol reusable when it already handles the same input shape, side effects, error behavior, and return shape, or when a small extension would preserve its current contract.
Avoid reusing a symbol just because its name is close. Fuzzy matches can surface unrelated code. Inspect signatures, implementation, callers, and tests before changing anything.
When no suitable symbol exists, keep the new implementation near the closest related module and align with that module's naming, error handling, and test style.
当现有符号已处理相同的输入格式、副作用、错误行为和返回格式,或只需少量扩展即可保持其现有约定时,可认为该符号可复用。
不要仅因名称相近就复用符号。模糊匹配可能会显示无关代码。在修改任何内容前,务必检查签名、实现、调用方和测试。
当没有合适的符号时,将新实现放在最接近的相关模块附近,并与该模块的命名、错误处理和测试风格保持一致。

Reporting Template

报告模板

Use a short note like this when the search affects your implementation:
markdown
I checked for existing symbols with `truffler` using queries like `normalize`, `path`, and `resolve`. The closest match was `normalizePath` in `src/files.ts`, so I reused that behavior instead of adding a separate helper.
If nothing relevant exists:
markdown
I searched with `truffler` for `parse`, `config`, and `loadConfig` across `src/`; the matches were unrelated, so I added a new helper in the nearest module.
当搜索结果影响你的实现时,可使用如下简短说明:
markdown
我使用 `truffler``normalize``path``resolve` 为查询词进行了搜索。最接近的匹配是 `src/files.ts` 中的 `normalizePath`,因此我复用了该功能,而非添加单独的辅助函数。
若未找到相关内容:
markdown
我使用 `truffler``src/` 目录下搜索了 `parse``config``loadConfig`;匹配结果均不相关,因此我在最近的模块中添加了一个新的辅助函数。