npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

equivalency

v3.13.0

Published

Declaratively define rules for string equivalence.

Downloads

580

Readme

Equivalency

Focus on the differences that matter.

Equivalency lets you declaratively define rules for string equivalence.

  • Several useful rules are provided out of the box, for example, Unicode normalization, capitalization, common puncutation and diacritical marks.
  • Custom rules can be created using plain strings, regexes, or functions.
  • Comparing two strings via an equivalency returns whether the two strings are equivalent according to that equivalency's ruleset, and can optionally return
    • the edit distance between the two fully transformed strings using the damerau-levenshtein algorithm
    • reasons why the strings differ
  • Equivalency instances can be cloned, making it easy to start with a root equivalency that takes care of universal concerns like Unicode normalization, then derive more specific equivalencies that are tailored to specific cases, like case- or punctuation-sensitivity.

Equivalency works in both Node and browsers back as far as IE 11 (full list of supported browsers).

Usage

const checker = require('equivalency');
const { Equivalency } = checker;

// Default rule is byte-equality.
checker.compare('a', 'a');
// { isEquivalent: true }

checker.compare('a', 'A');
// { isEquivalent: false }

// Specify which differences matter/don't matter.
checker.doesntMatter(Equivalency.CAPITALIZATION);
checker.compare('a', 'A');
// { isEquivalent: true }

checker.compare('Hot-dog', 'hotdog');
// { isEquivalent: false }

checker.doesntMatter(Equivalency.en.COMMON_PUNCTUATION);
checker.compare('Hot-dog', 'hotdog');
// { isEquivalent: true }

checker.compare('Go away, fly!', 'Go away; fly!');
// { isEquivalent: true }

checker.matters(',;');
checker.compare('Go away, fly!', 'Go away; fly!');
// { isEquivalent: false }

checker.compare('Go away, fly!', 'Go away; fly!',{giveReasons: true});
// { isEquivalent: false, reasons: [{name: ',;'}] }

// Return edit distance
const options = { calculateEditDistance: true };
checker.compare('show', 'shoe', options);
// { isEquivalent: false, editDistance: 1 }

const esChecker = new Equivalency();
esChecker.compare('adiós', 'adios');
// { isEquivalent: false }

const enChecker = new Equivalency();
enChecker.doesntMatter(Equivalency.ACCENTS);
enChecker.compare('adiós', 'adios');
// { isEquivalent: true }

// Root equivalency: normalizes Unicode, whitespace differences, and case.
const root = new Equivalency()
  .doesntMatter(Equivalency.UNICODE_NORMALIZATION)
  .doesntMatter(Equivalency.WHITESPACE_DIFFERENCES)
  .doesntMatter(Equivalency.CAPITALIZATION)

// Diacritic-blind equivalency cloned from root equivalency.
const equivalencyForDiacriticWarning = root
  .clone()
  .doesntMatter(Equivalency.COMMON_DIACRITICS);

const isMatch = root.compare(
  providedAnswer,
  expectedAnswer
).isEquivalent;

const isMatchExceptForDiacritics = equivalencyForDiacriticWarning.compare(
  providedAnswer,
  expectedAnswer
).isEquivalent;

Equivalency Rules are not applied strictly in the order they are supplied. All map rules are applied, and only then are function rules applied. Therefore, FunctionRules can apply transformations on top of MapRule transformations, but MapRules cannot apply transformations on top of FunctionRules. These two rule types are also applied in fundamentally different ways. MapRules are collapsed into a single map which is then used to transform the comparison strings in a single operation. When two MapRules have conflicting mappings, the mappings in the rule further down the rule chain takes precedence. FunctionRules cascade such that transformation operations are performed individually one after another in the order given.

Tests

BrowserStack
Status

Running tests

  • local: yarn test
  • Browserstack (for browser compatability, particular IE 11): BROWSER_STACK_ACCESS_KEY=<key> yarn run test:karma

Release steps

See the release doc.