field-aligner – Field Name Fuzzy Matching and Alignment
Map field names from any source to your defined canonical field keys. Recognizes Chinese, English, camelCase, and snake_case mixed usage. Pure function implementation (Node ESM, zero dependencies). Vocabulary is injectable, with a built-in generic example vocabulary.
Use Cases
- Integrating with ERP / open platforms / internal middle platforms: the field names from the other side differ from those in your database
- Merging multiple data sources: different systems have different names for the same semantic field
- Structuring documents / tables: mapping table headers to standard fields
Provided Functions
| Function | Description |
|---|---|
normalize(name) | Normalize: lowercase + remove _-space |
buildVocabulary(input) | Normalize {key:[aliases]} or [{key,aliases}] into a unified structure |
matchField(name, vocabulary, opts?) | Single field matching, returns {key, matched, score, via} |
batchMatchFields(names, vocabulary, opts?) | Batch matching |
DEFAULT_VOCABULARY | Generic example vocabulary (e-commerce/common fields) |
Matching strategy:
- Exact: After normalization, the key or any alias matches exactly →
via:'exact' - Fuzzy: Substring containment (longer overlap yields higher score), score ≥
fuzzyThreshold(default 0.6) →via:'fuzzy' - No match →
{key:null, matched:false, via:'none'}
Usage
import { matchField, batchMatchFields, DEFAULT_VOCABULARY } from './scripts/field-aligner.mjs';
// 1) Using the default vocabulary
const r = matchField('product title', DEFAULT_VOCABULARY);
// => { key: 'title', matched: true, score: 1, via: 'exact' }
// 2) Inject your own vocabulary
const myVocab = {
order_no: ['order number', 'orderNo'],
buyer_name: ['buyer', 'recipient'],
};
batchMatchFields(['order number', 'buyer', 'notes'], myVocab);
// => { order number:{key:'order_no',...}, buyer:{key:'buyer_name',...}, notes:{key:null,...} }
Self-Test
node scripts/field-aligner.mjs --selftest