From bbeb5acb2855f36ffdda913118faf30e7419f3dd Mon Sep 17 00:00:00 2001 From: bhs Date: Thu, 26 Nov 2020 15:12:06 +0900 Subject: [PATCH 1/2] Fix '?' mask does not work for IP pattern --- index.js | 14 +++++++------- internals/inputProcessor.ts | 14 +++++++------- internals/inputProcessorTest.ts | 12 ++++++++---- 3 files changed, 22 insertions(+), 18 deletions(-) diff --git a/index.js b/index.js index 796d258..a0e36fb 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..31e1337 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..ead5ccb 100644 --- a/internals/inputProcessorTest.ts +++ b/internals/inputProcessorTest.ts @@ -144,13 +144,17 @@ test('When providing complete value to the mask it should return complete as tru }); test('When providing complete value to the mask containing optional tokens in the end it should return complete as true', () => { - const inputProcessor = createInputProcessor('+595 00 00000000?'); - const inputValues = ['+595 00 0000000']; + const inputProcessor = createInputProcessor('00?0?.00?0?.00?0?.00?0?'); + const inputValues = ['255.255.255.255', '127.0.0.1']; const outputResults = enterValuesOneAtTime(inputValues, inputProcessor); expect(outputResults).toEqual([ { - text: '+595 00 0000000', + text: '255.255.255.255', + complete: true + }, + { + text: '127.0.0.1', complete: true } ]); @@ -174,4 +178,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 +} From da2890d845c5cbc80da8da87fd4a3f82dc18ec1e Mon Sep 17 00:00:00 2001 From: bhs Date: Fri, 27 Nov 2020 16:16:39 +0900 Subject: [PATCH 2/2] Add test for optional mask --- index.js | 2 +- internals/inputProcessor.ts | 4 ++-- internals/inputProcessorTest.ts | 36 +++++++++++++++++++++++++++++---- 3 files changed, 35 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index a0e36fb..04dbb4d 100644 --- a/index.js +++ b/index.js @@ -323,7 +323,7 @@ define("internals/inputProcessor", ["require", "exports", "internals/maskTokeniz } function currentCharMatchesRegex(currentChar, token) { const match = token && currentChar.match(token.regex); - return (match != null && (match[0] === currentChar || (!match[0] && token.optional))); + 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 31e1337..496177f 100644 --- a/internals/inputProcessor.ts +++ b/internals/inputProcessor.ts @@ -146,7 +146,7 @@ function canNextCharBeSkipped(currentChar: string, inputType: UserInputType, cur inputType === UserInputType.INSERTION && currentToken && currentToken.literal && (currentToken.optional || - !currentCharMatchesRegex(currentChar, currentToken)) + !currentCharMatchesRegex(currentChar, currentToken)) ); } @@ -169,5 +169,5 @@ function canCurrentCharBeRemovedFromInput(currentChar: string, inputType: UserIn function currentCharMatchesRegex(currentChar: string, token: ITokenRegex): boolean { const match = token && currentChar.match(token.regex); - return (match != null && (match[0] === currentChar || (!match[0] && token.optional))); + return match != null && (match[0] === currentChar || (!match[0] && token.optional)); } diff --git a/internals/inputProcessorTest.ts b/internals/inputProcessorTest.ts index ead5ccb..d9dfe38 100644 --- a/internals/inputProcessorTest.ts +++ b/internals/inputProcessorTest.ts @@ -144,21 +144,49 @@ test('When providing complete value to the mask it should return complete as tru }); test('When providing complete value to the mask containing optional tokens in the end it should return complete as true', () => { + const inputProcessor = createInputProcessor('+595 00 00000000?'); + const inputValues = ['+595 00 0000000']; + const outputResults = enterValuesOneAtTime(inputValues, inputProcessor); + + expect(outputResults).toEqual([ + { + text: '+595 00 0000000', + complete: true + } + ]); +}); + +test('IP format', () => { const inputProcessor = createInputProcessor('00?0?.00?0?.00?0?.00?0?'); - const inputValues = ['255.255.255.255', '127.0.0.1']; + const inputValues = ['127.0.0.100']; const outputResults = enterValuesOneAtTime(inputValues, inputProcessor); expect(outputResults).toEqual([ { - text: '255.255.255.255', + 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: '127.0.0.1', + text: '01 X 1234', complete: true - } + }, ]); }); +*/ function maskedResultToText(results: IMaskedTextResult[]): string[] { return results.map((result) => result.text);