Skip to content

Commit 3524361

Browse files
committed
Make enhanced the default for us_street_api
1 parent d916e82 commit 3524361

File tree

5 files changed

+107
-6
lines changed

5 files changed

+107
-6
lines changed

src/us_street/Client.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const Lookup = require("./Lookup");
33
const Batch = require("../Batch");
44
const UndefinedLookupError = require("../Errors").UndefinedLookupError;
55
const sendBatch = require("../util/sendBatch");
6-
const keyTranslationFormat = require("../util/apiToSDKKeyMap").usStreet;
6+
const buildUsStreetInputData = require("../util/buildUsStreetInputData");
77

88
/**
99
* This client sends lookups to the Smarty US Street API, <br>
@@ -28,15 +28,13 @@ class Client {
2828
let batch;
2929

3030
if (dataIsLookup) {
31-
if (data.maxCandidates == null && data.match == "enhanced")
32-
data.maxCandidates = 5;
3331
batch = new Batch();
3432
batch.add(data);
3533
} else {
3634
batch = data;
3735
}
3836

39-
return sendBatch(batch, this.sender, Candidate, keyTranslationFormat);
37+
return sendBatch(batch, this.sender, Candidate, null, buildUsStreetInputData);
4038
}
4139
}
4240

src/util/apiToSDKKeyMap.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ module.exports = {
1212
"match": "match",
1313
"format": "format",
1414
"candidates": "maxCandidates",
15-
"county_source": "countySource"
15+
"county_source": "countySource",
16+
"input_id": "inputId"
1617
},
1718
usAutocompletePro: {
1819
search: "search",

src/util/buildUsStreetInputData.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const buildInputData = require("./buildInputData");
2+
const keyTranslationFormat = require("./apiToSDKKeyMap").usStreet;
3+
4+
module.exports = (lookup) => {
5+
// Apply default match strategy and candidates logic per Go SDK behavior
6+
let effectiveMatch = lookup.match;
7+
let effectiveCandidates = lookup.maxCandidates;
8+
9+
// Default match strategy is "enhanced"
10+
if (!effectiveMatch) {
11+
effectiveMatch = "enhanced";
12+
}
13+
14+
// If match is "strict", don't send match parameter or default candidates
15+
if (effectiveMatch === "strict") {
16+
effectiveMatch = undefined;
17+
// Only send candidates if explicitly set
18+
if (!effectiveCandidates) {
19+
effectiveCandidates = undefined;
20+
}
21+
} else {
22+
// For non-strict (including default "enhanced"), set default candidates to 5 if not specified
23+
if (!effectiveCandidates) {
24+
effectiveCandidates = 5;
25+
}
26+
}
27+
28+
// Create a lookup copy with effective values for serialization
29+
const effectiveLookup = Object.assign({}, lookup, {
30+
match: effectiveMatch,
31+
maxCandidates: effectiveCandidates,
32+
});
33+
34+
return buildInputData(effectiveLookup, keyTranslationFormat);
35+
};

src/util/sendBatch.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const Request = require("../Request");
22
const Errors = require("../Errors");
33
const buildInputData = require("../util/buildInputData");
44

5-
module.exports = (batch, sender, Result, keyTranslationFormat) => {
5+
module.exports = (batch, sender, Result, keyTranslationFormat, customBuildInputData) => {
66
if (batch.isEmpty()) throw new Errors.BatchEmptyError;
77

88
let request = new Request();
@@ -22,6 +22,9 @@ module.exports = (batch, sender, Result, keyTranslationFormat) => {
2222

2323
function generateRequestPayload(batch) {
2424
return batch.lookups.map((lookup) => {
25+
if (customBuildInputData) {
26+
return customBuildInputData(lookup);
27+
}
2528
return buildInputData(lookup, keyTranslationFormat);
2629
});
2730
}

tests/us_street/test_Client.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,23 @@ describe("A US Street client", function () {
4242
urbanization: "9",
4343
match: "10",
4444
candidates: "11",
45+
input_id: "12",
46+
};
47+
48+
client.send(lookup);
49+
50+
expect(mockSender.request.parameters).to.deep.equal(expectedParameters);
51+
});
52+
53+
it("defaults to enhanced match with 5 candidates when no match type specified.", function () {
54+
let mockSender = new MockSender();
55+
const client = new Client(mockSender);
56+
let lookup = new Lookup();
57+
lookup.street = "123 Main St";
58+
let expectedParameters = {
59+
street: "123 Main St",
60+
match: "enhanced",
61+
candidates: 5,
4562
};
4663

4764
client.send(lookup);
@@ -64,6 +81,53 @@ describe("A US Street client", function () {
6481
expect(mockSender.request.parameters).to.deep.equal(expectedParameters);
6582
});
6683

84+
it("sends no match or candidates when match type is strict.", function () {
85+
let mockSender = new MockSender();
86+
const client = new Client(mockSender);
87+
let lookup = new Lookup();
88+
lookup.street = "123 Main St";
89+
lookup.match = "strict";
90+
let expectedParameters = {
91+
street: "123 Main St",
92+
};
93+
94+
client.send(lookup);
95+
96+
expect(mockSender.request.parameters).to.deep.equal(expectedParameters);
97+
});
98+
99+
it("sends candidates but not match when match is strict with explicit candidates.", function () {
100+
let mockSender = new MockSender();
101+
const client = new Client(mockSender);
102+
let lookup = new Lookup();
103+
lookup.street = "123 Main St";
104+
lookup.match = "strict";
105+
lookup.maxCandidates = 3;
106+
let expectedParameters = {
107+
street: "123 Main St",
108+
candidates: 3,
109+
};
110+
111+
client.send(lookup);
112+
113+
expect(mockSender.request.parameters).to.deep.equal(expectedParameters);
114+
});
115+
116+
it("sends match invalid when explicitly set.", function () {
117+
let mockSender = new MockSender();
118+
const client = new Client(mockSender);
119+
let lookup = new Lookup();
120+
lookup.match = "invalid";
121+
let expectedParameters = {
122+
match: "invalid",
123+
candidates: 5,
124+
};
125+
126+
client.send(lookup);
127+
128+
expect(mockSender.request.parameters).to.deep.equal(expectedParameters);
129+
});
130+
67131
it("doesn't send an empty batch.", function () {
68132
let mockSender = new MockSender();
69133
const client = new Client(mockSender);

0 commit comments

Comments
 (0)