@natlibfi/melinda-record-matching 2.2.0-alpha.2 → 2.2.1-alpha.1

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.
Files changed (68) hide show
  1. package/.github/CODEOWNERS +10 -0
  2. package/.github/dependabot.yml +40 -0
  3. package/.github/workflows/melinda-node-tests.yml +70 -0
  4. package/package.json +13 -13
  5. package/src/candidate-search/index.js +11 -8
  6. package/src/candidate-search/query-list/bib.js +2 -2
  7. package/src/index.js +70 -29
  8. package/src/match-detection/features/bib/authors.js +1 -1
  9. package/src/match-detection/features/bib/standard-identifier-factory.js +2 -1
  10. package/src/match-detection/index.js +29 -19
  11. package/src/match-detection/index.spec.js +17 -3
  12. package/src/matching-utils.js +1 -1
  13. package/dist/candidate-search/candidate-search-utils.js +0 -58
  14. package/dist/candidate-search/candidate-search-utils.js.map +0 -1
  15. package/dist/candidate-search/index.js +0 -273
  16. package/dist/candidate-search/index.js.map +0 -1
  17. package/dist/candidate-search/index.spec.js +0 -161
  18. package/dist/candidate-search/index.spec.js.map +0 -1
  19. package/dist/candidate-search/query-list/bib.js +0 -252
  20. package/dist/candidate-search/query-list/bib.js.map +0 -1
  21. package/dist/candidate-search/query-list/bib.spec.js +0 -72
  22. package/dist/candidate-search/query-list/bib.spec.js.map +0 -1
  23. package/dist/candidate-search/query-list/index.js +0 -67
  24. package/dist/candidate-search/query-list/index.js.map +0 -1
  25. package/dist/index.js +0 -508
  26. package/dist/index.js.map +0 -1
  27. package/dist/index.spec.js +0 -133
  28. package/dist/index.spec.js.map +0 -1
  29. package/dist/match-detection/features/bib/all-source-ids.js +0 -122
  30. package/dist/match-detection/features/bib/all-source-ids.js.map +0 -1
  31. package/dist/match-detection/features/bib/authors.js +0 -117
  32. package/dist/match-detection/features/bib/authors.js.map +0 -1
  33. package/dist/match-detection/features/bib/bibliographic-level.js +0 -42
  34. package/dist/match-detection/features/bib/bibliographic-level.js.map +0 -1
  35. package/dist/match-detection/features/bib/host-component.js +0 -42
  36. package/dist/match-detection/features/bib/host-component.js.map +0 -1
  37. package/dist/match-detection/features/bib/index.js +0 -104
  38. package/dist/match-detection/features/bib/index.js.map +0 -1
  39. package/dist/match-detection/features/bib/index.spec.js +0 -97
  40. package/dist/match-detection/features/bib/index.spec.js.map +0 -1
  41. package/dist/match-detection/features/bib/isbn.js +0 -55
  42. package/dist/match-detection/features/bib/isbn.js.map +0 -1
  43. package/dist/match-detection/features/bib/issn.js +0 -55
  44. package/dist/match-detection/features/bib/issn.js.map +0 -1
  45. package/dist/match-detection/features/bib/language.js +0 -113
  46. package/dist/match-detection/features/bib/language.js.map +0 -1
  47. package/dist/match-detection/features/bib/melinda-id.js +0 -52
  48. package/dist/match-detection/features/bib/melinda-id.js.map +0 -1
  49. package/dist/match-detection/features/bib/melinda-identifier-factory.js +0 -100
  50. package/dist/match-detection/features/bib/melinda-identifier-factory.js.map +0 -1
  51. package/dist/match-detection/features/bib/other-standard-identifier.js +0 -55
  52. package/dist/match-detection/features/bib/other-standard-identifier.js.map +0 -1
  53. package/dist/match-detection/features/bib/publication-time.js +0 -47
  54. package/dist/match-detection/features/bib/publication-time.js.map +0 -1
  55. package/dist/match-detection/features/bib/record-type.js +0 -42
  56. package/dist/match-detection/features/bib/record-type.js.map +0 -1
  57. package/dist/match-detection/features/bib/standard-identifier-factory.js +0 -125
  58. package/dist/match-detection/features/bib/standard-identifier-factory.js.map +0 -1
  59. package/dist/match-detection/features/bib/title.js +0 -91
  60. package/dist/match-detection/features/bib/title.js.map +0 -1
  61. package/dist/match-detection/features/index.js +0 -15
  62. package/dist/match-detection/features/index.js.map +0 -1
  63. package/dist/match-detection/index.js +0 -178
  64. package/dist/match-detection/index.js.map +0 -1
  65. package/dist/match-detection/index.spec.js +0 -81
  66. package/dist/match-detection/index.spec.js.map +0 -1
  67. package/dist/matching-utils.js +0 -95
  68. package/dist/matching-utils.js.map +0 -1
package/dist/index.js DELETED
@@ -1,508 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.matchDetection = exports.default = exports.candidateSearch = void 0;
7
-
8
- var _debug = _interopRequireDefault(require("debug"));
9
-
10
- var candidateSearch = _interopRequireWildcard(require("./candidate-search"));
11
-
12
- exports.candidateSearch = candidateSearch;
13
-
14
- var matchDetection = _interopRequireWildcard(require("./match-detection"));
15
-
16
- exports.matchDetection = matchDetection;
17
-
18
- function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
19
-
20
- function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
21
-
22
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
23
-
24
- /**
25
- *
26
- * @licstart The following is the entire license notice for the JavaScript code in this file.
27
- *
28
- * Melinda record matching modules for Javascript
29
- *
30
- * Copyright (C) 2020-2022 University Of Helsinki (The National Library Of Finland)
31
- *
32
- * This file is part of melinda-record-matching-js
33
- *
34
- * melinda-record-matching-js program is free software: you can redistribute it and/or modify
35
- * it under the terms of the GNU Lesser General Public License as
36
- * published by the Free Software Foundation, either version 3 of the
37
- * License, or (at your option) any later version.
38
- *
39
- * melinda-record-matching-js is distributed in the hope that it will be useful,
40
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
41
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
42
- * GNU Lesser General Public License for more details.
43
- *
44
- * You should have received a copy of the GNU Affero General Public License
45
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
46
- *
47
- * @licend The above is the entire license notice
48
- * for the JavaScript code in this file.
49
- *
50
- */
51
- var _default = ({
52
- detection: detectionOptions,
53
- search: searchOptions,
54
- maxMatches = 1,
55
- maxCandidates = 25,
56
- returnStrategy = false,
57
- returnQuery = false,
58
- returnNonMatches = false,
59
- returnFailures = false
60
- }) => {
61
- const debug = (0, _debug.default)('@natlibfi/melinda-record-matching:index');
62
- const debugData = debug.extend('data');
63
- debugData(`DetectionOptions: ${JSON.stringify(detectionOptions)}`);
64
- debugData(`SearchOptions: ${JSON.stringify(searchOptions)}`);
65
- debugData(`MaxMatches: ${JSON.stringify(maxMatches)}`);
66
- debugData(`MaxCandidates: ${JSON.stringify(maxCandidates)}`);
67
- debugData(`ReturnStrategy: ${JSON.stringify(returnStrategy)}`);
68
- debugData(`ReturnQuery: ${JSON.stringify(returnQuery)}`);
69
- debugData(`ReturnNonMatches: ${JSON.stringify(returnNonMatches)}`);
70
- debugData(`ReturnFailures: ${JSON.stringify(returnFailures)}`);
71
- const detect = (0, matchDetection.default)(detectionOptions, returnStrategy);
72
- return record => {
73
- const search = (0, candidateSearch.default)({ ...searchOptions,
74
- record,
75
- maxCandidates
76
- });
77
- return iterate({}); // candidateCount : amount of candidate records retrived from SRU for matching, NOT including current record set
78
- // matches : candidates that have been detected as matches by current matcher job
79
- // nonMatches : candidates that have been detected as non-matches by current matcher job (only if returnNonMatches is 'true')
80
- // duplicateCount : amount of candidate records that were retrieved from the SRU but not handled further because they were already found in the matches/nonMatches
81
- // state.totalRecords : amount of candidate records available to the current query (undefined, if there was no queries left)
82
- // state.query : current query (undefined if there was no queries left)
83
- // state.searchCounter : sequence for current search for current query (undefined, if there we no queries left)
84
- // state.queryCandidateCounter: amount of candidate records retrieved from SRU for matching for current query, including the current record set (undefined if there were no queries left)
85
- // state.queriesLeft : amount of queries left
86
- // state.queryCounter : sequence for current query
87
- // state.maxedQueries : queries that resulted in more than serverMaxResults hits
88
-
89
- async function iterate({
90
- initialState = {},
91
- matches = [],
92
- candidateCount = 0,
93
- nonMatches = [],
94
- duplicateCount = 0,
95
- nonMatchCount = 0,
96
- conversionFailures = []
97
- }) {
98
- debugData(`Starting next matcher iteration.`);
99
- const {
100
- records,
101
- failures,
102
- ...state
103
- } = await search(initialState);
104
- debugData(`Current state: ${JSON.stringify(state)}, matches: ${matches.length}, candidateCount: ${candidateCount}, nonMatches: ${nonMatches.length}, nonMatchCount: ${nonMatchCount}, conversionFailures: ${conversionFailures}`);
105
- const recordSetSize = records.length;
106
- const failureSetSize = failures.length;
107
- const newCandidateCount = candidateCount + recordSetSize + failureSetSize;
108
- const newConversionFailures = conversionFailures.concat(failures);
109
- debugData(`Failures: ${failures.length}, ConversionFailures: ${conversionFailures.length}, NewConversionFailures: ${newConversionFailures.length}`);
110
-
111
- if (recordSetSize > 0) {
112
- return handleRecordSet();
113
- }
114
-
115
- if (state.queriesLeft > 0) {
116
- debug(`Empty record set ${state.searchCounter} for ${state.query}, but there are ${state.queriesLeft} queries left`);
117
- return iterate({
118
- initialState: state,
119
- matches,
120
- candidateCount: newCandidateCount,
121
- nonMatches,
122
- nonMatchCount,
123
- duplicateCount,
124
- conversionFailures: newConversionFailures
125
- });
126
- }
127
-
128
- debug(`No (more) candidate records to check, no more queries left, matches: ${matches.length}`);
129
- return returnResult({
130
- matches,
131
- state,
132
- stopReason: '',
133
- nonMatches,
134
- nonMatchCount,
135
- candidateCount: newCandidateCount,
136
- duplicateCount,
137
- conversionFailures: newConversionFailures
138
- });
139
-
140
- function handleRecordSet() {
141
- debug(`Checking record set of ${recordSetSize} candidate records for possible matches, found by ${state.searchCounter} search for ${state.query}`);
142
- const matchResult = iterateRecords({
143
- records,
144
- recordSetSize,
145
- maxMatches,
146
- matches,
147
- nonMatches,
148
- nonMatchCount
149
- });
150
- const newDuplicateCount = duplicateCount + matchResult.duplicateCount;
151
- const newNonMatchCount = nonMatchCount + matchResult.nonMatchCount;
152
- const {
153
- newMatches,
154
- newNonMatches
155
- } = handleMatchResult(matchResult, matches, nonMatches);
156
-
157
- if (maxMatchesFound({
158
- matches: newMatches,
159
- maxMatches
160
- })) {
161
- return returnResult({
162
- matches: newMatches,
163
- state,
164
- stopReason: 'maxMatches',
165
- nonMatches: newNonMatches,
166
- duplicateCount: newDuplicateCount,
167
- candidateCount: newCandidateCount,
168
- nonMatchCount: newNonMatchCount,
169
- conversionFailures: newConversionFailures
170
- });
171
- }
172
-
173
- if (maxCandidatesRetrieved(newCandidateCount, maxCandidates)) {
174
- return returnResult({
175
- matches: newMatches,
176
- state,
177
- stopReason: 'maxCandidates',
178
- nonMatches: newNonMatches,
179
- duplicateCount: newDuplicateCount,
180
- candidateCount: newCandidateCount,
181
- nonMatchCount: newNonMatchCount,
182
- conversionFailures: newConversionFailures
183
- });
184
- }
185
-
186
- return iterate({
187
- initialState: state,
188
- matches: newMatches,
189
- candidateCount: newCandidateCount,
190
- nonMatches: newNonMatches,
191
- duplicateCount: newDuplicateCount,
192
- nonMatchCount: newNonMatchCount,
193
- conversionFailures: newConversionFailures
194
- });
195
- }
196
-
197
- function handleMatchResult(matchResult, matches, nonMatches) {
198
- debugData(`- Amount of new matches from record set: ${matchResult.matches.length}`); // eslint-disable-next-line functional/no-conditional-statement
199
-
200
- if (returnNonMatches) {
201
- debugData(`- Amount of new nonMatches from record set: ${matchResult.nonMatches.length}`);
202
- }
203
-
204
- const newMatches = matches.concat(returnQuery ? addQuery(matchResult.matches) : matchResult.matches);
205
- const newNonMatches = returnNonMatches ? nonMatches.concat(returnQuery ? addQuery(matchResult.nonMatches) : matchResult.nonMatches) : [];
206
- debugData(`- Total amount of matches: ${newMatches.length}`); // eslint-disable-next-line functional/no-conditional-statement
207
-
208
- if (returnNonMatches) {
209
- debugData(`- Total amount of nonMatches: ${newNonMatches.length}`);
210
- }
211
-
212
- return {
213
- newMatches,
214
- newNonMatches
215
- };
216
- }
217
-
218
- function addQuery(matches) {
219
- debugData(`Adding query ${state.query} to matches`);
220
- return matches.map(match => ({ ...match,
221
- matchQuery: state.query
222
- }));
223
- }
224
-
225
- function maxCandidatesRetrieved(candidateCount, maxCandidates) {
226
- debugData(`Total amount of candidate records retrieved: ${newCandidateCount} (max: ${maxCandidates})`);
227
-
228
- if (maxCandidates && candidateCount >= maxCandidates) {
229
- debug(`Stopped matching because maximum number of candidate records ${candidateCount} / ${maxCandidates} have been retrieved`);
230
- return true;
231
- }
232
- }
233
- } // matches : array of matching candidate records
234
- // nonMatches : array of nonMatching candidate records (if returnNonMatches option is true, otherwise empty array)
235
- // - candidate.id
236
- // - candidate.record
237
- // - probability
238
- // - strategy (if returnStrategy option is true)
239
- // - treshold (if returnStrategy option is true)
240
- // - matchQuery (if returnQuery option is true)
241
- // we could have here also returnRecords/returnMatchRecords/returnNonMatchRecord options that could be turned false for not to return actual record data
242
- // matchStatus.status: boolean, true if matcher retrieved and handled all found candidate records, false if it did not
243
- // matchStatus.stopReason: string ('maxMatches','maxCandidates','maxedQueries','conversionFailures', empty string/undefined), reason for stopping retrieving or handling the candidate records
244
- // - only one stopReason is returned (if there would be several possible stopReasons, stopReason is picked in the above order)
245
- // - currently stopReason can be non-empty also in cases where status is true, if matcher hit the stop reason when handling the last available candidate record
246
-
247
-
248
- function returnResult({
249
- matches,
250
- state,
251
- stopReason,
252
- nonMatches,
253
- duplicateCount,
254
- candidateCount,
255
- nonMatchCount,
256
- conversionFailures
257
- }) {
258
- const conversionFailureCount = conversionFailures.length;
259
- checkCounts({
260
- matches,
261
- nonMatches,
262
- candidateCount,
263
- duplicateCount,
264
- nonMatchCount,
265
- conversionFailureCount
266
- });
267
- const matchStatus = getMatchState(state, stopReason, conversionFailureCount); // add nonMatches to result only if returnNonMatches is 'true', otherwise nonMatches have not been gathered
268
-
269
- const matchesResult = returnNonMatches ? {
270
- matches,
271
- matchStatus,
272
- nonMatches
273
- } : {
274
- matches,
275
- matchStatus
276
- };
277
- const result = returnFailures ? { ...matchesResult,
278
- conversionFailures
279
- } : matchesResult;
280
- debugData(`ReturnFailures ${returnFailures}`);
281
- debugData(`${JSON.stringify(result)}`);
282
- return result; // note that in cases where the matching has been stopped because of maxMatches checkCounts won't (in most cases) match
283
-
284
- function checkCounts({
285
- matches,
286
- nonMatches,
287
- candidateCount,
288
- duplicateCount,
289
- nonMatchCount,
290
- conversionFailureCount
291
- }) {
292
- const matchCount = matches.length;
293
- debugData(`Return nonMatches: ${returnNonMatches}`);
294
- const chosenNonMatchCount = returnNonMatches ? nonMatches.length : nonMatchCount;
295
- const totalHandled = matchCount + chosenNonMatchCount + duplicateCount;
296
- debug(`candidateCount: ${candidateCount}, matches: ${matchCount}, nonMatches: ${chosenNonMatchCount}, duplicateCount: ${duplicateCount}, conversionFailureCount: ${conversionFailureCount}`);
297
- debug(`We got result for ${totalHandled} / ${candidateCount} retrieved candidates`);
298
-
299
- if (totalHandled !== candidateCount) {
300
- debug(`WARNING: Missing results for ${candidateCount - totalHandled} candidates`);
301
- return;
302
- }
303
-
304
- return;
305
- }
306
-
307
- function getMatchState(state, stopReason, conversionFailuresCount) {
308
- debugData(`${JSON.stringify(state)}`);
309
- debug(`We had ${conversionFailuresCount} retrieved candidates that could not be converted.`);
310
- debug(`Queries left ${state.queriesLeft}, Searches for current query left: ${state.resultSetOffset && state.resultSetOffset <= state.totalRecords}, non-retrieved records: ${state.totalRecords - state.queryCandidateCounter}, maxedQueries (${state.maxedQueries.length}): ${state.maxedQueries}`);
311
- debugData(`StopReason: <${stopReason}>`);
312
- const searchesLeft = state.resultSetOffset && state.resultSetOffset <= state.totalRecords;
313
- const nonRetrieved = searchesLeft ? state.totalRecords - state.queryCandidateCounter : 0;
314
- debugData(`nonRetrieved: ${nonRetrieved}`); // matchStatus.stopReason: string ('maxMatches','maxCandidates','maxedQueries','conversionFailures', empty string/undefined), reason for stopping retrieving or handling the candidate records
315
- // 'maxMatches' and 'maxCandidates' are in stopReason, 'maxedQueries' and 'conversionFailures' are created here
316
-
317
- if (state.queriesLeft > 0 || nonRetrieved > 0 || state.maxedQueries.length > 0 || conversionFailureCount > 0) {
318
- const maxedQueriesStopReason = state.maxedQueries.length > 0 ? 'maxedQueries' : undefined;
319
- const conversionFailuresStopReason = conversionFailureCount > 0 ? 'conversionFailures' : undefined;
320
- const newStopReason = stopReason === '' || stopReason === undefined ? maxedQueriesStopReason || conversionFailuresStopReason : stopReason;
321
- debugData(`MaxedQueriesStopReason: <${maxedQueriesStopReason}>`);
322
- debugData(`ConversionFailureStopReason <${conversionFailuresStopReason}>`);
323
- debugData(`NewStopReason: <${newStopReason}>`);
324
- debug(`Match status: false`);
325
- return {
326
- status: false,
327
- stopReason: newStopReason
328
- };
329
- }
330
-
331
- debug(`Match status: true`);
332
- return {
333
- status: true,
334
- stopReason
335
- };
336
- }
337
- }
338
-
339
- function iterateRecords({
340
- records,
341
- recordSetSize,
342
- maxMatches,
343
- matches = [],
344
- nonMatches = [],
345
- recordMatches = [],
346
- recordNonMatches = [],
347
- recordCount = 0,
348
- recordDuplicateCount = 0,
349
- recordNonMatchCount = 0
350
- }) {
351
- // recordSetSize : total amount of records in the current record set
352
- // recordCount : amount of records from the current record set that have been handled
353
- // maxMatches : setting for maximum amount found by current matcher job before the matcher job is stopped
354
- // recordDuplicateCount : amount of records from the current record set that are already included in matches/nonMatches results
355
- // recordNonMatchCount: amount of records from the current record set that are nonMatches (only is returnNonMatches setting is false)
356
- // records : non-handled records in the current record set
357
- // matches : found matches in the current matcher job
358
- // recordMatches : found matches in the current record set
359
- // recordNonMatches : found nonMatches in the current record set (only if returnNonMatches setting is true)
360
- const [candidate] = records;
361
- const newRecordCount = candidate ? recordCount + 1 : recordCount; // The matcher uses same matchDetection strategy for candidates from all candidate-searches -> matchDetection result for the same candidate is always same
362
- // Exceptions would happen if the candidate would have been updated in the database between candidate searches
363
- // Note that if returnNonMatches is false, matcher won't remember candidates that didn't match, so they will be matched again everytime they are retrieved by
364
- // different candidate search queries. Same candidate search query won't have duplicate records.
365
-
366
- if (candidate) {
367
- if (candidateNotInMatches(matches.concat(nonMatches), candidate)) {
368
- const {
369
- record: candidateRecord,
370
- id: candidateId
371
- } = candidate;
372
- debug(`Running matchDetection for record ${candidateId} (${newRecordCount}/${recordSetSize})`);
373
- const detectionResult = detect(record, candidateRecord);
374
- return handleDetectionResult(detectionResult, candidateId, candidateRecord);
375
- }
376
-
377
- return iterateRecords({
378
- records: records.slice(1),
379
- recordSetSize,
380
- maxMatches,
381
- matches,
382
- recordMatches,
383
- recordCount: newRecordCount,
384
- recordNonMatches,
385
- recordDuplicateCount: recordDuplicateCount + 1,
386
- recordNonMatchCount
387
- });
388
- }
389
-
390
- debug(`No more candidates, record set (${recordCount}/${recordSetSize}) done, ${recordMatches.length} matches found, ${recordDuplicateCount} candidates already handled, ${returnNonMatches ? `${recordNonMatches.length}` : `${recordNonMatchCount}`} nonMatches found.`);
391
- return {
392
- matches: recordMatches,
393
- nonMatches: returnNonMatches ? recordNonMatches : [],
394
- duplicateCount: recordDuplicateCount,
395
- nonMatchCount: recordNonMatchCount
396
- };
397
-
398
- function handleDetectionResult(detectionResult, candidateId, candidateRecord) {
399
- debugData(`MatchDetection results for ${candidateId} (${newRecordCount}/${recordSetSize}): ${JSON.stringify(detectionResult)}`);
400
-
401
- if (detectionResult.match || returnNonMatches) {
402
- debug(`${detectionResult.match ? `Record ${candidateId} (${newRecordCount}/${recordSetSize}) is a match!` : `Record ${candidateId} (${newRecordCount}/${recordSetSize}) is NOT a match!`}`);
403
- debugData(`Strategy: ${JSON.stringify(detectionResult.strategy)}, Treshold: ${JSON.stringify(detectionResult.treshold)}`);
404
- const matchResult = {
405
- probability: detectionResult.probability,
406
- candidate: {
407
- id: candidateId,
408
- record: candidateRecord
409
- }
410
- };
411
- const strategyResult = {
412
- strategy: detectionResult.strategy,
413
- treshold: detectionResult.treshold
414
- };
415
- const newMatch = returnStrategy ? { ...matchResult,
416
- ...strategyResult
417
- } : { ...matchResult
418
- };
419
- debugData(`${JSON.stringify(newMatch)}`);
420
- return handleRecordMatch(detectionResult.match, newMatch);
421
- }
422
-
423
- const newRecordNonMatchCount = recordNonMatchCount + 1;
424
- debugData(`- Total nonMatches after this detection: ${newRecordNonMatchCount}`);
425
- return iterateRecords({
426
- records: records.slice(1),
427
- recordSetSize,
428
- maxMatches,
429
- matches,
430
- recordMatches,
431
- recordCount: newRecordCount,
432
- recordNonMatches,
433
- recordDuplicateCount,
434
- recordNonMatchCount: newRecordNonMatchCount
435
- });
436
- }
437
-
438
- function handleRecordMatch(isMatch, newMatch) {
439
- const newRecordMatches = isMatch ? recordMatches.concat(newMatch) : recordMatches;
440
- const newRecordNonMatches = isMatch ? recordNonMatches : recordNonMatches.concat(newMatch);
441
- const newRecordNonMatchCount = isMatch ? recordNonMatchCount : recordNonMatchCount + 1;
442
- debugData(`- Total matches after this detection: ${matches.concat(newRecordMatches).length} (max: ${maxMatches})`); // eslint-disable-next-line functional/no-conditional-statement
443
-
444
- if (returnNonMatches) {
445
- debugData(`- Total nonMatches after this detection: ${nonMatches.concat(newRecordNonMatches).length}`);
446
- }
447
-
448
- debugData(`- Total nonMatchCount after this detection: ${recordNonMatchCount}`);
449
-
450
- if (maxMatchesFound({
451
- matches: matches.concat(newRecordMatches),
452
- maxMatches
453
- })) {
454
- debug(`MaxMatches (${maxMatches}) reached, handled candidates in record set: ${newRecordCount} non-handled candidates in record set ${recordSetSize - newRecordCount}`);
455
- return {
456
- matches: newRecordMatches,
457
- nonMatches: returnNonMatches ? newRecordNonMatches : [],
458
- duplicateCount: recordDuplicateCount,
459
- nonMatchCount: newRecordNonMatchCount
460
- };
461
- }
462
-
463
- return iterateRecords({
464
- records: records.slice(1),
465
- recordSetSize,
466
- maxMatches,
467
- matches,
468
- recordMatches: newRecordMatches,
469
- recordCount: newRecordCount,
470
- recordNonMatches: returnNonMatches ? newRecordNonMatches : [],
471
- duplicateCount: recordDuplicateCount,
472
- recordNonMatchCount: newRecordNonMatchCount
473
- });
474
- }
475
-
476
- function candidateNotInMatches(matches, candidate) {
477
- debug(`Checking that record ${candidate.id} is not already included in ${matches.length} matches/nonMatches`);
478
- const newCandidateId = candidate.id;
479
- debugData(`newCandidateId: ${newCandidateId}`);
480
- const result = matches.find(({
481
- candidate
482
- }) => candidate.id === newCandidateId);
483
- debugData(`Result: ${result}`);
484
-
485
- if (result) {
486
- debug(`${candidate.id} was already handled.`);
487
- return false;
488
- }
489
-
490
- debug(`${candidate.id} not found in matches/nonMatches`);
491
- return true;
492
- }
493
- }
494
-
495
- function maxMatchesFound({
496
- matches,
497
- maxMatches
498
- }) {
499
- if (maxMatches && matches.length >= maxMatches) {
500
- debug(`Stopping recordSet iteration: maxMatches (${maxMatches}) for matcher job found.`);
501
- return true;
502
- }
503
- }
504
- };
505
- };
506
-
507
- exports.default = _default;
508
- //# sourceMappingURL=index.js.map
package/dist/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/index.js"],"names":["detection","detectionOptions","search","searchOptions","maxMatches","maxCandidates","returnStrategy","returnQuery","returnNonMatches","returnFailures","debug","debugData","extend","JSON","stringify","detect","record","iterate","initialState","matches","candidateCount","nonMatches","duplicateCount","nonMatchCount","conversionFailures","records","failures","state","length","recordSetSize","failureSetSize","newCandidateCount","newConversionFailures","concat","handleRecordSet","queriesLeft","searchCounter","query","returnResult","stopReason","matchResult","iterateRecords","newDuplicateCount","newNonMatchCount","newMatches","newNonMatches","handleMatchResult","maxMatchesFound","maxCandidatesRetrieved","addQuery","map","match","matchQuery","conversionFailureCount","checkCounts","matchStatus","getMatchState","matchesResult","result","matchCount","chosenNonMatchCount","totalHandled","conversionFailuresCount","resultSetOffset","totalRecords","queryCandidateCounter","maxedQueries","searchesLeft","nonRetrieved","maxedQueriesStopReason","undefined","conversionFailuresStopReason","newStopReason","status","recordMatches","recordNonMatches","recordCount","recordDuplicateCount","recordNonMatchCount","candidate","newRecordCount","candidateNotInMatches","candidateRecord","id","candidateId","detectionResult","handleDetectionResult","slice","strategy","treshold","probability","strategyResult","newMatch","handleRecordMatch","newRecordNonMatchCount","isMatch","newRecordMatches","newRecordNonMatches","newCandidateId","find"],"mappings":";;;;;;;AA4BA;;AACA;;;;AACA;;;;;;;;;;AA9BA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;eAQe,CAAC;AAACA,EAAAA,SAAS,EAAEC,gBAAZ;AAA8BC,EAAAA,MAAM,EAAEC,aAAtC;AAAqDC,EAAAA,UAAU,GAAG,CAAlE;AAAqEC,EAAAA,aAAa,GAAG,EAArF;AAAyFC,EAAAA,cAAc,GAAG,KAA1G;AAAiHC,EAAAA,WAAW,GAAG,KAA/H;AAAsIC,EAAAA,gBAAgB,GAAG,KAAzJ;AAAgKC,EAAAA,cAAc,GAAG;AAAjL,CAAD,KAA6L;AAC1M,QAAMC,KAAK,GAAG,oBAAkB,yCAAlB,CAAd;AACA,QAAMC,SAAS,GAAGD,KAAK,CAACE,MAAN,CAAa,MAAb,CAAlB;AAEAD,EAAAA,SAAS,CAAE,qBAAoBE,IAAI,CAACC,SAAL,CAAeb,gBAAf,CAAiC,EAAvD,CAAT;AACAU,EAAAA,SAAS,CAAE,kBAAiBE,IAAI,CAACC,SAAL,CAAeX,aAAf,CAA8B,EAAjD,CAAT;AACAQ,EAAAA,SAAS,CAAE,eAAcE,IAAI,CAACC,SAAL,CAAeV,UAAf,CAA2B,EAA3C,CAAT;AACAO,EAAAA,SAAS,CAAE,kBAAiBE,IAAI,CAACC,SAAL,CAAeT,aAAf,CAA8B,EAAjD,CAAT;AACAM,EAAAA,SAAS,CAAE,mBAAkBE,IAA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;AAAV,KAAzB,EAAgD;AAC9C,UAAIA,UAAU,IAAIe,OAAO,CAACS,MAAR,IAAkBxB,UAApC,EAAgD;AAC9CM,QAAAA,KAAK,CAAE,6CAA4CN,UAAW,0BAAzD,CAAL;AACA,eAAO,IAAP;AACD;AACF;AACF,GAnRD;AAoRD,C","sourcesContent":["/**\n*\n* @licstart The following is the entire license notice for the JavaScript code in this file.\n*\n* Melinda record matching modules for Javascript\n*\n* Copyright (C) 2020-2022 University Of Helsinki (The National Library Of Finland)\n*\n* This file is part of melinda-record-matching-js\n*\n* melinda-record-matching-js program is free software: you can redistribute it and/or modify\n* it under the terms of the GNU Lesser General Public License as\n* published by the Free Software Foundation, either version 3 of the\n* License, or (at your option) any later version.\n*\n* melinda-record-matching-js is distributed in the hope that it will be useful,\n* but WITHOUT ANY WARRANTY; without even the implied warranty of\n* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n* GNU Lesser General Public License for more details.\n*\n* You should have received a copy of the GNU Affero General Public License\n* along with this program. If not, see <http://www.gnu.org/licenses/>.\n*\n* @licend The above is the entire license notice\n* for the JavaScript code in this file.\n*\n*/\n\nimport createDebugLogger from 'debug';\nimport createSearchInterface, * as candidateSearch from './candidate-search';\nimport createDetectionInterface, * as matchDetection from './match-detection';\n\nexport {candidateSearch, matchDetection};\n\nexport default ({detection: detectionOptions, search: searchOptions, maxMatches = 1, maxCandidates = 25, returnStrategy = false, returnQuery = false, returnNonMatches = false, returnFailures = false}) => {\n const debug = createDebugLogger('@natlibfi/melinda-record-matching:index');\n const debugData = debug.extend('data');\n\n debugData(`DetectionOptions: ${JSON.stringify(detectionOptions)}`);\n debugData(`SearchOptions: ${JSON.stringify(searchOptions)}`);\n debugData(`MaxMatches: ${JSON.stringify(maxMatches)}`);\n debugData(`MaxCandidates: ${JSON.stringify(maxCandidates)}`);\n debugData(`ReturnStrategy: ${JSON.stringify(returnStrategy)}`);\n debugData(`ReturnQuery: ${JSON.stringify(returnQuery)}`);\n debugData(`ReturnNonMatches: ${JSON.stringify(returnNonMatches)}`);\n debugData(`ReturnFailures: ${JSON.stringify(returnFailures)}`);\n\n\n const detect = createDetectionInterface(detectionOptions, returnStrategy);\n\n return record => {\n const search = createSearchInterface({...searchOptions, record, maxCandidates});\n return iterate({});\n\n // candidateCount : amount of candidate records retrived from SRU for matching, NOT including current record set\n // matches : candidates that have been detected as matches by current matcher job\n // nonMatches : candidates that have been detected as non-matches by current matcher job (only if returnNonMatches is 'true')\n // duplicateCount : amount of candidate records that were retrieved from the SRU but not handled further because they were already found in the matches/nonMatches\n\n // state.totalRecords : amount of candidate records available to the current query (undefined, if there was no queries left)\n // state.query : current query (undefined if there was no queries left)\n // state.searchCounter : sequence for current search for current query (undefined, if there we no queries left)\n // state.queryCandidateCounter: amount of candidate records retrieved from SRU for matching for current query, including the current record set (undefined if there were no queries left)\n // state.queriesLeft : amount of queries left\n // state.queryCounter : sequence for current query\n // state.maxedQueries : queries that resulted in more than serverMaxResults hits\n\n async function iterate({initialState = {}, matches = [], candidateCount = 0, nonMatches = [], duplicateCount = 0, nonMatchCount = 0, conversionFailures = []}) {\n debugData(`Starting next matcher iteration.`);\n const {records, failures, ...state} = await search(initialState);\n\n debugData(`Current state: ${JSON.stringify(state)}, matches: ${matches.length}, candidateCount: ${candidateCount}, nonMatches: ${nonMatches.length}, nonMatchCount: ${nonMatchCount}, conversionFailures: ${conversionFailures}`);\n const recordSetSize = records.length;\n const failureSetSize = failures.length;\n const newCandidateCount = candidateCount + recordSetSize + failureSetSize;\n\n const newConversionFailures = conversionFailures.concat(failures);\n debugData(`Failures: ${failures.length}, ConversionFailures: ${conversionFailures.length}, NewConversionFailures: ${newConversionFailures.length}`);\n\n if (recordSetSize > 0) {\n return handleRecordSet();\n }\n\n if (state.queriesLeft > 0) {\n debug(`Empty record set ${state.searchCounter} for ${state.query}, but there are ${state.queriesLeft} queries left`);\n return iterate({initialState: state, matches, candidateCount: newCandidateCount, nonMatches, nonMatchCount, duplicateCount, conversionFailures: newConversionFailures});\n }\n\n debug(`No (more) candidate records to check, no more queries left, matches: ${matches.length}`);\n return returnResult({matches, state, stopReason: '', nonMatches, nonMatchCount, candidateCount: newCandidateCount, duplicateCount, conversionFailures: newConversionFailures});\n\n function handleRecordSet() {\n debug(`Checking record set of ${recordSetSize} candidate records for possible matches, found by ${state.searchCounter} search for ${state.query}`);\n\n const matchResult = iterateRecords({records, recordSetSize, maxMatches, matches, nonMatches, nonMatchCount});\n\n const newDuplicateCount = duplicateCount + matchResult.duplicateCount;\n const newNonMatchCount = nonMatchCount + matchResult.nonMatchCount;\n const {newMatches, newNonMatches} = handleMatchResult(matchResult, matches, nonMatches);\n\n if (maxMatchesFound({matches: newMatches, maxMatches})) {\n return returnResult({matches: newMatches, state, stopReason: 'maxMatches', nonMatches: newNonMatches, duplicateCount: newDuplicateCount, candidateCount: newCandidateCount, nonMatchCount: newNonMatchCount, conversionFailures: newConversionFailures});\n }\n\n if (maxCandidatesRetrieved(newCandidateCount, maxCandidates)) {\n return returnResult({matches: newMatches, state, stopReason: 'maxCandidates', nonMatches: newNonMatches, duplicateCount: newDuplicateCount, candidateCount: newCandidateCount, nonMatchCount: newNonMatchCount, conversionFailures: newConversionFailures});\n }\n\n return iterate({initialState: state, matches: newMatches, candidateCount: newCandidateCount, nonMatches: newNonMatches, duplicateCount: newDuplicateCount, nonMatchCount: newNonMatchCount, conversionFailures: newConversionFailures});\n }\n\n function handleMatchResult(matchResult, matches, nonMatches) {\n debugData(`- Amount of new matches from record set: ${matchResult.matches.length}`);\n // eslint-disable-next-line functional/no-conditional-statement\n if (returnNonMatches) {\n debugData(`- Amount of new nonMatches from record set: ${matchResult.nonMatches.length}`);\n }\n\n const newMatches = matches.concat(returnQuery ? addQuery(matchResult.matches) : matchResult.matches);\n const newNonMatches = returnNonMatches ? nonMatches.concat(returnQuery ? addQuery(matchResult.nonMatches) : matchResult.nonMatches) : [];\n\n debugData(`- Total amount of matches: ${newMatches.length}`);\n // eslint-disable-next-line functional/no-conditional-statement\n if (returnNonMatches) {\n debugData(`- Total amount of nonMatches: ${newNonMatches.length}`);\n }\n\n return {newMatches, newNonMatches};\n }\n\n function addQuery(matches) {\n debugData(`Adding query ${state.query} to matches`);\n return matches.map((match) => ({...match, matchQuery: state.query}));\n }\n\n function maxCandidatesRetrieved(candidateCount, maxCandidates) {\n debugData(`Total amount of candidate records retrieved: ${newCandidateCount} (max: ${maxCandidates})`);\n if (maxCandidates && candidateCount >= maxCandidates) {\n debug(`Stopped matching because maximum number of candidate records ${candidateCount} / ${maxCandidates} have been retrieved`);\n return true;\n }\n }\n }\n\n // matches : array of matching candidate records\n // nonMatches : array of nonMatching candidate records (if returnNonMatches option is true, otherwise empty array)\n // - candidate.id\n // - candidate.record\n // - probability\n // - strategy (if returnStrategy option is true)\n // - treshold (if returnStrategy option is true)\n // - matchQuery (if returnQuery option is true)\n\n // we could have here also returnRecords/returnMatchRecords/returnNonMatchRecord options that could be turned false for not to return actual record data\n\n // matchStatus.status: boolean, true if matcher retrieved and handled all found candidate records, false if it did not\n // matchStatus.stopReason: string ('maxMatches','maxCandidates','maxedQueries','conversionFailures', empty string/undefined), reason for stopping retrieving or handling the candidate records\n // - only one stopReason is returned (if there would be several possible stopReasons, stopReason is picked in the above order)\n // - currently stopReason can be non-empty also in cases where status is true, if matcher hit the stop reason when handling the last available candidate record\n\n function returnResult({matches, state, stopReason, nonMatches, duplicateCount, candidateCount, nonMatchCount, conversionFailures}) {\n const conversionFailureCount = conversionFailures.length;\n checkCounts({matches, nonMatches, candidateCount, duplicateCount, nonMatchCount, conversionFailureCount});\n const matchStatus = getMatchState(state, stopReason, conversionFailureCount);\n // add nonMatches to result only if returnNonMatches is 'true', otherwise nonMatches have not been gathered\n const matchesResult = returnNonMatches ? {matches, matchStatus, nonMatches} : {matches, matchStatus};\n const result = returnFailures ? {...matchesResult, conversionFailures} : matchesResult;\n debugData(`ReturnFailures ${returnFailures}`);\n debugData(`${JSON.stringify(result)}`);\n return result;\n\n // note that in cases where the matching has been stopped because of maxMatches checkCounts won't (in most cases) match\n\n function checkCounts({matches, nonMatches, candidateCount, duplicateCount, nonMatchCount, conversionFailureCount}) {\n const matchCount = matches.length;\n debugData(`Return nonMatches: ${returnNonMatches}`);\n const chosenNonMatchCount = returnNonMatches ? nonMatches.length : nonMatchCount;\n const totalHandled = matchCount + chosenNonMatchCount + duplicateCount;\n debug(`candidateCount: ${candidateCount}, matches: ${matchCount}, nonMatches: ${chosenNonMatchCount}, duplicateCount: ${duplicateCount}, conversionFailureCount: ${conversionFailureCount}`);\n debug(`We got result for ${totalHandled} / ${candidateCount} retrieved candidates`);\n if (totalHandled !== candidateCount) {\n debug(`WARNING: Missing results for ${candidateCount - totalHandled} candidates`);\n return;\n }\n return;\n }\n\n function getMatchState(state, stopReason, conversionFailuresCount) {\n debugData(`${JSON.stringify(state)}`);\n debug(`We had ${conversionFailuresCount} retrieved candidates that could not be converted.`);\n debug(`Queries left ${state.queriesLeft}, Searches for current query left: ${state.resultSetOffset && state.resultSetOffset <= state.totalRecords}, non-retrieved records: ${state.totalRecords - state.queryCandidateCounter}, maxedQueries (${state.maxedQueries.length}): ${state.maxedQueries}`);\n\n debugData(`StopReason: <${stopReason}>`);\n\n const searchesLeft = state.resultSetOffset && state.resultSetOffset <= state.totalRecords;\n const nonRetrieved = searchesLeft ? state.totalRecords - state.queryCandidateCounter : 0;\n debugData(`nonRetrieved: ${nonRetrieved}`);\n\n // matchStatus.stopReason: string ('maxMatches','maxCandidates','maxedQueries','conversionFailures', empty string/undefined), reason for stopping retrieving or handling the candidate records\n // 'maxMatches' and 'maxCandidates' are in stopReason, 'maxedQueries' and 'conversionFailures' are created here\n\n if (state.queriesLeft > 0 || nonRetrieved > 0 || state.maxedQueries.length > 0 || conversionFailureCount > 0) {\n const maxedQueriesStopReason = state.maxedQueries.length > 0 ? 'maxedQueries' : undefined;\n const conversionFailuresStopReason = conversionFailureCount > 0 ? 'conversionFailures' : undefined;\n const newStopReason = stopReason === '' || stopReason === undefined ? maxedQueriesStopReason || conversionFailuresStopReason : stopReason;\n debugData(`MaxedQueriesStopReason: <${maxedQueriesStopReason}>`);\n debugData(`ConversionFailureStopReason <${conversionFailuresStopReason}>`);\n debugData(`NewStopReason: <${newStopReason}>`);\n debug(`Match status: false`);\n return {status: false, stopReason: newStopReason};\n }\n\n debug(`Match status: true`);\n return {status: true, stopReason};\n }\n }\n\n function iterateRecords({records, recordSetSize, maxMatches, matches = [], nonMatches = [], recordMatches = [], recordNonMatches = [], recordCount = 0, recordDuplicateCount = 0, recordNonMatchCount = 0}) {\n\n // recordSetSize : total amount of records in the current record set\n // recordCount : amount of records from the current record set that have been handled\n // maxMatches : setting for maximum amount found by current matcher job before the matcher job is stopped\n // recordDuplicateCount : amount of records from the current record set that are already included in matches/nonMatches results\n // recordNonMatchCount: amount of records from the current record set that are nonMatches (only is returnNonMatches setting is false)\n\n // records : non-handled records in the current record set\n // matches : found matches in the current matcher job\n // recordMatches : found matches in the current record set\n // recordNonMatches : found nonMatches in the current record set (only if returnNonMatches setting is true)\n\n const [candidate] = records;\n const newRecordCount = candidate ? recordCount + 1 : recordCount;\n\n // The matcher uses same matchDetection strategy for candidates from all candidate-searches -> matchDetection result for the same candidate is always same\n // Exceptions would happen if the candidate would have been updated in the database between candidate searches\n // Note that if returnNonMatches is false, matcher won't remember candidates that didn't match, so they will be matched again everytime they are retrieved by\n // different candidate search queries. Same candidate search query won't have duplicate records.\n\n if (candidate) {\n\n if (candidateNotInMatches(matches.concat(nonMatches), candidate)) {\n const {record: candidateRecord, id: candidateId} = candidate;\n debug(`Running matchDetection for record ${candidateId} (${newRecordCount}/${recordSetSize})`);\n const detectionResult = detect(record, candidateRecord);\n return handleDetectionResult(detectionResult, candidateId, candidateRecord);\n }\n return iterateRecords({records: records.slice(1), recordSetSize, maxMatches, matches, recordMatches, recordCount: newRecordCount, recordNonMatches, recordDuplicateCount: recordDuplicateCount + 1, recordNonMatchCount});\n }\n\n debug(`No more candidates, record set (${recordCount}/${recordSetSize}) done, ${recordMatches.length} matches found, ${recordDuplicateCount} candidates already handled, ${returnNonMatches ? `${recordNonMatches.length}` : `${recordNonMatchCount}`} nonMatches found.`);\n return {matches: recordMatches, nonMatches: returnNonMatches ? recordNonMatches : [], duplicateCount: recordDuplicateCount, nonMatchCount: recordNonMatchCount};\n\n function handleDetectionResult(detectionResult, candidateId, candidateRecord) {\n debugData(`MatchDetection results for ${candidateId} (${newRecordCount}/${recordSetSize}): ${JSON.stringify(detectionResult)}`);\n\n if (detectionResult.match || returnNonMatches) {\n debug(`${detectionResult.match ? `Record ${candidateId} (${newRecordCount}/${recordSetSize}) is a match!` : `Record ${candidateId} (${newRecordCount}/${recordSetSize}) is NOT a match!`}`);\n debugData(`Strategy: ${JSON.stringify(detectionResult.strategy)}, Treshold: ${JSON.stringify(detectionResult.treshold)}`);\n\n const matchResult = {\n probability: detectionResult.probability,\n candidate: {\n id: candidateId,\n record: candidateRecord\n }\n };\n const strategyResult = {\n strategy: detectionResult.strategy,\n treshold: detectionResult.treshold\n };\n const newMatch = returnStrategy ? {...matchResult, ...strategyResult} : {...matchResult};\n\n debugData(`${JSON.stringify(newMatch)}`);\n\n return handleRecordMatch(detectionResult.match, newMatch);\n }\n\n const newRecordNonMatchCount = recordNonMatchCount + 1;\n debugData(`- Total nonMatches after this detection: ${newRecordNonMatchCount}`);\n\n return iterateRecords({records: records.slice(1), recordSetSize, maxMatches, matches, recordMatches, recordCount: newRecordCount, recordNonMatches, recordDuplicateCount, recordNonMatchCount: newRecordNonMatchCount});\n }\n\n function handleRecordMatch(isMatch, newMatch) {\n const newRecordMatches = isMatch ? recordMatches.concat(newMatch) : recordMatches;\n const newRecordNonMatches = isMatch ? recordNonMatches : recordNonMatches.concat(newMatch);\n const newRecordNonMatchCount = isMatch ? recordNonMatchCount : recordNonMatchCount + 1;\n\n debugData(`- Total matches after this detection: ${matches.concat(newRecordMatches).length} (max: ${maxMatches})`);\n\n // eslint-disable-next-line functional/no-conditional-statement\n if (returnNonMatches) {\n debugData(`- Total nonMatches after this detection: ${nonMatches.concat(newRecordNonMatches).length}`);\n }\n debugData(`- Total nonMatchCount after this detection: ${recordNonMatchCount}`);\n\n if (maxMatchesFound({matches: matches.concat(newRecordMatches), maxMatches})) {\n debug(`MaxMatches (${maxMatches}) reached, handled candidates in record set: ${newRecordCount} non-handled candidates in record set ${recordSetSize - newRecordCount}`);\n return {matches: newRecordMatches, nonMatches: returnNonMatches ? newRecordNonMatches : [], duplicateCount: recordDuplicateCount, nonMatchCount: newRecordNonMatchCount};\n }\n\n return iterateRecords({records: records.slice(1), recordSetSize, maxMatches, matches, recordMatches: newRecordMatches, recordCount: newRecordCount, recordNonMatches: returnNonMatches ? newRecordNonMatches : [], duplicateCount: recordDuplicateCount, recordNonMatchCount: newRecordNonMatchCount});\n }\n\n function candidateNotInMatches(matches, candidate) {\n debug(`Checking that record ${candidate.id} is not already included in ${matches.length} matches/nonMatches`);\n const newCandidateId = candidate.id;\n debugData(`newCandidateId: ${newCandidateId}`);\n const result = matches.find(({candidate}) => candidate.id === newCandidateId);\n debugData(`Result: ${result}`);\n if (result) {\n debug(`${candidate.id} was already handled.`);\n return false;\n }\n debug(`${candidate.id} not found in matches/nonMatches`);\n return true;\n }\n }\n\n function maxMatchesFound({matches, maxMatches}) {\n if (maxMatches && matches.length >= maxMatches) {\n debug(`Stopping recordSet iteration: maxMatches (${maxMatches}) for matcher job found.`);\n return true;\n }\n }\n };\n};\n"],"file":"index.js"}