jonschlinkert / cache-base

Basic object store with methods like get/set/extend/omit
MIT License
56 stars 19 forks source link

feature: default value #5

Closed jonschlinkert closed 6 years ago

jonschlinkert commented 8 years ago

Consider allowing .get to return a default value.

app.get('foo', 'default-value');
tunnckoCore commented 8 years ago

Good idea. :+1:

doowb commented 8 years ago

I like this idea, but I think it should be a new method if we're going to still allow passing in multiple arguments to create the key:

app.get('foo', 'bar', 'baz');

That's a complicated case to figure out (e.g. is it using 'baz' as the default for foo.bar or the getting the value for foo.bar.baz).

Something like app.getDefault() or app.default() might be good and it will indicate that the last value will always be the default if a value doesn't exist for the key created using the rest of the arguments.

The other option is to only allow 2 arguments to .get where the first argument may be a string or an array and the second is the default value. I think this is how we use .get most of the time anyway.

tunnckoCore commented 8 years ago

I think this is how we use .get most of the time anyway.

Exactly, it's enough. Otherwise going to be too complicated.

jonschlinkert commented 6 years ago

I'm updating some things in cache-base now.

I think it should be a new method if we're going to still allow passing in multiple arguments to create the key:

I think we should just remove support for passing multiple arguments, and passing keys as an array, and just support strings. I've never really liked this feature, for the types of reasons you're pointing out. Currently, if the key is passed as more than one argument or an array of segments, it's joined into a dot-notated string anyway, which makes it hard to see the point of supporting that. In earlier versions of js, it was nice to be able to dynamically build up keys that way, but it's not necessary and complicates the code.

Closing, I don't think we'll add support for this, but I might want to add support for a similar feature using a different method.