-
-
Notifications
You must be signed in to change notification settings - Fork 105
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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)) { | ||
this[key] = file[key]; | ||
} | ||
}, this); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I prefer to avoid this binding. I know There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fair enough, I'll move it to |
||
} | ||
|
||
File.prototype.isBuffer = function() { | ||
|
@@ -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; | ||
}; | ||
|
@@ -141,6 +144,12 @@ File.prototype.inspect = function() { | |
return '<File ' + inspect.join(' ') + '>'; | ||
}; | ||
|
||
File.builtInFields = ['_contents', 'contents', 'stat', 'history', 'path', 'base', 'cwd']; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this should be exposed on the constructor. Not sure of a better way to do it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm open to suggestions. My main concern is for it to be configurable on objects that extend from class Vinyl2 extends Vinyl {}
Vinyl2.builtInFields = ['foo', 'bar', ...Vinyl2.builtInFields]; And everything works as expected. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe a second argument to the constructor? Not sure though There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll just chime in and say that exposing it as a static property on the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It can't be 2nd argument to the constructor, as that would imply it's upon user to decide what is a built in property, while this decision is 100% decided by the current virtual object implementation. The alternative can be just a file scoped variable not exposed to the user: var builtInFields = ['_contents', 'contents', 'stat', 'history', 'path', 'base', 'cwd'];
File.isCustomProp = function(key) {
return builtInFields.indexOf(key) === -1;
}; And anyone that extends class Vinyl2 extends Vinyl {
static isCustomProp(key) {
return super.isCustomProp(key) && ['my', 'own', 'props'].indexOf(key) === -1;
}
} I'd actually even prefer this as this way we won't have to be juggling configs on constructor properties. |
||
|
||
File.isCustomProp = function(key) { | ||
return this.builtInFields.indexOf(key) === -1; | ||
}; | ||
|
||
File.isVinyl = function(file) { | ||
return (file && file._isVinyl === true) || false; | ||
}; | ||
|
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 iffile.isBuffer()
is a function. Of course it is if it inherits from Vinyl :)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Example: