Skip to content

Commit

Permalink
Merge branch 'module' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
mnater committed Mar 28, 2019
2 parents 0226d03 + 7abf3ac commit 6f7129a
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 23 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
### Added
* feat: tearDown event (issue #67)
* feat: loadError event (issue #59)
* feat: hyphenopoly.module is now browserifyable

### Removed
* BREAKING CHANGE: remove `Hyphenopoly.setup.classnames` – use [selectors](https://github.com/mnater/Hyphenopoly/wiki/Global-Hyphenopoly-Object#new-with-version-260-selectors) instead
Expand Down
20 changes: 13 additions & 7 deletions exampleSync.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
/* eslint-env node */
/* eslint no-console: 0 */
"use strict";
// For RunKit:
const hyphenopoly = require("hyphenopoly");

// For local node:
// const hyphenopoly = require("./hyphenopoly.module.js");
/*
* For local node:
* const hyphenopoly = require("./hyphenopoly.module.js");
*/

const hyphenator = hyphenopoly.config({
"sync": true,
"require": ["de", "en-us"],
"hyphen": "•",
"exceptions": {
"en-us": "en-han-ces"
}
},
"hyphen": "•",
"require": ["de", "en-us"],
"sync": true
});

const hy1 = hyphenator.get("en-us")("hyphenation enhances justification.");
const hy2 = hyphenator.get("de")("Silbentrennung verbessert den Blocksatz.");


console.log(hy1);
console.log(hy2);
console.log(hy2);
57 changes: 41 additions & 16 deletions hyphenopoly.module.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@
/* eslint no-console: 0 */
"use strict";

const fs = require("fs");
/*
* Use 'fs' in node environment and fallback to http if the module gets executed
* in a browser environment (e.g. browserified)
*/
let loader = require("fs");

const {StringDecoder} = require("string_decoder");

Expand All @@ -22,7 +26,6 @@ const decode = (function makeDecoder() {
};
}());


/**
* Create Object without standard Object-prototype
* @returns {Object} empty object
Expand Down Expand Up @@ -115,7 +118,6 @@ H.supportedLanguages = [
"rm",
"ro",
"ru",
"sa",
"sh-cyrl",
"sh-latn",
"sk",
Expand All @@ -135,13 +137,31 @@ H.supportedLanguages = [
* Read a file and call callback
* Use "fs" (node) or "http" (browser)
* @param {string} file - the filename
* @param {function} cb - callback function
* @param {function} cb - callback function with args (error, data)
* @returns {undefined}
*/
function readFile(file, cb) {
/* eslint-disable security/detect-non-literal-fs-filename */
fs.readFile(file, cb);
/* eslint-enable security/detect-non-literal-fs-filename */
function readFile(file, cb, sync) {
if (H.c.loader === "fs") {
/* eslint-disable security/detect-non-literal-fs-filename */
if (sync) {
// eslint-disable-next-line no-sync
return loader.readFileSync(file);
}
loader.readFile(file, cb);
/* eslint-enable security/detect-non-literal-fs-filename */
} else {
console.log(file);
loader.get(file, function onData(res) {
const rawData = [];
res.on("data", function onChunk(chunk) {
rawData.push(chunk);
});
res.on("end", function onEnd() {
cb(null, Buffer.concat(rawData));
});
});
}
return null;
}

/**
Expand All @@ -150,9 +170,9 @@ function readFile(file, cb) {
*/
function loadWasm() {
if (H.c.sync) {
/* eslint-disable security/detect-non-literal-fs-filename, no-sync */
const data = fs.readFileSync(`${H.c.paths.maindir}hyphenEngine.wasm`);
/* eslint-enable security/detect-non-literal-fs-filename, no-sync */
/* eslint-disable security/detect-non-literal-fs-filename */
const data = readFile(`${H.c.paths.maindir}hyphenEngine.wasm`, null, true);
/* eslint-enable security/detect-non-literal-fs-filename */
H.binaries.set("hyphenEngine", new Uint8Array(data).buffer);
H.events.dispatch("engineLoaded");
} else {
Expand All @@ -168,7 +188,8 @@ function loadWasm() {
H.binaries.set("hyphenEngine", new Uint8Array(data).buffer);
H.events.dispatch("engineLoaded");
}
}
},
false
);
}
}
Expand All @@ -180,9 +201,7 @@ function loadWasm() {
*/
function loadHpb(lang) {
if (H.c.sync) {
/* eslint-disable security/detect-non-literal-fs-filename, no-sync */
const data = fs.readFileSync(`${H.c.paths.patterndir}${lang}.hpb`);
/* eslint-enable security/detect-non-literal-fs-filename, no-sync */
const data = readFile(`${H.c.paths.patterndir}${lang}.hpb`, null, true);
H.binaries.set(lang, new Uint8Array(data).buffer);
H.events.dispatch("hpbLoaded", {"msg": lang});
} else {
Expand All @@ -198,7 +217,8 @@ function loadHpb(lang) {
H.binaries.set(lang, new Uint8Array(data).buffer);
H.events.dispatch("hpbLoaded", {"msg": lang});
}
}
},
false
);
}
}
Expand Down Expand Up @@ -773,6 +793,7 @@ H.config = function config(userConfig) {
"exceptions": setProp(empty(), 2),
"hyphen": setProp(String.fromCharCode(173), 2),
"leftmin": setProp(0, 2),
"loader": setProp("fs", 2),
"minWordLength": setProp(6, 2),
"normalize": setProp(false, 2),
"orphanControl": setProp(1, 2),
Expand All @@ -795,6 +816,10 @@ H.config = function config(userConfig) {
);
});
H.c = settings;
if (H.c.loader !== "fs") {
// eslint-disable-next-line global-require
loader = require("http");
}
if (H.c.handleEvent) {
Object.keys(H.c.handleEvent).forEach(function add(name) {
/* eslint-disable security/detect-object-injection */
Expand Down
Binary file removed patterns/cop.hpb
Binary file not shown.
Binary file removed patterns/la-medi.hpb
Binary file not shown.
40 changes: 40 additions & 0 deletions test/http.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/* eslint-env node */
/* eslint global-require: 0, func-names: 0, no-shadow: 0 */
"use strict";
const t = require("tap");

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("run config with one language", async function (t) {
const deHyphenator = await H9Y.config({
"loader": "http",
"paths": {
"maindir": "http://mnater.github.io/Hyphenopoly/",
"patterndir": "http://mnater.github.io/Hyphenopoly/patterns/"
},
"require": ["de"]
});
t.test("return a function", function (t) {
t.equal(typeof deHyphenator, "function", typeof deHyphenator);
t.end();
});
t.test("hyphenate one word", function (t) {
t.equal(deHyphenator("Silbentrennung"), "Sil\u00ADben\u00ADtren\u00ADnung", deHyphenator("Silbentrennung"));
t.end();
});
t.test("hyphenate two words", function (t) {
t.equal(deHyphenator("Silbentrennung Algorithmus"), "Sil\u00ADben\u00ADtren\u00ADnung Al\u00ADgo\u00ADrith\u00ADmus", deHyphenator("Silbentrennung Algorithmus"));
t.end();
});
t.end();
});

0 comments on commit 6f7129a

Please sign in to comment.