JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 12
  • Score
    100M100P100Q56963F
  • License MIT

A generic search algorithm for complex objects

Package Exports

  • search-algorithm
  • search-algorithm/dist/index.js
  • search-algorithm/dist/index.mjs

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (search-algorithm) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

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.