highmark-markdown 0.0.451 → 0.0.453

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.
@@ -4,8 +4,8 @@ import { arrayUtilities } from "necessary";
4
4
 
5
5
  import IndexMatch from "../index/match";
6
6
 
7
- import { EMPTY_STRING, SINGLE_SPACE } from "../constants";
8
7
  import { forEach, mapKeys, mapValues } from "../utilities/object";
8
+ import { Y_STRING, S_STRING, IES_STRING, EMPTY_STRING, SINGLE_SPACE, PARENTHESISED_S_STRING } from "../constants";
9
9
 
10
10
  const { compress } = arrayUtilities;
11
11
 
@@ -14,16 +14,16 @@ export function indexMapFromDivisionMarkdownNodes(divisionMarkdownNodes, context
14
14
 
15
15
  removeIgnoredWords(indexMap, context);
16
16
 
17
- adjustProperNouns(indexMap, context);
18
-
19
- adjustAcronyms(indexMap, context);
20
-
21
17
  adjustMixedPlurals(indexMap, context);
22
18
 
23
19
  adjustPluralPlurals(indexMap, context);
24
20
 
25
21
  adjustSingularPlurals(indexMap, context);
26
22
 
23
+ adjustProperNouns(indexMap, context);
24
+
25
+ adjustAcronyms(indexMap, context);
26
+
27
27
  compressPageNumbers(indexMap);
28
28
 
29
29
  return indexMap;
@@ -145,7 +145,7 @@ function adjustProperNouns(indexMap, context) {
145
145
  function preparePlainText(plainText) {
146
146
  plainText = plainText.toLowerCase(); ///
147
147
 
148
- plainText = plainText.replace(/[^a-z ]/g, SINGLE_SPACE);
148
+ plainText = plainText.replace(/[^a-z0-9 ]/g, SINGLE_SPACE);
149
149
 
150
150
  plainText = plainText.replace(/\s+/g, SINGLE_SPACE);
151
151
 
@@ -184,31 +184,45 @@ function adjustAcronyms(indexMap, context) {
184
184
  function adjustMixedPlurals(indexMap, context) {
185
185
  const { indexOptions } = context,
186
186
  { plurals } = indexOptions,
187
- mixedPlurals = mixedPluralsFromPlurals(plurals);
187
+ mixedPlurals = reducePlurals(plurals, isMixed),
188
+ pluralPlurals = mapPlurals(mixedPlurals, mixedToPlural),
189
+ singularPlurals = mapPlurals(mixedPlurals, mixedToSingular);
188
190
 
189
191
  forEach(indexMap, (wordOrPhrase, pageNumbers) => {
190
- const entryPlural = isPlural(wordOrPhrase);
192
+ const singularPluralsIncludesWordOrPhrase = singularPlurals.includes(wordOrPhrase);
191
193
 
192
- if (entryPlural) {
193
- const singularEntry = wordOrPhrase.replace(/s$/, EMPTY_STRING),
194
- mixedEntry = `${singularEntry}(s)`,
195
- mixedPluralsIncludesMixedEntry = mixedPlurals.includes(mixedEntry),
196
- entryMixedPlural = mixedPluralsIncludesMixedEntry; ///
194
+ if (singularPluralsIncludesWordOrPhrase) {
195
+ const singularWordOrPhrase = wordOrPhrase, ///
196
+ singularPageNumbers = pageNumbers, ///
197
+ mixedWordOrPhrase = singularToMixed(singularWordOrPhrase),
198
+ mixedPageNumbers = indexMap[mixedWordOrPhrase] || [];
197
199
 
198
- if (entryMixedPlural) {
199
- const pluralPageNumbers = pageNumbers, ///
200
- singularPageNumbers = indexMap[singularEntry] || [];
200
+ pageNumbers = [
201
+ ...singularPageNumbers,
202
+ ...mixedPageNumbers
203
+ ];
201
204
 
202
- pageNumbers = [ ///
203
- ...pluralPageNumbers,
204
- ...singularPageNumbers
205
- ];
205
+ delete indexMap[singularWordOrPhrase];
206
+
207
+ indexMap[mixedWordOrPhrase] = pageNumbers;
208
+ }
206
209
 
207
- delete indexMap[wordOrPhrase];
208
- delete indexMap[singularEntry];
210
+ const pluralPluralsIncludesWordOrPhrase = pluralPlurals.includes(wordOrPhrase);
209
211
 
210
- indexMap[mixedEntry] = pageNumbers;
211
- }
212
+ if (pluralPluralsIncludesWordOrPhrase) {
213
+ const pluralWordOrPhrase = wordOrPhrase, ///
214
+ pluralPageNumbers = pageNumbers, ///
215
+ mixedWordOrPhrase = pluralToMixed(pluralWordOrPhrase),
216
+ mixedPageNumbers = indexMap[mixedWordOrPhrase] || [];
217
+
218
+ pageNumbers = [
219
+ ...pluralPageNumbers,
220
+ ...mixedPageNumbers
221
+ ];
222
+
223
+ delete indexMap[pluralWordOrPhrase];
224
+
225
+ indexMap[mixedWordOrPhrase] = pageNumbers;
212
226
  }
213
227
  });
214
228
  }
@@ -216,29 +230,26 @@ function adjustMixedPlurals(indexMap, context) {
216
230
  function adjustPluralPlurals(indexMap, context) {
217
231
  const { indexOptions } = context,
218
232
  { plurals } = indexOptions,
219
- pluralPlurals = pluralPluralsFromPlurals(plurals);
233
+ pluralPlurals = reducePlurals(plurals, isPlural),
234
+ singularPlurals = mapPlurals(pluralPlurals, pluralToSingular);
220
235
 
221
236
  forEach(indexMap, (wordOrPhrase, pageNumbers) => {
222
- const entryPlural = isPlural(wordOrPhrase);
237
+ const singularPluralsIncludesWordOrPhrase = singularPlurals.includes(wordOrPhrase);
223
238
 
224
- if (entryPlural) {
225
- const singularEntry = wordOrPhrase.replace(/s$/, EMPTY_STRING),
226
- pluralPluralsIncludesEntry = pluralPlurals.includes(wordOrPhrase),
227
- entryPluralPlural = pluralPluralsIncludesEntry; ///
239
+ if (singularPluralsIncludesWordOrPhrase) {
240
+ const singularWordOrPhrase = wordOrPhrase, ///
241
+ singularPageNumbers = pageNumbers, ///
242
+ pluralWordOrPhrase = singularToPlural(wordOrPhrase),
243
+ pluralPageNumbers = indexMap[pluralWordOrPhrase] || [];
228
244
 
229
- if (entryPluralPlural) {
230
- const pluralPageNumbers = pageNumbers, ///
231
- singularPageNumbers = indexMap[singularEntry] || [];
232
-
233
- pageNumbers = [ ///
234
- ...pluralPageNumbers,
235
- ...singularPageNumbers
236
- ];
245
+ pageNumbers = [
246
+ ...singularPageNumbers,
247
+ ...pluralPageNumbers
248
+ ];
237
249
 
238
- delete indexMap[singularEntry];
250
+ delete indexMap[singularWordOrPhrase];
239
251
 
240
- indexMap[wordOrPhrase] = pageNumbers;
241
- }
252
+ indexMap[pluralWordOrPhrase] = pageNumbers;
242
253
  }
243
254
  });
244
255
  }
@@ -246,98 +257,113 @@ function adjustPluralPlurals(indexMap, context) {
246
257
  function adjustSingularPlurals(indexMap, context) {
247
258
  const { indexOptions } = context,
248
259
  { plurals } = indexOptions,
249
- singularPlurals = singularPluralsFromPlurals(plurals);
260
+ singularPlurals = reducePlurals(plurals, isSingular),
261
+ pluralPlurals = mapPlurals(singularPlurals, singularToPlural);
250
262
 
251
263
  forEach(indexMap, (wordOrPhrase, pageNumbers) => {
252
- const entryPlural = isPlural(wordOrPhrase);
253
-
254
- if (entryPlural) {
255
- const singularEntry = wordOrPhrase.replace(/s$/, EMPTY_STRING),
256
- singularPluralsIncludesSingularEntry = singularPlurals.includes(singularEntry),
257
- entrySingularPlural = singularPluralsIncludesSingularEntry; ///
264
+ const pluralPluralsIncludesWordOrPhrase = pluralPlurals.includes(wordOrPhrase);
258
265
 
259
- if (entrySingularPlural) {
260
- const pluralPageNumbers = pageNumbers, ///
261
- singularPageNumbers = indexMap[singularEntry] || [];
266
+ if (pluralPluralsIncludesWordOrPhrase) {
267
+ const singularWordOrPhrase = pluralToSingular(wordOrPhrase),
268
+ singularPageNumbers = indexMap[singularWordOrPhrase] || [],
269
+ pluralWordOrPhrase = wordOrPhrase, ///
270
+ pluralPageNumbers = pageNumbers; ///
262
271
 
263
- pageNumbers = [ ///
264
- ...pluralPageNumbers,
265
- ...singularPageNumbers
272
+ pageNumbers = [
273
+ ...singularPageNumbers,
274
+ ...pluralPageNumbers
266
275
  ];
267
276
 
268
- delete indexMap[wordOrPhrase];
277
+ delete indexMap[pluralWordOrPhrase];
269
278
 
270
- indexMap[singularEntry] = pageNumbers;
279
+ indexMap[singularWordOrPhrase] = pageNumbers;
271
280
  }
272
- }
273
281
  });
274
282
  }
275
283
 
276
- function singularPluralsFromPlurals(plurals) {
277
- const singularPlurals = plurals.reduce((singularPlurals, plural) => {
278
- const pluralSingular = isSingular(plural);
284
+ function reducePlurals(plurals, callback) {
285
+ plurals = plurals.reduce((plurals, plural) => { ///
286
+ const result = callback(plural);
279
287
 
280
- if (pluralSingular) {
281
- const singularPlural = plural; ///
282
-
283
- singularPlurals.push(singularPlural);
288
+ if (result) {
289
+ plurals.push(plural);
284
290
  }
285
291
 
286
- return singularPlurals;
292
+ return plurals;
287
293
  }, []);
288
294
 
289
- return singularPlurals;
295
+ return plurals;
290
296
  }
291
297
 
292
- function pluralPluralsFromPlurals(plurals) {
293
- const pluralPlurals = plurals.reduce((pluralPlurals, plural) => {
294
- const pluralPlural = isPlural(plural);
298
+ function mapPlurals(plurals, callback) {
299
+ plurals = plurals.map((plural) => { ///
300
+ plural = callback(plural);
295
301
 
296
- if (pluralPlural) {
297
- const pluralPlural = plural; ///
302
+ return plural;
303
+ });
298
304
 
299
- pluralPlurals.push(pluralPlural);
300
- }
305
+ return plurals;
306
+ }
301
307
 
302
- return pluralPlurals;
303
- }, []);
308
+ function isSingular(text) {
309
+ const indexMatches = /[^s)]$/.test(text),
310
+ singular = indexMatches; ///
304
311
 
305
- return pluralPlurals;
312
+ return singular;
306
313
  }
307
314
 
308
- function mixedPluralsFromPlurals(plurals) {
309
- const mixedPlurals = plurals.reduce((mixedPlurals, plural) => {
310
- const pluralMixed = isMixed(plural);
315
+ function isPlural(text) {
316
+ const indexMatches = /(ies|s)$/.test(text),
317
+ plural = indexMatches; ///
311
318
 
312
- if (pluralMixed) {
313
- const mixedPlural = plural; ///
319
+ return plural;
320
+ }
314
321
 
315
- mixedPlurals.push(mixedPlural);
316
- }
322
+ function isMixed(text) {
323
+ const indexMatches = /\(s\)$/.test(text),
324
+ mixed = indexMatches; ///
317
325
 
318
- return mixedPlurals;
319
- }, []);
326
+ return mixed;
327
+ }
320
328
 
321
- return mixedPlurals;
329
+ function mixedToPlural(text) {
330
+ text = text ///
331
+ .replace(/\(s\)$/, S_STRING);
332
+
333
+ return text;
322
334
  }
323
335
 
324
- function isSingular(text) {
325
- const indexMatches = /[^s)]$/.test(text),
326
- pluralSingular = indexMatches; ///
336
+ function pluralToMixed(text) {
337
+ text = text ///
338
+ .replace(/s$/, PARENTHESISED_S_STRING)
327
339
 
328
- return pluralSingular;
340
+ return text;
329
341
  }
330
342
 
331
- function isPlural(text) {
332
- const indexMatches = /s$/.test(text),
333
- plural = indexMatches; ///
343
+ function mixedToSingular(text) {
344
+ text = text ///
345
+ .replace(/\(s\)$/, EMPTY_STRING);
334
346
 
335
- return plural;
347
+ return text;
336
348
  }
337
349
 
338
- function isMixed(text) {
339
- const indexMatches = /\(s\)$/.test(text),
340
- plural = indexMatches; ///
350
+ function singularToMixed(text) {
351
+ text = `${text}(s)`;
341
352
 
342
- return plural;
353
+ return text;
354
+ }
355
+
356
+ function singularToPlural(text) {
357
+ text = `${text}s` ///
358
+ .replace(/ys$/, IES_STRING);
359
+
360
+ return text;
361
+ }
362
+
363
+ function pluralToSingular(text) {
364
+ text = text ///
365
+ .replace(/ies$/, Y_STRING)
366
+ .replace(/s$/, EMPTY_STRING);
367
+
368
+ return text;
343
369
  }