Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added option "greedy" for translations which lets pattern greedily match #608

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

warpspin
Copy link

This minor patch adds an option "greedy" for translations, which is a simpler but sometimes useful form of recursion: characters will be matched as long as they match, then the rest of the mask will continue to be matched.

This e.g. allows matching of traditional decimal numbers with a fixed number of fraction digits up to any length AND a sign, which is not possible with normal reverse recursive matching:

Example:

$(sel).mask( 's0=,00', {
   translation: {
        's': { pattern: /-/, optional: true },
        '=': { pattern: /\d/, greedy: true }
   }
});

(Allows 1,23 or -1,23 or 12,23 or -12,23 and so on)

This will probably also solve most of the issues people sometimes (like #436 )have with negative numbers without a huge invasive money-related patch.

@igorescobar
Copy link
Owner

@warpspin Hello there!

Can you give other application for this greedy option?

@warpspin
Copy link
Author

It's basically an implementation of the regexp "+" modifier for the mask, so it's useful anywhere you have variable-width fields. Other uses I personally have are for example variable length part numbers like "manufacturer-productgroup-producttype-WIDTHxHEIGHT-variant" (e.g. something like "EG-DG1000-14568234-100x50-C") where one or more fields are variable length but we still want to require the "-" or the "x".

Of course, anything the patch can do can be done by simply adding lots of "optional" keys to the mask - it just shortens the masks with a repeater.

@igorescobar
Copy link
Owner

igorescobar commented Sep 27, 2017

@warpspin and have you checked how it works with the reverse: true?

@warpspin
Copy link
Author

Worked in my tests. I suppose the more critical combination would be if a greedy option is somehow combined with a recursive option. I don't think this would give sensible results anymore, though I haven't tried it yet.

@igorescobar
Copy link
Owner

@warpspin Yeah... that's what I meant. I tested yesterday and it didn't worked.

@warpspin
Copy link
Author

I think it's simply not a sensible combination with the way greedy is currently implemented, I can't think of a use case for it.

Another possibility, though, would be to not have the greedy repetitions as a seperate feature but instead to allow explicit recursion start/end markers, like e.g.

$(sel).mask('something {9.99}0 something-else', {
   reverse: true,
   translation: {
       '{':  { recursionStart: true },
       '}':  { recursionEnd: true }
   }
});

where { would set the resetPos and } would jump back to the resetPos if the next input character matches the start of the recursion (meanings of course reverted in reverse mode). This would allow recursion to not only happen at the start or the end of input and solve all use-cases for greedy as well without needing it as a seperate feature.

If you're interested in such an extended version of the feature, I'd start working on it.

@igorescobar
Copy link
Owner

igorescobar commented Sep 27, 2017

@warpspin Yeah, It can get complicated. The thing is that I believe most of the users use the reverse: true for monetary input so the users would for sure try to combine the possibility of having the - suffix with the reverse mode mainly because when you are inputting monetary values you usually starts with cents and the reverse: true in that case becomes handy.

What you said can become something interesting but maybe with different names, what do you think something like this:

$(sel).mask('something {9.99}0 something-else', {
   reverse: true,
   translation: {
       '{':  { reverseStart: true },
       '}':  { reverseEnd: true }
   }
});

or

$(sel).mask('something {9.99}0 something-else', {
   reverse: { start: '{', end: '}' }
});

I rather use reverse so the users can relate it with the reverse: true functionality.

But... the whole point is.... we need to see if we can add this functionality without tons of code... that's why I never focused on features to accommodate currency problems. It can get messy real fast.

@warpspin
Copy link
Author

The reverse name is a false analogy, though, as the recursionStart/End thing would also work without reverse and is just an extension of the recursive feature to more than character and to anywhere within the string instead of only the start/end depending on the reverse direction.

@igorescobar
Copy link
Owner

@warpspin I agree but I can't think of any use case for this but with the reverse: true. I'm just trying to think regarding the DSL... what would make more sense to the user and would make it more intuitive. Under the hood we can call it with the terms we want :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants