From e604d61862c920042a84ccf04fa1588d23bc5233 Mon Sep 17 00:00:00 2001 From: Ben-hur Santos Ott Date: Tue, 23 May 2017 17:38:51 -0300 Subject: [PATCH] Adding `tinymask` to fix some custom mask issues. --- README.md | 4 ++++ lib/masks/custom.mask.js | 39 +++------------------------------------ package.json | 5 +++-- 3 files changed, 10 insertions(+), 38 deletions(-) diff --git a/README.md b/README.md index ef9c7a29..5ed69d4e 100644 --- a/README.md +++ b/README.md @@ -349,6 +349,10 @@ var money = MaskService.toMask('money', '123', { # Changelog + +## 1.5.1 +* Adding `tinymask` to fix some custom mask issues. + ## 1.5.0 * Adding new and powerfull `custom` engine mask \m/. diff --git a/lib/masks/custom.mask.js b/lib/masks/custom.mask.js index 10b2e813..fe7864d1 100644 --- a/lib/masks/custom.mask.js +++ b/lib/masks/custom.mask.js @@ -1,3 +1,4 @@ +import TinyMask from 'tinymask'; import BaseMask from './_base.mask'; const DEFAULT_TRANSLATION = { @@ -30,42 +31,8 @@ export default class CustomMask extends BaseMask { let { mask } = settings; let translation = this.mergeSettings(DEFAULT_TRANSLATION, settings.translation); - var result = ''; - - const maskSize = mask.length; - const valueSize = value.length; - - var maskResolved = 0; - var valueResolved = 0; - - while (maskResolved < maskSize && valueResolved < valueSize) { - const valueChar = value[valueResolved]; - const maskChar = mask[maskResolved]; - - if (valueChar === maskChar) { - result += valueChar; - maskResolved++; - valueResolved++; - continue; - } - - const handler = translation[maskChar]; - - if (!handler) { - result += maskChar; - maskResolved++; - continue; - } - - var masked = handler(valueChar); - if (invalidValues.indexOf(masked) < 0) { - result += masked - maskResolved++ - } - valueResolved++ - } - - return result; + var masked = new TinyMask(mask, { translation }).mask(value); + return masked; } getRawValue(maskedValue, settings) { diff --git a/package.json b/package.json index 871ed255..cc756a33 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-native-masked-text", - "version": "1.5.0", + "version": "1.5.1", "description": "Text and TextInput with mask for React Native applications", "main": "index.js", "scripts": { @@ -23,7 +23,8 @@ }, "homepage": "https://github.com/benhurott/react-native-masked-text#readme", "dependencies": { - "moment": "^2.18.1" + "moment": "^2.18.1", + "tinymask": "^1.0.2" }, "devDependencies": { "babel-preset-react-native": "^1.9.1",