@dinvader/autonbsp 0.1.2 → 0.1.4

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
@@ -1,3 +1,111 @@
1
1
  # Auto NBSP
2
2
 
3
- > TODO: write readme
3
+ Automatically replace whitespace in string with non-breaking spaces (`\u00A0` or ` `) to prevent hanging articles, prepositions and digits.
4
+
5
+ **Important:** this package does not parse or traverse AST. Using it with raw HTML or Markdown strings will likely break your content or markup.
6
+
7
+ For rich Markdown content use [remark-plugin-autonbsp](https://www.npmjs.com/package/remark-plugin-autonbsp).
8
+
9
+ ## Installation
10
+
11
+ Install with npm or any other package manager:
12
+
13
+ ```sh
14
+ npm install @dinvader/autonbsp
15
+ ```
16
+
17
+ ```sh
18
+ yarn add @dinvader/autonbsp
19
+ ```
20
+
21
+
22
+ ## Usage
23
+
24
+ Basic example:
25
+
26
+ ```ts
27
+ import { autoNBSP } from '@dinvader/autonbsp';
28
+
29
+ const inputText = 'An example string with 6 tokens.';
30
+
31
+ const result = autoNBSP(
32
+ inputText,
33
+ {
34
+ mode: 'html',
35
+ afterDigits: true,
36
+ prepositions: ['an', 'with'],
37
+ }
38
+ );
39
+
40
+ console.log(result);
41
+ // -> "An example string with 6 tokens.";
42
+ ```
43
+
44
+ There are two config presets available for Russian and English languages. You can extend them for your needs.
45
+
46
+ ```ts
47
+ import { autoNBSP } from '@dinvader/autonbsp';
48
+ import nbspEn from '@dinvader/autonbsp/presets/en';
49
+ import nbspRu from '@dinvader/autonbsp/presets/ru';
50
+
51
+ console.log(autoNBSP('5 items in stock', nbspEn));
52
+ // -> "5\u00A0items in\u00A0stock"
53
+
54
+ console.log(autoNBSP('5 items in stock', { ...nbspEn, prepositions: '', mode: 'html' }));
55
+ // -> "5 items in stock"
56
+
57
+ console.log(autoNBSP('5 шт. на складе', nbspEn));
58
+ // -> "5\u00A0шт. на\u00A0складе"
59
+ ```
60
+
61
+ ## Configuration options
62
+
63
+ ### `mode?: 'utf' | 'html'`
64
+
65
+ Defines the replacement used for matched whitespace.
66
+
67
+ - `'utf'` replaces whitespace with the Unicode non-breaking space character `'\u00A0'` or ` `.
68
+ - `'html'` replaces whitespace with the HTML entity string `' '`.
69
+
70
+ Default value is `'utf'`.
71
+
72
+ ### `betweenDigits?: boolean`
73
+
74
+ Replace runs of whitespace that occur between two adjacent digits.
75
+
76
+ Examples:
77
+ - `"123 456"` → `"123\u00A0456"`
78
+ - `"1\t 2"` → `"1\u00A02"`
79
+
80
+ `false` by default.
81
+
82
+ ### `afterDigits?: boolean | 'before-letter'`
83
+
84
+
85
+ Replace runs of whitespace that occur immediately after a digit.
86
+
87
+ - `true` replaces whitespace when followed by any non-digit character.
88
+ - `'before-letter'` replaces whitespace only when followed by a Unicode letter.
89
+
90
+ Examples:
91
+
92
+ - `"2 pieces"` → `"2\u00A0pieces"`
93
+ - `"5 %"` → `"5\u00A0%"`
94
+ - `"5 %"` → `"5 %"` with `'before-letter'` option
95
+
96
+ `false` by default.
97
+
98
+ ### `prepositions?: string | string[];`
99
+
100
+ Replace runs of whitespace that occur after specified whole-word prepositions.
101
+
102
+ Prepositions may be provided as:
103
+
104
+ - an array of strings (e.g. `['a', 'in', 'for']`)
105
+ - a pipe-separated regular expression pattern (e.g. `'a|in|for'`)
106
+
107
+ Matching is case-insensitive and respects word boundaries.
108
+
109
+ ## License
110
+
111
+ [MIT](LICENSE) © [Mikhail Panichev](https://mpanichev.ru)
@@ -2,4 +2,4 @@ import { AutoNBSPConfig } from './types';
2
2
  /**
3
3
  * Replaces runs of whitespace with non-breaking spaces according to configurable rules.
4
4
  */
5
- export declare const autoNBSP: (text: string, config?: AutoNBSPConfig) => string;
5
+ export declare const autoNBSP: (text: string, config: AutoNBSPConfig) => string;
package/dist/autonbsp.js CHANGED
@@ -2,7 +2,7 @@ const l = {
2
2
  html: " ",
3
3
  utf: " "
4
4
  }, d = function(i, n) {
5
- const { mode: o = "utf", betweenDigits: a = !1, afterDigits: r = !1, prepositions: t } = n || {}, s = l[o];
5
+ const { mode: o = "utf", betweenDigits: a = !1, afterDigits: r = !1, prepositions: t } = n, s = l[o];
6
6
  let e = i;
7
7
  if (t) {
8
8
  const p = Array.isArray(t) ? t.join("|") : t, c = new RegExp(`(?<=\\s|^)(${p})\\s+`, "giu");
package/dist/types.d.ts CHANGED
@@ -41,5 +41,5 @@ export interface AutoNBSPConfig {
41
41
  *
42
42
  * Matching is case-insensitive and respects word boundaries.
43
43
  */
44
- prepositions?: string | string[];
44
+ prepositions?: string | string[] | readonly string[];
45
45
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@dinvader/autonbsp",
3
3
  "private": false,
4
- "version": "0.1.2",
4
+ "version": "0.1.4",
5
5
  "license": "MIT",
6
6
  "author": "Mikhail Panichev <denis.invader@gmail.com>",
7
7
  "repository": "github:denisinvader/autonbsp",