exifreader 4.53.0 → 4.55.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (100) hide show
  1. package/README.md +404 -33
  2. package/bin/analyze.js +139 -9
  3. package/bin/build-pages.js +21 -35
  4. package/bin/cli.js +81 -13
  5. package/bin/generate-tag-modules.js +287 -0
  6. package/dist/exif-reader.js +1 -1
  7. package/dist/exif-reader.js.map +1 -1
  8. package/exif-reader.d.ts +213 -38
  9. package/package.json +71 -11
  10. package/src/base64-image.js +27 -0
  11. package/src/composable/brand.js +10 -0
  12. package/src/composable/composition-tag-filter.js +97 -0
  13. package/src/composable/composition.js +195 -0
  14. package/src/composable/core.d.ts +148 -0
  15. package/src/composable/core.js +526 -0
  16. package/src/composable/exif-block-reader.js +197 -0
  17. package/src/composable/formats/avif.js +19 -0
  18. package/src/composable/formats/gif.js +28 -0
  19. package/src/composable/formats/heic.js +19 -0
  20. package/src/composable/formats/iso-bmff-file-reader.js +14 -0
  21. package/src/composable/formats/jpeg.js +55 -0
  22. package/src/composable/formats/jxl.js +30 -0
  23. package/src/composable/formats/png.js +187 -0
  24. package/src/composable/formats/tiff.js +18 -0
  25. package/src/composable/formats/webp.js +28 -0
  26. package/src/composable/formats/xml.js +18 -0
  27. package/src/composable/formats.d.ts +17 -0
  28. package/src/composable/formats.js +17 -0
  29. package/src/composable/group-helpers.js +129 -0
  30. package/src/composable/groups/canon.js +26 -0
  31. package/src/composable/groups/composite.js +43 -0
  32. package/src/composable/groups/exif.js +48 -0
  33. package/src/composable/groups/gps.js +75 -0
  34. package/src/composable/groups/icc.js +52 -0
  35. package/src/composable/groups/iptc.js +86 -0
  36. package/src/composable/groups/maker-notes.js +21 -0
  37. package/src/composable/groups/mpf.js +25 -0
  38. package/src/composable/groups/pentax.js +34 -0
  39. package/src/composable/groups/photoshop.js +18 -0
  40. package/src/composable/groups/thumbnail.js +131 -0
  41. package/src/composable/groups/xmp.js +118 -0
  42. package/src/composable/groups.d.ts +20 -0
  43. package/src/composable/groups.js +105 -0
  44. package/src/composable/iptc-block-reader.js +44 -0
  45. package/src/composable/read-view.js +165 -0
  46. package/src/composable/tag-binding-name.js +17 -0
  47. package/src/composable/tags/exif.d.ts +295 -0
  48. package/src/composable/tags/exif.js +396 -0
  49. package/src/composable/tags/iptc.d.ts +84 -0
  50. package/src/composable/tags/iptc.js +109 -0
  51. package/src/composable/value-budget.js +24 -0
  52. package/src/composite.js +2 -2
  53. package/src/decompression.js +60 -0
  54. package/src/dom-parser.js +5 -6
  55. package/src/engine.js +332 -0
  56. package/src/errors.js +8 -2
  57. package/src/exif-reader.d.ts +8 -0
  58. package/src/exif-reader.js +118 -1121
  59. package/src/file-loaders.js +74 -1
  60. package/src/file-tags.js +9 -9
  61. package/src/image-header-avif.js +7 -8
  62. package/src/image-header-gif.js +9 -10
  63. package/src/image-header-heic.js +7 -8
  64. package/src/image-header-iso-bmff.js +71 -92
  65. package/src/image-header-jpeg.js +60 -59
  66. package/src/image-header-jxl.js +59 -65
  67. package/src/image-header-png.js +199 -76
  68. package/src/image-header-tiff.js +7 -4
  69. package/src/image-header-webp.js +29 -35
  70. package/src/image-header.js +25 -59
  71. package/src/iptc-tag-names-descriptions.js +334 -0
  72. package/src/iptc-tag-names.js +50 -306
  73. package/src/iptc-tags.js +41 -27
  74. package/src/jfif-tags.js +9 -9
  75. package/src/load-auto.js +2 -2
  76. package/src/loadview-pipeline.js +197 -498
  77. package/src/mpf-tags.js +10 -7
  78. package/src/node-require.js +15 -2
  79. package/src/png-text-tags.js +90 -327
  80. package/src/tag-filter-config.js +24 -15
  81. package/src/tag-filter.js +67 -3
  82. package/src/tag-names-0th-ifd-descriptions.js +232 -0
  83. package/src/tag-names-0th-ifd.js +49 -173
  84. package/src/tag-names-common.js +263 -211
  85. package/src/tag-names-exif-ifd-descriptions.js +249 -0
  86. package/src/tag-names-exif-ifd.js +55 -187
  87. package/src/tag-names-gps-ifd-descriptions.js +156 -0
  88. package/src/tag-names-gps-ifd.js +39 -146
  89. package/src/tag-names-utils.js +33 -10
  90. package/src/tags-helpers.js +14 -24
  91. package/src/tags.js +23 -11
  92. package/src/thumbnail.js +7 -5
  93. package/src/types.js +2 -0
  94. package/src/unsupported-format-error.js +21 -0
  95. package/src/utils.js +64 -119
  96. package/src/writer/exif-tag-table.js +321 -0
  97. package/src/xml.js +12 -15
  98. package/src/xmp-tag-names.js +42 -21
  99. package/src/xmp-tags.js +9 -10
  100. package/webpack.config.js +1 -1
@@ -0,0 +1,526 @@
1
+ /* This Source Code Form is subject to the terms of the Mozilla Public
2
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
3
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
+
5
+ import Composition from './composition.js';
6
+ import {FORMAT_BRAND, GROUP_BRAND, FILTER_BRAND} from './brand.js';
7
+ import {readView} from './read-view.js';
8
+ import {createCompositionTagFilter} from './composition-tag-filter.js';
9
+ import {createTagFilter} from '../tag-filter.js';
10
+ import {FILTER_GROUPS} from '../tag-filter-config.js';
11
+ import {UnsupportedFormatError} from '../unsupported-format-error.js';
12
+ import {objectAssign, getDataViewFromBytes, assertPromiseSupport} from '../utils.js';
13
+
14
+ const FORMATS_IMPORT = '\'exifreader/formats\'';
15
+ const GROUPS_IMPORT = '\'exifreader/groups\'';
16
+ const TAGS_IMPORT = '\'exifreader/tags/exif\' or \'exifreader/tags/iptc\'';
17
+ const CORE_IMPORT = '\'exifreader/core\'';
18
+ const TABLE_KEYS = ['formats', 'groups', 'tags', 'tagFilter', 'domParser', 'decompress'];
19
+ const DECOMPRESS_KEYS = ['brotli', 'deflate', 'maxDecompressedSize'];
20
+ const BOOLEAN_OPTIONS = ['expanded', 'includeUnknown', 'computed', 'includeOffsets'];
21
+ const SUPPORTED_OPTIONS = BOOLEAN_OPTIONS.concat(['includeTags', 'excludeTags']);
22
+ const INPUT_MESSAGE = 'reader.load: data must be an ArrayBuffer, a SharedArrayBuffer, a DataView, a typed array such as Uint8Array or a Node Buffer, or a Blob or File';
23
+
24
+ export const errors = {UnsupportedFormatError};
25
+
26
+ // Only this object references narrowIncludeTags and assertOptions, so a bundle
27
+ // without the tag filter leaves the caller's includeTags handling and the
28
+ // checks of includeTags and excludeTags out too.
29
+ export const tagFilter = {brand: FILTER_BRAND, createTagFilter, narrowIncludeTags, getNarrowedTagFilterFactory, assertOptions};
30
+
31
+ /**
32
+ * Builds a reader of the given formats, groups and tags only.
33
+ *
34
+ * @param {{formats: Array<object>, groups: (Array<object>|undefined), tags: (Array<object>|undefined), tagFilter: (object|undefined), domParser: (object|undefined), decompress: (object|undefined)}} tables
35
+ * Values imported from 'exifreader/formats', 'exifreader/groups',
36
+ * 'exifreader/tags/exif' and 'exifreader/tags/iptc', `tagFilter` from
37
+ * 'exifreader/core' to let load take includeTags and excludeTags,
38
+ * `domParser`, an object with a parseFromString method, for xmp where there
39
+ * is no global DOMParser, and `decompress`, {brotli, deflate,
40
+ * maxDecompressedSize} as ExifReader.load takes it, for every load (each
41
+ * load gets the whole limit).
42
+ * @returns {{load: function}} `load(data, options)` always returns a Promise
43
+ * of the tags, grouped unless `expanded` is false.
44
+ * @throws {TypeError} When a table is not an array or holds a value that was
45
+ * not imported from its module, formats is empty, tagFilter is given and
46
+ * is not the tagFilter export, domParser is given and has no
47
+ * parseFromString method, or decompress is given and is not an object of
48
+ * optional brotli and deflate functions and a maxDecompressedSize of zero
49
+ * or more, or has another key.
50
+ */
51
+ export function createReader(tables) {
52
+ const given = tables || {};
53
+ assertTableKeys(given);
54
+ const {formats, groups = [], tags = [], tagFilter: filter, domParser, decompress} = given;
55
+ assertTables(formats, groups, tags);
56
+ assertFilter(filter);
57
+ assertDomParserShape(domParser);
58
+ const decompressConfig = getDecompressConfig(decompress);
59
+
60
+ const composition = Composition.createComposition({formats, groups});
61
+ const unsupportedFormatMessage = getUnsupportedFormatMessage(composition.formats);
62
+ const includeTagsMapping = getIncludeTagsMapping(composition, tags);
63
+ const readGroups = groups.concat(getParseOnlyGroups(includeTagsMapping));
64
+ const exifDescriptors = tags.filter((tag) => tag.group === 'exif').concat(getExifDependencies(readGroups));
65
+ const addsExif = needsExif(exifDescriptors) && composition.composed.groups.exif !== true;
66
+ const runGroups = addsExif ? readGroups.concat([getParseOnlyExifGroup(exifDescriptors, groups)]) : readGroups;
67
+ const runComposition = runGroups.length > groups.length ? Composition.createComposition({formats, groups: runGroups}) : composition;
68
+ const environments = getEnvironments(groups, filter ? filter.createTagFilter : getCompositionTagFilterFactory(readGroups));
69
+ const environmentChecks = getEnvironmentChecks(groups);
70
+
71
+ return {load};
72
+
73
+ function load(data, options) {
74
+ assertPromiseSupport();
75
+ return Promise.resolve().then(() => {
76
+ const loadOptions = getLoadOptions(options);
77
+ const expanded = loadOptions.expanded === undefined ? true : loadOptions.expanded;
78
+ assertTagFilterOptions(filter, loadOptions);
79
+ environmentChecks.forEach((check) => check(domParser));
80
+ return getInputDataView(data).then((dataView) => {
81
+ const run = getRun(objectAssign({}, loadOptions, {domParser, decompress: decompressConfig, expanded}), includeTagsMapping, addsExif, filter, environments);
82
+ return readView(runComposition, dataView, run.options, run.environment);
83
+ }).catch((error) => {
84
+ throw getReadError(error, unsupportedFormatMessage);
85
+ }).then((result) => {
86
+ if (expanded) {
87
+ // png already holds every pngFile and pngText tag; the
88
+ // default entry keeps the two keys only for compatibility.
89
+ delete result.pngFile;
90
+ delete result.pngText;
91
+ deleteEmptyGroups(result);
92
+ } else if (composition.composed.groups.thumbnail !== true) {
93
+ // In flat output the name thumbnail is the thumbnail's alone.
94
+ delete result.thumbnail;
95
+ }
96
+ return result;
97
+ });
98
+ });
99
+ }
100
+ }
101
+
102
+ // for...in, as the destructuring above also reads an inherited key.
103
+ function assertTableKeys(tables) {
104
+ for (const key in tables) {
105
+ if (TABLE_KEYS.indexOf(key) === -1) {
106
+ throw new TypeError('createReader: unknown key \'' + key + '\': the keys are formats, groups, tags, tagFilter, domParser and decompress');
107
+ }
108
+ }
109
+ }
110
+
111
+ function assertTables(formats, groups, tags) {
112
+ assertArray(formats, 'formats', FORMATS_IMPORT);
113
+ if (formats.length === 0) {
114
+ throw new TypeError('createReader: formats must hold at least one value imported from ' + FORMATS_IMPORT);
115
+ }
116
+ assertArray(groups, 'groups', GROUPS_IMPORT);
117
+ assertArray(tags, 'tags', TAGS_IMPORT);
118
+ assertElements(formats, 'formats', FORMATS_IMPORT, (format) => !!format && format.brand === FORMAT_BRAND);
119
+ assertElements(groups, 'groups', GROUPS_IMPORT, (group) => !!group && group.brand === GROUP_BRAND);
120
+ assertElements(tags, 'tags', TAGS_IMPORT, isTagDescriptor);
121
+ }
122
+
123
+ function assertArray(table, name, importName) {
124
+ if (!Array.isArray(table)) {
125
+ throw new TypeError('createReader: ' + name + ' must be an array of values imported from ' + importName);
126
+ }
127
+ }
128
+
129
+ function assertElements(table, name, importName, isValid) {
130
+ for (let i = 0; i < table.length; i++) {
131
+ if (!isValid(table[i])) {
132
+ throw new TypeError('createReader: ' + name + '[' + i + '] is not a value imported from ' + importName);
133
+ }
134
+ }
135
+ }
136
+
137
+ // The reader a descriptor links to checks its group and dictionary.
138
+ function isTagDescriptor(tag) {
139
+ return !!tag
140
+ && typeof tag.name === 'string'
141
+ && Array.isArray(tag.ids)
142
+ && tag.ids.length > 0
143
+ && tag.ids.every((id) => typeof id === 'number')
144
+ && !!tag.reader
145
+ && typeof tag.reader.createParseOnlyGroup === 'function'
146
+ && typeof tag.reader.isDescriptor === 'function'
147
+ && tag.reader.isDescriptor(tag);
148
+ }
149
+
150
+ function assertFilter(filter) {
151
+ if (filter !== undefined && !(filter && filter.brand === FILTER_BRAND)) {
152
+ throw new TypeError('createReader: tagFilter must be the tagFilter imported from ' + CORE_IMPORT);
153
+ }
154
+ }
155
+
156
+ function assertDomParserShape(domParser) {
157
+ if (domParser !== undefined && !(domParser && typeof domParser.parseFromString === 'function')) {
158
+ throw new TypeError('createReader: domParser must be a DOM parser, an object with a parseFromString method');
159
+ }
160
+ }
161
+
162
+ // Each field is read once, so a getter cannot pass the checks with one value
163
+ // and give load another.
164
+ function getDecompressConfig(decompress) {
165
+ if (decompress === undefined) {
166
+ return undefined;
167
+ }
168
+ if (!isPlainObject(decompress)) {
169
+ throw new TypeError('createReader: decompress must be an object with brotli, deflate and maxDecompressedSize');
170
+ }
171
+ assertDecompressKeys(decompress);
172
+ const {brotli, deflate, maxDecompressedSize} = decompress;
173
+ assertDecompressFunction(brotli, 'brotli');
174
+ assertDecompressFunction(deflate, 'deflate');
175
+ if (maxDecompressedSize !== undefined && !(typeof maxDecompressedSize === 'number' && maxDecompressedSize >= 0)) {
176
+ throw new TypeError('createReader: decompress.maxDecompressedSize must be a number of bytes, zero or more');
177
+ }
178
+ return {brotli, deflate, maxDecompressedSize};
179
+ }
180
+
181
+ function isPlainObject(value) {
182
+ return Object.prototype.toString.call(value) === '[object Object]';
183
+ }
184
+
185
+ // for...in, as reading the fields also reads an inherited key.
186
+ function assertDecompressKeys(decompress) {
187
+ for (const key in decompress) {
188
+ if (DECOMPRESS_KEYS.indexOf(key) === -1) {
189
+ throw new TypeError('createReader: decompress has no key \'' + key + '\': the keys are brotli, deflate and maxDecompressedSize');
190
+ }
191
+ }
192
+ }
193
+
194
+ function assertDecompressFunction(value, key) {
195
+ if (value !== undefined && typeof value !== 'function') {
196
+ throw new TypeError('createReader: decompress.' + key + ' must be a function');
197
+ }
198
+ }
199
+
200
+ function getUnsupportedFormatMessage(formats) {
201
+ const names = formats.map((format) => format.header.fileType.value);
202
+ return 'reader.load: none of this reader\'s formats (' + names.join(', ') + ') recognizes the input: it is in another format or is not an image';
203
+ }
204
+
205
+ // The includeTags config this reader stands for: every composed group whole,
206
+ // file for FileType, and the descriptors of a group composed by tags alone.
207
+ function getIncludeTagsMapping(composition, tags) {
208
+ const mapping = Object.create(null);
209
+ mapping.file = true;
210
+ const groups = composition.groups.concat(composition.derived);
211
+ for (let i = 0; i < groups.length; i++) {
212
+ mapping[groups[i].key] = true;
213
+ }
214
+ for (let i = 0; i < tags.length; i++) {
215
+ const key = tags[i].group;
216
+ if (mapping[key] !== true) {
217
+ mapping[key] = (mapping[key] || []).concat([tags[i]]);
218
+ }
219
+ }
220
+ return mapping;
221
+ }
222
+
223
+ // Built by the reader the descriptors link to, for each group other than exif
224
+ // composed by descriptors alone. Exif comes last, as these add to what it reads.
225
+ function getParseOnlyGroups(mapping) {
226
+ const parseOnlyGroups = [];
227
+ for (const key in mapping) {
228
+ if (key !== 'exif' && mapping[key] !== true) {
229
+ parseOnlyGroups.push(mapping[key][0].reader.createParseOnlyGroup(mapping[key]));
230
+ }
231
+ }
232
+ return parseOnlyGroups;
233
+ }
234
+
235
+ // From the groups as given: composing merges the maker-note groups into one
236
+ // that carries no exifDependencies.
237
+ function getExifDependencies(groups) {
238
+ let dependencies = [];
239
+ for (let i = 0; i < groups.length; i++) {
240
+ if (groups[i].exifDependencies !== undefined) {
241
+ dependencies = dependencies.concat(groups[i].exifDependencies);
242
+ }
243
+ }
244
+ return dependencies;
245
+ }
246
+
247
+ // The groups that consume a block Exif carries and the derived groups carry
248
+ // Exif dependencies, so with the tag descriptors these are all that read Exif.
249
+ function needsExif(exifDescriptors) {
250
+ return exifDescriptors.length > 0;
251
+ }
252
+
253
+ // Built by the reader the descriptors link to, so a bundle without one has no
254
+ // Exif reading code.
255
+ function getParseOnlyExifGroup(exifDescriptors, groups) {
256
+ return exifDescriptors[0].reader.createParseOnlyGroup(exifDescriptors, getIfd1TagNames(groups));
257
+ }
258
+
259
+ function getIfd1TagNames(groups) {
260
+ for (let i = 0; i < groups.length; i++) {
261
+ if (groups[i].ifd1TagNames !== undefined) {
262
+ return groups[i].ifd1TagNames;
263
+ }
264
+ }
265
+ return undefined;
266
+ }
267
+
268
+ // From the groups as given: parse-only groups carry no environment.
269
+ function getEnvironments(groups, createFilter) {
270
+ const environment = {createTagFilter: createFilter};
271
+ for (let i = 0; i < groups.length; i++) {
272
+ objectAssign(environment, groups[i].environment);
273
+ }
274
+ return {
275
+ environment,
276
+ // includeTags then only caps what is read, so excludeTags applies to every group.
277
+ limitingEnvironment: objectAssign({}, environment, {includeTagsIsLimit: true}),
278
+ };
279
+ }
280
+
281
+ // Without the tag filter a load passes no includeTags or excludeTags, so the
282
+ // filter only ever sees the reader's own includeTags config.
283
+ function getCompositionTagFilterFactory(groups) {
284
+ const exifDependencyNames = getNames(getExifDependencies(groups));
285
+ return ({includeTags}) => createCompositionTagFilter(includeTags, exifDependencyNames);
286
+ }
287
+
288
+ function getEnvironmentChecks(groups) {
289
+ const checks = [];
290
+ for (let i = 0; i < groups.length; i++) {
291
+ if (groups[i].assertEnvironment !== undefined) {
292
+ checks.push(groups[i].assertEnvironment);
293
+ }
294
+ }
295
+ return checks;
296
+ }
297
+
298
+ function getLoadOptions(options) {
299
+ if (options === undefined) {
300
+ return {};
301
+ }
302
+ if (options === null || typeof options !== 'object') {
303
+ throw new TypeError('reader.load: options must be an object');
304
+ }
305
+ // for...in, as objectAssign copies an inherited key too.
306
+ for (const key in options) {
307
+ assertSupportedOption(key);
308
+ }
309
+ BOOLEAN_OPTIONS.forEach((name) => assertBooleanOption(options, name));
310
+ return options;
311
+ }
312
+
313
+ function assertSupportedOption(key) {
314
+ if (key === 'async') {
315
+ throw new TypeError('reader.load: the async option is not supported: load always returns a Promise');
316
+ }
317
+ if (key === 'length') {
318
+ throw new TypeError('reader.load: the length option is not supported: to read the first bytes of a Blob or File, pass blob.slice(0, length)');
319
+ }
320
+ if (key === 'domParser' || key === 'decompress') {
321
+ throw new TypeError('reader.load: the ' + key + ' option is not supported: pass ' + key + ' to createReader');
322
+ }
323
+ if (SUPPORTED_OPTIONS.indexOf(key) === -1) {
324
+ throw new TypeError('reader.load: unknown option \'' + key + '\'');
325
+ }
326
+ }
327
+
328
+ function assertBooleanOption(options, name) {
329
+ if (options[name] !== undefined && typeof options[name] !== 'boolean') {
330
+ throw new TypeError('reader.load: ' + name + ' must be true or false');
331
+ }
332
+ }
333
+
334
+ function assertTagFilterOptions(filter, options) {
335
+ if (filter) {
336
+ filter.assertOptions(options);
337
+ return;
338
+ }
339
+ if (options.includeTags) {
340
+ throw getMissingFilterError('includeTags');
341
+ }
342
+ if (options.excludeTags) {
343
+ throw getMissingFilterError('excludeTags');
344
+ }
345
+ }
346
+
347
+ function getMissingFilterError(option) {
348
+ return new TypeError('reader.load: ' + option + ' needs the tag filter: add tagFilter, imported from ' + CORE_IMPORT + ', to createReader');
349
+ }
350
+
351
+ function getInputDataView(data) {
352
+ if (isBlobLike(data)) {
353
+ return Promise.resolve(data.arrayBuffer()).then(getBytesDataView);
354
+ }
355
+ return Promise.resolve(getBytesDataView(data));
356
+ }
357
+
358
+ function isBlobLike(data) {
359
+ return !!data
360
+ && typeof data.arrayBuffer === 'function'
361
+ && typeof data.slice === 'function'
362
+ && typeof data.size === 'number';
363
+ }
364
+
365
+ function getBytesDataView(bytes) {
366
+ try {
367
+ return getDataViewFromBytes(bytes);
368
+ } catch (error) {
369
+ if (error instanceof TypeError) {
370
+ throw new TypeError(INPUT_MESSAGE);
371
+ }
372
+ throw error;
373
+ }
374
+ }
375
+
376
+ // The composition alone caps a whole group. Only a parse-only Exif group and
377
+ // tag descriptors need includeTags, and a caller's includeTags is narrowed to
378
+ // what the reader composes, never widened.
379
+ function getRun(options, mapping, addsExif, filter, environments) {
380
+ if (options.includeTags) {
381
+ return {
382
+ options: objectAssign({}, options, {async: true, includeTags: filter.narrowIncludeTags(options.includeTags, mapping)}),
383
+ environment: objectAssign({}, environments.environment, {createTagFilter: filter.getNarrowedTagFilterFactory(mapping)}),
384
+ };
385
+ }
386
+ if (addsExif) {
387
+ return {
388
+ options: objectAssign({}, options, {async: true, includeTags: getIncludeTagsConfig(mapping)}),
389
+ environment: environments.limitingEnvironment,
390
+ };
391
+ }
392
+ return {options: objectAssign({}, options, {async: true}), environment: environments.environment};
393
+ }
394
+
395
+ function getIncludeTagsConfig(mapping) {
396
+ const config = {};
397
+ for (const key in mapping) {
398
+ config[key] = mapping[key] === true ? true : getNames(mapping[key]);
399
+ }
400
+ return config;
401
+ }
402
+
403
+ function getReadError(error, unsupportedFormatMessage) {
404
+ if (error instanceof UnsupportedFormatError) {
405
+ return new UnsupportedFormatError(unsupportedFormatMessage);
406
+ }
407
+ return error;
408
+ }
409
+
410
+ function deleteEmptyGroups(tags) {
411
+ Object.keys(tags).forEach((key) => {
412
+ if (isPlainObject(tags[key]) && Object.keys(tags[key]).length === 0) {
413
+ delete tags[key];
414
+ }
415
+ });
416
+ }
417
+
418
+ // The shared filter skips what it cannot read: a misspelt group, a value that
419
+ // is not true or an array, a selector that is neither a name nor an id.
420
+ function assertOptions(options) {
421
+ assertGroups(options.includeTags, 'includeTags');
422
+ assertGroups(options.excludeTags, 'excludeTags');
423
+ }
424
+
425
+ function assertGroups(groups, option) {
426
+ if (!groups) {
427
+ return;
428
+ }
429
+ if (!isPlainObject(groups)) {
430
+ throw new TypeError('reader.load: ' + option + ' must be an object keyed by group');
431
+ }
432
+ Object.keys(groups).forEach((key) => {
433
+ if (FILTER_GROUPS[key] !== true) {
434
+ throw new TypeError('reader.load: ' + option + ' has no group \'' + key + '\': the groups are ' + getGroupList());
435
+ }
436
+ if (!isGroupValue(groups[key])) {
437
+ throw new TypeError('reader.load: ' + option + '.' + key + ' must be true, false or an array of tag names or ids');
438
+ }
439
+ });
440
+ }
441
+
442
+ function getGroupList() {
443
+ const names = Object.keys(FILTER_GROUPS);
444
+ return names.slice(0, -1).join(', ') + ' and ' + names[names.length - 1];
445
+ }
446
+
447
+ function isGroupValue(value) {
448
+ return value === undefined || typeof value === 'boolean' || (Array.isArray(value) && value.every(isTagSelector));
449
+ }
450
+
451
+ function isTagSelector(selector) {
452
+ return typeof selector === 'string' || typeof selector === 'number';
453
+ }
454
+
455
+ function narrowIncludeTags(includeTags, mapping) {
456
+ const narrowed = {};
457
+ Object.keys(includeTags).forEach((key) => {
458
+ if (!includeTags[key]) {
459
+ return;
460
+ }
461
+ if (mapping[key] === true) {
462
+ narrowed[key] = includeTags[key];
463
+ } else if (mapping[key] !== undefined) {
464
+ narrowed[key] = narrowToDescriptors(includeTags[key], mapping[key]);
465
+ }
466
+ });
467
+ return narrowed;
468
+ }
469
+
470
+ function narrowToDescriptors(value, descriptors) {
471
+ if (!Array.isArray(value)) {
472
+ return getNames(descriptors);
473
+ }
474
+ const selectors = [];
475
+ descriptors.forEach((descriptor) => {
476
+ Array.prototype.push.apply(selectors, getDescriptorSelectors(value, descriptor));
477
+ });
478
+ return selectors;
479
+ }
480
+
481
+ // A name selects every id of that name, so a descriptor selected by name passes
482
+ // on its name and one selected only by id passes on just those ids.
483
+ function getDescriptorSelectors(value, descriptor) {
484
+ const matching = value.filter((selector) => matchesDescriptor(selector, descriptor));
485
+ const selectedByName = matching.some((selector) => typeof selector === 'string');
486
+ return selectedByName ? [descriptor.name] : matching;
487
+ }
488
+
489
+ function matchesDescriptor(selector, descriptor) {
490
+ if (typeof selector === 'number') {
491
+ return descriptor.ids.indexOf(selector) !== -1;
492
+ }
493
+ return typeof selector === 'string' && selector.toLowerCase() === descriptor.name.toLowerCase();
494
+ }
495
+
496
+ function getNames(descriptors) {
497
+ return descriptors.map((descriptor) => descriptor.name);
498
+ }
499
+
500
+ // A narrowed id also matches a tag of that id in another IFD of the group, an
501
+ // unknown tag included, so a group of descriptors returns only their names.
502
+ function getNarrowedTagFilterFactory(mapping) {
503
+ const names = getDescriptorNames(mapping);
504
+ return (options) => {
505
+ const filter = createTagFilter(options);
506
+ return objectAssign({}, filter, {
507
+ shouldReturnTag(groupKey, tagName, tagId) {
508
+ return filter.shouldReturnTag(groupKey, tagName, tagId)
509
+ && (names[groupKey] === undefined || names[groupKey][String(tagName).toLowerCase()] === true);
510
+ },
511
+ });
512
+ };
513
+ }
514
+
515
+ function getDescriptorNames(mapping) {
516
+ const names = Object.create(null);
517
+ for (const key in mapping) {
518
+ if (mapping[key] !== true) {
519
+ names[key] = Object.create(null);
520
+ mapping[key].forEach((descriptor) => {
521
+ names[key][descriptor.name.toLowerCase()] = true;
522
+ });
523
+ }
524
+ }
525
+ return names;
526
+ }