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

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.
@@ -0,0 +1,96 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.REPLACE_PREFIXES = exports.BIB_STRIP_F884S_SETTINGS = exports.BIB_PREVALIDATION_FIX_SETTINGS = exports.BIB_PREIMPORT_FIX_SETTINGS = exports.BIB_POSTMERGE_FIX_SETTINGS = exports.BIB_HANDLE_TEMP_URNS_SETTINGS = exports.BIB_FORMAT_SETTINGS = exports.BIB_F035_TO_SID = exports.AUTNAME_PREIMPORT_FIX_SETTINGS = void 0;
7
+
8
+ /**
9
+ *
10
+ * @licstart The following is the entire license notice for the JavaScript code in this file.
11
+ *
12
+ * Shared modules for microservices of Melinda rest api batch import system
13
+ *
14
+ * Copyright (C) 2020-2022 University Of Helsinki (The National Library Of Finland)
15
+ *
16
+ * This file is part of melinda-rest-api-commons
17
+ *
18
+ * melinda-rest-api-commons program is free software: you can redistribute it and/or modify
19
+ * it under the terms of the GNU Affero General Public License as
20
+ * published by the Free Software Foundation, either version 3 of the
21
+ * License, or (at your option) any later version.
22
+ *
23
+ * melinda-rest-api-commons is distributed in the hope that it will be useful,
24
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
25
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26
+ * GNU Affero General Public License for more details.
27
+ *
28
+ * You should have received a copy of the GNU Affero General Public License
29
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
30
+ *
31
+ * @licend The above is the entire license notice
32
+ * for the JavaScript code in this file.
33
+ *
34
+ */
35
+ // This could be formatted so that 035-prefix and SID contents would be better connected than by just an array index
36
+ const BIB_F035_TO_SID = {
37
+ f035Filters: [/^\(FI-BTJ\)/u, /^\(FI-TATI\)/u],
38
+ fSIDFilters: ['FI-BTJ', 'tati']
39
+ }; // There is somewhere other implementations of this same standardPrefixes to alephInternalPrefixes normalization
40
+
41
+ exports.BIB_F035_TO_SID = BIB_F035_TO_SID;
42
+ const REPLACE_PREFIXES = [{
43
+ oldPrefix: 'FI-MELINDA',
44
+ newPrefix: 'FIN01',
45
+ prefixReplaceCodes: ['w']
46
+ }, {
47
+ oldPrefix: 'FI-ASTERI-S',
48
+ newPrefix: 'FIN10',
49
+ prefixReplaceCodes: ['0']
50
+ }, {
51
+ oldPrefix: 'FI-ASTERI-N',
52
+ newPrefix: 'FIN11',
53
+ prefixReplaceCodes: ['0']
54
+ }, {
55
+ oldPrefix: 'FI-ASTERI-A',
56
+ newPrefix: 'FIN12',
57
+ prefixReplaceCodes: ['0']
58
+ }, {
59
+ oldPrefix: 'FI-ASTERI-W',
60
+ newPrefix: 'FIN13',
61
+ prefixReplaceCodes: ['0']
62
+ }]; // If placed in config.js testing needs envs
63
+ // BIB_FORMAT_SETTINGS is outdated, use other settings instead
64
+
65
+ exports.REPLACE_PREFIXES = REPLACE_PREFIXES;
66
+ const BIB_FORMAT_SETTINGS = {
67
+ replacePrefixes: REPLACE_PREFIXES,
68
+ generateMissingSIDs: BIB_F035_TO_SID
69
+ };
70
+ exports.BIB_FORMAT_SETTINGS = BIB_FORMAT_SETTINGS;
71
+ const BIB_PREVALIDATION_FIX_SETTINGS = {
72
+ generateMissingSIDs: BIB_F035_TO_SID
73
+ };
74
+ exports.BIB_PREVALIDATION_FIX_SETTINGS = BIB_PREVALIDATION_FIX_SETTINGS;
75
+ const BIB_POSTMERGE_FIX_SETTINGS = {
76
+ handleTempUrns: true,
77
+ stripF884s: true
78
+ };
79
+ exports.BIB_POSTMERGE_FIX_SETTINGS = BIB_POSTMERGE_FIX_SETTINGS;
80
+ const BIB_PREIMPORT_FIX_SETTINGS = {
81
+ replacePrefixes: REPLACE_PREFIXES
82
+ };
83
+ exports.BIB_PREIMPORT_FIX_SETTINGS = BIB_PREIMPORT_FIX_SETTINGS;
84
+ const AUTNAME_PREIMPORT_FIX_SETTINGS = {
85
+ replacePrefixes: REPLACE_PREFIXES
86
+ };
87
+ exports.AUTNAME_PREIMPORT_FIX_SETTINGS = AUTNAME_PREIMPORT_FIX_SETTINGS;
88
+ const BIB_HANDLE_TEMP_URNS_SETTINGS = {
89
+ handleTempUrns: true
90
+ };
91
+ exports.BIB_HANDLE_TEMP_URNS_SETTINGS = BIB_HANDLE_TEMP_URNS_SETTINGS;
92
+ const BIB_STRIP_F884S_SETTINGS = {
93
+ stripF884s: true
94
+ };
95
+ exports.BIB_STRIP_F884S_SETTINGS = BIB_STRIP_F884S_SETTINGS;
96
+ //# sourceMappingURL=fix-constants.js.map
@@ -0,0 +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"],"m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CAAvC;;AAIA,MAAMa,6BAA6B,GAAG;EAC3CJ,cAAc,EAAE;AAD2B,CAAtC;;AAIA,MAAMK,wBAAwB,GAAG;EACtCJ,UAAU,EAAE;AAD0B,CAAjC"}
@@ -0,0 +1,161 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.handleTempUrns = handleTempUrns;
7
+
8
+ var _debug = _interopRequireDefault(require("debug"));
9
+
10
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
+
12
+ /**
13
+ *
14
+ * @licstart The following is the entire license notice for the JavaScript code in this file.
15
+ *
16
+ * Shared modules for microservices of Melinda rest api batch import system
17
+ *
18
+ * Copyright (C) 2020-2022 University Of Helsinki (The National Library Of Finland)
19
+ *
20
+ * This file is part of melinda-rest-api-commons
21
+ *
22
+ * melinda-rest-api-commons program is free software: you can redistribute it and/or modify
23
+ * it under the terms of the GNU Affero General Public License as
24
+ * published by the Free Software Foundation, either version 3 of the
25
+ * License, or (at your option) any later version.
26
+ *
27
+ * melinda-rest-api-commons is distributed in the hope that it will be useful,
28
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
29
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30
+ * GNU Affero General Public License for more details.
31
+ *
32
+ * You should have received a copy of the GNU Affero General Public License
33
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
34
+ *
35
+ * @licend The above is the entire license notice
36
+ * for the JavaScript code in this file.
37
+ *
38
+ */
39
+ //import {MarcRecord} from '@natlibfi/marc-record';
40
+ const debug = (0, _debug.default)('@natlibfi/melinda-rest-api-commons:fixRecord:handleTempUrns');
41
+ const debugData = debug.extend('data'); // eslint-disable-next-line max-statements
42
+
43
+ function handleTempUrns(newRecord, options) {
44
+ if (options !== true) {
45
+ debug(`NOT running handleTempUrns fixer, no options`);
46
+ return newRecord;
47
+ }
48
+
49
+ debug(`Running handleTempUrns fixer`);
50
+ debugData(`Options for handleTempUrns: ${JSON.stringify(options)}`);
51
+ const {
52
+ f856sUrnsWithTempSubfields,
53
+ f856sUrnsWithNoTempSubfields
54
+ } = getf856sUrns(newRecord); // None of the URNs has temp subfields, we don't need to do anything
55
+
56
+ if (f856sUrnsWithTempSubfields.length < 1) {
57
+ debug(`No Urns with temp subfield`);
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
+
61
+
62
+ const existingLegalDepositURN = validateLD(f856sUrnsWithNoTempSubfields);
63
+ debug(`existingLegalDepositURN: ${existingLegalDepositURN}`);
64
+
65
+ if (existingLegalDepositURN) {
66
+ debug(`We have an existing LD URN, we can delete temp fields (${f856sUrnsWithTempSubfields.length})`);
67
+ newRecord.removeFields(f856sUrnsWithTempSubfields);
68
+ return newRecord;
69
+ } // We do not have an existing legalDepositURN, we should use the tempURN and remove the temp subfields from it
70
+
71
+
72
+ if (!existingLegalDepositURN && f856sUrnsWithTempSubfields.length > 0) {
73
+ //debug(`All Urns (${f856sUrn.length}) have a temp subfield`);
74
+ debugData(`Original temp URNs: (${JSON.stringify(f856sUrnsWithTempSubfields)})`);
75
+ const fixedFields = f856sUrnsWithTempSubfields.map(removeTempSubfield).filter(field => field);
76
+ debug(`We removed temp subfields: (${JSON.stringify(fixedFields)})`);
77
+ newRecord.removeFields(f856sUrnsWithTempSubfields);
78
+ newRecord.insertFields(fixedFields);
79
+ 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
+
82
+ }
83
+
84
+ function getf856sUrns(newRecord) {
85
+ const hasURN = f => f.tag === '856' && f.subfields.some(({
86
+ code,
87
+ value
88
+ }) => code === 'u' && /urn.fi/u.test(value));
89
+
90
+ const hasTempSubfield = f => f.subfields.some(({
91
+ code,
92
+ value
93
+ }) => code === '9' && /^MELINDA<TEMP>$/u.test(value));
94
+
95
+ const hasNoTempSubfield = f => !f.subfields.some(({
96
+ code,
97
+ value
98
+ }) => code === '9' && /^MELINDA<TEMP>$/u.test(value));
99
+
100
+ const f856sUrn = newRecord.fields.filter(hasURN);
101
+ const f856sUrnsWithTempSubfields = f856sUrn.filter(hasTempSubfield);
102
+ const f856sUrnsWithNoTempSubfields = f856sUrn.filter(hasNoTempSubfield);
103
+ debugData(`URN f856s: ${JSON.stringify(f856sUrn)}`);
104
+ debugData(`URN f856s with temp subfield: ${JSON.stringify(f856sUrnsWithTempSubfields)}`);
105
+ return {
106
+ f856sUrn,
107
+ f856sUrnsWithTempSubfields,
108
+ f856sUrnsWithNoTempSubfields
109
+ };
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
112
+
113
+
114
+ function validateLD(f856sUrn) {
115
+ debug(`Validating the existence of legal deposit subfields`);
116
+ const LD_SUBFIELDS = [{
117
+ code: 'z',
118
+ value: 'Käytettävissä vapaakappalekirjastoissa'
119
+ }, {
120
+ code: '5',
121
+ value: 'FI-Vapaa'
122
+ }];
123
+ const f856sUrnWithLdSubfields = f856sUrn.filter(field => fieldHasLDSubfields(field, LD_SUBFIELDS));
124
+
125
+ if (f856sUrnWithLdSubfields.length > 0) {
126
+ debug(`Record has ${f856sUrnWithLdSubfields.length} URN fields with all necessary legal deposit subfields`);
127
+ debugData(`f856sUrnWithLdSubfields: ${JSON.stringify(f856sUrnWithLdSubfields)}`);
128
+ return true;
129
+ }
130
+
131
+ return false;
132
+ }
133
+
134
+ function fieldHasLDSubfields(field, ldSubfields) {
135
+ if (ldSubfields.every(ldsf => field.subfields.some(sf => sf.code === ldsf.code && sf.value === ldsf.value))) {
136
+ return true;
137
+ }
138
+ }
139
+
140
+ function removeTempSubfield(field) {
141
+ // Handle non-numeric fields, and fields with a numeric tag of 010 and greater
142
+ // Aleph's FMT as a controlfield might be a problem
143
+ 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
145
+
146
+ if (filteredSubfields.length < 1) {
147
+ return false;
148
+ }
149
+
150
+ return {
151
+ tag: field.tag,
152
+ ind1: field.ind1,
153
+ ind2: field.ind2,
154
+ subfields: filteredSubfields
155
+ };
156
+ } // return controlFields as is
157
+
158
+
159
+ return field;
160
+ }
161
+ //# sourceMappingURL=fix-handle-tempurns.js.map
@@ -0,0 +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,IAAI,CAACC,SAAL,CAAeF,OAAf,CAAwB,EAAxD,CAAT;EAEA,MAAM;IAACG,0BAAD;IAA6BC;EAA7B,IAA6DC,YAAY,CAACN,SAAD,CAA/E,CAViD,CAYjD;;EACA,IAAII,0BAA0B,CAACG,MAA3B,GAAoC,CAAxC,EAA2C;IACzCZ,KAAK,CAAE,4BAAF,CAAL;IACA,OAAOK,SAAP;EACD,CAhBgD,CAkBjD;;;EACA,MAAMQ,uBAAuB,GAAGC,UAAU,CAACJ,4BAAD,CAA1C;EACAV,KAAK,CAAE,4BAA2Ba,uBAAwB,EAArD,CAAL;;EAEA,IAAIA,uBAAJ,EAA6B;IAC3Bb,KAAK,CAAE,0DAAyDS,0BAA0B,CAACG,MAAO,GAA7F,CAAL;IACAP,SAAS,CAACU,YAAV,CAAuBN,0BAAvB;IACA,OAAOJ,SAAP;EACD,CA1BgD,CA4BjD;;;EACA,IAAI,CAACQ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E,CAACZ,KAAH,KAAaW,IAAI,CAACX,KAAtE,CAA1B,CAAJ,EAA6G;IAC3G,OAAO,IAAP;EACD;AACF;;AAED,SAASV,kBAAT,CAA4BE,KAA5B,EAAmC;EAEjC;EACA;EACA,IAAI,CAACqB,KAAK,CAACrB,KAAK,CAACI,GAAP,CAAN,IAAqBkB,QAAQ,CAACtB,KAAK,CAACI,GAAP,EAAY,EAAZ,CAAR,IAA2B,EAApD,EAAwD;IAEtD,MAAMmB,iBAAiB,GAAGvB,KAAK,CAACK,SAAN,CAAgBN,MAAhB,CAAuBqB,EAAE,IAAIA,EAAE,CAACb,IAAH,KAAY,GAAZ,IAAmB,CAAE,kBAAD,CAAqBE,IAArB,CAA0BW,EAAE,CAACZ,KAA7B,CAAjD,CAA1B,CAFsD,CAItD;;IACA,IAAIe,iBAAiB,CAAC/B,MAAlB,GAA2B,CAA/B,EAAkC;MAChC,OAAO,KAAP;IACD;;IAED,OAAO;MACLY,GAAG,EAAEJ,KAAK,CAACI,GADN;MAELoB,IAAI,EAAExB,KAAK,CAACwB,IAFP;MAGLC,IAAI,EAAEzB,KAAK,CAACyB,IAHP;MAILpB,SAAS,EAAEkB;IAJN,CAAP;EAMD,CAnBgC,CAoBjC;;;EACA,OAAOvB,KAAP;AACD"}
@@ -0,0 +1,91 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.stripF884s = stripF884s;
7
+
8
+ var _marcRecord = require("@natlibfi/marc-record");
9
+
10
+ var _debug = _interopRequireDefault(require("debug"));
11
+
12
+ var _fixUtils = require("./fix-utils");
13
+
14
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
+
16
+ /**
17
+ *
18
+ * @licstart The following is the entire license notice for the JavaScript code in this file.
19
+ *
20
+ * Shared modules for microservices of Melinda rest api batch import system
21
+ *
22
+ * Copyright (C) 2020-2022 University Of Helsinki (The National Library Of Finland)
23
+ *
24
+ * This file is part of melinda-rest-api-commons
25
+ *
26
+ * melinda-rest-api-commons program is free software: you can redistribute it and/or modify
27
+ * it under the terms of the GNU Affero General Public License as
28
+ * published by the Free Software Foundation, either version 3 of the
29
+ * License, or (at your option) any later version.
30
+ *
31
+ * melinda-rest-api-commons is distributed in the hope that it will be useful,
32
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
33
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34
+ * GNU Affero General Public License for more details.
35
+ *
36
+ * You should have received a copy of the GNU Affero General Public License
37
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
38
+ *
39
+ * @licend The above is the entire license notice
40
+ * for the JavaScript code in this file.
41
+ *
42
+ */
43
+ function stripF884s(newRecord, options) {
44
+ const debug = (0, _debug.default)('@natlibfi/melinda-rest-api-commons:fixRecord:stripF884s');
45
+ const debugData = debug.extend('data');
46
+
47
+ if (options !== true) {
48
+ debug(`NOT running stripF884s fixer, no options`);
49
+ return newRecord;
50
+ }
51
+
52
+ debug(`Running stripF884s fixer`);
53
+ debugData(`Options for stripF884s: ${JSON.stringify(options)}`); // Handle only f884 with $5 MELINDA - let's no remove random f884s
54
+
55
+ const isMelindaf884 = f => f.tag === '884' && f.subfields.some(({
56
+ code,
57
+ value
58
+ }) => code === '5' && value === 'MELINDA');
59
+
60
+ const f884Melindas = newRecord.fields.filter(isMelindaf884);
61
+ debugData(`Melinda's f884s (${f884Melindas.length}) \n ${JSON.stringify(f884Melindas)}`);
62
+
63
+ if (f884Melindas.length < 2) {
64
+ debug(`Not enough Melinda's f884.s to filter (${f884Melindas.length})`);
65
+ return newRecord;
66
+ } // Sort fields by date subfield $g, so we'll keep the oldest one
67
+
68
+
69
+ 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
71
+
72
+ 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
75
+
76
+ newRecord.removeFields(f884Melindas);
77
+ newRecord.insertFields(uniqFields);
78
+ return newRecord; // Keep just first instance of each similar field, compare without subfield with subfieldCode
79
+
80
+ function uniqWithOutSubfield(fields, subfieldCode) {
81
+ return fields.reduce((uniq, field) => {
82
+ if (!uniq.some(f => _marcRecord.MarcRecord.isEqual((0, _fixUtils.removeSubfield)(f, subfieldCode), (0, _fixUtils.removeSubfield)(field, subfieldCode)))) {
83
+ // eslint-disable-line functional/no-conditional-statement
84
+ uniq.push(field); // eslint-disable-line functional/immutable-data
85
+ }
86
+
87
+ return uniq;
88
+ }, []);
89
+ }
90
+ }
91
+ //# sourceMappingURL=fix-strip-f884s.js.map
@@ -0,0 +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,CAAa,MAAb,CAAlB;;EAEA,IAAIJ,OAAO,KAAK,IAAhB,EAAsB;IACpBC,KAAK,CAAE,0CAAF,CAAL;IACA,OAAOF,SAAP;EACD;;EAEDE,KAAK,CAAE,0BAAF,CAAL;EACAE,SAAS,CAAE,2BAA0BE,IAAI,CAACC,SAAL,CAAeN,OAAf,CAAwB,EAApD,CAAT,CAV6C,CAY7C;;EACA,MAAMO,aAAa,GAAGC,CAAC,IAAIA,CAAC,CAACC,GAAF,KAAU,KAAV,IAAmBD,CAAC,CAACE,SAAF,CAAYC,IAAZ,CAAiB,CAAC;IAACC,IAAD;IAAOC;EAAP,CAAD,KAAmBD,IAAI,KAAK,GAAT,IAAgBC,KAAK,KAAK,SAA9D,CAA9C;;EAEA,MAAMC,YAAY,GAAGf,SAAS,CAACgB,MAAV,CAAiBC,MAAjB,CAAwBT,aAAxB,CAArB;EACAJ,SAAS,CAAE,oBAAmBW,YAAY,CAACG,MAAO,QAAOZ,IAAI,CAACC,SAAL,CAAeQ,YAAf,CAA6B,EAA7E,CAAT;;EAEA,IAAIA,YAAY,CAACG,MAAb,GAAsB,CAA1B,EAA6B;IAC3BhB,KAAK,CAAE,0CAAyCa,YAAY,CAACG,MAAO,GAA/D,CAAL;IACA,OAAOlB,SAAP;EACD,CArB4C,CAuB7C;;;EACA,MAAMmB,YAAY,GAAG,IAAAC,mCAAA,EAA0BL,YAA1B,EAAwC,GAAxC,CAArB;EACAX,SAAS,CAAE,kDAAiDe,YAAY,CAACD,MAAO,QAAOZ,IAAI,CAACC,SAAL,CAAeY,YAAf,CAA6B,EAA3G,CAAT,CAzB6C,CA2B7C;;EACA,MAAME,UAAU,GAAGC,mBAAmB,CAACH,YAAD,EAAe,GAAf,CAAtC;EACAf,SAAS,CAAE,yCAAwCiB,UAAU,CAACH,MAAO,QAAOZ,IAAI,CAACC,SAAL,CAAec,UAAf,CAA2B,EAA9F,CAAT,CA7B6C,CA+B7C;EACA;;EACArB,SAAS,CAACuB,YAAV,CAAuBR,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"}
@@ -0,0 +1,88 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.removeSubfield = removeSubfield;
7
+ exports.sortFieldsBySubfieldValue = sortFieldsBySubfieldValue;
8
+
9
+ /**
10
+ *
11
+ * @licstart The following is the entire license notice for the JavaScript code in this file.
12
+ *
13
+ * Shared modules for microservices of Melinda rest api batch import system
14
+ *
15
+ * Copyright (C) 2020-2022 University Of Helsinki (The National Library Of Finland)
16
+ *
17
+ * This file is part of melinda-rest-api-commons
18
+ *
19
+ * melinda-rest-api-commons program is free software: you can redistribute it and/or modify
20
+ * it under the terms of the GNU Affero General Public License as
21
+ * published by the Free Software Foundation, either version 3 of the
22
+ * License, or (at your option) any later version.
23
+ *
24
+ * melinda-rest-api-commons is distributed in the hope that it will be useful,
25
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
26
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27
+ * GNU Affero General Public License for more details.
28
+ *
29
+ * You should have received a copy of the GNU Affero General Public License
30
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
31
+ *
32
+ * @licend The above is the entire license notice
33
+ * for the JavaScript code in this file.
34
+ *
35
+ */
36
+ function removeSubfield(field, code) {
37
+ // Handle non-numeric fields, and fields with a numeric tag of 010 and greater
38
+ // Aleph's FMT as a controlfield might be a problem
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
41
+
42
+ if (filteredSubfields.length < 1) {
43
+ return false;
44
+ }
45
+
46
+ return {
47
+ tag: field.tag,
48
+ ind1: field.ind1,
49
+ ind2: field.ind2,
50
+ subfields: filteredSubfields
51
+ };
52
+ } // return controlFields as is
53
+
54
+
55
+ return field;
56
+ } // sort fields by value of each fields first subfield with subfielCode
57
+
58
+
59
+ function sortFieldsBySubfieldValue(fields, subfieldCode) {
60
+ return [...fields].sort((a, b) => {
61
+ const a1value = getFirstSubfieldValue(a, subfieldCode);
62
+ const b1value = getFirstSubfieldValue(b, subfieldCode);
63
+
64
+ if (a1value && !b1value) {
65
+ return -1;
66
+ }
67
+
68
+ if (!a1value && b1value) {
69
+ return 1;
70
+ }
71
+
72
+ if (a1value > b1value) {
73
+ return 1;
74
+ }
75
+
76
+ if (b1value > a1value) {
77
+ return -1;
78
+ }
79
+
80
+ return 0;
81
+ }); // get value for the for instance of subfield with subfieldCode
82
+
83
+ function getFirstSubfieldValue(field, subfieldCode) {
84
+ const subs = field.subfields ? field.subfields.filter(subf => subf.code === subfieldCode) : [];
85
+ return subs.length > 0 ? subs[0].value : '';
86
+ }
87
+ }
88
+ //# sourceMappingURL=fix-utils.js.map
@@ -0,0 +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,SA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