-
Notifications
You must be signed in to change notification settings - Fork 249
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(custom): adding new engine to custom mask
- Loading branch information
Ben-hur Santos Ott
committed
May 22, 2017
1 parent
09512c2
commit 249c042
Showing
5 changed files
with
212 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,86 @@ | ||
import BaseMask from './_base.mask'; | ||
|
||
const DEFAULT_TRANSLATION = { | ||
'9': function (val) { | ||
return val.replace(/[^0-9]+/g, ''); | ||
}, | ||
'A': function (val) { | ||
return val.replace(/[^a-zA-Z]+/g, ''); | ||
}, | ||
'S': function (val) { | ||
return val.replace(/[^a-zA-Z0-9]+/g, ''); | ||
}, | ||
'*': function (val) { | ||
return val | ||
} | ||
} | ||
|
||
var invalidValues = [null, undefined, '']; | ||
|
||
export default class CustomMask extends BaseMask { | ||
static getType() { | ||
return 'custom'; | ||
} | ||
|
||
getKeyboardType() { | ||
return "default"; | ||
} | ||
|
||
getValue(value, settings) { | ||
return this.getVMasker().toPattern(value, settings.mask); | ||
} | ||
|
||
getRawValue(maskedValue, settings) { | ||
if(!!settings && settings.getRawValue) { | ||
return settings.getRawValue(maskedValue, settings); | ||
} | ||
|
||
return maskedValue; | ||
} | ||
|
||
validate(value, settings) { | ||
if(!!settings && settings.validator) { | ||
return settings.validator(value, settings); | ||
} | ||
|
||
return true; | ||
} | ||
} | ||
static getType() { | ||
return 'custom'; | ||
} | ||
|
||
getKeyboardType() { | ||
return "default"; | ||
} | ||
|
||
getValue(value, settings) { | ||
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; | ||
} | ||
|
||
getRawValue(maskedValue, settings) { | ||
if (!!settings && settings.getRawValue) { | ||
return settings.getRawValue(maskedValue, settings); | ||
} | ||
|
||
return maskedValue; | ||
} | ||
|
||
validate(value, settings) { | ||
if (!!settings && settings.validator) { | ||
return settings.validator(value, settings); | ||
} | ||
|
||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters