Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make constructor set custom properties #92

Merged
merged 5 commits into from
Jul 28, 2016
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ function File(file) {
this.contents = file.contents || null;

this._isVinyl = true;

// Set custom properties
Object.keys(file).forEach(function(key) {
if (this.constructor.isCustomProp(key)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be checked to be a function before calling it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'll always be a function, since extends inherits static methods as well. It'd be the same as checking if file.isBuffer() is a function. Of course it is if it inherits from Vinyl :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Example:

function Foo() {}

Foo.prototype.check = function () {
    console.log(this.constructor.stat());
};

Foo.stat = function () {
    return 'foo';
};

class Bar extends Foo {}

class Baz extends Foo {
    static stat() {
        return 'baz';
    }
}

new Foo().check(); // foo
new Bar().check(); // foo
new Baz().check(); // baz

this[key] = file[key];
}
}, this);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer to avoid this binding. I know self is messier but I'm always nervous about bind performance

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough, I'll move it to self.

}

File.prototype.isBuffer = function() {
Expand Down Expand Up @@ -82,13 +89,9 @@ File.prototype.clone = function(opt) {

// Clone our custom properties
Object.keys(this).forEach(function(key) {
// Ignore built-in fields
if (key === '_contents' || key === 'stat' ||
key === 'history' || key === 'path' ||
key === 'base' || key === 'cwd') {
return;
if (this.constructor.isCustomProp(key)) {
file[key] = opt.deep ? clone(this[key], true) : this[key];
}
file[key] = opt.deep ? clone(this[key], true) : this[key];
}, this);
return file;
};
Expand Down Expand Up @@ -141,6 +144,13 @@ File.prototype.inspect = function() {
return '<File ' + inspect.join(' ') + '>';
};

File.isCustomProp = function(key) {
var builtInFields = [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can probably be hoisted to the top of the file (less array allocation).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had it like that originally, but wanted it to be part of the method so you don't have to go looking for that array, and was under the impression that JS engines optimize stuff like that out with no performance hit. No problem moving it to the top thought.

'_contents', 'contents', 'stat', 'history', 'path', 'base', 'cwd',
];
return builtInFields.indexOf(key) === -1;
};

File.isVinyl = function(file) {
return (file && file._isVinyl === true) || false;
};
Expand Down
7 changes: 7 additions & 0 deletions test/File.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ describe('File', function() {
file.contents.should.equal(val);
done();
});

it('should set custom properties', function(done) {
var sourceMap = {};
var file = new File({ sourceMap: sourceMap });
file.sourceMap.should.equal(sourceMap);
done();
});
});

describe('isBuffer()', function() {
Expand Down