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

Suggestion - Support Extending Objects #7

Open
Albert-IV opened this issue May 27, 2014 · 5 comments
Open

Suggestion - Support Extending Objects #7

Albert-IV opened this issue May 27, 2014 · 5 comments

Comments

@Albert-IV
Copy link

It would be pretty awesome if there was a way of using dottie to extend the native Object prototype. I suggest this because there's a lot of verboicity in dottie.get(obj, 'some.property').

dottie.extend();
// Extends Object prototype

var values = {
  some: {
    nested: {
        key: 'foobar';
    }
  }
}

values.dGet('some.nested.key'); // returns 'foobar'
values.dGet('some.undefined.key') // returns undefined

values.dSet('some.nested.value', someValue);  // creates nested structure


var somePostData = {
  'user.name': 'Gummy Bear',
  'user.email': 'gummybear@candymountain.com',
  'user.professional.title': 'King',
  'user.professional.employer': 'Candy Mountain'
};

var transformed = somePostData.dTransform();

var somePostData = {
  'user_name' : 'Mick Hansen',
  'user_email' : 'maker@mhansen.io'
};

var delimited = somePostData.transform({ delimiter: "_" });

If you're OK with doing something like this I can start writing the extension code.

@mickhansen
Copy link
Owner

Extending native objects in javascript is usually frowned upon. It introduces issue with enumerability etc, but i suppose that could be worked around with defneProperty in newer javascript version.

Another solution could be to create a wrapper ala:

var values = dottie.wrap({
  some: {
    nested: {
        key: 'foobar';
    }
  }
});

Although that would probably kill normal object access.

@mickhansen
Copy link
Owner

@Albert-IV
Copy link
Author

Yeah, I was thinking of using defineProperty to ensure it was non-enumerable and throwing or returning an Error if dottie.extend() is called in an unsupported environment.

We could also get fancy and have the users specify what properties the .extend() call would set those helpers to, so users don't have data locations overwritten unexpectedly.

dottie.extend({
  // <dottie call> : <extension property>
  get : 'dottieGetMethod',
  set : 'dottieSetMethod'
  // etc
})

obj.dottieGetMethod('some.property')

Extending the native Object is frowned upon by some people, but not all. Most of the problems people have with extending native stuff comes from people trying to extend DOM objects (which has a bunch of issues depending on the browser).

That's why I was wanting it to be an optional .extend() call rather than default behavior (it isn't optimal in a bunch of use-cases).

@mickhansen
Copy link
Owner

Yeah as long as we make the extension explicit and optional i'm all for.

Perhaps the default method names should me obj.$get, obj.$set etc - Same short names but most likely no clashes.

@Albert-IV
Copy link
Author

Sure, we can have the defaults be $ by default.

I'm not against adding the dottie.wrap(<object>) syntax either. We could use that as a single-object extension, or alternatively a helper that allows you to use .get() .set() syntax to return/ alter values from the wrapped object. How do you think we should handle those sort of calls?

Since we're good with dottie.extend(), I'll start with adding with the syntax into a PR. Let me know what you think about how .wrap() should work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants