Skip to content

Commit

Permalink
feat: add smallWords feat to module
Browse files Browse the repository at this point in the history
  • Loading branch information
mnater committed Aug 7, 2020
1 parent 91bd926 commit 81479a0
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
11 changes: 11 additions & 0 deletions hyphenopoly.module.js
Expand Up @@ -526,6 +526,16 @@ function createTextHyphenator(lang) {
orphanController
);
}
if (H.c.smallWords > 0) {
const re = RegExp(`(?=(\\s\\S{1,${H.c.smallWords}}\\s))`, "gu");
const matches = [];
tn.replace(re, (orig, $1) => {
matches.push($1);
});
matches.forEach((m) => {
tn = tn.replace(RegExp(`(\\s)${m.slice(1)}`), `$1${m.slice(1, -1)}\u00A0`);
});
}
return tn;
});
}
Expand Down Expand Up @@ -669,6 +679,7 @@ H.config = ((userConfig) => {
["require", []],
["rightmin", 0],
["rightminPerLang", new Map()],
["smallWords", 0],
["substitute", new Map()],
["sync", false]
]));
Expand Down
34 changes: 34 additions & 0 deletions test/configurations.js
Expand Up @@ -345,3 +345,37 @@ t.test("set options: orphanControl", function (t) {
});
t.end();
});

t.test("set options: smallWords", function (t) {
let H9Y = null;
t.beforeEach(function setup(done) {
H9Y = require("../hyphenopoly.module");
done();
});

t.afterEach(function tearDown(done) {
H9Y = null;
delete require.cache[require.resolve("../hyphenopoly.module")];
done();
});

t.test("smallWords: 1", async function (t) {
const hyphenator = await H9Y.config({
"hyphen": "•",
"require": ["en-us"],
"smallWords": 1
});
t.equal(hyphenator("Yesterday I saw a cat."), "Yes•ter•day I\u00A0saw a\u00A0cat.");
t.end();
});
t.test("smallWords: 2", async function (t) {
const hyphenator = await H9Y.config({
"hyphen": "•",
"require": ["en-us"],
"smallWords": 2
});
t.equal(hyphenator("Yesterday I saw a cat in my backyard."), "Yes•ter•day I\u00A0saw a\u00A0cat in\u00A0my\u00A0back•yard.");
t.end();
});
t.end();
});

0 comments on commit 81479a0

Please sign in to comment.