最近在写 TS ,希望实现一个类似 .gitignore 的功能,已有代码如下:
/**
* Check if a file should be ignored based on patterns
*/
export function shouldIgnore(filePath: string, ignorePatterns: string[]): boolean {
// Simple implementation of glob pattern matching
return ignorePatterns.some(pattern => {
if (pattern.endsWith('/**')) {
const dirPattern = pattern.slice(0, -3);
return filePath.startsWith(dirPattern);
} else if (pattern.startsWith('**/*.')) {
const ext = pattern.slice(3);
return filePath.endsWith(ext);
} else if (pattern.includes('*')) {
const regex = new RegExp('^' + pattern.replace(/\*/g, '.*') + '$');
return regex.test(path.basename(filePath));
}
return filePath.includes(pattern);
});
}
在 JS 层面使用正则匹配,显然不是什么好方案。单元测试也过不了。
于是整理代码和报错信息,找了四个免费的模型,问问思路。
先说结论:DeepSeek-r1 思考了 298 秒,我一度以为其陷入了死循环,但最后其在第一轮给出的方案,被 Grok-3 、 Gemini 2.0 、 ChatGPT 统统认可是最优方案之一。且后三者都没有在第一轮对话给出这个方案。
下面来看具体过程。
第一轮
第一轮提问
请帮我排查错误原因并修正代码。我希望实现和 .gitignore 一样的效果。如有必要可以引入第三方匹配 lib (比如可以获取更高的性能、更全的功能)
先看最快的三个回答:
Grok-3
Grok-3 推荐了 minimatch 工具。
Gemini 2.0 Flash Thinking
Gemini 2.0 推荐了 micromatch 。
ChatGPT
ChatGPT-推理 最原生,推荐了 glob 。
在等了很久后, DeepSeek-r1 终于给出答案。我本来已经对其不抱希望。
DeepSeek-r1
其推荐了 ignore 工具。这大大引起我的兴趣,因为从名字而言,这是和我的 .gitignore 需求最接近的。
到底哪个工具好
于是我又问:minimatch 和 glob 和 ignore 这三个 lib 有什么区别?更推荐哪个?
ChatGPT
Gemini
Gemini
Grok
Grok
显然,最终 Grok 和 Gemini 明显更加推荐 DeepSeek-r1 一开始就推荐的 ignore 工具。
这里很疑惑:
- 明明 ignore 最合适,为什么这三家没有想到?(我的首轮提问中明明已经有了
.gitignore
这样的关键词?) - 为什么 DeepSeek-r1 思考了这么久,想到了 ignore ?思考时间与答案质量成正比吗?
但是看起来, DeepSeek-r1 的大部分时间在思考如何写算法。而最后,其才临门一脚想到了 ignore 。感觉本次实验取样不足,没有置信度。
对了,关于第二轮的问题“minimatch 和 glob 和 ignore 这三个 lib 有什么区别?更推荐哪个?”
,DeepSeek-r1 是怎么回答的呢?