A generic search algorithm for complex objects
npm install search-algorithm
.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.
`typescript
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.
`typescript
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.
`typescript
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.
`typescript
import { getObjectValues } from './getObjectValues';
import { compareStringsWithDistance } from './compareStringsWithDistance';
export function searchObjects(objects: any[], target: string, threshold: number): any[] {
// Implementation here...
}
`
Usage
$3
1. Clone the repository:
`sh
git clone
`
2. Navigate to the project directory:
`sh
cd search-algorithm
`
3. Install the dependencies:
`sh
npm install
`
$3
To run the tests, use the following command:
`sh
npm test
`
$3
Here is an example of how to use the searchObjects function:
`typescript
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.
`typescript
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;
`
$3
This file configures tsup for bundling the project.
`typescript
import { defineConfig } from 'tsup';
export default defineConfig({
format: ['cjs', 'esm'],
entry: ['src/index.ts'],
dts: true,
shims: true,
skipNodeModulesBundle: true,
clean: true
});
``