aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2015-09-06 17:35:10 +0100
committerMichael Williamson <mike@zwobble.org>2015-09-06 17:35:10 +0100
commitcccaa16098484a7d9df26c18c0b6aa5b9defbeb5 (patch)
tree2df9fc9050639651315d29ec24b2b65f9ed55d07
parente16404b95f86cd0d938f1f9ebc4bacd7a5aa7042 (diff)
Extract uriToPath
-rw-r--r--lib/docx/files.js19
-rw-r--r--tests/docx/files.tests.js8
2 files changed, 21 insertions, 6 deletions
diff --git a/lib/docx/files.js b/lib/docx/files.js
index 6596a01..6e6f797 100644
--- a/lib/docx/files.js
+++ b/lib/docx/files.js
@@ -4,15 +4,12 @@ var promises = require("../promises");
exports.Files = Files;
+exports.uriToPath = uriToPath;
function Files() {
- function read(path, encoding) {
- // TODO: check whether this works for Windows file URIs
- var fileUriPrefix = "file:///";
- if (startsWith(path, fileUriPrefix)) {
- return readFile(path.slice(fileUriPrefix.length - 1), encoding);
- }
+ function read(uri, encoding) {
+ return readFile(uriToPath(uri), encoding);
}
return {
@@ -22,6 +19,16 @@ function Files() {
var readFile = promises.promisify(fs.readFile.bind(fs));
+
+function uriToPath(uri) {
+ var fileUriPrefix = "file:///";
+ if (startsWith(uri, fileUriPrefix)) {
+ return uri.slice(fileUriPrefix.length - 1);
+ } else {
+ throw new Error("Could not convert URI to path: " + uri);
+ }
+}
+
function startsWith(string, search) {
return string.indexOf(search) === 0;
}
diff --git a/tests/docx/files.tests.js b/tests/docx/files.tests.js
index d3f3391..fecc6eb 100644
--- a/tests/docx/files.tests.js
+++ b/tests/docx/files.tests.js
@@ -4,6 +4,7 @@ var assert = require("assert");
var promises = require("../../lib/promises");
var Files = require("../../lib/docx/files").Files;
+var uriToPath = require("../../lib/docx/files").uriToPath;
var testing = require("../testing");
var test = testing.test;
@@ -22,3 +23,10 @@ describe("Files", function() {
});
});
});
+
+
+describe("uriToPath", function() {
+ test("leading slash is retained on non-Windows file URIs", function() {
+ assert.equal(uriToPath("file:///a/b/c"), "/a/b/c");
+ });
+});