Skip to content

Commit acd54e8

Browse files
committed
fix(matches): report array-path keys as dotted strings
Match results for array-path keys (e.g. keys: [['author', 'firstName']]) leaked the raw string[] into match.key, contradicting the documented FuseResultMatch.key: string type. Emit the canonical dotted-string id ('author.firstName') instead. Surfaced while tightening the result formatter: the match projection is now a pure, typed function (formatMatches, renamed from transformMatches) that returns FuseResultMatch[] instead of mutating an any, which let TypeScript catch the leak. Dropped the one-line transformScore and the transformer array in favor of direct assignment in format().
1 parent 5985a7e commit acd54e8

15 files changed

Lines changed: 117 additions & 198 deletions

dist/fuse.basic.cjs

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,45 +1024,33 @@ class MaxHeap {
10241024
}
10251025
}
10261026

1027-
function transformMatches(result, data) {
1028-
const matches = result.matches;
1029-
data.matches = [];
1030-
if (!isDefined(matches)) {
1031-
return;
1032-
}
1033-
matches.forEach(match => {
1027+
function formatMatches(result) {
1028+
const matches = [];
1029+
result.matches.forEach(match => {
10341030
if (!isDefined(match.indices) || !match.indices.length) {
10351031
return;
10361032
}
1037-
const {
1038-
indices,
1039-
value
1040-
} = match;
10411033
const obj = {
1042-
indices,
1043-
value
1034+
indices: match.indices,
1035+
value: match.value
10441036
};
10451037
if (match.key) {
1046-
obj.key = match.key.src;
1038+
// `key.id` is the canonical dotted-string identity (array paths joined
1039+
// with '.'); `key.src` is the raw user input and can be a string[].
1040+
obj.key = match.key.id;
10471041
}
10481042
if (match.idx > -1) {
10491043
obj.refIndex = match.idx;
10501044
}
1051-
data.matches.push(obj);
1045+
matches.push(obj);
10521046
});
1053-
}
1054-
1055-
function transformScore(result, data) {
1056-
data.score = result.score;
1047+
return matches;
10571048
}
10581049

10591050
function format(results, docs, {
10601051
includeMatches = Config.includeMatches,
10611052
includeScore = Config.includeScore
10621053
} = {}) {
1063-
const transformers = [];
1064-
if (includeMatches) transformers.push(transformMatches);
1065-
if (includeScore) transformers.push(transformScore);
10661054
return results.map(result => {
10671055
const {
10681056
idx
@@ -1071,11 +1059,8 @@ function format(results, docs, {
10711059
item: docs[idx],
10721060
refIndex: idx
10731061
};
1074-
if (transformers.length) {
1075-
transformers.forEach(transformer => {
1076-
transformer(result, data);
1077-
});
1078-
}
1062+
if (includeMatches) data.matches = formatMatches(result);
1063+
if (includeScore) data.score = result.score;
10791064
return data;
10801065
});
10811066
}

dist/fuse.basic.min.cjs

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

dist/fuse.basic.min.mjs

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

dist/fuse.basic.mjs

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,45 +1022,33 @@ class MaxHeap {
10221022
}
10231023
}
10241024

1025-
function transformMatches(result, data) {
1026-
const matches = result.matches;
1027-
data.matches = [];
1028-
if (!isDefined(matches)) {
1029-
return;
1030-
}
1031-
matches.forEach(match => {
1025+
function formatMatches(result) {
1026+
const matches = [];
1027+
result.matches.forEach(match => {
10321028
if (!isDefined(match.indices) || !match.indices.length) {
10331029
return;
10341030
}
1035-
const {
1036-
indices,
1037-
value
1038-
} = match;
10391031
const obj = {
1040-
indices,
1041-
value
1032+
indices: match.indices,
1033+
value: match.value
10421034
};
10431035
if (match.key) {
1044-
obj.key = match.key.src;
1036+
// `key.id` is the canonical dotted-string identity (array paths joined
1037+
// with '.'); `key.src` is the raw user input and can be a string[].
1038+
obj.key = match.key.id;
10451039
}
10461040
if (match.idx > -1) {
10471041
obj.refIndex = match.idx;
10481042
}
1049-
data.matches.push(obj);
1043+
matches.push(obj);
10501044
});
1051-
}
1052-
1053-
function transformScore(result, data) {
1054-
data.score = result.score;
1045+
return matches;
10551046
}
10561047

10571048
function format(results, docs, {
10581049
includeMatches = Config.includeMatches,
10591050
includeScore = Config.includeScore
10601051
} = {}) {
1061-
const transformers = [];
1062-
if (includeMatches) transformers.push(transformMatches);
1063-
if (includeScore) transformers.push(transformScore);
10641052
return results.map(result => {
10651053
const {
10661054
idx
@@ -1069,11 +1057,8 @@ function format(results, docs, {
10691057
item: docs[idx],
10701058
refIndex: idx
10711059
};
1072-
if (transformers.length) {
1073-
transformers.forEach(transformer => {
1074-
transformer(result, data);
1075-
});
1076-
}
1060+
if (includeMatches) data.matches = formatMatches(result);
1061+
if (includeScore) data.score = result.score;
10771062
return data;
10781063
});
10791064
}

dist/fuse.cjs

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1419,45 +1419,33 @@ class MaxHeap {
14191419
}
14201420
}
14211421

1422-
function transformMatches(result, data) {
1423-
const matches = result.matches;
1424-
data.matches = [];
1425-
if (!isDefined(matches)) {
1426-
return;
1427-
}
1428-
matches.forEach(match => {
1422+
function formatMatches(result) {
1423+
const matches = [];
1424+
result.matches.forEach(match => {
14291425
if (!isDefined(match.indices) || !match.indices.length) {
14301426
return;
14311427
}
1432-
const {
1433-
indices,
1434-
value
1435-
} = match;
14361428
const obj = {
1437-
indices,
1438-
value
1429+
indices: match.indices,
1430+
value: match.value
14391431
};
14401432
if (match.key) {
1441-
obj.key = match.key.src;
1433+
// `key.id` is the canonical dotted-string identity (array paths joined
1434+
// with '.'); `key.src` is the raw user input and can be a string[].
1435+
obj.key = match.key.id;
14421436
}
14431437
if (match.idx > -1) {
14441438
obj.refIndex = match.idx;
14451439
}
1446-
data.matches.push(obj);
1440+
matches.push(obj);
14471441
});
1448-
}
1449-
1450-
function transformScore(result, data) {
1451-
data.score = result.score;
1442+
return matches;
14521443
}
14531444

14541445
function format(results, docs, {
14551446
includeMatches = Config.includeMatches,
14561447
includeScore = Config.includeScore
14571448
} = {}) {
1458-
const transformers = [];
1459-
if (includeMatches) transformers.push(transformMatches);
1460-
if (includeScore) transformers.push(transformScore);
14611449
return results.map(result => {
14621450
const {
14631451
idx
@@ -1466,11 +1454,8 @@ function format(results, docs, {
14661454
item: docs[idx],
14671455
refIndex: idx
14681456
};
1469-
if (transformers.length) {
1470-
transformers.forEach(transformer => {
1471-
transformer(result, data);
1472-
});
1473-
}
1457+
if (includeMatches) data.matches = formatMatches(result);
1458+
if (includeScore) data.score = result.score;
14741459
return data;
14751460
});
14761461
}

dist/fuse.min.cjs

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

dist/fuse.min.mjs

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

dist/fuse.mjs

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1417,45 +1417,33 @@ class MaxHeap {
14171417
}
14181418
}
14191419

1420-
function transformMatches(result, data) {
1421-
const matches = result.matches;
1422-
data.matches = [];
1423-
if (!isDefined(matches)) {
1424-
return;
1425-
}
1426-
matches.forEach(match => {
1420+
function formatMatches(result) {
1421+
const matches = [];
1422+
result.matches.forEach(match => {
14271423
if (!isDefined(match.indices) || !match.indices.length) {
14281424
return;
14291425
}
1430-
const {
1431-
indices,
1432-
value
1433-
} = match;
14341426
const obj = {
1435-
indices,
1436-
value
1427+
indices: match.indices,
1428+
value: match.value
14371429
};
14381430
if (match.key) {
1439-
obj.key = match.key.src;
1431+
// `key.id` is the canonical dotted-string identity (array paths joined
1432+
// with '.'); `key.src` is the raw user input and can be a string[].
1433+
obj.key = match.key.id;
14401434
}
14411435
if (match.idx > -1) {
14421436
obj.refIndex = match.idx;
14431437
}
1444-
data.matches.push(obj);
1438+
matches.push(obj);
14451439
});
1446-
}
1447-
1448-
function transformScore(result, data) {
1449-
data.score = result.score;
1440+
return matches;
14501441
}
14511442

14521443
function format(results, docs, {
14531444
includeMatches = Config.includeMatches,
14541445
includeScore = Config.includeScore
14551446
} = {}) {
1456-
const transformers = [];
1457-
if (includeMatches) transformers.push(transformMatches);
1458-
if (includeScore) transformers.push(transformScore);
14591447
return results.map(result => {
14601448
const {
14611449
idx
@@ -1464,11 +1452,8 @@ function format(results, docs, {
14641452
item: docs[idx],
14651453
refIndex: idx
14661454
};
1467-
if (transformers.length) {
1468-
transformers.forEach(transformer => {
1469-
transformer(result, data);
1470-
});
1471-
}
1455+
if (includeMatches) data.matches = formatMatches(result);
1456+
if (includeScore) data.score = result.score;
14721457
return data;
14731458
});
14741459
}

dist/fuse.worker.min.mjs

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

dist/fuse.worker.mjs

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1400,45 +1400,33 @@ class MaxHeap {
14001400
}
14011401
}
14021402

1403-
function transformMatches(result, data) {
1404-
const matches = result.matches;
1405-
data.matches = [];
1406-
if (!isDefined(matches)) {
1407-
return;
1408-
}
1409-
matches.forEach(match => {
1403+
function formatMatches(result) {
1404+
const matches = [];
1405+
result.matches.forEach(match => {
14101406
if (!isDefined(match.indices) || !match.indices.length) {
14111407
return;
14121408
}
1413-
const {
1414-
indices,
1415-
value
1416-
} = match;
14171409
const obj = {
1418-
indices,
1419-
value
1410+
indices: match.indices,
1411+
value: match.value
14201412
};
14211413
if (match.key) {
1422-
obj.key = match.key.src;
1414+
// `key.id` is the canonical dotted-string identity (array paths joined
1415+
// with '.'); `key.src` is the raw user input and can be a string[].
1416+
obj.key = match.key.id;
14231417
}
14241418
if (match.idx > -1) {
14251419
obj.refIndex = match.idx;
14261420
}
1427-
data.matches.push(obj);
1421+
matches.push(obj);
14281422
});
1429-
}
1430-
1431-
function transformScore(result, data) {
1432-
data.score = result.score;
1423+
return matches;
14331424
}
14341425

14351426
function format(results, docs, {
14361427
includeMatches = Config.includeMatches,
14371428
includeScore = Config.includeScore
14381429
} = {}) {
1439-
const transformers = [];
1440-
if (includeMatches) transformers.push(transformMatches);
1441-
if (includeScore) transformers.push(transformScore);
14421430
return results.map(result => {
14431431
const {
14441432
idx
@@ -1447,11 +1435,8 @@ function format(results, docs, {
14471435
item: docs[idx],
14481436
refIndex: idx
14491437
};
1450-
if (transformers.length) {
1451-
transformers.forEach(transformer => {
1452-
transformer(result, data);
1453-
});
1454-
}
1438+
if (includeMatches) data.matches = formatMatches(result);
1439+
if (includeScore) data.score = result.score;
14551440
return data;
14561441
});
14571442
}

0 commit comments

Comments
 (0)