-
-
Notifications
You must be signed in to change notification settings - Fork 1
Asynchronous API
For scenarios involving large datasets, high-latency metrics, or deferred computation (e.g., via Web Workers or remote metric services), CmpStr provides a fully asynchronous API. All asynchronous methods mirror the synchronous core API in naming, parameters, and return structures — with the sole difference that results are returned as Promise objects.
Asynchronous comparison is particularly useful in performance-sensitive or UI-bound environments, as well as when chaining computations across I/O boundaries without blocking the main execution flow.
Instances are created in the same way as with the synchronous API:
const cmpAsync = CmpStrAsync.create( options? );All configuration and option management methods (setOptions, mergeOptions, setOption, getOptions, etc.) are inherited from CmpStr and behave identically. Instance-level options can be overridden per call without affecting global state.
Below is the complete set of methods provided by the asynchronous CmpStr API. All methods return Promise objects and are intended to be used with await or standard promise chaining.
-
testAsync ( a: string, b: string, opt? ) : Promise< ResultLike >
Asynchronously compares two strings and returns a structured comparison result. Supports optional per-call configuration overrides. -
compareAsync ( a: string, b: string, opt? ) : Promise< number >
Performs the same comparison astestAsyncbut resolves to the normalized similarity score (0..1) only. -
pairsAsync ( a: MetricInput, b: MetricInput, opt? ) : Promise< BatchResultLike >
Performs index-wise pair comparisons betweena[i]andb[i]. Both input arrays must have the same length.
-
batchTestAsync ( a: MetricInput, b: MetricInput, opt? ) : Promise< BatchResultLike >
Performs a full cross-comparison of all elements inaagainst all elements inb(a × b). -
batchSortedAsync ( a: MetricInput, b: MetricInput, dir = 'desc', opt? ) : Promise< BatchResultLike >
Identical tobatchTestAsync, but resolves to a result set sorted by similarity score in ascending or descending order.
-
matchAsync ( a: MetricInput, b: MetricInput, threshold: number, opt? ) : Promise< BatchResultLike >
Returns only those comparisons whose similarity score is greater than or equal to the specifiedthreshold. -
closestAsync ( a: MetricInput, b: MetricInput, n = 1, opt? ) : Promise< BatchResultLike >
Resolves to thenmost similar matches with the highest scores. -
furthestAsync ( a: MetricInput, b: MetricInput, n = 1, opt? ) : Promise< BatchResultLike >
Resolves to thenleast similar matches with the lowest scores.
-
matrixAsync ( input: string[], opt? ) : Promise< number[][] >
Asynchronously computes a square similarity matrix for all pairwise combinations of the input values.
-
phoneticIndexAsync ( input: string, algo?: string, opt?: PhoneticOptions ) : Promise< string >
Asynchronous method for determining phonetic indexes of given strings. -
searchAsync ( needle: string, haystack: string[], flags?: NormalizeFlags, processors?: CmpStrProcessors ) : Promise< string[] >
Performs an asynchronous filtered and normalized substring search across thehaystackarray.
-
structuredLookupAsync ( query: string, data: T[], key: keyof T, opt?: StructuredDataOptions ) : Promise< StructuredResultLike >
Performs an asynchronous batch comparison against structured data by extracting a specific property and returning results with original objects attached. -
structuredMatchAsync ( query: string, data: T[], key: keyof T, threshold: number, opt?: StructuredDataOptions ) : Promise< StructuredResultLike >
Performs an asynchronous batch comparison and returns only results above the threshold for structured data. -
structuredClosestAsync ( query: string, data: T[], key: keyof T, n: number = 1, opt?: StructuredDataOptions ) : Promise< StructuredResultLike >
Returns thenclosest matches from a batch comparison of structured data. -
structuredFurthestAsync ( query: string, data: T[], key: keyof T, n: number = 1, opt?: StructuredDataOptions ) : Promise< StructuredResultLike >
Returns thenfurthest matches from a batch comparison of structured data. -
structuredPairsAsync ( data: T[], key: keyof T, other: O[], otherKey: keyof O, opt?: StructuredDataOptions ) : Promise< StructuredResultLike >
Performs an asynchronous pairwise comparison between two (different) arrays of structured objects by extracting specific properties and returning results with original objects attached.
import { CmpStrAsync } from 'cmpstr';
const cmp = CmpStrAsync.create( { metric: 'levenshtein', flags: 'i' } );
const result = await cmp.testAsync( 'kITTen', 'Sitting' );
// result: { source: 'kITTen', target: 'Sitting', match: 0.57142… }
const batch = await cmp.batchTestAsync( [ 'hello', 'Hallo' ], [ 'hey', 'hola' ] );
// batch: [ { source: 'hello', target: 'hey', match: 0.4 }, … ]
const matrix = await cmp.matrixAsync( [ 'foo', 'bar', 'baz' ] );
// matrix: [ [ 1, 0, 0 ], [ 0, 1, 0.666… ], [ 0, 0.666…, 1 ] ]- All configuration and option handling is inherited from the synchrounous API.
- All methods return
Promiseobjects. - The API is designed for use with
async/awaitas well as.then()chaining. - Method semantics and result structures are identical to their synchronous counterparts.
For further details, refer to the Wiki sections on similarity metrics, phonetic algorithms, normalization and filtering, and advanced extension points.
CmpStr 3.2 / API Reference • FAQ • npm Package • jsDelivr • DevDocs
CmpStr is a lightweight, fast and well performing package for calculating string similarity.
Getting Started
Installation & Setup
Quick Start
API Reference
Documentation
Similarity Metrics
Phonetic Algorithms
Normalization & Filtering
Comparison Modes
Structured Data
Asynchronous API
Diff & Text Analysis
Extending CmpStr
Project Management