
Security News
Axios Supply Chain Attack Reaches OpenAI macOS Signing Pipeline, Forces Certificate Rotation
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.
ts-function-similarity
Advanced tools
Ultra-fast TypeScript function similarity analyzer with 5-8x performance improvements - supports multiple directories and files
A tool that recursively extracts TypeScript functions from a directory, parses them using the TypeScript AST parser, and compares them using Levenshtein distance to calculate similarity scores.
@typescript-eslint/typescript-estreenpm install -g ts-function-similarity
# Now you can use 'ts-similarity' command anywhere
ts-similarity ./src --min 70
cd ts-function-similarity
npm install
npm run build
# Optional: Link globally
npm link
ts-similarity ./src --min 70
# Analyze current directory
npm start
# Analyze specific directory
npm start ./src
# Analyze multiple directories
npm start ./src ./lib ./utils
# Analyze specific files
npm start src/file1.ts src/file2.ts
# Mix directories and files
npm start ./src file.ts
# Using --dir flag (can be used multiple times)
npm start -- -d ./src -d ./lib --min 70
# Show only functions with 70%+ similarity
npm start -- --min 70
# Find potential duplicates (90%+ similarity)
npm start -- --duplicates
# Limit results to top 10
npm start -- --limit 10
-d, --dir <path> - Directory or file to analyze (can be used multiple times, default: current directory)-m, --min <number> - Minimum similarity score to display (0-100, default: 50)--min-lines <number> - Minimum number of lines for functions to compare (default: 0, no filter)-l, --limit <number> - Maximum number of results to display (default: 20)--duplicates - Only show potential duplicates (similarity >= 90%)-h, --help - Show help messageThe tool supports analyzing multiple directories and files in a single run. This is useful for:
Cross-module comparison: Compare functions across different parts of your codebase
npm start ./src/components ./src/services ./src/utils
Monorepo analysis: Compare functions across multiple packages
npm start ./packages/core ./packages/ui ./packages/api
Specific file comparison: Compare only certain files
npm start src/auth.ts src/user.ts lib/helpers.ts
Mixed analysis: Combine directories and files
npm start ./src specific-file.ts
Output example:
Analyzing TypeScript files in 3 directories:
1. ./src/components
2. ./src/services
3. ./src/utils
Extracting functions...
./src/components: found 342 functions
./src/services: found 189 functions
./src/utils: found 127 functions
Total: 658 functions
import { FunctionExtractor } from './functionExtractor';
import { FunctionComparator } from './comparator';
// Extract functions from multiple directories
const extractor = new FunctionExtractor();
const functions1 = await extractor.extractFromDirectory('./src');
const functions2 = await extractor.extractFromDirectory('./lib');
const allFunctions = [...functions1, ...functions2];
// Compare all functions
const comparator = new FunctionComparator();
const results = comparator.compareAll(allFunctions, {
minSimilarity: 50,
normalize: true,
excludeSelf: true,
});
// Find duplicates
const duplicates = comparator.findDuplicates(allFunctions, 90);
@typescript-eslint/typescript-estreeThe Levenshtein distance measures the minimum number of single-character edits (insertions, deletions, or substitutions) needed to transform one string into another. This tool converts that distance into a similarity percentage:
similarity = ((max_length - distance) / max_length) × 100
The tool displays:
Analyzing TypeScript files in: src
Extracting functions...
Found 1204 functions
Comparing functions (724,806 comparisons)...
Progress: 25% (181,201/724,806)
Progress: 50% (362,403/724,806)
Progress: 75% (543,604/724,806)
Progress: 100% (724,806/724,806)
Found 10 similar function pairs
================================================================================
1. Similarity: 85.32% (Raw: 78.45%)
Function 1: calculateTotal (11 lines)
Location: src/utils/math.ts:15-25
Function 2: computeSum (11 lines)
Location: src/helpers/calculator.ts:42-52
================================================================================
The --min-lines option allows you to focus on substantial code blocks and ignore small utility functions that might coincidentally match.
Problem: Small functions (2-3 lines) often have high similarity scores but aren't meaningful duplicates:
// These might show 100% similarity but aren't really duplicates
const add = (a, b) => a + b;
const sum = (x, y) => x + y;
Solution: Use --min-lines to filter out small functions and prioritize larger code blocks:
# Only compare functions with 10 or more lines
npm start -- ./src --min-lines 10
# Find duplicates in substantial functions (15+ lines)
npm start -- ./src --duplicates --min-lines 15
# Focus on larger functions with high similarity
npm start -- ./src --min 80 --min-lines 20
The --min-lines filter provides dramatic performance improvements by pre-filtering functions before comparison:
$ npm start -- ./src --min-lines 10
# Example with 1204 functions:
# Without filter: 724,806 comparisons
# With --min-lines 10 (500 functions): 124,750 comparisons (83% reduction)
# With --min-lines 20 (200 functions): 19,900 comparisons (97% reduction)
Real-world example:
Filtering: 7 functions meet minimum 4 lines (5 filtered out)
Performance: 21 comparisons instead of 66 (68% reduction)
$ npm start -- ./src --min 70 --min-lines 10
# Without --min-lines: 150 results (many 2-3 line functions)
# With --min-lines 10: 23 results (only substantial functions)
ts-function-similarity/
├── src/
│ ├── index.ts # CLI entry point
│ ├── functionExtractor.ts # AST parsing and function extraction
│ ├── levenshtein.ts # Levenshtein distance algorithm
│ └── comparator.ts # Function comparison logic
├── package.json
├── tsconfig.json
└── README.md
When analyzing large codebases, consider these optimization strategies:
Use a higher similarity threshold: Set --min 80 or --min 90 to filter out most comparisons early
npm start -- ./src --min 90
Use duplicates mode: This automatically sets the threshold to 90%
npm start -- ./src --duplicates
Memory usage: The tool is configured with 8GB heap size by default. For very large codebases (2000+ functions), you may need more:
node --max-old-space-size=16384 dist/index.js ./src --min 90
Complexity: With N functions, the tool performs N×(N-1)/2 comparisons:
This package includes an MCP (Model Context Protocol) server that allows AI assistants like Claude to analyze your TypeScript code for function similarity.
See MCP.md for detailed setup instructions and usage examples.
Quick setup for Claude Desktop:
npm install -g ts-function-similarity~/Library/Application Support/Claude/claude_desktop_config.json):
{
"mcpServers": {
"ts-function-similarity": {
"command": "ts-similarity-mcp"
}
}
}
MIT - See LICENSE file for details
FAQs
Ultra-fast TypeScript function similarity analyzer with 5-8x performance improvements - supports multiple directories and files
The npm package ts-function-similarity receives a total of 5 weekly downloads. As such, ts-function-similarity popularity was classified as not popular.
We found that ts-function-similarity demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.

Security News
Open source is under attack because of how much value it creates. It has been the foundation of every major software innovation for the last three decades. This is not the time to walk away from it.

Security News
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.