Skip to content
This repository was archived by the owner on Aug 15, 2024. It is now read-only.

Commit 17644e3

Browse files
authored
Merge pull request #39 from eventualbuddha/allow-encoding-in-object
fix: allow encoding to come from options
2 parents 4d1a2f9 + 5e82254 commit 17644e3

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

lib/MemoryFileSystem.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ class MemoryFileSystem {
106106
}
107107
}
108108

109-
readFileSync(_path, encoding) {
109+
readFileSync(_path, optionsOrEncoding) {
110110
const path = pathToArray(_path);
111111
let current = this.data;
112112
let i = 0
@@ -122,6 +122,7 @@ class MemoryFileSystem {
122122
throw new MemoryFileSystemError(errors.code.ENOENT, _path);
123123
}
124124
current = current[path[i]];
125+
const encoding = typeof optionsOrEncoding === "object" ? optionsOrEncoding.encoding : optionsOrEncoding;
125126
return encoding ? current.toString(encoding) : current;
126127
}
127128

@@ -206,8 +207,8 @@ class MemoryFileSystem {
206207
throw new MemoryFileSystemError(errors.code.ENOSYS, _path);
207208
}
208209

209-
writeFileSync(_path, content, encoding) {
210-
if(!content && !encoding) throw new Error("No content");
210+
writeFileSync(_path, content, optionsOrEncoding) {
211+
if(!content && !optionsOrEncoding) throw new Error("No content");
211212
const path = pathToArray(_path);
212213
if(path.length === 0) {
213214
throw new MemoryFileSystemError(errors.code.EISDIR, _path);
@@ -221,7 +222,8 @@ class MemoryFileSystem {
221222
}
222223
if(isDir(current[path[i]]))
223224
throw new MemoryFileSystemError(errors.code.EISDIR, _path);
224-
current[path[i]] = encoding || typeof content === "string" ? new Buffer(content, encoding) : content;
225+
const encoding = typeof optionsOrEncoding === "object" ? optionsOrEncoding.encoding : optionsOrEncoding;
226+
current[path[i]] = optionsOrEncoding || typeof content === "string" ? new Buffer(content, encoding) : content;
225227
return;
226228
}
227229

test/MemoryFileSystem.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ describe("files", function() {
8181
fs.writeFileSync("/test/hello-world.txt", buf);
8282
fs.readFileSync("/test/hello-world.txt").should.be.eql(buf);
8383
fs.readFileSync("/test/hello-world.txt", "utf-8").should.be.eql("Hello World");
84+
fs.readFileSync("/test/hello-world.txt", {encoding: "utf-8"}).should.be.eql("Hello World");
8485
(function() {
8586
fs.readFileSync("/test/other-file");
8687
}).should.throw();
@@ -92,6 +93,8 @@ describe("files", function() {
9293
var stat = fs.statSync("/a");
9394
stat.isFile().should.be.eql(true);
9495
stat.isDirectory().should.be.eql(false);
96+
fs.writeFileSync("/b", "Test", {encoding: "utf-8"});
97+
fs.readFileSync("/b", "utf-8").should.be.eql("Test");
9598
});
9699
});
97100
describe("errors", function() {

0 commit comments

Comments
 (0)