@lunarisapp/hyphen 0.0.6 → 1.1.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.
package/README.md CHANGED
@@ -2,15 +2,22 @@
2
2
 
3
3
  [![NPM Version](https://img.shields.io/npm/v/%40lunarisapp%2Fhyphen)](https://www.npmjs.com/package/@lunarisapp/hyphen)
4
4
  [![NPM Downloads](https://img.shields.io/npm/dm/%40lunarisapp%2Fhyphen)](https://www.npmjs.com/package/@lunarisapp/hyphen)
5
- [![CI/CD Status](https://img.shields.io/github/actions/workflow/status/LunarisApp/text-tools/checks.yml?label=CI%2FCD)](https://github.com/LunarisApp/text-tools/actions/workflows/checks.yml)
5
+ [![CI Status](https://img.shields.io/github/actions/workflow/status/LunarisApp/text-tools/checks.yml?label=CI)](https://github.com/LunarisApp/text-tools/actions/workflows/checks.yml)
6
6
 
7
7
  A hyphenation library based on [LibreOffice dictionaries](https://git.libreoffice.org/dictionaries), inspired by [pyphen](https://github.com/Kozea/Pyphen).
8
8
 
9
9
  > [!TIP]
10
10
  > See also:
11
+ > - [@lunarisapp/language](https://github.com/LunarisApp/text-tools/tree/main/packages/language) for core linguistic breakdown: vowels, consonants, words, and sentences.
11
12
  > - [@lunarisapp/stats](https://github.com/LunarisApp/text-tools/tree/main/packages/stats) for text statistics (e.g. word count, sentence count, etc.).
12
13
  > - [@lunarisapp/readability](https://github.com/LunarisApp/text-tools/tree/main/packages/readability) for text readability scores (e.g. Flesch Reading Ease).
13
14
 
15
+ ## Features
16
+
17
+ - Browser & Node.js support
18
+ - Multi-language support
19
+ - TypeScript support
20
+
14
21
  ## Installation
15
22
 
16
23
  ```bash
@@ -23,9 +30,9 @@ npm install @lunarisapp/hyphen
23
30
  import { TextHyphen } from '@lunarisapp/hyphen';
24
31
 
25
32
  const textHyphen = new TextHyphen({
26
- lang: 'en_US',
27
- left: 2,
28
- right: 2,
33
+ lang: 'en_US', // optional, en_US by default
34
+ left: 2, // optional, 2 by default
35
+ right: 2, // optional, 2 by default
29
36
  });
30
37
 
31
38
  // Gets the textHyphenation points for a word.
package/dist/index.d.mts CHANGED
@@ -5,6 +5,10 @@ declare const beBy: HyphenDict;
5
5
  declare const bgBG: HyphenDict;
6
6
  declare const ca: HyphenDict;
7
7
  declare const daDK: HyphenDict;
8
+ declare const deAT: HyphenDict;
9
+ /**
10
+ * @deprecated Use `deAT` instead.
11
+ */
8
12
  declare const deDT: HyphenDict;
9
13
  declare const deCH: HyphenDict;
10
14
  declare const deDE: HyphenDict;
@@ -101,12 +105,13 @@ type Language = keyof typeof dictionaries;
101
105
  declare class TextHyphen {
102
106
  private readonly left;
103
107
  private readonly right;
104
- private hd;
108
+ private readonly hd;
105
109
  constructor(props?: {
106
110
  lang?: Language;
107
111
  left?: number;
108
112
  right?: number;
109
113
  });
114
+ private filteredPositions;
110
115
  /**
111
116
  * Get the positions of possible hyphenation points in a word
112
117
  * @param word
@@ -137,4 +142,4 @@ declare class TextHyphen {
137
142
  inserted(word: string, hyphen?: string): string;
138
143
  }
139
144
 
140
- export { type Language, TextHyphen, afZa, asIn, beBy, bgBG, ca, daDK, deCH, deDE, deDT, dictionaries, enGB, enUS, eo, es, etEE, fr, gl, hrHR, huHU, idID, is, itIT, knIN, lt, lvLV, mnMN, mrIN, nbNO, nlNL, nnNO, paIN, plPL, ptBR, ptPT, roRO, ruRU, saIN, skSK, slSI, sqAL, sr, srLatn, sv, teIN, thTH, ukUA, zuZA };
145
+ export { type Language, TextHyphen, afZa, asIn, beBy, bgBG, ca, daDK, deAT, deCH, deDE, deDT, dictionaries, enGB, enUS, eo, es, etEE, fr, gl, hrHR, huHU, idID, is, itIT, knIN, lt, lvLV, mnMN, mrIN, nbNO, nlNL, nnNO, paIN, plPL, ptBR, ptPT, roRO, ruRU, saIN, skSK, slSI, sqAL, sr, srLatn, sv, teIN, thTH, ukUA, zuZA };
package/dist/index.d.ts CHANGED
@@ -5,6 +5,10 @@ declare const beBy: HyphenDict;
5
5
  declare const bgBG: HyphenDict;
6
6
  declare const ca: HyphenDict;
7
7
  declare const daDK: HyphenDict;
8
+ declare const deAT: HyphenDict;
9
+ /**
10
+ * @deprecated Use `deAT` instead.
11
+ */
8
12
  declare const deDT: HyphenDict;
9
13
  declare const deCH: HyphenDict;
10
14
  declare const deDE: HyphenDict;
@@ -101,12 +105,13 @@ type Language = keyof typeof dictionaries;
101
105
  declare class TextHyphen {
102
106
  private readonly left;
103
107
  private readonly right;
104
- private hd;
108
+ private readonly hd;
105
109
  constructor(props?: {
106
110
  lang?: Language;
107
111
  left?: number;
108
112
  right?: number;
109
113
  });
114
+ private filteredPositions;
110
115
  /**
111
116
  * Get the positions of possible hyphenation points in a word
112
117
  * @param word
@@ -137,4 +142,4 @@ declare class TextHyphen {
137
142
  inserted(word: string, hyphen?: string): string;
138
143
  }
139
144
 
140
- export { type Language, TextHyphen, afZa, asIn, beBy, bgBG, ca, daDK, deCH, deDE, deDT, dictionaries, enGB, enUS, eo, es, etEE, fr, gl, hrHR, huHU, idID, is, itIT, knIN, lt, lvLV, mnMN, mrIN, nbNO, nlNL, nnNO, paIN, plPL, ptBR, ptPT, roRO, ruRU, saIN, skSK, slSI, sqAL, sr, srLatn, sv, teIN, thTH, ukUA, zuZA };
145
+ export { type Language, TextHyphen, afZa, asIn, beBy, bgBG, ca, daDK, deAT, deCH, deDE, deDT, dictionaries, enGB, enUS, eo, es, etEE, fr, gl, hrHR, huHU, idID, is, itIT, knIN, lt, lvLV, mnMN, mrIN, nbNO, nlNL, nnNO, paIN, plPL, ptBR, ptPT, roRO, ruRU, saIN, skSK, slSI, sqAL, sr, srLatn, sv, teIN, thTH, ukUA, zuZA };
package/dist/index.js CHANGED
@@ -27,6 +27,7 @@ __export(index_exports, {
27
27
  bgBG: () => bgBG,
28
28
  ca: () => ca,
29
29
  daDK: () => daDK,
30
+ deAT: () => deAT,
30
31
  deCH: () => deCH,
31
32
  deDE: () => deDE,
32
33
  deDT: () => deDT,
@@ -239254,6 +239255,7 @@ var beBy = hyph_be_BY_default;
239254
239255
  var bgBG = hyph_bg_BG_default;
239255
239256
  var ca = hyph_ca_default;
239256
239257
  var daDK = hyph_da_DK_default;
239258
+ var deAT = hyph_de_AT_default;
239257
239259
  var deDT = hyph_de_AT_default;
239258
239260
  var deCH = hyph_de_CH_default;
239259
239261
  var deDE = hyph_de_DE_default;
@@ -239369,50 +239371,61 @@ var HyphenDictParser = class {
239369
239371
  this.cache = /* @__PURE__ */ new Map();
239370
239372
  this.maxLen = 0;
239371
239373
  const lines = content.split("\n");
239372
- lines.forEach((line) => {
239373
- line = line.trim();
239374
- if (!line || IGNORED.some((i) => line.startsWith(i))) return;
239375
- const pattern = line.replace(
239374
+ for (const line of lines) {
239375
+ const trimmedLine = line.trim();
239376
+ if (!trimmedLine || IGNORED.some((i) => trimmedLine.startsWith(i))) {
239377
+ continue;
239378
+ }
239379
+ const pattern = trimmedLine.replace(
239376
239380
  /\^{2}([0-9a-f]{2})/g,
239377
- (match, hex) => String.fromCharCode(parseInt(hex, 16))
239381
+ (_match, hex) => String.fromCharCode(Number.parseInt(hex, 16))
239378
239382
  );
239379
239383
  const parts = Array.from(pattern.matchAll(/(\d?)(\D?)/g));
239380
239384
  const tags = [];
239381
239385
  const values = [];
239382
- parts.forEach(([, num, char]) => {
239383
- tags.push(char);
239384
- values.push(num ? parseInt(num) : 0);
239385
- });
239386
- if (Math.max(...values) === 0) return;
239386
+ for (const [, num, char] of parts) {
239387
+ tags.push(char ?? "");
239388
+ values.push(num ? Number.parseInt(num, 10) : 0);
239389
+ }
239390
+ if (Math.max(...values) === 0) {
239391
+ continue;
239392
+ }
239387
239393
  let start = 0, end = values.length;
239388
- while (!values[start]) start++;
239389
- while (!values[end - 1]) end--;
239394
+ while (!values[start]) {
239395
+ start++;
239396
+ }
239397
+ while (!values[end - 1]) {
239398
+ end--;
239399
+ }
239390
239400
  this.patterns.set(tags.join(""), [start, values.slice(start, end)]);
239391
- });
239392
- this.maxLen = Math.max(
239393
- ...Array.from(this.patterns.keys()).map((k) => k.length)
239394
- );
239401
+ }
239402
+ const lengths = Array.from(this.patterns.keys()).map((k) => k.length);
239403
+ this.maxLen = lengths.length > 0 ? Math.max(...lengths) : 0;
239395
239404
  }
239396
239405
  positions(word) {
239397
- word = word.toLowerCase();
239398
- if (this.cache.has(word)) return this.cache.get(word);
239399
- const references = new Array(word.length + 2).fill(0);
239400
- const extendedWord = `.${word}.`;
239406
+ const lowerWord = word.toLowerCase();
239407
+ if (this.cache.has(lowerWord)) {
239408
+ return this.cache.get(lowerWord);
239409
+ }
239410
+ const references = new Array(lowerWord.length + 2).fill(0);
239411
+ const extendedWord = `.${lowerWord}.`;
239401
239412
  for (let i = 0; i < extendedWord.length - 1; i++) {
239402
239413
  for (let j = i + 1; j < Math.min(i + this.maxLen, extendedWord.length) + 1; j++) {
239403
239414
  const pattern = this.patterns.get(extendedWord.slice(i, j));
239404
- if (!pattern) continue;
239415
+ if (!pattern) {
239416
+ continue;
239417
+ }
239405
239418
  const [offset, values] = pattern;
239406
- values.forEach((val, idx) => {
239419
+ for (let idx = 0; idx < values.length; idx++) {
239407
239420
  references[i + offset + idx] = Math.max(
239408
- val,
239421
+ values[idx],
239409
239422
  references[i + offset + idx]
239410
239423
  );
239411
- });
239424
+ }
239412
239425
  }
239413
239426
  }
239414
239427
  const positions = references.map((val, idx) => val % 2 ? new DataInt(idx - 1) : null).filter(Boolean);
239415
- this.cache.set(word, positions);
239428
+ this.cache.set(lowerWord, positions);
239416
239429
  return positions;
239417
239430
  }
239418
239431
  };
@@ -239424,20 +239437,23 @@ var TextHyphen = class {
239424
239437
  const dict = dictionaries[lang] || enUS;
239425
239438
  this.hd = new HyphenDictParser(dict);
239426
239439
  }
239440
+ filteredPositions(word) {
239441
+ const rightLimit = word.length - this.right;
239442
+ return this.hd.positions(word).filter((pos) => this.left <= pos.value && pos.value <= rightLimit);
239443
+ }
239427
239444
  /**
239428
239445
  * Get the positions of possible hyphenation points in a word
239429
239446
  * @param word
239430
239447
  */
239431
239448
  positions(word) {
239432
- const rightLimit = word.length - this.right;
239433
- return this.hd.positions(word).map((pos) => pos.value).filter((pos) => this.left <= pos && pos <= rightLimit);
239449
+ return this.filteredPositions(word).map((pos) => pos.value);
239434
239450
  }
239435
239451
  /**
239436
239452
  * Get iterator for all possible variants of hyphenating the word.
239437
239453
  * @param word
239438
239454
  */
239439
239455
  *iterate(word) {
239440
- for (const position of this.hd.positions(word).reverse()) {
239456
+ for (const position of this.filteredPositions(word).reverse()) {
239441
239457
  if (position.data) {
239442
239458
  const [change, index, cut] = position.data;
239443
239459
  const updatedIndex = index + position.value;
@@ -239469,9 +239485,9 @@ var TextHyphen = class {
239469
239485
  * @param hyphen
239470
239486
  */
239471
239487
  wrap(word, width, hyphen = "-") {
239472
- width -= hyphen.length;
239488
+ const effectiveWidth = width - hyphen.length;
239473
239489
  for (const [w1, w2] of this.iterate(word)) {
239474
- if (w1.length <= width) {
239490
+ if (w1.length <= effectiveWidth) {
239475
239491
  return [w1 + hyphen, w2];
239476
239492
  }
239477
239493
  }
@@ -239484,9 +239500,9 @@ var TextHyphen = class {
239484
239500
  */
239485
239501
  inserted(word, hyphen = "-") {
239486
239502
  const letters = [...word];
239487
- this.positions(word).reverse().forEach((pos) => {
239503
+ for (const pos of this.positions(word).reverse()) {
239488
239504
  letters.splice(pos, 0, hyphen);
239489
- });
239505
+ }
239490
239506
  return letters.join("");
239491
239507
  }
239492
239508
  };
@@ -239499,6 +239515,7 @@ var TextHyphen = class {
239499
239515
  bgBG,
239500
239516
  ca,
239501
239517
  daDK,
239518
+ deAT,
239502
239519
  deCH,
239503
239520
  deDE,
239504
239521
  deDT,