Skip to content

Commit

Permalink
Quick proof-of-concept for encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
phated committed Jun 13, 2017
1 parent e014ed1 commit 7acbf69
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
28 changes: 28 additions & 0 deletions index.js
Expand Up @@ -4,6 +4,8 @@ var path = require('path');
var isBuffer = require('buffer').Buffer.isBuffer;

var clone = require('clone');
var iconv = require('iconv-lite');
var shimmer = require('shimmer');
var isStream = require('is-stream');
var cloneable = require('cloneable-readable');
var replaceExt = require('replace-ext');
Expand Down Expand Up @@ -49,6 +51,8 @@ function File(file) {

this._symlink = null;

this._encoding = 'utf8';

// Set custom properties
Object.keys(file).forEach(function(key) {
if (self.constructor.isCustomProp(key)) {
Expand Down Expand Up @@ -183,10 +187,34 @@ Object.defineProperty(File.prototype, 'contents', {
val = cloneable(val);
}

var self = this;;

if (isBuffer(val)) {
shimmer(val, 'toString', function(orig) {
return function() {
if (arguments.length) {
return orig.apply(this, arguments);
}

return iconv.decode(val, self.encoding);
};
});
}

this._contents = val;
},
});

Object.defineProperty(File.prototype, 'encoding', {
get: function() {
return this._encoding;
},
set: function(encoding) {
// TODO: validate encoding
this._encoding = encoding;
}
})

Object.defineProperty(File.prototype, 'cwd', {
get: function() {
return this._cwd;
Expand Down
4 changes: 3 additions & 1 deletion package.json
Expand Up @@ -30,9 +30,11 @@
"clone-buffer": "^1.0.0",
"clone-stats": "^1.0.0",
"cloneable-readable": "^1.0.0",
"iconv-lite": "^0.4.18",
"is-stream": "^1.1.0",
"remove-trailing-separator": "^1.0.1",
"replace-ext": "^1.0.0"
"replace-ext": "^1.0.0",
"shimmer": "^1.1.0"
},
"devDependencies": {
"eslint": "^1.7.3",
Expand Down

0 comments on commit 7acbf69

Please sign in to comment.