letsgetrandy / brototype

Bro, do you even?

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

what about keys with dots

paulmillr opened this issue · comments

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

???

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

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 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 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']

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

commented

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?

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.

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