Skip to content

✨Add support for mask-trim-zeros attribute on amp-inputmask.#22175

Merged
cvializ merged 3 commits intoampproject:masterfrom
cvializ:feature/mask-zero
May 8, 2019
Merged

✨Add support for mask-trim-zeros attribute on amp-inputmask.#22175
cvializ merged 3 commits intoampproject:masterfrom
cvializ:feature/mask-zero

Conversation

@cvializ
Copy link
Copy Markdown
Contributor

@cvializ cvializ commented May 7, 2019

Fixes #21706 (the original document will need to add mask-trim-zeros="0")

✅Adds unit tests, since the integration tests ended up being flaky
📖Renamed README.md to fit the convention for the other components

A small refactor to improve unit-testability.

@cvializ cvializ requested a review from sparhami May 7, 2019 15:22
<label>CPF: <input name="cpf" mask="000.000.000-00" placeholder="000.051.271-20" mask-trim-zeros="0"></label>
<input type="submit">
</form>
<p>It should displays <strong>000.051.271-20</strong>, but is showing <u>005.127.120</u></p>
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

displays -> display, remove "but ..."?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll remove that line since it was just explanation for a repro case

config['alias'] = 'custom';
config['customMask'] = inputmaskMask;
this.controller_ = this.Inputmask_(config);
return;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returning early from a constructor feels weird (actually, even having control flow logic in the constructor itself feels weird). I think this would feel a bit more natural if your constructor looked like:

this.Inputmask_ = Mask.getInputmask_(element);

this.element_ = element;

this.controller_ = this.createController_();

where you could return early from the createController_ method.

Copy link
Copy Markdown
Contributor Author

@cvializ cvializ May 7, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

solid point, I had forgotten it was a constructor when I wrote that : P I'll remove the early return but since the config depends on which controller is created I'll keep it in one method to keep state in one place

*/
export function removePrefix(value, prefixes) {
const longestPrefix = prefixes
.filter(prefix => value.indexOf(prefix) == 0)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a huge deal, but would use startsWith from src/string. A little more clear on what it is doing, and also uses lastIndexOf to stop searching immediately if it doesn't start with the other string.

const prefixes = opts['prefixes'];
const trimZeros = opts['trimZeros'];

let processedValue = value;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tend to like to remove special cases, and just go through one code path. I think this should be equivalent?

const trimZeros = opts['trimZeros'] || 0;
const processedValue = value.replace(new RegExp('^0{0,${trimZeros}}'), '').replace(/[\s]/g, '');

I think having the code a bit shorter and easier to read outweighs any marginal performance benefits (i.e. from doing a replace that doesn't do anything).

const prefixes = {};
for (let i = 0; i < masks.length; i++) {
const prefix = getMaskPrefix(masks[i]);
// The array of subprefixes grows with the factorial of prefix.length
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we print a warning for the developer here?

@cvializ
Copy link
Copy Markdown
Contributor Author

cvializ commented May 7, 2019

Addressed feedback, PTAL

@cvializ cvializ force-pushed the feature/mask-zero branch from 42f3176 to 4ae3c50 Compare May 8, 2019 17:43
@cvializ cvializ merged commit f1b2863 into ampproject:master May 8, 2019
twifkak added a commit to twifkak/amphtml that referenced this pull request May 10, 2019
@twifkak twifkak mentioned this pull request May 10, 2019
twifkak added a commit that referenced this pull request May 10, 2019
* cl/247283168 Revision bump for #22187

* cl/247285377 Revision bump for #22175

* cl/247510905 Revision bump for #22205
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

amp-inputmask: Leading Zero been stripped when I paste into field

4 participants