-
Notifications
You must be signed in to change notification settings - Fork 17
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 the Keypather class auto-instance. #10
Conversation
Hey I wasn't sure how I broke it... I thought the other option 'force' tests would fail if I broke the option... I'm trying to detect options if supplied when explicitly calling into the Keypather(opts) class, |
this.force = (force !== undefined) ? Boolean(force) : true; // force - default: true | ||
function Keypather (opts) { | ||
if (!(this instanceof Keypather)) { return new Keypather(opts); }// Auto instantiate | ||
this.force = (opts && opts.force && Boolean(opts.force) === false ? false : true); // force - default: true |
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.
This should be:
this.force = (opts && typeof opts.force !== 'undefined' && Boolean(opts.force) === false ? false : true);
else it's always true.
This wouldn't work since you have to create a instance of var keypath = require('keypather');
// keypath is a function
var obj = {
...
};
keypath.get(obj, "foo.bar.baz"); // This would break get is not a function of keypath. |
Hey @stoeffel - Hey thanks for the reply. // Instead of setting the method on the prototype, something like this:
Keypather.get = function() { /* ... */ } I'm not 100% sure, but is this a valid pattern to allow this kind of usage: |
Hmm, maybe this: module.exports = factoryAndInstance;
function factoryAndInstance (opts) {
var keypather = new Keypather(opts && opts.force);
return keypather;
};
Keypather.call(factoryAndInstance);
factoryAndInstance.__proto__ = new Keypather().__proto__; |
^ that would export a functioning keypather instance defaulted with |
Hey guys... I updated the code... It's still failing... I was wondering how important having the force option is? It adds a layer of branching in the code that might be handled a little differently... I was thinking of moving this kind of functionality under something like |
this.force = (force !== undefined) ? Boolean(force) : true; // force - default: true | ||
function Keypather (opts) { | ||
if (!(this instanceof Keypather)) { return new Keypather(opts); }// Auto instantiate | ||
this.force = (opts && typeof opts.force !== 'undefined' && Boolean(opts.force) === false ? false : true); |
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.
I don't think you need these modifications just revert it to what it was
this.force = (force !== undefined) ? Boolean(force) : true; // force - default: true
Thanks @tjmehta ... I forgot to roll that back... Keypather() is back to the way it was
Just updated... Woo hoo! |
Travis is still failing. You should run the test before pushing ( |
Going to close this for now, feel free to reopen if you continue work on this. |
Hey @tjmehta I love your code's approach & scope of feature support... Anyway I will create a separate issue to discuss some of my thoughts. Anyway, here's a patch to eliminate the need for double parenthesis:
See my updates to the README.md