brototype icon indicating copy to clipboard operation
brototype copied to clipboard

what about keys with dots

Open paulmillr opened this issue 11 years ago • 8 comments
trafficstars

var a = {}
a['shit.bro'] = {5: 'total shit'};

???

paulmillr avatar Sep 26 '14 00:09 paulmillr

Coding keys like that is bound to lead to confusion, and not a good idea.

ohhaiohhai avatar Sep 26 '14 19:09 ohhaiohhai

I disagree, there are appropriate times to use keys with periods, such as when dealing with i18n translations, ie. es['user.profile.greet']

Here's a simple implementation https://github.com/miguelmota/getprop

miguelmota avatar Jan 05 '15 23:01 miguelmota

@miguelmota I would have to disagree with you. Any i18n implementation I've used had tiered containers, ie...

lang = {
    user : {
        profile: {
            greet: "Profile greeting for lang"
        }
    }
}

Plloi avatar Feb 23 '15 21:02 Plloi

@Plloi yes that's probably preferred, I was simply giving a basic example since the issue relates to keys with periods. Other examples are numeric literals, ie. obj['1.2e+35']

miguelmota avatar Feb 23 '15 22:02 miguelmota

would you be open to something like an escape character? obj['1.2e+35'] could be translated to 'obj.1\.2e+35'

deltreey avatar Oct 09 '15 18:10 deltreey

Hmm. I wonder how I missed this issue. The example of numeric literals seems like a valid use case.

I'm not sure about backslash escaping though... seems like an invitation for unpredictable bugs, given that backslash escaping already has a function in strings.

Maybe doubling up the dots? bro.doYouEven('1..2e+35') Or perhaps adding a dontExpandDots option to the functions?

letsgetrandy avatar Oct 09 '15 18:10 letsgetrandy

I apologize. I meant 'obj.1\\.2e+35' but you make a valid point. I don't know if I like the idea of .. because there may be situation where people are trying to find keys that don't exist like 'data.value.total' could be 'data..total' in the event of a typo or an automated system which makes for a confusing error/result.

deltreey avatar Oct 09 '15 18:10 deltreey

Just use lodash _.get https://lodash.com/docs#get

miguelmota avatar Oct 09 '15 18:10 miguelmota