@natlibfi/melinda-rest-api-commons 2.3.2-alpha.6 → 2.3.2-alpha.8

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.
@@ -1 +1 @@
1
- {"version":3,"file":"fix-constants.js","names":["BIB_F035_TO_SID","f035Filters","fSIDFilters","REPLACE_PREFIXES","oldPrefix","newPrefix","prefixReplaceCodes","BIB_FORMAT_SETTINGS","replacePrefixes","generateMissingSIDs","BIB_PREVALIDATION_FIX_SETTINGS","BIB_POSTMERGE_FIX_SETTINGS","handleTempUrns","stripF884s","BIB_PREIMPORT_FIX_SETTINGS","AUTNAME_PREIMPORT_FIX_SETTINGS","BIB_HANDLE_TEMP_URNS_SETTINGS","BIB_STRIP_F884S_SETTINGS"],"sources":["../../src/helpers/fix-constants.js"],"sourcesContent":["/**\n*\n* @licstart The following is the entire license notice for the JavaScript code in this file.\n*\n* Shared modules for microservices of Melinda rest api batch import system\n*\n* Copyright (C) 2020-2022 University Of Helsinki (The National Library Of Finland)\n*\n* This file is part of melinda-rest-api-commons\n*\n* melinda-rest-api-commons program is free software: you can redistribute it and/or modify\n* it under the terms of the GNU Affero 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-rest-api-commons 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 Affero 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\n// This could be formatted so that 035-prefix and SID contents would be better connected than by just an array index\nexport const BIB_F035_TO_SID = {\n f035Filters: [/^\\(FI-BTJ\\)/u, /^\\(FI-TATI\\)/u],\n fSIDFilters: ['FI-BTJ', 'tati']\n};\n\n// There is somewhere other implementations of this same standardPrefixes to alephInternalPrefixes normalization\nexport const REPLACE_PREFIXES = [\n {\n oldPrefix: 'FI-MELINDA',\n newPrefix: 'FIN01',\n prefixReplaceCodes: ['w']\n },\n {\n oldPrefix: 'FI-ASTERI-S',\n newPrefix: 'FIN10',\n prefixReplaceCodes: ['0']\n },\n {\n oldPrefix: 'FI-ASTERI-N',\n newPrefix: 'FIN11',\n prefixReplaceCodes: ['0']\n },\n {\n oldPrefix: 'FI-ASTERI-A',\n newPrefix: 'FIN12',\n prefixReplaceCodes: ['0']\n },\n {\n oldPrefix: 'FI-ASTERI-W',\n newPrefix: 'FIN13',\n prefixReplaceCodes: ['0']\n }\n];\n\n\n// If placed in config.js testing needs envs\n// BIB_FORMAT_SETTINGS is outdated, use other settings instead\n\n\nexport const BIB_FORMAT_SETTINGS = {\n replacePrefixes: REPLACE_PREFIXES,\n generateMissingSIDs: BIB_F035_TO_SID\n};\n\nexport const BIB_PREVALIDATION_FIX_SETTINGS = {\n generateMissingSIDs: BIB_F035_TO_SID\n};\n\nexport const BIB_POSTMERGE_FIX_SETTINGS = {\n handleTempUrns: true,\n stripF884s: true\n};\n\nexport const BIB_PREIMPORT_FIX_SETTINGS = {\n replacePrefixes: REPLACE_PREFIXES\n};\n\nexport const AUTNAME_PREIMPORT_FIX_SETTINGS = {\n replacePrefixes: REPLACE_PREFIXES\n};\n\nexport const BIB_HANDLE_TEMP_URNS_SETTINGS = {\n handleTempUrns: true\n};\n\nexport const BIB_STRIP_F884S_SETTINGS = {\n stripF884s: true\n};\n"],"mappings":";;;;;;;AAAA;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;AAEA;AACO,MAAMA,eAAe,GAAG;EAC7BC,WAAW,EAAE,CAAC,cAAD,EAAiB,eAAjB,CADgB;EAE7BC,WAAW,EAAE,CAAC,QAAD,EAAW,MAAX;AAFgB,CAAxB,C,CAKP;;;AACO,MAAMC,gBAAgB,GAAG,CAC9B;EACEC,SAAS,EAAE,YADb;EAEEC,SAAS,EAAE,OAFb;EAGEC,kBAAkB,EAAE,CAAC,GAAD;AAHtB,CAD8B,EAM9B;EACEF,SAAS,EAAE,aADb;EAEEC,SAAS,EAAE,OAFb;EAGEC,kBAAkB,EAAE,CAAC,GAAD;AAHtB,CAN8B,EAW9B;EACEF,SAAS,EAAE,aADb;EAEEC,SAAS,EAAE,OAFb;EAGEC,kBAAkB,EAAE,CAAC,GAAD;AAHtB,CAX8B,EAgB9B;EACEF,SAAS,EAAE,aADb;EAEEC,SAAS,EAAE,OAFb;EAGEC,kBAAkB,EAAE,CAAC,GAAD;AAHtB,CAhB8B,EAqB9B;EACEF,SAAS,EAAE,aADb;EAEEC,SAAS,EAAE,OAFb;EAGEC,kBAAkB,EAAE,CAAC,GAAD;AAHtB,CArB8B,CAAzB,C,CA6BP;AACA;;;AAGO,MAAMC,mBAAmB,GAAG;EACjCC,eAAe,EAAEL,gBADgB;EAEjCM,mBAAmB,EAAET;AAFY,CAA5B;;AAKA,MAAMU,8BAA8B,GAAG;EAC5CD,mBAAmB,EAAET;AADuB,CAAvC;;AAIA,MAAMW,0BAA0B,GAAG;EACxCC,cAAc,EAAE,IADwB;EAExCC,UAAU,EAAE;AAF4B,CAAnC;;AAKA,MAAMC,0BAA0B,GAAG;EACxCN,eAAe,EAAEL;AADuB,CAAnC;;AAIA,MAAMY,8BAA8B,GAAG;EAC5CP,eAAe,EAAEL;AAD2B,CAAvC;;AAIA,MAAMa,6BAA6B,GAAG;EAC3CJ,cAAc,EAAE;AAD2B,CAAtC;;AAIA,MAAMK,wBAAwB,GAAG;EACtCJ,UAAU,EAAE;AAD0B,CAAjC"}
1
+ {"version":3,"file":"fix-constants.js","names":["BIB_F035_TO_SID","f035Filters","fSIDFilters","REPLACE_PREFIXES","oldPrefix","newPrefix","prefixReplaceCodes","BIB_FORMAT_SETTINGS","replacePrefixes","generateMissingSIDs","BIB_PREVALIDATION_FIX_SETTINGS","BIB_POSTMERGE_FIX_SETTINGS","handleTempUrns","stripF884s","BIB_PREIMPORT_FIX_SETTINGS","AUTNAME_PREIMPORT_FIX_SETTINGS","BIB_HANDLE_TEMP_URNS_SETTINGS","BIB_STRIP_F884S_SETTINGS"],"sources":["../../src/helpers/fix-constants.js"],"sourcesContent":["/**\n*\n* @licstart The following is the entire license notice for the JavaScript code in this file.\n*\n* Shared modules for microservices of Melinda rest api batch import system\n*\n* Copyright (C) 2020-2022 University Of Helsinki (The National Library Of Finland)\n*\n* This file is part of melinda-rest-api-commons\n*\n* melinda-rest-api-commons program is free software: you can redistribute it and/or modify\n* it under the terms of the GNU Affero 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-rest-api-commons 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 Affero 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\n// This could be formatted so that 035-prefix and SID contents would be better connected than by just an array index\nexport const BIB_F035_TO_SID = {\n f035Filters: [/^\\(FI-BTJ\\)/u, /^\\(FI-TATI\\)/u],\n fSIDFilters: ['FI-BTJ', 'tati']\n};\n\n// There is somewhere other implementations of this same standardPrefixes to alephInternalPrefixes normalization\nexport const REPLACE_PREFIXES = [\n {\n oldPrefix: 'FI-MELINDA',\n newPrefix: 'FIN01',\n prefixReplaceCodes: ['w']\n },\n {\n oldPrefix: 'FI-ASTERI-S',\n newPrefix: 'FIN10',\n prefixReplaceCodes: ['0']\n },\n {\n oldPrefix: 'FI-ASTERI-N',\n newPrefix: 'FIN11',\n prefixReplaceCodes: ['0']\n },\n {\n oldPrefix: 'FI-ASTERI-A',\n newPrefix: 'FIN12',\n prefixReplaceCodes: ['0']\n },\n {\n oldPrefix: 'FI-ASTERI-W',\n newPrefix: 'FIN13',\n prefixReplaceCodes: ['0']\n }\n];\n\n\n// If placed in config.js testing needs envs\n// BIB_FORMAT_SETTINGS is outdated, use other settings instead\n\n\nexport const BIB_FORMAT_SETTINGS = {\n replacePrefixes: REPLACE_PREFIXES,\n generateMissingSIDs: BIB_F035_TO_SID\n};\n\nexport const BIB_PREVALIDATION_FIX_SETTINGS = {\n generateMissingSIDs: BIB_F035_TO_SID\n};\n\nexport const BIB_POSTMERGE_FIX_SETTINGS = {\n handleTempUrns: true,\n stripF884s: true\n};\n\nexport const BIB_PREIMPORT_FIX_SETTINGS = {\n replacePrefixes: REPLACE_PREFIXES\n};\n\nexport const AUTNAME_PREIMPORT_FIX_SETTINGS = {\n replacePrefixes: REPLACE_PREFIXES\n};\n\nexport const BIB_HANDLE_TEMP_URNS_SETTINGS = {\n handleTempUrns: true\n};\n\nexport const BIB_STRIP_F884S_SETTINGS = {\n stripF884s: true\n};\n"],"mappings":";;;;;;AAAA;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;;AAEA;AACO,MAAMA,eAAe,GAAG;EAC7BC,WAAW,EAAE,CAAC,cAAc,EAAE,eAAe,CAAC;EAC9CC,WAAW,EAAE,CAAC,QAAQ,EAAE,MAAM;AAChC,CAAC;;AAED;AAAA;AACO,MAAMC,gBAAgB,GAAG,CAC9B;EACEC,SAAS,EAAE,YAAY;EACvBC,SAAS,EAAE,OAAO;EAClBC,kBAAkB,EAAE,CAAC,GAAG;AAC1B,CAAC,EACD;EACEF,SAAS,EAAE,aAAa;EACxBC,SAAS,EAAE,OAAO;EAClBC,kBAAkB,EAAE,CAAC,GAAG;AAC1B,CAAC,EACD;EACEF,SAAS,EAAE,aAAa;EACxBC,SAAS,EAAE,OAAO;EAClBC,kBAAkB,EAAE,CAAC,GAAG;AAC1B,CAAC,EACD;EACEF,SAAS,EAAE,aAAa;EACxBC,SAAS,EAAE,OAAO;EAClBC,kBAAkB,EAAE,CAAC,GAAG;AAC1B,CAAC,EACD;EACEF,SAAS,EAAE,aAAa;EACxBC,SAAS,EAAE,OAAO;EAClBC,kBAAkB,EAAE,CAAC,GAAG;AAC1B,CAAC,CACF;;AAGD;AACA;AAAA;AAGO,MAAMC,mBAAmB,GAAG;EACjCC,eAAe,EAAEL,gBAAgB;EACjCM,mBAAmB,EAAET;AACvB,CAAC;AAAC;AAEK,MAAMU,8BAA8B,GAAG;EAC5CD,mBAAmB,EAAET;AACvB,CAAC;AAAC;AAEK,MAAMW,0BAA0B,GAAG;EACxCC,cAAc,EAAE,IAAI;EACpBC,UAAU,EAAE;AACd,CAAC;AAAC;AAEK,MAAMC,0BAA0B,GAAG;EACxCN,eAAe,EAAEL;AACnB,CAAC;AAAC;AAEK,MAAMY,8BAA8B,GAAG;EAC5CP,eAAe,EAAEL;AACnB,CAAC;AAAC;AAEK,MAAMa,6BAA6B,GAAG;EAC3CJ,cAAc,EAAE;AAClB,CAAC;AAAC;AAEK,MAAMK,wBAAwB,GAAG;EACtCJ,UAAU,EAAE;AACd,CAAC;AAAC"}
@@ -4,11 +4,8 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.handleTempUrns = handleTempUrns;
7
-
8
7
  var _debug = _interopRequireDefault(require("debug"));
9
-
10
8
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
-
12
9
  /**
13
10
  *
14
11
  * @licstart The following is the entire license notice for the JavaScript code in this file.
@@ -36,39 +33,41 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
36
33
  * for the JavaScript code in this file.
37
34
  *
38
35
  */
36
+
39
37
  //import {MarcRecord} from '@natlibfi/marc-record';
38
+
40
39
  const debug = (0, _debug.default)('@natlibfi/melinda-rest-api-commons:fixRecord:handleTempUrns');
41
- const debugData = debug.extend('data'); // eslint-disable-next-line max-statements
40
+ const debugData = debug.extend('data');
42
41
 
42
+ // eslint-disable-next-line max-statements
43
43
  function handleTempUrns(newRecord, options) {
44
44
  if (options !== true) {
45
45
  debug(`NOT running handleTempUrns fixer, no options`);
46
46
  return newRecord;
47
47
  }
48
-
49
48
  debug(`Running handleTempUrns fixer`);
50
49
  debugData(`Options for handleTempUrns: ${JSON.stringify(options)}`);
51
50
  const {
52
51
  f856sUrnsWithTempSubfields,
53
52
  f856sUrnsWithNoTempSubfields
54
- } = getf856sUrns(newRecord); // None of the URNs has temp subfields, we don't need to do anything
53
+ } = getf856sUrns(newRecord);
55
54
 
55
+ // None of the URNs has temp subfields, we don't need to do anything
56
56
  if (f856sUrnsWithTempSubfields.length < 1) {
57
57
  debug(`No Urns with temp subfield`);
58
58
  return newRecord;
59
- } // Do we have an existing URN with legal deposit subfields, if we have, we can remove the temp URN field(s)
60
-
59
+ }
61
60
 
61
+ // Do we have an existing URN with legal deposit subfields, if we have, we can remove the temp URN field(s)
62
62
  const existingLegalDepositURN = validateLD(f856sUrnsWithNoTempSubfields);
63
63
  debug(`existingLegalDepositURN: ${existingLegalDepositURN}`);
64
-
65
64
  if (existingLegalDepositURN) {
66
65
  debug(`We have an existing LD URN, we can delete temp fields (${f856sUrnsWithTempSubfields.length})`);
67
66
  newRecord.removeFields(f856sUrnsWithTempSubfields);
68
67
  return newRecord;
69
- } // We do not have an existing legalDepositURN, we should use the tempURN and remove the temp subfields from it
70
-
68
+ }
71
69
 
70
+ // We do not have an existing legalDepositURN, we should use the tempURN and remove the temp subfields from it
72
71
  if (!existingLegalDepositURN && f856sUrnsWithTempSubfields.length > 0) {
73
72
  //debug(`All Urns (${f856sUrn.length}) have a temp subfield`);
74
73
  debugData(`Original temp URNs: (${JSON.stringify(f856sUrnsWithTempSubfields)})`);
@@ -77,8 +76,8 @@ function handleTempUrns(newRecord, options) {
77
76
  newRecord.removeFields(f856sUrnsWithTempSubfields);
78
77
  newRecord.insertFields(fixedFields);
79
78
  return newRecord;
80
- } // Should we check that the non-temp URN has 2nd ind '0' - meaning that the URN handles the actual resource itself?
81
-
79
+ }
80
+ // Should we check that the non-temp URN has 2nd ind '0' - meaning that the URN handles the actual resource itself?
82
81
  }
83
82
 
84
83
  function getf856sUrns(newRecord) {
@@ -86,17 +85,14 @@ function getf856sUrns(newRecord) {
86
85
  code,
87
86
  value
88
87
  }) => code === 'u' && /urn.fi/u.test(value));
89
-
90
88
  const hasTempSubfield = f => f.subfields.some(({
91
89
  code,
92
90
  value
93
91
  }) => code === '9' && /^MELINDA<TEMP>$/u.test(value));
94
-
95
92
  const hasNoTempSubfield = f => !f.subfields.some(({
96
93
  code,
97
94
  value
98
95
  }) => code === '9' && /^MELINDA<TEMP>$/u.test(value));
99
-
100
96
  const f856sUrn = newRecord.fields.filter(hasURN);
101
97
  const f856sUrnsWithTempSubfields = f856sUrn.filter(hasTempSubfield);
102
98
  const f856sUrnsWithNoTempSubfields = f856sUrn.filter(hasNoTempSubfield);
@@ -107,9 +103,10 @@ function getf856sUrns(newRecord) {
107
103
  f856sUrnsWithTempSubfields,
108
104
  f856sUrnsWithNoTempSubfields
109
105
  };
110
- } // ---- LD-functions below are same as in https://github.com/NatLibFi/marc-record-validators-melinda/blob/feature-updates-to-urn/src/urn.js
111
- // we propably should develop the urn/legaldeposit -validator to use also here
106
+ }
112
107
 
108
+ // ---- LD-functions below are same as in https://github.com/NatLibFi/marc-record-validators-melinda/blob/feature-updates-to-urn/src/urn.js
109
+ // we propably should develop the urn/legaldeposit -validator to use also here
113
110
 
114
111
  function validateLD(f856sUrn) {
115
112
  debug(`Validating the existence of legal deposit subfields`);
@@ -121,41 +118,36 @@ function validateLD(f856sUrn) {
121
118
  value: 'FI-Vapaa'
122
119
  }];
123
120
  const f856sUrnWithLdSubfields = f856sUrn.filter(field => fieldHasLDSubfields(field, LD_SUBFIELDS));
124
-
125
121
  if (f856sUrnWithLdSubfields.length > 0) {
126
122
  debug(`Record has ${f856sUrnWithLdSubfields.length} URN fields with all necessary legal deposit subfields`);
127
123
  debugData(`f856sUrnWithLdSubfields: ${JSON.stringify(f856sUrnWithLdSubfields)}`);
128
124
  return true;
129
125
  }
130
-
131
126
  return false;
132
127
  }
133
-
134
128
  function fieldHasLDSubfields(field, ldSubfields) {
135
129
  if (ldSubfields.every(ldsf => field.subfields.some(sf => sf.code === ldsf.code && sf.value === ldsf.value))) {
136
130
  return true;
137
131
  }
138
132
  }
139
-
140
133
  function removeTempSubfield(field) {
141
134
  // Handle non-numeric fields, and fields with a numeric tag of 010 and greater
142
135
  // Aleph's FMT as a controlfield might be a problem
143
136
  if (!isNaN(field.tag) && parseInt(field.tag, 10) >= 10) {
144
- const filteredSubfields = field.subfields.filter(sf => sf.code !== '9' && !/^MELINDA<TEMP>$/u.test(sf.value)); // Remove whole field if there are no subfields left
137
+ const filteredSubfields = field.subfields.filter(sf => sf.code !== '9' && !/^MELINDA<TEMP>$/u.test(sf.value));
145
138
 
139
+ // Remove whole field if there are no subfields left
146
140
  if (filteredSubfields.length < 1) {
147
141
  return false;
148
142
  }
149
-
150
143
  return {
151
144
  tag: field.tag,
152
145
  ind1: field.ind1,
153
146
  ind2: field.ind2,
154
147
  subfields: filteredSubfields
155
148
  };
156
- } // return controlFields as is
157
-
158
-
149
+ }
150
+ // return controlFields as is
159
151
  return field;
160
152
  }
161
153
  //# sourceMappingURL=fix-handle-tempurns.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"fix-handle-tempurns.js","names":["debug","createDebugLogger","debugData","extend","handleTempUrns","newRecord","options","JSON","stringify","f856sUrnsWithTempSubfields","f856sUrnsWithNoTempSubfields","getf856sUrns","length","existingLegalDepositURN","validateLD","removeFields","fixedFields","map","removeTempSubfield","filter","field","insertFields","hasURN","f","tag","subfields","some","code","value","test","hasTempSubfield","hasNoTempSubfield","f856sUrn","fields","LD_SUBFIELDS","f856sUrnWithLdSubfields","fieldHasLDSubfields","ldSubfields","every","ldsf","sf","isNaN","parseInt","filteredSubfields","ind1","ind2"],"sources":["../../src/helpers/fix-handle-tempurns.js"],"sourcesContent":["\n/**\n*\n* @licstart The following is the entire license notice for the JavaScript code in this file.\n*\n* Shared modules for microservices of Melinda rest api batch import system\n*\n* Copyright (C) 2020-2022 University Of Helsinki (The National Library Of Finland)\n*\n* This file is part of melinda-rest-api-commons\n*\n* melinda-rest-api-commons program is free software: you can redistribute it and/or modify\n* it under the terms of the GNU Affero 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-rest-api-commons 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 Affero 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\n//import {MarcRecord} from '@natlibfi/marc-record';\nimport createDebugLogger from 'debug';\n\nconst debug = createDebugLogger('@natlibfi/melinda-rest-api-commons:fixRecord:handleTempUrns');\nconst debugData = debug.extend('data');\n\n// eslint-disable-next-line max-statements\nexport function handleTempUrns(newRecord, options) {\n\n if (options !== true) {\n debug(`NOT running handleTempUrns fixer, no options`);\n return newRecord;\n }\n\n debug(`Running handleTempUrns fixer`);\n debugData(`Options for handleTempUrns: ${JSON.stringify(options)}`);\n\n const {f856sUrnsWithTempSubfields, f856sUrnsWithNoTempSubfields} = getf856sUrns(newRecord);\n\n // None of the URNs has temp subfields, we don't need to do anything\n if (f856sUrnsWithTempSubfields.length < 1) {\n debug(`No Urns with temp subfield`);\n return newRecord;\n }\n\n // Do we have an existing URN with legal deposit subfields, if we have, we can remove the temp URN field(s)\n const existingLegalDepositURN = validateLD(f856sUrnsWithNoTempSubfields);\n debug(`existingLegalDepositURN: ${existingLegalDepositURN}`);\n\n if (existingLegalDepositURN) {\n debug(`We have an existing LD URN, we can delete temp fields (${f856sUrnsWithTempSubfields.length})`);\n newRecord.removeFields(f856sUrnsWithTempSubfields);\n return newRecord;\n }\n\n // We do not have an existing legalDepositURN, we should use the tempURN and remove the temp subfields from it\n if (!existingLegalDepositURN && f856sUrnsWithTempSubfields.length > 0) {\n //debug(`All Urns (${f856sUrn.length}) have a temp subfield`);\n debugData(`Original temp URNs: (${JSON.stringify(f856sUrnsWithTempSubfields)})`);\n const fixedFields = f856sUrnsWithTempSubfields.map(removeTempSubfield).filter(field => field);\n debug(`We removed temp subfields: (${JSON.stringify(fixedFields)})`);\n newRecord.removeFields(f856sUrnsWithTempSubfields);\n newRecord.insertFields(fixedFields);\n return newRecord;\n }\n // Should we check that the non-temp URN has 2nd ind '0' - meaning that the URN handles the actual resource itself?\n}\n\nfunction getf856sUrns(newRecord) {\n const hasURN = f => f.tag === '856' && f.subfields.some(({code, value}) => code === 'u' && (/urn.fi/u).test(value));\n\n const hasTempSubfield = f => f.subfields.some(({code, value}) => code === '9' && (/^MELINDA<TEMP>$/u).test(value));\n const hasNoTempSubfield = f => !f.subfields.some(({code, value}) => code === '9' && (/^MELINDA<TEMP>$/u).test(value));\n\n const f856sUrn = newRecord.fields.filter(hasURN);\n const f856sUrnsWithTempSubfields = f856sUrn.filter(hasTempSubfield);\n const f856sUrnsWithNoTempSubfields = f856sUrn.filter(hasNoTempSubfield);\n\n debugData(`URN f856s: ${JSON.stringify(f856sUrn)}`);\n debugData(`URN f856s with temp subfield: ${JSON.stringify(f856sUrnsWithTempSubfields)}`);\n return {f856sUrn, f856sUrnsWithTempSubfields, f856sUrnsWithNoTempSubfields};\n}\n\n\n// ---- LD-functions below are same as in https://github.com/NatLibFi/marc-record-validators-melinda/blob/feature-updates-to-urn/src/urn.js\n// we propably should develop the urn/legaldeposit -validator to use also here\n\nfunction validateLD(f856sUrn) {\n debug(`Validating the existence of legal deposit subfields`);\n\n const LD_SUBFIELDS = [\n {code: 'z', value: 'Käytettävissä vapaakappalekirjastoissa'},\n {code: '5', value: 'FI-Vapaa'}\n ];\n\n const f856sUrnWithLdSubfields = f856sUrn.filter(field => fieldHasLDSubfields(field, LD_SUBFIELDS));\n if (f856sUrnWithLdSubfields.length > 0) {\n debug(`Record has ${f856sUrnWithLdSubfields.length} URN fields with all necessary legal deposit subfields`);\n debugData(`f856sUrnWithLdSubfields: ${JSON.stringify(f856sUrnWithLdSubfields)}`);\n return true;\n }\n return false;\n}\n\nfunction fieldHasLDSubfields(field, ldSubfields) {\n if (ldSubfields.every(ldsf => field.subfields.some(sf => sf.code === ldsf.code && sf.value === ldsf.value))) {\n return true;\n }\n}\n\nfunction removeTempSubfield(field) {\n\n // Handle non-numeric fields, and fields with a numeric tag of 010 and greater\n // Aleph's FMT as a controlfield might be a problem\n if (!isNaN(field.tag) && parseInt(field.tag, 10) >= 10) {\n\n const filteredSubfields = field.subfields.filter(sf => sf.code !== '9' && !(/^MELINDA<TEMP>$/u).test(sf.value));\n\n // Remove whole field if there are no subfields left\n if (filteredSubfields.length < 1) {\n return false;\n }\n\n return {\n tag: field.tag,\n ind1: field.ind1,\n ind2: field.ind2,\n subfields: filteredSubfields\n };\n }\n // return controlFields as is\n return field;\n}\n\n\n"],"mappings":";;;;;;;AA8BA;;;;AA7BA;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;AAEA;AAGA,MAAMA,KAAK,GAAG,IAAAC,cAAA,EAAkB,6DAAlB,CAAd;AACA,MAAMC,SAAS,GAAGF,KAAK,CAACG,MAAN,CAAa,MAAb,CAAlB,C,CAEA;;AACO,SAASC,cAAT,CAAwBC,SAAxB,EAAmCC,OAAnC,EAA4C;EAEjD,IAAIA,OAAO,KAAK,IAAhB,EAAsB;IACpBN,KAAK,CAAE,8CAAF,CAAL;IACA,OAAOK,SAAP;EACD;;EAEDL,KAAK,CAAE,8BAAF,CAAL;EACAE,SAAS,CAAE,+BAA8BK,IA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AFP;MAGLC,IAAI,EAAEzB,KAAK,CAACyB,IAHP;MAILpB,SAAS,EAAEkB;IAJN,CAAP;EAMD,CAnBgC,CAoBjC;;;EACA,OAAOvB,KAAP;AACD"}
1
+ {"version":3,"file":"fix-handle-tempurns.js","names":["debug","createDebugLogger","debugData","extend","handleTempUrns","newRecord","options","JSON","stringify","f856sUrnsWithTempSubfields","f856sUrnsWithNoTempSubfields","getf856sUrns","length","existingLegalDepositURN","validateLD","removeFields","fixedFields","map","removeTempSubfield","filter","field","insertFields","hasURN","f","tag","subfields","some","code","value","test","hasTempSubfield","hasNoTempSubfield","f856sUrn","fields","LD_SUBFIELDS","f856sUrnWithLdSubfields","fieldHasLDSubfields","ldSubfields","every","ldsf","sf","isNaN","parseInt","filteredSubfields","ind1","ind2"],"sources":["../../src/helpers/fix-handle-tempurns.js"],"sourcesContent":["\n/**\n*\n* @licstart The following is the entire license notice for the JavaScript code in this file.\n*\n* Shared modules for microservices of Melinda rest api batch import system\n*\n* Copyright (C) 2020-2022 University Of Helsinki (The National Library Of Finland)\n*\n* This file is part of melinda-rest-api-commons\n*\n* melinda-rest-api-commons program is free software: you can redistribute it and/or modify\n* it under the terms of the GNU Affero 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-rest-api-commons 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 Affero 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\n//import {MarcRecord} from '@natlibfi/marc-record';\nimport createDebugLogger from 'debug';\n\nconst debug = createDebugLogger('@natlibfi/melinda-rest-api-commons:fixRecord:handleTempUrns');\nconst debugData = debug.extend('data');\n\n// eslint-disable-next-line max-statements\nexport function handleTempUrns(newRecord, options) {\n\n if (options !== true) {\n debug(`NOT running handleTempUrns fixer, no options`);\n return newRecord;\n }\n\n debug(`Running handleTempUrns fixer`);\n debugData(`Options for handleTempUrns: ${JSON.stringify(options)}`);\n\n const {f856sUrnsWithTempSubfields, f856sUrnsWithNoTempSubfields} = getf856sUrns(newRecord);\n\n // None of the URNs has temp subfields, we don't need to do anything\n if (f856sUrnsWithTempSubfields.length < 1) {\n debug(`No Urns with temp subfield`);\n return newRecord;\n }\n\n // Do we have an existing URN with legal deposit subfields, if we have, we can remove the temp URN field(s)\n const existingLegalDepositURN = validateLD(f856sUrnsWithNoTempSubfields);\n debug(`existingLegalDepositURN: ${existingLegalDepositURN}`);\n\n if (existingLegalDepositURN) {\n debug(`We have an existing LD URN, we can delete temp fields (${f856sUrnsWithTempSubfields.length})`);\n newRecord.removeFields(f856sUrnsWithTempSubfields);\n return newRecord;\n }\n\n // We do not have an existing legalDepositURN, we should use the tempURN and remove the temp subfields from it\n if (!existingLegalDepositURN && f856sUrnsWithTempSubfields.length > 0) {\n //debug(`All Urns (${f856sUrn.length}) have a temp subfield`);\n debugData(`Original temp URNs: (${JSON.stringify(f856sUrnsWithTempSubfields)})`);\n const fixedFields = f856sUrnsWithTempSubfields.map(removeTempSubfield).filter(field => field);\n debug(`We removed temp subfields: (${JSON.stringify(fixedFields)})`);\n newRecord.removeFields(f856sUrnsWithTempSubfields);\n newRecord.insertFields(fixedFields);\n return newRecord;\n }\n // Should we check that the non-temp URN has 2nd ind '0' - meaning that the URN handles the actual resource itself?\n}\n\nfunction getf856sUrns(newRecord) {\n const hasURN = f => f.tag === '856' && f.subfields.some(({code, value}) => code === 'u' && (/urn.fi/u).test(value));\n\n const hasTempSubfield = f => f.subfields.some(({code, value}) => code === '9' && (/^MELINDA<TEMP>$/u).test(value));\n const hasNoTempSubfield = f => !f.subfields.some(({code, value}) => code === '9' && (/^MELINDA<TEMP>$/u).test(value));\n\n const f856sUrn = newRecord.fields.filter(hasURN);\n const f856sUrnsWithTempSubfields = f856sUrn.filter(hasTempSubfield);\n const f856sUrnsWithNoTempSubfields = f856sUrn.filter(hasNoTempSubfield);\n\n debugData(`URN f856s: ${JSON.stringify(f856sUrn)}`);\n debugData(`URN f856s with temp subfield: ${JSON.stringify(f856sUrnsWithTempSubfields)}`);\n return {f856sUrn, f856sUrnsWithTempSubfields, f856sUrnsWithNoTempSubfields};\n}\n\n\n// ---- LD-functions below are same as in https://github.com/NatLibFi/marc-record-validators-melinda/blob/feature-updates-to-urn/src/urn.js\n// we propably should develop the urn/legaldeposit -validator to use also here\n\nfunction validateLD(f856sUrn) {\n debug(`Validating the existence of legal deposit subfields`);\n\n const LD_SUBFIELDS = [\n {code: 'z', value: 'Käytettävissä vapaakappalekirjastoissa'},\n {code: '5', value: 'FI-Vapaa'}\n ];\n\n const f856sUrnWithLdSubfields = f856sUrn.filter(field => fieldHasLDSubfields(field, LD_SUBFIELDS));\n if (f856sUrnWithLdSubfields.length > 0) {\n debug(`Record has ${f856sUrnWithLdSubfields.length} URN fields with all necessary legal deposit subfields`);\n debugData(`f856sUrnWithLdSubfields: ${JSON.stringify(f856sUrnWithLdSubfields)}`);\n return true;\n }\n return false;\n}\n\nfunction fieldHasLDSubfields(field, ldSubfields) {\n if (ldSubfields.every(ldsf => field.subfields.some(sf => sf.code === ldsf.code && sf.value === ldsf.value))) {\n return true;\n }\n}\n\nfunction removeTempSubfield(field) {\n\n // Handle non-numeric fields, and fields with a numeric tag of 010 and greater\n // Aleph's FMT as a controlfield might be a problem\n if (!isNaN(field.tag) && parseInt(field.tag, 10) >= 10) {\n\n const filteredSubfields = field.subfields.filter(sf => sf.code !== '9' && !(/^MELINDA<TEMP>$/u).test(sf.value));\n\n // Remove whole field if there are no subfields left\n if (filteredSubfields.length < 1) {\n return false;\n }\n\n return {\n tag: field.tag,\n ind1: field.ind1,\n ind2: field.ind2,\n subfields: filteredSubfields\n };\n }\n // return controlFields as is\n return field;\n}\n\n\n"],"mappings":";;;;;;AA8BA;AAAsC;AA7BtC;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AGvB,KAAK,CAACK,SAAS,CAACN,MAAM,CAACqB,EAAE,IAAIA,EAAE,CAACb,IAAI,KAAK,GAAG,IAAI,CAAE,kBAAkB,CAAEE,IAAI,CAACW,EAAE,CAACZ,KAAK,CAAC,CAAC;;IAE/G;IACA,IAAIe,iBAAiB,CAAC/B,MAAM,GAAG,CAAC,EAAE;MAChC,OAAO,KAAK;IACd;IAEA,OAAO;MACLY,GAAG,EAAEJ,KAAK,CAACI,GAAG;MACdoB,IAAI,EAAExB,KAAK,CAACwB,IAAI;MAChBC,IAAI,EAAEzB,KAAK,CAACyB,IAAI;MAChBpB,SAAS,EAAEkB;IACb,CAAC;EACH;EACA;EACA,OAAOvB,KAAK;AACd"}
@@ -4,15 +4,10 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.stripF884s = stripF884s;
7
-
8
7
  var _marcRecord = require("@natlibfi/marc-record");
9
-
10
8
  var _debug = _interopRequireDefault(require("debug"));
11
-
12
9
  var _fixUtils = require("./fix-utils");
13
-
14
10
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
-
16
11
  /**
17
12
  *
18
13
  * @licstart The following is the entire license notice for the JavaScript code in this file.
@@ -40,43 +35,44 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
40
35
  * for the JavaScript code in this file.
41
36
  *
42
37
  */
38
+
43
39
  function stripF884s(newRecord, options) {
44
40
  const debug = (0, _debug.default)('@natlibfi/melinda-rest-api-commons:fixRecord:stripF884s');
45
41
  const debugData = debug.extend('data');
46
-
47
42
  if (options !== true) {
48
43
  debug(`NOT running stripF884s fixer, no options`);
49
44
  return newRecord;
50
45
  }
51
-
52
46
  debug(`Running stripF884s fixer`);
53
- debugData(`Options for stripF884s: ${JSON.stringify(options)}`); // Handle only f884 with $5 MELINDA - let's no remove random f884s
47
+ debugData(`Options for stripF884s: ${JSON.stringify(options)}`);
54
48
 
49
+ // Handle only f884 with $5 MELINDA - let's no remove random f884s
55
50
  const isMelindaf884 = f => f.tag === '884' && f.subfields.some(({
56
51
  code,
57
52
  value
58
53
  }) => code === '5' && value === 'MELINDA');
59
-
60
54
  const f884Melindas = newRecord.fields.filter(isMelindaf884);
61
55
  debugData(`Melinda's f884s (${f884Melindas.length}) \n ${JSON.stringify(f884Melindas)}`);
62
-
63
56
  if (f884Melindas.length < 2) {
64
57
  debug(`Not enough Melinda's f884.s to filter (${f884Melindas.length})`);
65
58
  return newRecord;
66
- } // Sort fields by date subfield $g, so we'll keep the oldest one
67
-
59
+ }
68
60
 
61
+ // Sort fields by date subfield $g, so we'll keep the oldest one
69
62
  const sortedFields = (0, _fixUtils.sortFieldsBySubfieldValue)(f884Melindas, 'g');
70
- debugData(`Melinda's f884s sorted by 'g' - oldest first: (${sortedFields.length}) \n ${JSON.stringify(sortedFields)}`); // Drop fields that are similar without date subfield $g
63
+ debugData(`Melinda's f884s sorted by 'g' - oldest first: (${sortedFields.length}) \n ${JSON.stringify(sortedFields)}`);
71
64
 
65
+ // Drop fields that are similar without date subfield $g
72
66
  const uniqFields = uniqWithOutSubfield(sortedFields, 'g');
73
- debugData(`Melindas f884s uniqued without sf $g (${uniqFields.length}) \n ${JSON.stringify(uniqFields)}`); // Replace original Melinda-f884s with remaining Melinda-f884s
74
- // NOTE: this sorts MELINDA-f884s after possible other f884s
67
+ debugData(`Melindas f884s uniqued without sf $g (${uniqFields.length}) \n ${JSON.stringify(uniqFields)}`);
75
68
 
69
+ // Replace original Melinda-f884s with remaining Melinda-f884s
70
+ // NOTE: this sorts MELINDA-f884s after possible other f884s
76
71
  newRecord.removeFields(f884Melindas);
77
72
  newRecord.insertFields(uniqFields);
78
- return newRecord; // Keep just first instance of each similar field, compare without subfield with subfieldCode
73
+ return newRecord;
79
74
 
75
+ // Keep just first instance of each similar field, compare without subfield with subfieldCode
80
76
  function uniqWithOutSubfield(fields, subfieldCode) {
81
77
  return fields.reduce((uniq, field) => {
82
78
  if (!uniq.some(f => _marcRecord.MarcRecord.isEqual((0, _fixUtils.removeSubfield)(f, subfieldCode), (0, _fixUtils.removeSubfield)(field, subfieldCode)))) {
@@ -1 +1 @@
1
- {"version":3,"file":"fix-strip-f884s.js","names":["stripF884s","newRecord","options","debug","createDebugLogger","debugData","extend","JSON","stringify","isMelindaf884","f","tag","subfields","some","code","value","f884Melindas","fields","filter","length","sortedFields","sortFieldsBySubfieldValue","uniqFields","uniqWithOutSubfield","removeFields","insertFields","subfieldCode","reduce","uniq","field","MarcRecord","isEqual","removeSubfield","push"],"sources":["../../src/helpers/fix-strip-f884s.js"],"sourcesContent":["/**\n*\n* @licstart The following is the entire license notice for the JavaScript code in this file.\n*\n* Shared modules for microservices of Melinda rest api batch import system\n*\n* Copyright (C) 2020-2022 University Of Helsinki (The National Library Of Finland)\n*\n* This file is part of melinda-rest-api-commons\n*\n* melinda-rest-api-commons program is free software: you can redistribute it and/or modify\n* it under the terms of the GNU Affero 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-rest-api-commons 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 Affero 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 {MarcRecord} from '@natlibfi/marc-record';\nimport createDebugLogger from 'debug';\nimport {sortFieldsBySubfieldValue, removeSubfield} from './fix-utils';\n\n\nexport function stripF884s(newRecord, options) {\n const debug = createDebugLogger('@natlibfi/melinda-rest-api-commons:fixRecord:stripF884s');\n const debugData = debug.extend('data');\n\n if (options !== true) {\n debug(`NOT running stripF884s fixer, no options`);\n return newRecord;\n }\n\n debug(`Running stripF884s fixer`);\n debugData(`Options for stripF884s: ${JSON.stringify(options)}`);\n\n // Handle only f884 with $5 MELINDA - let's no remove random f884s\n const isMelindaf884 = f => f.tag === '884' && f.subfields.some(({code, value}) => code === '5' && value === 'MELINDA');\n\n const f884Melindas = newRecord.fields.filter(isMelindaf884);\n debugData(`Melinda's f884s (${f884Melindas.length}) \\n ${JSON.stringify(f884Melindas)}`);\n\n if (f884Melindas.length < 2) {\n debug(`Not enough Melinda's f884.s to filter (${f884Melindas.length})`);\n return newRecord;\n }\n\n // Sort fields by date subfield $g, so we'll keep the oldest one\n const sortedFields = sortFieldsBySubfieldValue(f884Melindas, 'g');\n debugData(`Melinda's f884s sorted by 'g' - oldest first: (${sortedFields.length}) \\n ${JSON.stringify(sortedFields)}`);\n\n // Drop fields that are similar without date subfield $g\n const uniqFields = uniqWithOutSubfield(sortedFields, 'g');\n debugData(`Melindas f884s uniqued without sf $g (${uniqFields.length}) \\n ${JSON.stringify(uniqFields)}`);\n\n // Replace original Melinda-f884s with remaining Melinda-f884s\n // NOTE: this sorts MELINDA-f884s after possible other f884s\n newRecord.removeFields(f884Melindas);\n newRecord.insertFields(uniqFields);\n return newRecord;\n\n // Keep just first instance of each similar field, compare without subfield with subfieldCode\n function uniqWithOutSubfield(fields, subfieldCode) {\n return fields.reduce((uniq, field) => {\n if (!uniq.some(f => MarcRecord.isEqual(removeSubfield(f, subfieldCode), removeSubfield(field, subfieldCode)))) { // eslint-disable-line functional/no-conditional-statement\n uniq.push(field); // eslint-disable-line functional/immutable-data\n }\n\n return uniq;\n }, []);\n }\n}\n"],"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;AAOO,SAASA,UAAT,CAAoBC,SAApB,EAA+BC,OAA/B,EAAwC;EAC7C,MAAMC,KAAK,GAAG,IAAAC,cAAA,EAAkB,yDAAlB,CAAd;EACA,MAAMC,SAAS,GAAGF,KAAK,CAACG,MAAN,CAA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,YAAvB;EACAf,SAAS,CAACwB,YAAV,CAAuBH,UAAvB;EACA,OAAOrB,SAAP,CAnC6C,CAqC7C;;EACA,SAASsB,mBAAT,CAA6BN,MAA7B,EAAqCS,YAArC,EAAmD;IACjD,OAAOT,MAAM,CAACU,MAAP,CAAc,CAACC,IAAD,EAAOC,KAAP,KAAiB;MACpC,IAAI,CAACD,IAAI,CAACf,IAAL,CAAUH,CAAC,IAAIoB,sBAAA,CAAWC,OAAX,CAAmB,IAAAC,wBAAA,EAAetB,CAAf,EAAkBgB,YAAlB,CAAnB,EAAoD,IAAAM,wBAAA,EAAeH,KAAf,EAAsBH,YAAtB,CAApD,CAAf,CAAL,EAA+G;QAAE;QAC/GE,IAAI,CAACK,IAAL,CAAUJ,KAAV,EAD6G,CAC3F;MACnB;;MAED,OAAOD,IAAP;IACD,CANM,EAMJ,EANI,CAAP;EAOD;AACF"}
1
+ {"version":3,"file":"fix-strip-f884s.js","names":["stripF884s","newRecord","options","debug","createDebugLogger","debugData","extend","JSON","stringify","isMelindaf884","f","tag","subfields","some","code","value","f884Melindas","fields","filter","length","sortedFields","sortFieldsBySubfieldValue","uniqFields","uniqWithOutSubfield","removeFields","insertFields","subfieldCode","reduce","uniq","field","MarcRecord","isEqual","removeSubfield","push"],"sources":["../../src/helpers/fix-strip-f884s.js"],"sourcesContent":["/**\n*\n* @licstart The following is the entire license notice for the JavaScript code in this file.\n*\n* Shared modules for microservices of Melinda rest api batch import system\n*\n* Copyright (C) 2020-2022 University Of Helsinki (The National Library Of Finland)\n*\n* This file is part of melinda-rest-api-commons\n*\n* melinda-rest-api-commons program is free software: you can redistribute it and/or modify\n* it under the terms of the GNU Affero 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-rest-api-commons 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 Affero 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 {MarcRecord} from '@natlibfi/marc-record';\nimport createDebugLogger from 'debug';\nimport {sortFieldsBySubfieldValue, removeSubfield} from './fix-utils';\n\n\nexport function stripF884s(newRecord, options) {\n const debug = createDebugLogger('@natlibfi/melinda-rest-api-commons:fixRecord:stripF884s');\n const debugData = debug.extend('data');\n\n if (options !== true) {\n debug(`NOT running stripF884s fixer, no options`);\n return newRecord;\n }\n\n debug(`Running stripF884s fixer`);\n debugData(`Options for stripF884s: ${JSON.stringify(options)}`);\n\n // Handle only f884 with $5 MELINDA - let's no remove random f884s\n const isMelindaf884 = f => f.tag === '884' && f.subfields.some(({code, value}) => code === '5' && value === 'MELINDA');\n\n const f884Melindas = newRecord.fields.filter(isMelindaf884);\n debugData(`Melinda's f884s (${f884Melindas.length}) \\n ${JSON.stringify(f884Melindas)}`);\n\n if (f884Melindas.length < 2) {\n debug(`Not enough Melinda's f884.s to filter (${f884Melindas.length})`);\n return newRecord;\n }\n\n // Sort fields by date subfield $g, so we'll keep the oldest one\n const sortedFields = sortFieldsBySubfieldValue(f884Melindas, 'g');\n debugData(`Melinda's f884s sorted by 'g' - oldest first: (${sortedFields.length}) \\n ${JSON.stringify(sortedFields)}`);\n\n // Drop fields that are similar without date subfield $g\n const uniqFields = uniqWithOutSubfield(sortedFields, 'g');\n debugData(`Melindas f884s uniqued without sf $g (${uniqFields.length}) \\n ${JSON.stringify(uniqFields)}`);\n\n // Replace original Melinda-f884s with remaining Melinda-f884s\n // NOTE: this sorts MELINDA-f884s after possible other f884s\n newRecord.removeFields(f884Melindas);\n newRecord.insertFields(uniqFields);\n return newRecord;\n\n // Keep just first instance of each similar field, compare without subfield with subfieldCode\n function uniqWithOutSubfield(fields, subfieldCode) {\n return fields.reduce((uniq, field) => {\n if (!uniq.some(f => MarcRecord.isEqual(removeSubfield(f, subfieldCode), removeSubfield(field, subfieldCode)))) { // eslint-disable-line functional/no-conditional-statement\n uniq.push(field); // eslint-disable-line functional/immutable-data\n }\n\n return uniq;\n }, []);\n }\n}\n"],"mappings":";;;;;;AA4BA;AACA;AACA;AAAsE;AA9BtE;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;;AAOO,SAASA,UAAU,CAACC,SAAS,EAAEC,OAAO,EAAE;EAC7C,MAAMC,KAAK,GAAG,IAAAC,cAAiB,EAAC,yDAAyD,CAAC;EAC1F,MAAMC,SAAS,GAAGF,KAAK,CAACG,MAAM,CAAC,MAAM,CAAC;EAEtC,IAAIJ,OAAO,KAAK,IAAI,EAAE;IACpBC,KAAK,CAAE,0CAAyC,CAAC;IACjD,OAAOF,SAAS;EAClB;EAEAE,KAAK,CAAE,0BAAyB,CAAC;EACjCE,SAAS,CAAE,2BAA0BE,IAAI,CAACC,SAAS,CAACN,OAAO,CAAE,EAAC,CAAC;;EAE/D;EACA,MAAMO,aAAa,GAAGC,CAAC,IAAIA,CAAC,CAACC,GAAG,KAAK,KAAK,IAAID,CAAC,CAACE,SAAS,CAACC,IAAI,CAAC,CAAC;IAACC,IAAI;IAAEC;EAAK,CAAC,KAAKD,IAAI,KAAK,GAAG,IAAIC,KAAK,KAAK,SAAS,CAAC;EAEtH,MAAMC,YAAY,GAAGf,SAAS,CAACgB,MAAM,CAACC,MAAM,CAACT,aAAa,CAAC;EAC3DJ,SAAS,CAAE,oBAAmBW,YAAY,CAACG,MAAO,QAAOZ,IAAI,CAACC,SAAS,CAACQ,YAAY,CAAE,EAAC,CAAC;EAExF,IAAIA,YAAY,CAACG,MAAM,GAAG,CAAC,EAAE;IAC3BhB,KAAK,CAAE,0CAAyCa,YAAY,CAACG,MAAO,GAAE,CAAC;IACvE,OAAOlB,SAAS;EAClB;;EAEA;EACA,MAAMmB,YAAY,GAAG,IAAAC,mCAAyB,EAACL,YAAY,EAAE,GAAG,CAAC;EACjEX,SAAS,CAAE,kDAAiDe,YAAY,CAACD,MAAO,QAAOZ,IAAI,CAACC,SAAS,CAACY,YAAY,CAAE,EAAC,CAAC;;EAEtH;EACA,MAAME,UAAU,GAAGC,mBAAmB,CAACH,YAAY,EAAE,GAAG,CAAC;EACzDf,SAAS,CAAE,yCAAwCiB,UAAU,CAACH,MAAO,QAAOZ,IAAI,CAACC,SAAS,CAACc,UAAU,CAAE,EAAC,CAAC;;EAEzG;EACA;EACArB,SAAS,CAACuB,YAAY,CAACR,YAAY,CAAC;EACpCf,SAAS,CAACwB,YAAY,CAACH,UAAU,CAAC;EAClC,OAAOrB,SAAS;;EAEhB;EACA,SAASsB,mBAAmB,CAACN,MAAM,EAAES,YAAY,EAAE;IACjD,OAAOT,MAAM,CAACU,MAAM,CAAC,CAACC,IAAI,EAAEC,KAAK,KAAK;MACpC,IAAI,CAACD,IAAI,CAACf,IAAI,CAACH,CAAC,IAAIoB,sBAAU,CAACC,OAAO,CAAC,IAAAC,wBAAc,EAACtB,CAAC,EAAEgB,YAAY,CAAC,EAAE,IAAAM,wBAAc,EAACH,KAAK,EAAEH,YAAY,CAAC,CAAC,CAAC,EAAE;QAAE;QAC/GE,IAAI,CAACK,IAAI,CAACJ,KAAK,CAAC,CAAC,CAAC;MACpB;;MAEA,OAAOD,IAAI;IACb,CAAC,EAAE,EAAE,CAAC;EACR;AACF"}
@@ -5,7 +5,6 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.removeSubfield = removeSubfield;
7
7
  exports.sortFieldsBySubfieldValue = sortFieldsBySubfieldValue;
8
-
9
8
  /**
10
9
  *
11
10
  * @licstart The following is the entire license notice for the JavaScript code in this file.
@@ -33,53 +32,48 @@ exports.sortFieldsBySubfieldValue = sortFieldsBySubfieldValue;
33
32
  * for the JavaScript code in this file.
34
33
  *
35
34
  */
35
+
36
36
  function removeSubfield(field, code) {
37
37
  // Handle non-numeric fields, and fields with a numeric tag of 010 and greater
38
38
  // Aleph's FMT as a controlfield might be a problem
39
39
  if (!isNaN(field.tag) && parseInt(field.tag, 10) >= 10) {
40
- const filteredSubfields = field.subfields.filter(sf => sf.code !== code); // Remove whole field if there are no subfields left
40
+ const filteredSubfields = field.subfields.filter(sf => sf.code !== code);
41
41
 
42
+ // Remove whole field if there are no subfields left
42
43
  if (filteredSubfields.length < 1) {
43
44
  return false;
44
45
  }
45
-
46
46
  return {
47
47
  tag: field.tag,
48
48
  ind1: field.ind1,
49
49
  ind2: field.ind2,
50
50
  subfields: filteredSubfields
51
51
  };
52
- } // return controlFields as is
53
-
54
-
52
+ }
53
+ // return controlFields as is
55
54
  return field;
56
- } // sort fields by value of each fields first subfield with subfielCode
57
-
55
+ }
58
56
 
57
+ // sort fields by value of each fields first subfield with subfielCode
59
58
  function sortFieldsBySubfieldValue(fields, subfieldCode) {
60
59
  return [...fields].sort((a, b) => {
61
60
  const a1value = getFirstSubfieldValue(a, subfieldCode);
62
61
  const b1value = getFirstSubfieldValue(b, subfieldCode);
63
-
64
62
  if (a1value && !b1value) {
65
63
  return -1;
66
64
  }
67
-
68
65
  if (!a1value && b1value) {
69
66
  return 1;
70
67
  }
71
-
72
68
  if (a1value > b1value) {
73
69
  return 1;
74
70
  }
75
-
76
71
  if (b1value > a1value) {
77
72
  return -1;
78
73
  }
79
-
80
74
  return 0;
81
- }); // get value for the for instance of subfield with subfieldCode
82
-
75
+ });
76
+ // get value for the for instance of subfield with subfieldCode
83
77
  function getFirstSubfieldValue(field, subfieldCode) {
84
78
  const subs = field.subfields ? field.subfields.filter(subf => subf.code === subfieldCode) : [];
85
79
  return subs.length > 0 ? subs[0].value : '';
@@ -1 +1 @@
1
- {"version":3,"file":"fix-utils.js","names":["removeSubfield","field","code","isNaN","tag","parseInt","filteredSubfields","subfields","filter","sf","length","ind1","ind2","sortFieldsBySubfieldValue","fields","subfieldCode","sort","a","b","a1value","getFirstSubfieldValue","b1value","subs","subf","value"],"sources":["../../src/helpers/fix-utils.js"],"sourcesContent":["/**\n*\n* @licstart The following is the entire license notice for the JavaScript code in this file.\n*\n* Shared modules for microservices of Melinda rest api batch import system\n*\n* Copyright (C) 2020-2022 University Of Helsinki (The National Library Of Finland)\n*\n* This file is part of melinda-rest-api-commons\n*\n* melinda-rest-api-commons program is free software: you can redistribute it and/or modify\n* it under the terms of the GNU Affero 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-rest-api-commons 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 Affero 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\nexport function removeSubfield(field, code) {\n\n // Handle non-numeric fields, and fields with a numeric tag of 010 and greater\n // Aleph's FMT as a controlfield might be a problem\n if (!isNaN(field.tag) && parseInt(field.tag, 10) >= 10) {\n\n const filteredSubfields = field.subfields.filter(sf => sf.code !== code);\n\n // Remove whole field if there are no subfields left\n if (filteredSubfields.length < 1) {\n return false;\n }\n\n return {\n tag: field.tag,\n ind1: field.ind1,\n ind2: field.ind2,\n subfields: filteredSubfields\n };\n }\n // return controlFields as is\n return field;\n}\n\n\n// sort fields by value of each fields first subfield with subfielCode\nexport function sortFieldsBySubfieldValue(fields, subfieldCode) {\n return [...fields].sort((a, b) => {\n const a1value = getFirstSubfieldValue(a, subfieldCode);\n const b1value = getFirstSubfieldValue(b, subfieldCode);\n if (a1value && !b1value) {\n return -1;\n }\n if (!a1value && b1value) {\n return 1;\n }\n if (a1value > b1value) {\n return 1;\n }\n if (b1value > a1value) {\n return -1;\n }\n return 0;\n });\n // get value for the for instance of subfield with subfieldCode\n function getFirstSubfieldValue(field, subfieldCode) {\n const subs = field.subfields ? field.subfields.filter(subf => subf.code === subfieldCode) : [];\n return subs.length > 0 ? subs[0].value : '';\n }\n}\n"],"mappings":";;;;;;;;AAAA;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;AAEO,SAASA,cAAT,CAAwBC,KAAxB,EAA+BC,IAA/B,EAAqC;EAE1C;EACA;EACA,IAAI,CAACC,KAAK,CAACF,KAAK,CAACG,GAAP,CAAN,IAAqBC,QAAQ,CAACJ,KAAK,CAACG,GAAP,EAAY,EAAZ,CAAR,IAA2B,EAApD,EAAwD;IAEtD,MAAME,iBAAiB,GAAGL,KAAK,CAACM,SAAN,CAAgBC,MAAhB,CAAuBC,EAAE,IAAIA,EAAE,CAACP,IAAH,KAAYA,IAAzC,CAA1B,CAFsD,CAItD;;IACA,IAAII,iBAAiB,CAACI,MAAlB,GAA2B,CAA/B,EAAkC;MAChC,OAAO,KAAP;IACD;;IAED,OAAO;MACLN,GAAG,EAAEH,KAAK,CAACG,GADN;MAELO,IAAI,EAAEV,KAAK,CAACU,IAFP;MAGLC,IAAI,EAAEX,KAAK,CAACW,IAHP;MAILL,SAAS,EAAED;IAJN,CAAP;EAMD,CAnByC,CAoB1C;;;EACA,OAAOL,KAAP;AACD,C,CAGD;;;AACO,SAASY,yBAAT,CAAmCC,MAAnC,EAA2CC,YAA3C,EAAyD;EAC9D,OAAO,CAAC,GAAGD,MAAJ,EAAYE,IAAZ,CAAiB,CAACC,CAAD,EAAIC,CAAJ,KAAU;IAChC,MAAMC,OAAO,GAAGC,qBAAqB,CAACH,CAAD,EAAIF,YAAJ,CAArC;IACA,MAAMM,OAAO,GAAGD,qBAAqB,CAACF,CAAD,EAAIH,YAAJ,CAArC;;IACA,IAAII,OAAO,IAAI,CAACE,OAAhB,EAAyB;MACvB,OAAO,CAAC,CAAR;IACD;;IACD,IAAI,CAACF,OAAD,IAAYE,OAAhB,EAAyB;MACvB,OAAO,CAAP;IACD;;IACD,IAAIF,OAAO,GAAGE,OAAd,EAAuB;MACrB,OAAO,CAAP;IACD;;IACD,IAAIA,OAAO,GAAGF,OAAd,EAAuB;MACrB,OAAO,CAAC,CAAR;IACD;;IACD,OAAO,CAAP;EACD,CAhBM,CAAP,CAD8D,CAkB9D;;EACA,SAASC,qBAAT,CAA+BnB,KAA/B,EAAsCc,YAAtC,EAAoD;IAClD,MAAMO,IAAI,GAAGrB,KAAK,CAACM,SAAN,GAAkBN,KAAK,CAACM,SAAN,CAAgBC,MAAhB,CAAuBe,IAAI,IAAIA,IAAI,CAACrB,IAAL,KAAca,YAA7C,CAAlB,GAA+E,EAA5F;IACA,OAAOO,IAAI,CAACZ,MAAL,GAAc,CAAd,GAAkBY,IAAI,CAAC,CAAD,CAAJ,CAAQE,KAA1B,GAAkC,EAAzC;EACD;AACF"}
1
+ {"version":3,"file":"fix-utils.js","names":["removeSubfield","field","code","isNaN","tag","parseInt","filteredSubfields","subfields","filter","sf","length","ind1","ind2","sortFieldsBySubfieldValue","fields","subfieldCode","sort","a","b","a1value","getFirstSubfieldValue","b1value","subs","subf","value"],"sources":["../../src/helpers/fix-utils.js"],"sourcesContent":["/**\n*\n* @licstart The following is the entire license notice for the JavaScript code in this file.\n*\n* Shared modules for microservices of Melinda rest api batch import system\n*\n* Copyright (C) 2020-2022 University Of Helsinki (The National Library Of Finland)\n*\n* This file is part of melinda-rest-api-commons\n*\n* melinda-rest-api-commons program is free software: you can redistribute it and/or modify\n* it under the terms of the GNU Affero 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-rest-api-commons 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 Affero 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\nexport function removeSubfield(field, code) {\n\n // Handle non-numeric fields, and fields with a numeric tag of 010 and greater\n // Aleph's FMT as a controlfield might be a problem\n if (!isNaN(field.tag) && parseInt(field.tag, 10) >= 10) {\n\n const filteredSubfields = field.subfields.filter(sf => sf.code !== code);\n\n // Remove whole field if there are no subfields left\n if (filteredSubfields.length < 1) {\n return false;\n }\n\n return {\n tag: field.tag,\n ind1: field.ind1,\n ind2: field.ind2,\n subfields: filteredSubfields\n };\n }\n // return controlFields as is\n return field;\n}\n\n\n// sort fields by value of each fields first subfield with subfielCode\nexport function sortFieldsBySubfieldValue(fields, subfieldCode) {\n return [...fields].sort((a, b) => {\n const a1value = getFirstSubfieldValue(a, subfieldCode);\n const b1value = getFirstSubfieldValue(b, subfieldCode);\n if (a1value && !b1value) {\n return -1;\n }\n if (!a1value && b1value) {\n return 1;\n }\n if (a1value > b1value) {\n return 1;\n }\n if (b1value > a1value) {\n return -1;\n }\n return 0;\n });\n // get value for the for instance of subfield with subfieldCode\n function getFirstSubfieldValue(field, subfieldCode) {\n const subs = field.subfields ? field.subfields.filter(subf => subf.code === subfieldCode) : [];\n return subs.length > 0 ? subs[0].value : '';\n }\n}\n"],"mappings":";;;;;;;AAAA;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;;AAEO,SAASA,cAAc,CAACC,KAAK,EAAEC,IAAI,EAAE;EAE1C;EACA;EACA,IAAI,CAACC,KAAK,CAACF,KAAK,CAACG,GAAG,CAAC,IAAIC,QAAQ,CAACJ,KAAK,CAACG,GAAG,EAAE,EAAE,CAAC,IAAI,EAAE,EAAE;IAEtD,MAAME,iBAAiB,GAAGL,KAAK,CAACM,SAAS,CAACC,MAAM,CAACC,EAAE,IAAIA,EAAE,CAACP,IAAI,KAAKA,IAAI,CAAC;;IAEx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fixRecord;
10
-
11
10
  var _marcRecord = require("@natlibfi/marc-record");
12
-
13
11
  var _melindaBackendCommons = require("@natlibfi/melinda-backend-commons");
14
-
15
12
  var _debug = _interopRequireDefault(require("debug"));
16
-
17
13
  var _fixHandleTempurns = require("./fix-handle-tempurns");
18
-
19
14
  var _fixStripF884s = require("./fix-strip-f884s");
20
-
21
15
  var _fixConstants = require("./fix-constants");
22
-
23
16
  Object.keys(_fixConstants).forEach(function (key) {
24
17
  if (key === "default" || key === "__esModule") return;
25
18
  if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
@@ -31,9 +24,7 @@ Object.keys(_fixConstants).forEach(function (key) {
31
24
  }
32
25
  });
33
26
  });
34
-
35
27
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
36
-
37
28
  /**
38
29
  *
39
30
  * @licstart The following is the entire license notice for the JavaScript code in this file.
@@ -61,47 +52,48 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
61
52
  * for the JavaScript code in this file.
62
53
  *
63
54
  */
55
+
64
56
  const logger = (0, _melindaBackendCommons.createLogger)();
65
57
  const debug = (0, _debug.default)('@natlibfi/melinda-rest-api-commons:fixRecord');
66
58
  const debugData = debug.extend('data');
67
-
68
59
  function fixRecord(record, settings = {}) {
69
60
  logger.verbose(`We will apply formatting to the record according to settings: ${JSON.stringify(settings)}`);
70
-
71
61
  const newRecord = _marcRecord.MarcRecord.clone(record, {
72
62
  subfieldValues: false
73
63
  });
64
+ debugData(`settings: ${JSON.stringify(settings)}`);
74
65
 
75
- debugData(`settings: ${JSON.stringify(settings)}`); // We should handle these in some prettier way ...
76
- // Fix 1, generate SIDs
66
+ // We should handle these in some prettier way ...
77
67
 
68
+ // Fix 1, generate SIDs
78
69
  const generateMissingSIDsOptions = settings.generateMissingSIDs || undefined;
79
- const newRecord1 = generateMissingSIDs(newRecord, generateMissingSIDsOptions); // Fix 2, replace identifier prefixes
70
+ const newRecord1 = generateMissingSIDs(newRecord, generateMissingSIDsOptions);
80
71
 
72
+ // Fix 2, replace identifier prefixes
81
73
  const replacePrefixesOptions = settings.replacePrefixes || [];
82
- const newRecord2 = replaceAllPrefixes(newRecord1, replacePrefixesOptions); // Fix 3, handle temporary URNs from legal deposit record import
74
+ const newRecord2 = replaceAllPrefixes(newRecord1, replacePrefixesOptions);
83
75
 
84
- const newRecord3 = (0, _fixHandleTempurns.handleTempUrns)(newRecord2, settings.handleTempUrns); // Fix 4, strip extra field 884s
76
+ // Fix 3, handle temporary URNs from legal deposit record import
77
+ const newRecord3 = (0, _fixHandleTempurns.handleTempUrns)(newRecord2, settings.handleTempUrns);
85
78
 
79
+ // Fix 4, strip extra field 884s
86
80
  const newRecord4 = (0, _fixStripF884s.stripF884s)(newRecord3, settings.stripF884s);
87
81
  return newRecord4.toObject();
88
82
  }
89
-
90
83
  function replaceAllPrefixes(record, replacePrefixesOptions) {
91
84
  if (replacePrefixesOptions.length < 1) {
92
85
  debug(`NOT running replacePrefixes, no options`);
93
86
  return record;
94
87
  }
95
-
96
88
  debug(`Running replacePrefixes fixer`);
97
89
  debugData(`replacePrefixesOptions: ${JSON.stringify(replacePrefixesOptions)}`);
98
90
  replacePrefixesOptions.forEach(options => {
99
91
  replacePrefix(record, options);
100
92
  });
101
93
  return record;
102
- } // Replace prefix in all specified subfields
103
-
94
+ }
104
95
 
96
+ // Replace prefix in all specified subfields
105
97
  function replacePrefix(record, options) {
106
98
  const {
107
99
  oldPrefix,
@@ -118,28 +110,27 @@ function replacePrefix(record, options) {
118
110
  subfield.value = subfield.value.replace(pattern, replacement); // eslint-disable-line functional/immutable-data
119
111
  });
120
112
  });
113
+
121
114
  return record;
122
115
  }
123
-
124
116
  function generateMissingSIDs(record, options) {
125
117
  const f035Filters = options && options.f035Filters ? options.f035Filters : [];
126
118
  const fSIDFilters = options && options.fSIDFilters ? options.fSIDFilters : [];
127
-
128
119
  if (f035Filters.length !== fSIDFilters.length || f035Filters.length < 1 || fSIDFilters.length < 1) {
129
120
  debug(`NOT running generateMissingsSIDs, no options`);
130
121
  return record;
131
122
  }
132
-
133
123
  debug(`Running generateMissingSIDs fixer`);
134
124
  debugData(`generateMissingSIDs options: ${JSON.stringify(options)}`);
135
125
  const f035s = f035ToSidInfo(record.get(/^035$/u));
136
126
  const fSIDs = sidsToSidInfo(record.get(/^SID$/u));
137
- const sidsToBeAdded = genNewSids(fSIDs.length === 0 ? f035s // test that SIDs are not there yet
138
- : f035s.filter(f035SidInfo => fSIDs.some(fSIDInfo => f035SidInfo.SID !== fSIDInfo.SID && f035SidInfo.value !== fSIDInfo.value))); // Add new SIDs
127
+ const sidsToBeAdded = genNewSids(fSIDs.length === 0 ? f035s
128
+ // test that SIDs are not there yet
129
+ : f035s.filter(f035SidInfo => fSIDs.some(fSIDInfo => f035SidInfo.SID !== fSIDInfo.SID && f035SidInfo.value !== fSIDInfo.value)));
139
130
 
131
+ // Add new SIDs
140
132
  sidsToBeAdded.forEach(sidField => record.insertField(sidField));
141
133
  return record;
142
-
143
134
  function genNewSids(sidsToBeAdded) {
144
135
  return sidsToBeAdded.map(sidInfo => ({
145
136
  tag: 'SID',
@@ -154,38 +145,32 @@ function generateMissingSIDs(record, options) {
154
145
  }]
155
146
  }));
156
147
  }
157
-
158
148
  function sidsToSidInfo(SIDs) {
159
149
  return SIDs.flatMap(({
160
150
  subfields
161
151
  }) => {
162
152
  const [SID] = subfields.filter(sub => sub.code === 'b' && fSIDFilters.includes(sub.value)).map(sub => sub.value);
163
153
  const [value] = subfields.filter(sub => sub.code === 'c' && sub.value !== undefined).map(sub => sub.value);
164
-
165
154
  if (SID && value) {
166
155
  return {
167
156
  SID,
168
157
  value
169
158
  };
170
159
  }
171
-
172
160
  return undefined;
173
161
  }).filter(value => value !== undefined);
174
162
  }
175
-
176
163
  function f035ToSidInfo(f035s) {
177
164
  return f035s.flatMap(({
178
165
  subfields
179
166
  }) => {
180
167
  const [wantedSub] = subfields.filter(sub => sub.code === 'a' && f035Filters.some(regexp => regexp.test(sub.value)));
181
-
182
168
  if (wantedSub) {
183
169
  return {
184
170
  SID: fSIDFilters[f035Filters.findIndex(regexp => regexp.test(wantedSub.value))],
185
171
  value: wantedSub.value.slice(wantedSub.value.indexOf(')') + 1)
186
172
  };
187
173
  }
188
-
189
174
  return undefined;
190
175
  }).filter(value => value !== undefined);
191
176
  }