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

calculate-string-similarity

v1.0.9

Published

A Node.js package to calculate string similarity.

Downloads

280

Readme

calculate-string-similarity

The calculate-string-similarity package is designed to provide advanced functionalities for comparing strings in Node.js applications. String matching and comparison are fundamental operations in many software applications, ranging from spell correction to text analysis and beyond. This package aims to simplify and enhance these operations by offering a comprehensive set of features and options.

Key Features

  • String Matching: The package offers robust string matching capabilities, allowing developers to compare strings with precision and accuracy.

  • Advanced Comparison: It goes beyond simple string equality checks and provides options for advanced comparison, including case sensitivity, order sensitivity, and more.

  • Customization: Developers can customize the comparison process according to their specific requirements. This includes fine-tuning parameters such as case sensitivity and order sensitivity to achieve the desired results.

Installation

npm install calculate-string-similarity

You can install calculate-string-similarity via npm:

Features:

1. String Similarity Calculator

Overview

The calculate-string-similarity package provides functionality for comparing strings with various options, including case sensitivity and order sensitivity.

const { getSimilarity } = require('calculate-string-similarity');

console.log("Test cases for getSimilarity function:");
console.log("----------------------------------------");

// Test case 1: Case-sensitive and order-sensitive comparison with identical strings
console.log("Test case 1:", getSimilarity("hello", "hello")); // Expected output: 100

// Test case 2: Case-insensitive comparison with identical strings
console.log("Test case 2:", getSimilarity("hello", "HELLO", { caseSensitive: false })); // Expected output: 100

// Test case 3: Case-sensitive and order-sensitive comparison with different strings
console.log("Test case 3:", getSimilarity("hello", "holle", { caseSensitive: true, orderSensitive: true })); // Expected output: 60

// Test case 4: Case-insensitive and order-insensitive comparison with partially different strings
console.log("Test case 4:", getSimilarity("hello", "holle", { caseSensitive: false, orderSensitive: false })); // Expected output: 100

// Test case 5: Custom strings with partial similarity
console.log("Test case 5:", getSimilarity("apple", "aple")); // Expected output: 80

// Test case 6: Null input strings
try {
  getSimilarity(null, "HELLO", { caseSensitive: false });
} catch (error) {
  console.log("Test case 6:", error.message); // Expected output: "Input strings are required."
}

// Test case 7: Invalid input parameters
try {
  getSimilarity("hello", "HELLO", { caseSensitive: "false" });
} catch (error) {
  console.log("Test case 7:", error.message); // Expected output: "Invalid input parameters."
}

Parameters

  • string1: The first string to compare.
  • string2: The second string to compare.
  • options: An optional object containing additional parameters:
    • caseSensitive: A boolean indicating whether the comparison should be case-sensitive. Default is true.
    • orderSensitive: A boolean indicating whether the comparison should be order-sensitive. Default is true.

2. Array String Similarity Calculator

Overview

This package provides a utility function getSimilarities to calculate similarities between a given input string and an array of strings. It can be useful for various applications such as spell correction, autocomplete suggestions, and text analysis.

const { getSimilarities } = require('calculate-string-similarity');
const inputString = "apple";
const stringList = ["appl", "apricot", "orange", "banana", "pineapple"];

console.log("Test cases for getSimilarities function:");
console.log("------------------------------------------");

// Test case 8: Case-sensitive and order-sensitive comparison with custom threshold and ordering
console.log("Test case 11:", getSimilarities("apple", ["aple", "banana", "orange"], { caseSensitive: true, orderSensitive: true, threshold: 30, thresholdType: '>=', orderBy: 'similarity', order: 'ascending', numberOfOutputs: 2 }));
// Expected output: [{ Input: 'aple', Similarity: 80 }, { Input: 'banana', Similarity: 0 }]

// Test case 9: Case-insensitive comparison with custom threshold and ordering
console.log("Test case 12:", getSimilarities("apple", ["aple", "banana", "orange"], { caseSensitive: false, threshold: 60, thresholdType: '<=', orderBy: 'string', order: 'descending' }));
// Expected output: [{ Input: 'orange', Similarity: 0 }, { Input: 'aple', Similarity: 80 }]

// Test case 10: Default behavior with order-insensitive comparison and custom threshold
console.log("Test case 13:", getSimilarities("apple", ["aple", "banana", "orange"], { orderSensitive: false, threshold: 70, thresholdType: '>' }));
// Expected output: [{ Input: 'aple', Similarity: 80 }]

// Test case 11: Null input strings
try {
  getSimilarities(null, ["aple", "banana", "orange"], { caseSensitive: false });
} catch (error) {
  console.log("Test case 14:", error.message); // Expected output: "Input strings are required."
}

// Test case 12: Invalid input parameters
try {
  getSimilarities("apple", ["aple", "banana", "orange"], { caseSensitive: "false" });
} catch (error) {
  console.log("Test case 15:", error.message); // Expected output: "Invalid input parameters."
}
// Test case 13: Case-insensitive comparison with custom threshold and ordering
console.log("Test case 12:", getSimilarities("apple", ["aple", "banana", "orange"], { caseSensitive: false, threshold: 60, thresholdType: '<=', orderBy: 'string', order: 'descending' }));
// Expected output: [{ Input: 'orange', Similarity: 0 }, { Input: 'aple', Similarity: 80 }]

// Test case 14: Default behavior with order-insensitive comparison and custom threshold
console.log("Test case 13:", getSimilarities("apple", ["aple", "banana", "orange"], { orderSensitive: false, threshold: 70, thresholdType: '>' }));
// Expected output: [{ Input: 'aple', Similarity: 80 }]

// Test case 15: Null input strings
try {
  getSimilarities(null, ["aple", "banana", "orange"], { caseSensitive: false });
} catch (error) {
  console.log("Test case 14:", error.message); // Expected output: "Input strings are required."
}

// Test case 16: Invalid input parameters
try {
  getSimilarities("apple", ["aple", "banana", "orange"], { caseSensitive: "false" });
} catch (error) {
  console.log("Test case 15:", error.message); // Expected output: "Invalid input parameters."
}

Parameters

  • inputString: The input string for which similarities need to be calculated.
  • stringList: An array of strings against which the similarity of the input string will be calculated.
  • options: An optional object containing additional parameters:
    • threshold: The minimum similarity percentage required for a string to be considered similar. Default is 0.
    • thresholdType: The type of threshold comparison to be applied for similarity percentage. It determines whether the similarity percentage should be greater than (>), greater than or equal to (>=), less than (<), or less than or equal to (<=) the specified threshold value. Default is >.
    • orderBy: The parameter to order the results by, either 'similarity' or 'name'. Default is 'similarity'.
    • order: The order in which to sort the results, either 'ascending' or 'descending'. Default is 'descending'.
    • numberOfOutputs: The number of top similarity results to return. Default is Infinity.