Aliases without whitespaces
vonagam opened this issue · 7 comments
How about adding aliases without whitespaces for retrieving code by message?
statuses[message.replace(/ /g, '')] = status;
So that you can write status.NotFound
to get code.
There is a drawback that status('NotFound')
will not be equal in behaviour to status['NotFound']
though. Is it a dealbreaker?
That's for the suggestion! In order to really know, let's step back and can you state what the goal you are trying to achieve is? For example how would exposing everything another time really help when it already works like statuses['Not Found']
?
Well, the only value is in writing less symbols. No need to write brackets, quotation marks, whitespaces between words (and optionally inside brackets if it is your style) (min 5 symbols less).
statuses[ 'Not Found' ]
//or
statuses['Not Found']
//vs
statuses.NotFound
I think last one is more pleasant to write and read. But it is just a stylistic option, so if you think it is not worth supporting, i have no problem with that. (By the way thanks for a quick response.)
So there are existing status messages with dash characters and even a single quote characher. Simply removing white space won't actually work for every code currently (and there is no telling what new ones will come in the future).
If the idea is to expose a message to number map, where the message is also a javascript identifier, it may need some additional robustness to generate such an identifier to account for the existing messages, but ideally for future ones too so we don't mess it up on a future release, haha.
I looked into codes.json before: only two contained dashes, one contained quote mark, all of them were unpopular ones, so only whitespaces were the real obstacle.
If stripping those is ok with you, then easy way is to simply strip all non-word characters: message.replace(/\W/g, '')
Well, I think calling them unpopular is an opinion unless you have some data to back up that statement as a fact. I think they should all just be treated as first class citizens.
If you want to make the call of some being unpopular and therefore don't have an identifier, it would be trivial to publish a module that just used this as a dependency to npm and implemented that decision logic for eveyone to use.
So, is message.replace(/\W/g, '')
ok?
Sorry I left this issue hanging around. In my opinion (as the module's author), this is something outside the scope of this module; the API this module presents is the message as-is, including whitespace and other characters. You're welcome to create a wrapper (either internal to your own code or one published on npm) if you are looking for a different API.