@ukhomeoffice/cop-react-form-renderer 6.14.2 → 6.14.3-test
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -75,7 +75,8 @@ const setupDefaultObjectValue = (defaultObject, data) => {
|
|
|
75
75
|
// defaultObject.sourced is an object with values that should
|
|
76
76
|
// be used as field names to get values from data.
|
|
77
77
|
if (defaultObj.sourced) {
|
|
78
|
-
|
|
78
|
+
var _result;
|
|
79
|
+
result = (_result = result) !== null && _result !== void 0 ? _result : {};
|
|
79
80
|
Object.keys(defaultObj.sourced).every(key => {
|
|
80
81
|
const sourcedValue = typeof defaultObj.sourced[key] === 'string' && (0, _getSourceData.default)(data, defaultObj.sourced[key]);
|
|
81
82
|
if (sourcedValue === undefined && defaultObj.skipIfSourceInvalid) {
|
|
@@ -7,80 +7,63 @@ exports.default = void 0;
|
|
|
7
7
|
var _copReactComponents = require("@ukhomeoffice/cop-react-components");
|
|
8
8
|
var _getOptions = _interopRequireDefault(require("./getOptions"));
|
|
9
9
|
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
10
|
-
|
|
10
|
+
// Global Imports
|
|
11
|
+
|
|
12
|
+
// Local imports
|
|
11
13
|
|
|
12
|
-
const assignFullMatchScore = (subStringMatchIndex, label) => {
|
|
13
|
-
const matchAtBeginning = subStringMatchIndex === 0;
|
|
14
|
-
let score = 0;
|
|
15
|
-
if (matchAtBeginning) {
|
|
16
|
-
score = 1;
|
|
17
|
-
}
|
|
18
|
-
if (!matchAtBeginning) {
|
|
19
|
-
const isCompleteWord = label.substring(subStringMatchIndex - 1).startsWith(" ");
|
|
20
|
-
score = isCompleteWord ? 2 : 3;
|
|
21
|
-
}
|
|
22
|
-
return score;
|
|
23
|
-
};
|
|
24
|
-
const noMatch = score => score === 0;
|
|
25
|
-
const lowerCaseMatch = (searchTerm, query) => typeof searchTerm !== 'string' ? false : searchTerm.toLowerCase().includes(query);
|
|
26
|
-
const assignSynonymsScore = (option, searchQuery) => {
|
|
27
|
-
let score = 0;
|
|
28
|
-
const isMatch = option.synonyms.some(synonym => lowerCaseMatch(synonym, searchQuery));
|
|
29
|
-
if (isMatch) {
|
|
30
|
-
score = 4;
|
|
31
|
-
}
|
|
32
|
-
return score;
|
|
33
|
-
};
|
|
34
|
-
const assignExtraFieldsScore = (config, option, searchQuery) => {
|
|
35
|
-
let score = 0;
|
|
36
|
-
const isMatch = config.item.extraFieldsToSearch.some(field => {
|
|
37
|
-
const fieldToSearch = option[field];
|
|
38
|
-
return lowerCaseMatch(fieldToSearch, searchQuery);
|
|
39
|
-
});
|
|
40
|
-
if (isMatch) {
|
|
41
|
-
score = 5;
|
|
42
|
-
}
|
|
43
|
-
return score;
|
|
44
|
-
};
|
|
45
|
-
const mapToScoreAndLabel = (option, searchQuery, labelMapper, config) => {
|
|
46
|
-
var _config$item;
|
|
47
|
-
const label = labelMapper ? labelMapper(option).toLowerCase() : option.label.toLowerCase() || '';
|
|
48
|
-
let score = 0;
|
|
49
|
-
const subStringMatchIndex = label.indexOf(searchQuery);
|
|
50
|
-
const isFullMatch = subStringMatchIndex !== -1;
|
|
51
|
-
if (isFullMatch) {
|
|
52
|
-
score = assignFullMatchScore(subStringMatchIndex, label);
|
|
53
|
-
}
|
|
54
|
-
if (noMatch(score) && option.synonyms) {
|
|
55
|
-
score = assignSynonymsScore(option, searchQuery);
|
|
56
|
-
}
|
|
57
|
-
if (noMatch(score) && (_config$item = config.item) !== null && _config$item !== void 0 && _config$item.extraFieldsToSearch) {
|
|
58
|
-
score = assignExtraFieldsScore(config, option, searchQuery);
|
|
59
|
-
}
|
|
60
|
-
return {
|
|
61
|
-
score,
|
|
62
|
-
label,
|
|
63
|
-
option
|
|
64
|
-
};
|
|
65
|
-
};
|
|
66
|
-
const scoreOptions = (searchQuery, labelMapper, config) => (scoredOptions, option) => {
|
|
67
|
-
const scoredOption = mapToScoreAndLabel(option, searchQuery, labelMapper, config);
|
|
68
|
-
if (scoredOption.score > 0) {
|
|
69
|
-
scoredOptions.push(scoredOption);
|
|
70
|
-
}
|
|
71
|
-
return scoredOptions;
|
|
72
|
-
};
|
|
73
|
-
const compareByScoreAndLabel = (r1, r2) => (r1.score - r2.score) * 16 + r1.label.localeCompare(r2.label);
|
|
74
14
|
const getAutocompleteSource = config => {
|
|
75
15
|
let options = [];
|
|
76
16
|
(0, _getOptions.default)(config, val => {
|
|
77
17
|
options = val;
|
|
78
18
|
});
|
|
79
|
-
const
|
|
80
|
-
return (query,
|
|
81
|
-
const
|
|
82
|
-
|
|
83
|
-
|
|
19
|
+
const labelMaker = config !== null && config !== void 0 && config.item ? _copReactComponents.Utils.itemLabel(config.item) : null;
|
|
20
|
+
return (query, populateResults) => {
|
|
21
|
+
const lcQuery = query ? query.toLowerCase() : '';
|
|
22
|
+
|
|
23
|
+
// go through all options and give them a grade
|
|
24
|
+
|
|
25
|
+
const results = options.map(opt => {
|
|
26
|
+
const label = labelMaker ? labelMaker(opt) : opt.label || '';
|
|
27
|
+
const lcLabel = label.toLowerCase();
|
|
28
|
+
|
|
29
|
+
// result 'score'
|
|
30
|
+
let grade = 0;
|
|
31
|
+
|
|
32
|
+
// highest result - match at start of string
|
|
33
|
+
const index = lcLabel.indexOf(lcQuery);
|
|
34
|
+
if (index === 0) {
|
|
35
|
+
grade = 1;
|
|
36
|
+
} else if (index > 0) {
|
|
37
|
+
grade = lcLabel.substring(index - 1).startsWith(" ") ? 2 : 3;
|
|
38
|
+
} else {
|
|
39
|
+
var _config$item;
|
|
40
|
+
if (opt.synonyms) {
|
|
41
|
+
const match = opt.synonyms.some(synonym => synonym.toLowerCase().includes(lcQuery));
|
|
42
|
+
if (match) {
|
|
43
|
+
grade = 4;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
if (!grade && (_config$item = config.item) !== null && _config$item !== void 0 && _config$item.extraFieldsToSearch) {
|
|
47
|
+
const found = config.item.extraFieldsToSearch.some(field => {
|
|
48
|
+
if (typeof opt[field] !== 'string') {
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
return opt[field].toLowerCase().includes(lcQuery);
|
|
52
|
+
});
|
|
53
|
+
if (found) grade = 5;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return {
|
|
57
|
+
grade,
|
|
58
|
+
label,
|
|
59
|
+
opt
|
|
60
|
+
};
|
|
61
|
+
}).filter(result => result.grade > 0);
|
|
62
|
+
|
|
63
|
+
// sort results and then map to just opts
|
|
64
|
+
populateResults(results
|
|
65
|
+
// the sort prioritises grade but also sorts by string value
|
|
66
|
+
.sort((r1, r2) => (r1.grade - r2.grade) * 16 + r1.label.localeCompare(r2.label)).map(r => r.opt));
|
|
84
67
|
};
|
|
85
68
|
};
|
|
86
69
|
var _default = exports.default = getAutocompleteSource;
|
|
@@ -6,9 +6,10 @@ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbol
|
|
|
6
6
|
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
7
7
|
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
|
|
8
8
|
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
|
|
9
|
-
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
9
|
+
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } // Local imports
|
|
10
10
|
describe('utils', () => {
|
|
11
11
|
describe('Data', () => {
|
|
12
|
+
// test data
|
|
12
13
|
const gbCurrency = {
|
|
13
14
|
currencyName: 'Great British Pounds',
|
|
14
15
|
currencyCode: 'GBP',
|
|
@@ -24,7 +25,7 @@ describe('utils', () => {
|
|
|
24
25
|
value: 'USD',
|
|
25
26
|
label: 'USD',
|
|
26
27
|
slangTerm: 'bucks',
|
|
27
|
-
synonyms: ['greenbacks'
|
|
28
|
+
synonyms: ['greenbacks']
|
|
28
29
|
};
|
|
29
30
|
const options = {
|
|
30
31
|
alpha: {
|
|
@@ -80,15 +81,6 @@ describe('utils', () => {
|
|
|
80
81
|
}]);
|
|
81
82
|
});
|
|
82
83
|
});
|
|
83
|
-
it('should only return first 6000 results that have some type of match', () => {
|
|
84
|
-
const moreThanMaxNumberOfOptions = Array(6001).fill(options.alpha);
|
|
85
|
-
const config = {
|
|
86
|
-
data: {
|
|
87
|
-
options: moreThanMaxNumberOfOptions
|
|
88
|
-
}
|
|
89
|
-
};
|
|
90
|
-
(0, _getAutocompleteSource.default)(config)(options.alpha.label, results => expect(results.length).toEqual(6000));
|
|
91
|
-
});
|
|
92
84
|
it('should handle an empty query', () => {
|
|
93
85
|
const CONFIG = {
|
|
94
86
|
data: {
|
|
@@ -125,18 +117,6 @@ describe('utils', () => {
|
|
|
125
117
|
expect(results).toEqual([options.alpha, options.bravo]);
|
|
126
118
|
});
|
|
127
119
|
});
|
|
128
|
-
it('should handle a non string query', () => {
|
|
129
|
-
const CONFIG = {
|
|
130
|
-
data: {
|
|
131
|
-
options: [options.alpha, options.bravo]
|
|
132
|
-
}
|
|
133
|
-
};
|
|
134
|
-
const SOURCE = (0, _getAutocompleteSource.default)(CONFIG);
|
|
135
|
-
SOURCE(1, results => {
|
|
136
|
-
expect(results.length).toEqual(2);
|
|
137
|
-
expect(results).toEqual([options.alpha, options.bravo]);
|
|
138
|
-
});
|
|
139
|
-
});
|
|
140
120
|
it('should handle a missing label on an option', () => {
|
|
141
121
|
const CONFIG = {
|
|
142
122
|
data: {
|
|
@@ -191,7 +171,8 @@ describe('utils', () => {
|
|
|
191
171
|
item: {
|
|
192
172
|
value: 'currencyCode',
|
|
193
173
|
label: 'currencyCode',
|
|
194
|
-
|
|
174
|
+
// eslint-disable-next-line no-template-curly-in-string
|
|
175
|
+
format: '${currencyName} (${currencyCode})'
|
|
195
176
|
},
|
|
196
177
|
data: {
|
|
197
178
|
options: testCurrencies
|
|
@@ -213,7 +194,8 @@ describe('utils', () => {
|
|
|
213
194
|
item: {
|
|
214
195
|
value: 'currencyCode',
|
|
215
196
|
label: 'currencyCode',
|
|
216
|
-
|
|
197
|
+
// eslint-disable-next-line no-template-curly-in-string
|
|
198
|
+
format: '${currencyName} (${currencyCode})'
|
|
217
199
|
},
|
|
218
200
|
data: {
|
|
219
201
|
options: testCurrencies
|
|
@@ -230,7 +212,8 @@ describe('utils', () => {
|
|
|
230
212
|
item: {
|
|
231
213
|
value: 'currencyCode',
|
|
232
214
|
label: 'currencyCode',
|
|
233
|
-
|
|
215
|
+
// eslint-disable-next-line no-template-curly-in-string
|
|
216
|
+
format: '${currencyName} (${currencyCode})'
|
|
234
217
|
},
|
|
235
218
|
data: {
|
|
236
219
|
options: testCurrencies
|
|
@@ -247,7 +230,8 @@ describe('utils', () => {
|
|
|
247
230
|
item: {
|
|
248
231
|
value: 'currencyCode',
|
|
249
232
|
label: 'currencyCode',
|
|
250
|
-
|
|
233
|
+
// eslint-disable-next-line no-template-curly-in-string
|
|
234
|
+
format: '${currencyName} (${currencyCode})'
|
|
251
235
|
},
|
|
252
236
|
data: {
|
|
253
237
|
options: testCurrencies
|
|
@@ -264,7 +248,8 @@ describe('utils', () => {
|
|
|
264
248
|
item: {
|
|
265
249
|
value: 'currencyCode',
|
|
266
250
|
label: 'currencyCode',
|
|
267
|
-
|
|
251
|
+
// eslint-disable-next-line no-template-curly-in-string
|
|
252
|
+
format: '${currencyName} (${currencyCode})'
|
|
268
253
|
},
|
|
269
254
|
data: {
|
|
270
255
|
options: [gbCurrency, {
|
|
@@ -296,7 +281,8 @@ describe('utils', () => {
|
|
|
296
281
|
item: {
|
|
297
282
|
value: 'currencyCode',
|
|
298
283
|
label: 'currencyCode',
|
|
299
|
-
|
|
284
|
+
// eslint-disable-next-line no-template-curly-in-string
|
|
285
|
+
format: '${currencyName} (${currencyCode})'
|
|
300
286
|
},
|
|
301
287
|
data: {
|
|
302
288
|
options: testCurrencies
|
|
@@ -314,8 +300,9 @@ describe('utils', () => {
|
|
|
314
300
|
item: {
|
|
315
301
|
value: 'currencyCode',
|
|
316
302
|
label: 'currencyCode',
|
|
317
|
-
|
|
318
|
-
|
|
303
|
+
// eslint-disable-next-line no-template-curly-in-string
|
|
304
|
+
format: '${currencyName} (${currencyCode})',
|
|
305
|
+
extraFieldsToSearch: ['slangTerm']
|
|
319
306
|
},
|
|
320
307
|
data: {
|
|
321
308
|
options: testCurrencies
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ukhomeoffice/cop-react-form-renderer",
|
|
3
|
-
"version": "6.14.
|
|
3
|
+
"version": "6.14.3-test",
|
|
4
4
|
"private": false,
|
|
5
5
|
"scripts": {
|
|
6
6
|
"clean": "rimraf dist",
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
"yalc-publish": "yarn compile-with-maps && cp -r src dist/src && yalc publish --push"
|
|
17
17
|
},
|
|
18
18
|
"dependencies": {
|
|
19
|
-
"@ukhomeoffice/cop-react-components": "4.7.
|
|
19
|
+
"@ukhomeoffice/cop-react-components": "4.7.7",
|
|
20
20
|
"axios": "^0.23.0",
|
|
21
21
|
"dayjs": "^1.11.0",
|
|
22
22
|
"govuk-frontend": "^5.0.0",
|