diff --git a/index.js b/index.js index 796d258..04dbb4d 100644 --- a/index.js +++ b/index.js @@ -306,24 +306,24 @@ define("internals/inputProcessor", ["require", "exports", "internals/maskTokeniz } function canNextCharBeSkipped(currentChar, inputType, currentToken) { return (inputType === UserInputType.INSERTION && - currentToken.literal && - currentToken.optional && - !currentCharMatchesRegex(currentChar, currentToken)); + currentToken && currentToken.literal && + (currentToken.optional || + !currentCharMatchesRegex(currentChar, currentToken))); } function canNextCharBeAutoCompleted(currentChar, inputType, currentToken) { return (inputType === UserInputType.INSERTION && - currentToken.literal && + currentToken && currentToken.literal && !currentToken.optional && !currentCharMatchesRegex(currentChar, currentToken)); } function canCurrentCharBeRemovedFromInput(currentChar, inputType, currentToken) { return (inputType === UserInputType.INSERTION && - !currentToken.literal && + currentToken && !currentToken.literal && !currentCharMatchesRegex(currentChar, currentToken)); } function currentCharMatchesRegex(currentChar, token) { - const match = currentChar.match(token.regex); - return (match != null && match[0] === currentChar); + const match = token && currentChar.match(token.regex); + return match != null && (match[0] === currentChar || (!match[0] && token.optional)); } }); define("index", ["require", "exports", "react", "react", "react-native", "internals/inputProcessor"], function (require, exports, React, react_1, react_native_1, inputProcessor_1) { diff --git a/internals/inputProcessor.ts b/internals/inputProcessor.ts index edbce58..496177f 100644 --- a/internals/inputProcessor.ts +++ b/internals/inputProcessor.ts @@ -144,16 +144,16 @@ function autofillNextChars(currentChar: string, inputType: UserInputType, tokens function canNextCharBeSkipped(currentChar: string, inputType: UserInputType, currentToken: ITokenRegex): boolean { return ( inputType === UserInputType.INSERTION && - currentToken.literal && - currentToken.optional && - !currentCharMatchesRegex(currentChar, currentToken) + currentToken && currentToken.literal && + (currentToken.optional || + !currentCharMatchesRegex(currentChar, currentToken)) ); } function canNextCharBeAutoCompleted(currentChar: string, inputType: UserInputType, currentToken: ITokenRegex): boolean { return ( inputType === UserInputType.INSERTION && - currentToken.literal && + currentToken && currentToken.literal && !currentToken.optional && !currentCharMatchesRegex(currentChar, currentToken) ); @@ -162,12 +162,12 @@ function canNextCharBeAutoCompleted(currentChar: string, inputType: UserInputTyp function canCurrentCharBeRemovedFromInput(currentChar: string, inputType: UserInputType, currentToken: ITokenRegex): boolean { return ( inputType === UserInputType.INSERTION && - !currentToken.literal && + currentToken && !currentToken.literal && !currentCharMatchesRegex(currentChar, currentToken) ); } function currentCharMatchesRegex(currentChar: string, token: ITokenRegex): boolean { - const match = currentChar.match(token.regex); - return (match != null && match[0] === currentChar); + const match = token && currentChar.match(token.regex); + return match != null && (match[0] === currentChar || (!match[0] && token.optional)); } diff --git a/internals/inputProcessorTest.ts b/internals/inputProcessorTest.ts index e3bbf66..d9dfe38 100644 --- a/internals/inputProcessorTest.ts +++ b/internals/inputProcessorTest.ts @@ -156,6 +156,38 @@ test('When providing complete value to the mask containing optional tokens in th ]); }); +test('IP format', () => { + const inputProcessor = createInputProcessor('00?0?.00?0?.00?0?.00?0?'); + const inputValues = ['127.0.0.100']; + const outputResults = enterValuesOneAtTime(inputValues, inputProcessor); + + expect(outputResults).toEqual([ + { + text: '127.0.0.100', + complete: true + } + ]); +}); + +/* TODO +test('Korean car number format', () => { + const inputProcessor = createInputProcessor('s?s? 00?0? s 0000'); + const inputValues = ['AB 01 X 1234', '01 X 1234']; + const outputResults = enterValuesOneAtTime(inputValues, inputProcessor); + + expect(outputResults).toEqual([ + { + text: 'AB 01 X 1234', + complete: true + }, + { + text: '01 X 1234', + complete: true + }, + ]); +}); +*/ + function maskedResultToText(results: IMaskedTextResult[]): string[] { return results.map((result) => result.text); } @@ -174,4 +206,4 @@ function pressKeysOneAtTime(keys: string[], inputProcessor: InputProcessorFuncti function enterValuesOneAtTime(values: string[], inputProcessor: InputProcessorFunction): IMaskedTextResult[] { return values.map((value) => inputProcessor(value, UserInputType.INSERTION)); -} \ No newline at end of file +}