jaridmargolin / formatter.js

Format html inputs to match a specified pattern
http://firstopinion.github.io/formatter.js
2.48k stars 235 forks source link

How come you choose to write your own pattern language when we have regex? #17

Closed ollym closed 10 years ago

ollym commented 10 years ago

Surely it would make a lot more sense to accept a regex-compatable subset for a pattern? For example:

$('#credit-input').formatter({
  'pattern': '{{9999}}-{{9999}}-{{9999}}-{{9999}}'
});

Would end up as:

$('#credit-input').formatter({
  'pattern': '\d{4}-\d{4}-\d{4}-\d{4}'
});

just curious?

jaridmargolin commented 10 years ago

The goal was to create something that could be used by absolutely anybody. I feel the pattern created looks less intimidating, and in turn more consumable by the wide range of developers who work on the web.

ollym commented 10 years ago

But at a cost, it's way more restrictive for the professional. I suggest you have two pattern formatters, and accept both strings and RegExp objects. Therefore you can tell which format the developer is using.

Calling toString() on a regex object returns the pattern string, and so people could use either. What do you think?

jaridmargolin commented 10 years ago

I think you make a good point. Added to my todo list, unless you want to send a PR. Thanks for the suggestion.

zhuangya commented 10 years ago

i agree with @ollym, since i can not validate the input is a valid date or not :)

jaridmargolin commented 10 years ago

Behind the scenes, Formatter breaks apart the string pattern and creates two objects. The first object contains the regular expression and corresponding placement of each user input character. The second object contains the formatted character and corresponding placement. In order to support regular expression patterns, I would have to implement a regular expression parser. This is a whole bag of complexity that I am not ready to add right now. In the meantime, a new method was recently created to offer a little bit more flexibility. See Issue #24