You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 7-8.RSVP
Socket
Socket
Sign inDemoInstall

fuzzy-matching

Package Overview
Dependencies
1
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

fuzzy-matching

Fuzzy string matcher


Version published
Maintainers
1
Created

Readme

Source

fuzzy-matching

NPM version Build Status Dependencies Status devDependencies Status

Fuzzy string matcher

Installation

npm install fuzzy-matching

Code sample

var FuzzyMatching = require('fuzzy-matching');

var fm = new FuzzyMatching(['tough', 'thought', 'through', 'Café']);

// Finds words
console.log(fm.get('tough')); // --> { distance: 1, value: 'tough' }

// Finds words that are spelled wrong by looking at the closest ressembling word
console.log(fm.get('thouhgt')); // --> { distance: 0.7142857142857143, value: 'thought' }
// Beware when words in your dictionary are very close
console.log(fm.get('throught')); // --> { distance: 0.875, value: 'through' },
                                 // though you may have wanted to get 'thought'

// Case insensitive
console.log(fm.get('ThRouHg').value); // --> through

// Accent-proof
console.log(fm.get('cafe').value); // --> Café

// Add words after creation
console.log(fm.get('dinosaur')); // --> { distance: 0, value: null }
                                // because too remote to anything in the dictionary
fm.add('dinosaur');
console.log(fm.get('dinosaur')); // --> { distance: 1, value: 'dinosaur' }

// Want to limit to a certain degree of resemblance?
console.log(fm.get('touch', { maxChanges: 0 }).value); // --> null
console.log(fm.get('touch', { maxChanges: 1 }).value); // --> tough

Use case: quizzes or user inputs with certain expected answers

var FuzzyMatching = require('fuzzy-matching');

var possibleAnswers = ['Jupiter', 'Mercury', 'Venus', 'Earth'],
    fm = new FuzzyMatching(possibleAnswers),
    answer = 'Mercury';

console.log('Which planet is the closest to the Sun: ' + possibleAnswers.join(', ') + '?');

// Some user stuff...

var userAnswer = 'mercuyr';
var correctedAnswer = fm.get(userAnswer, { maxChanges: 2 }).value;
if (answer === correctedAnswer) {
    console.log('That\'s right!, it\'s ' + answer + '!');
} else {
    console.log('Sorry buddy, the answer was ' + answer + '.');
}

Keywords

FAQs

Package last updated on 04 May 2015

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc