Algorith est une bibliothèque Node.js modulaire offrant une collection complète d'algorithmes de similarité textuelle et un moteur de génération aléatoire avancé. Le projet suit les principes de conception moderne JavaScript avec une approche fonctionnelle et une architecture extensible.
algorith/
├── 📁 algorithms/ # Modules d'algorithmes
│ ├── cosineSimilarity.js # Similarité cosinus
│ ├── diceCoefficient.js # Coefficient de Dice
│ ├── hamming.js # Distance de Hamming
│ ├── jaccardSimilarity.js # Similarité de Jaccard
│ ├── jaro-winkler.js # Algorithme Jaro-Winkler
│ ├── jaro.js # Algorithme Jaro
│ ├── levenshtein.js # Distance de Levenshtein
│ ├── RandomEngine.js # Moteur de génération aléatoire
│ ├── soundex.js # Algorithme Soundex
│ └── trigramScore.js # Score basé sur les trigrammes
├── 📁 test/ # Suite de tests
│ ├── similarity.test.js # Tests des algorithmes de similarité
│ └── randomEngine.test.js # Tests du moteur aléatoire
├── 📁 docs/ # Documentation (générée)
├── 📄 index.js # Point d'entrée principal
├── 📄 index.d.ts # Définitions TypeScript
├── 📄 benchmark.js # Outils de performance
├── 📄 package.json # Configuration npm
├── 📄 README.md # Documentation utilisateur
├── 📄 EXAMPLES.md # Exemples pratiques
├── 📄 CONTRIBUTING.md # Guide de contribution
├── 📄 CHANGELOG.md # Historique des versions
├── 📄 LICENSE # Licence MIT
└── 📄 .gitignore # Configuration Git
- Fonction Pure : Chaque algorithme est une fonction pure sans effet de bord
- Interface Uniforme :
function(str1, str2, options?) => number [0,1] - Validation Robuste : Gestion des cas limites et validation des types
// Pattern standard pour chaque algorithme
module.exports = function algorithmName(str1, str2, options = {}) {
// 1. Validation des entrées
if (typeof str1 !== 'string' || typeof str2 !== 'string') {
throw new Error('Both parameters must be strings');
}
// 2. Gestion des cas limites
if (str1 === str2) return 1;
if (str1.length === 0 || str2.length === 0) return 0;
// 3. Logique de l'algorithme
// ...
// 4. Normalisation du résultat [0,1]
return normalizedScore;
};| Algorithme | Complexité | Cas d'Usage | Performance |
|---|---|---|---|
| Hamming | O(n) | Chaînes de même longueur | ⭐⭐⭐⭐⭐ |
| Levenshtein | O(n×m) | Correction orthographique | ⭐⭐⭐ |
| Jaro | O(n×m) | Noms de personnes | ⭐⭐⭐⭐ |
| Jaro-Winkler | O(n×m) | Préfixes communs | ⭐⭐⭐⭐ |
| Jaccard | O(n+m) | Ensembles de mots | ⭐⭐⭐⭐ |
| Cosine | O(n+m) | Analyse de documents | ⭐⭐⭐⭐ |
| Dice | O(n+m) | Bigrammes | ⭐⭐⭐⭐ |
| Trigram | O(n+m) | Recherche floue | ⭐⭐⭐⭐ |
class RandomEngine {
constructor(seed = Date.now()) {
this.seed = seed;
this.state = this.initializeState(seed);
}
// Interface publique cohérente
uniform() { /* ... */ }
normal(mean, stddev) { /* ... */ }
// ... autres méthodes
}1. Générateur PRNG (Linear Congruential Generator)
- Algorithme déterministe avec seed
- État interne de 32 bits
- Période de ~2^32 valeurs
2. Distributions Probabilistes
- Normale : Box-Muller transform
- Exponentielle : Méthode d'inversion
- Poisson : Algorithme de Knuth
- Binomiale : Simulation directe
- Géométrique : Méthode d'inversion
3. Génération de Bruit Procédural
- Perlin 1D : Interpolation cubique avec gradients
- Value Noise : Interpolation de valeurs aléatoires
- White/Pink Noise : Génération spectrale
// index.js - Point d'entrée unifié
const algorithms = {
levenshtein: require('./algorithms/levenshtein'),
jaroWinkler: require('./algorithms/jaro-winkler'),
// ...
};
function compareAll(str1, str2) {
return Object.keys(algorithms).reduce((results, name) => {
results[name] = algorithms[name](str1, str2);
return results;
}, {});
}// Sélection dynamique d'algorithme
function chooseBestAlgorithm(context) {
if (context.sameLength) return 'hamming';
if (context.shortStrings) return 'jaro';
if (context.documents) return 'cosine';
return 'levenshtein';
}const random = new RandomEngine(12345)
.configure({ distribution: 'normal' })
.seed(newSeed);1. Early Returns
if (str1 === str2) return 1;
if (str1.length === 0 || str2.length === 0) return 0;2. Algorithmes Optimisés
- Levenshtein : Optimisation de l'espace O(min(n,m))
- Jaro : Pré-calcul des correspondances
- Trigram : Cache des n-grammes
3. Memory Management
// Réutilisation des buffers pour éviter les allocations
const buffer = new Array(maxLength);
function optimizedAlgorithm(str1, str2) {
// Réutiliser buffer au lieu de créer de nouveaux arrays
}Le système de benchmark surveille :
- Latence moyenne par opération
- Throughput (opérations/seconde)
- Utilisation mémoire
- Tests de stress avec grandes données
1. Tests Unitaires (114 tests)
describe('Algorithm', () => {
it('should handle basic cases', () => { /* ... */ });
it('should handle edge cases', () => { /* ... */ });
it('should be deterministic', () => { /* ... */ });
it('should validate performance', () => { /* ... */ });
});2. Tests de Propriétés
- Symétrie :
f(a,b) === f(b,a) - Identité :
f(a,a) === 1 - Normalisation :
0 ≤ f(a,b) ≤ 1
3. Tests d'Intégration
- Validation cross-algorithme
- Tests de régression
- Validation des formats d'export
1. Validation des Types
function validateInputs(str1, str2) {
if (typeof str1 !== 'string') throw new TypeError('str1 must be string');
if (typeof str2 !== 'string') throw new TypeError('str2 must be string');
}2. Validation des Plages
function validateRange(value, min = 0, max = 1) {
if (value < min || value > max) {
throw new RangeError(`Value ${value} outside range [${min}, ${max}]`);
}
}1. Template Standard
// algorithms/newAlgorithm.js
module.exports = function newAlgorithm(str1, str2, options = {}) {
// Validation
// Logique
// Normalisation
return score;
};2. Intégration
// index.js
const newAlgorithm = require('./algorithms/newAlgorithm');
module.exports = { ...existing, newAlgorithm };3. Tests
// test/newAlgorithm.test.js
// Tests standard + tests spécifiques1. Middleware Pattern
function withLogging(algorithm) {
return function(str1, str2, options) {
console.time(algorithm.name);
const result = algorithm(str1, str2, options);
console.timeEnd(algorithm.name);
return result;
};
}2. Plugin System
const enhanced = algorith.use(loggingPlugin, cachingPlugin);1. Optimisation des Exports
// Exports ES6 et CommonJS
module.exports = algorithms;
module.exports.default = algorithms;2. Tree-shaking Support
// Exports nommés pour bundlers modernes
export { levenshtein, jaroWinkler };
export default algorithms;1. TypeScript Support
- Définitions complètes dans
index.d.ts - Support IntelliSense et auto-complétion
- Validation de types au build
2. Documentation Générée
- JSDoc → Markdown automatique
- Exemples exécutables
- Benchmarks intégrés
| Métrique | Objectif | Actuel |
|---|---|---|
| Test Coverage | >95% | 100% |
| Build Time | <30s | ~10s |
| Package Size | <100KB | ~45KB |
| Latency P95 | <1ms | ~0.1ms |
1. CI/CD Pipeline
- Tests automatiques sur commit
- Benchmarks de régression
- Validation cross-platform
2. Performance Monitoring
- Tracking des métriques par version
- Alertes sur régressions
- Profiling automatique
- Support Bruit 2D/3D
- WebAssembly pour performances critiques
- Streaming API pour grandes données
- Cache intelligent adaptatif
- Support navigateur (ESM)
- Worker threads pour parallélisation
- Machine Learning pour optimisations
- API GraphQL pour microservices
Cette architecture garantit maintenabilité, performance et extensibilité pour l'évolution future du projet.