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

search-algorithm

v1.0.15

Published

A generic search algorithm for complex objects

Downloads

32

Readme

Search Algorithm

Overview

This project implements a generic search algorithm for complex objects. It allows you to search through an array of objects and find those that match a target string based on the Levenshtein distance. The algorithm is designed to handle deeply nested objects and arrays, and it can be configured to ignore empty and null values.

Project Structure

.gitignore
ignore-these/
jest.config.ts
package.json
src/
    compareStringsWithDistance.ts
    getObjectValues.ts
    index.ts
    levenshteinDistance.ts
tests/
    compareStringsWithDistance.test.ts
    getObjectValues.test.ts
    integrationtests.test.ts
    levenshteinDistance.test.ts
    systemtests.test copy.ts
    systemtests.test.ts
tsconfig.json

Key Components

levenshteinDistance.ts

This file contains the implementation of the Levenshtein distance algorithm, which calculates the minimum number of single-character edits required to change one word into another.

export function levenshteinDistance(word1: string, word2: string): number {
    // Implementation here...
}

getObjectValues.ts

This file contains a function that extracts all string values from a complex object, including nested objects and arrays.

export function getObjectValues(obj: any): string[] {
    // Implementation here...
}

compareStringsWithDistance.ts

This file contains a function that compares the values extracted from an object with a target string and returns an array of values with their corresponding Levenshtein distances.

import { levenshteinDistance } from './levenshteinDistance';
import { getObjectValues } from './getObjectValues';

export function compareStringsWithDistance(obj: any, target: string): { value: string, distance: number }[] {
    // Implementation here...
}

index.ts

This file contains the main search function that uses the above components to search through an array of objects and return those that match the target string based on a threshold score.

import { getObjectValues } from './getObjectValues';
import { compareStringsWithDistance } from './compareStringsWithDistance';

export function searchObjects(objects: any[], target: string, threshold: number): any[] {
    // Implementation here...
}

Usage

Installation

  1. Clone the repository:

    git clone <repository-url>
  2. Navigate to the project directory:

    cd search-algorithm
  3. Install the dependencies:

    npm install

Running Tests

To run the tests, use the following command:

npm test

Example

Here is an example of how to use the searchObjects function:

import { searchObjects } from './src/index';

const objectsArray = [
    { name: 'Alice', description: 'The quick brown fox.', extra: 'A swift animal.' },
    { name: 'Bob', description: 'Hello world!', extra: 'Just a friendly greeting.' },
    // More objects...
];

const targetString = 'quick brown fox';
const thresholdScore = 4;

const foundObjects = searchObjects(objectsArray, targetString, thresholdScore);

console.log(foundObjects);

Configuration

jest.config.ts

This file configures Jest for testing TypeScript files.

import type { Config } from 'jest';

const config: Config = {
    preset: 'ts-jest',
    testEnvironment: 'node',
    moduleFileExtensions: ['ts', 'js'],
    transform: {
        '^.+\\.tsx?$': 'ts-jest',
    },
    testMatch: ['**/tests/**/*.test.ts'],
    moduleDirectories: ['node_modules', 'src'],
};

export default config;

tsup.config.ts

This file configures tsup for bundling the project.

import { defineConfig } from 'tsup';

export default defineConfig({
    format: ['cjs', 'esm'],
    entry: ['src/index.ts'],
    dts: true,
    shims: true,
    skipNodeModulesBundle: true,
    clean: true
});

License

This project is licensed under the MIT License. See the LICENSE file for details.

Author

Stephen Wike


This README provides an overview of the project, its structure, key components, and instructions on how to use it. For more detailed information, refer to the source code and comments within the files.