Browse Source

update datetime fuzzy logic

Robin Herbots 5 years ago
parent
commit
d424e39add

+ 1 - 0
CHANGELOG.md

@@ -3,6 +3,7 @@
 ## [5.0.6 - UNRELEASED]
 ### Updates
 - enhance alternation logic
+- update datetime alias
  
 ### Fixed
 - Can't remove "placeholder" from datetime alias #2438

+ 1 - 1
bower.json

@@ -1,6 +1,6 @@
 {
   "name": "inputmask",
-  "version": "5.0.6-beta.25",
+  "version": "5.0.6-beta.26",
   "main": [
 	  "./index.js",
     "./css/inputmask.css"

+ 1 - 1
composer.json

@@ -1,7 +1,7 @@
 {
   "name": "robinherbots/inputmask",
   "description": "Inputmask is a javascript library which creates an input mask.  Inputmask can run against vanilla javascript, jQuery and jqlite.",
-  "version": "5.0.6-beta.25",
+  "version": "5.0.6-beta.26",
   "type": "library",
   "keywords": ["jquery", "plugins", "input", "form", "inputmask", "mask"],
   "homepage": "http://robinherbots.github.io/Inputmask",

+ 21 - 25
dist/inputmask.js

@@ -3,7 +3,7 @@
  * https://github.com/RobinHerbots/Inputmask
  * Copyright (c) 2010 - 2020 Robin Herbots
  * Licensed under the MIT license
- * Version: 5.0.6-beta.25
+ * Version: 5.0.6-beta.26
  */
 !function webpackUniversalModuleDefinition(root, factory) {
     if ("object" == typeof exports && "object" == typeof module) module.exports = factory(); else if ("function" == typeof define && define.amd) define([], factory); else {
@@ -426,6 +426,14 @@
             var inputmask = this, maskset = this.maskset;
             return maskset.validPositions[pos] ? maskset.validPositions[pos] : (tests || getTests.call(this, pos))[0];
         }
+        function isSubsetOf(source, target, opts) {
+            function expand(pattern) {
+                for (var expanded = [], start = -1, end, i = 0, l = pattern.length; i < l; i++) if ("-" === pattern.charAt(i)) for (end = pattern.charCodeAt(i + 1); ++start < end; ) expanded.push(String.fromCharCode(start)); else start = pattern.charCodeAt(i), 
+                expanded.push(pattern.charAt(i));
+                return expanded.join("");
+            }
+            return source.match.def === target.match.nativeDef || !(!(opts.regex || source.match.fn instanceof RegExp && target.match.fn instanceof RegExp) || !0 === source.match.static || !0 === target.match.static) && -1 !== expand(target.match.fn.toString().replace(/[[\]/]/g, "")).indexOf(expand(source.match.fn.toString().replace(/[[\]/]/g, "")));
+        }
         function getTests(pos, ndxIntlzr, tstPs) {
             var inputmask = this, $ = this.dependencyLib, maskset = this.maskset, opts = this.opts, el = this.el, maskTokens = maskset.maskToken, testPos = ndxIntlzr ? tstPs : 0, ndxInitializer = ndxIntlzr ? ndxIntlzr.slice() : [ 0 ], matches = [], insertStop = !1, latestMatch, cacheDependency = ndxIntlzr ? ndxIntlzr.join("") : "";
             function resolveTestFromToken(maskToken, ndxInitializer, loopNdx, quantifierRecurse) {
@@ -450,14 +458,6 @@
                         }
                         return void 0 !== targetAlternation ? resolveNdxInitializer(pos, alternateNdx) : void 0;
                     }
-                    function isSubsetOf(source, target) {
-                        function expand(pattern) {
-                            for (var expanded = [], start = -1, end, i = 0, l = pattern.length; i < l; i++) if ("-" === pattern.charAt(i)) for (end = pattern.charCodeAt(i + 1); ++start < end; ) expanded.push(String.fromCharCode(start)); else start = pattern.charCodeAt(i), 
-                            expanded.push(pattern.charAt(i));
-                            return expanded.join("");
-                        }
-                        return source.match.def === target.match.nativeDef || !(!(opts.regex || source.match.fn instanceof RegExp && target.match.fn instanceof RegExp) || !0 === source.match.static || !0 === target.match.static) && -1 !== expand(target.match.fn.toString().replace(/[[\]/]/g, "")).indexOf(expand(source.match.fn.toString().replace(/[[\]/]/g, "")));
-                    }
                     function staticCanMatchDefinition(source, target) {
                         return !0 === source.match.static && !0 !== target.match.static && target.match.fn.test(source.match.def, maskset, pos, !1, opts, !1);
                     }
@@ -538,11 +538,11 @@
                                                     dropMatch = !0, setMergeLocators(altMatch2, altMatch);
                                                     break;
                                                 }
-                                                if (isSubsetOf(altMatch, altMatch2)) {
+                                                if (isSubsetOf(altMatch, altMatch2, opts)) {
                                                     setMergeLocators(altMatch, altMatch2) && (dropMatch = !0, malternateMatches.splice(malternateMatches.indexOf(altMatch2), 0, altMatch));
                                                     break;
                                                 }
-                                                if (isSubsetOf(altMatch2, altMatch)) {
+                                                if (isSubsetOf(altMatch2, altMatch, opts)) {
                                                     setMergeLocators(altMatch2, altMatch);
                                                     break;
                                                 }
@@ -620,7 +620,8 @@
             value: !0
         }), exports.determineTestTemplate = determineTestTemplate, exports.getDecisionTaker = getDecisionTaker, 
         exports.getMaskTemplate = getMaskTemplate, exports.getPlaceholder = getPlaceholder, 
-        exports.getTest = getTest, exports.getTests = getTests, exports.getTestTemplate = getTestTemplate;
+        exports.getTest = getTest, exports.getTests = getTests, exports.getTestTemplate = getTestTemplate, 
+        exports.isSubsetOf = isSubsetOf;
     }, function(module, exports, __webpack_require__) {
         "use strict";
         Object.defineProperty(exports, "__esModule", {
@@ -837,7 +838,7 @@
         }
         function positionCanMatchDefinition(pos, testDefinition, opts) {
             for (var inputmask = this, maskset = this.maskset, valid = !1, tests = _validationTests.getTests.call(this, pos), tndx = 0; tndx < tests.length; tndx++) {
-                if (tests[tndx].match && (!(tests[tndx].match.nativeDef !== testDefinition.match[opts.shiftPositions ? "def" : "nativeDef"] || opts.shiftPositions && testDefinition.match.static) || tests[tndx].match.nativeDef === testDefinition.match.nativeDef)) {
+                if (tests[tndx].match && (tests[tndx].match.nativeDef === testDefinition.match[opts.shiftPositions ? "def" : "nativeDef"] && (!opts.shiftPositions || !testDefinition.match.static) || tests[tndx].match.nativeDef === testDefinition.match.nativeDef || opts.regex && !tests[tndx].match.static && tests[tndx].match.fn.test(testDefinition.input))) {
                     valid = !0;
                     break;
                 }
@@ -2455,17 +2456,11 @@
                 postValidation: function postValidation(buffer, pos, c, currentResult, opts, maskset, strict, fromCheckval) {
                     var inputmask = this, tokenMatch, validator;
                     if (strict) return !0;
-                    if (!1 === currentResult) return tokenMatch = getTokenMatch(pos + 1, opts), tokenMatch.targetMatch && tokenMatch.targetMatchIndex === pos && 1 < tokenMatch.targetMatch[0].length && void 0 !== formatCode[tokenMatch.targetMatch[0]] && (validator = formatCode[tokenMatch.targetMatch[0]][0], 
-                    new RegExp(validator).test("0" + c)) ? {
-                        insert: [ {
-                            pos: pos,
-                            c: "0"
-                        }, {
-                            pos: pos + 1,
-                            c: c
-                        } ],
-                        pos: pos + 1
-                    } : currentResult;
+                    if (!1 === currentResult && (tokenMatch = getTokenMatch(pos + 1, opts), tokenMatch.targetMatch && tokenMatch.targetMatchIndex === pos && 1 < tokenMatch.targetMatch[0].length && void 0 !== formatCode[tokenMatch.targetMatch[0]] && (validator = formatCode[tokenMatch.targetMatch[0]][0], 
+                    new RegExp(validator).test("0" + c) && (buffer[pos] = "0", buffer[pos + 1] = c, 
+                    currentResult = {
+                        pos: pos + 2
+                    })), !1 === currentResult)) return currentResult;
                     if (currentResult.fuzzy && (buffer = currentResult.buffer, pos = currentResult.pos), 
                     tokenMatch = getTokenMatch(pos, opts), tokenMatch.targetMatch && tokenMatch.targetMatch[0] && void 0 !== formatCode[tokenMatch.targetMatch[0]]) {
                         validator = formatCode[tokenMatch.targetMatch[0]][0];
@@ -2480,7 +2475,8 @@
                         refreshFromBuffer: {
                             start: pos,
                             end: currentResult.pos
-                        }
+                        },
+                        pos: currentResult.pos
                     } : result;
                 },
                 onKeyDown: function onKeyDown(e, buffer, caretPos, opts) {

File diff suppressed because it is too large
+ 2 - 2
dist/inputmask.min.js


+ 21 - 25
dist/jquery.inputmask.js

@@ -3,7 +3,7 @@
  * https://github.com/RobinHerbots/Inputmask
  * Copyright (c) 2010 - 2020 Robin Herbots
  * Licensed under the MIT license
- * Version: 5.0.6-beta.25
+ * Version: 5.0.6-beta.26
  */
 !function webpackUniversalModuleDefinition(root, factory) {
     if ("object" == typeof exports && "object" == typeof module) module.exports = factory(require("jquery")); else if ("function" == typeof define && define.amd) define([ "jquery" ], factory); else {
@@ -426,6 +426,14 @@
             var inputmask = this, maskset = this.maskset;
             return maskset.validPositions[pos] ? maskset.validPositions[pos] : (tests || getTests.call(this, pos))[0];
         }
+        function isSubsetOf(source, target, opts) {
+            function expand(pattern) {
+                for (var expanded = [], start = -1, end, i = 0, l = pattern.length; i < l; i++) if ("-" === pattern.charAt(i)) for (end = pattern.charCodeAt(i + 1); ++start < end; ) expanded.push(String.fromCharCode(start)); else start = pattern.charCodeAt(i), 
+                expanded.push(pattern.charAt(i));
+                return expanded.join("");
+            }
+            return source.match.def === target.match.nativeDef || !(!(opts.regex || source.match.fn instanceof RegExp && target.match.fn instanceof RegExp) || !0 === source.match.static || !0 === target.match.static) && -1 !== expand(target.match.fn.toString().replace(/[[\]/]/g, "")).indexOf(expand(source.match.fn.toString().replace(/[[\]/]/g, "")));
+        }
         function getTests(pos, ndxIntlzr, tstPs) {
             var inputmask = this, $ = this.dependencyLib, maskset = this.maskset, opts = this.opts, el = this.el, maskTokens = maskset.maskToken, testPos = ndxIntlzr ? tstPs : 0, ndxInitializer = ndxIntlzr ? ndxIntlzr.slice() : [ 0 ], matches = [], insertStop = !1, latestMatch, cacheDependency = ndxIntlzr ? ndxIntlzr.join("") : "";
             function resolveTestFromToken(maskToken, ndxInitializer, loopNdx, quantifierRecurse) {
@@ -450,14 +458,6 @@
                         }
                         return void 0 !== targetAlternation ? resolveNdxInitializer(pos, alternateNdx) : void 0;
                     }
-                    function isSubsetOf(source, target) {
-                        function expand(pattern) {
-                            for (var expanded = [], start = -1, end, i = 0, l = pattern.length; i < l; i++) if ("-" === pattern.charAt(i)) for (end = pattern.charCodeAt(i + 1); ++start < end; ) expanded.push(String.fromCharCode(start)); else start = pattern.charCodeAt(i), 
-                            expanded.push(pattern.charAt(i));
-                            return expanded.join("");
-                        }
-                        return source.match.def === target.match.nativeDef || !(!(opts.regex || source.match.fn instanceof RegExp && target.match.fn instanceof RegExp) || !0 === source.match.static || !0 === target.match.static) && -1 !== expand(target.match.fn.toString().replace(/[[\]/]/g, "")).indexOf(expand(source.match.fn.toString().replace(/[[\]/]/g, "")));
-                    }
                     function staticCanMatchDefinition(source, target) {
                         return !0 === source.match.static && !0 !== target.match.static && target.match.fn.test(source.match.def, maskset, pos, !1, opts, !1);
                     }
@@ -538,11 +538,11 @@
                                                     dropMatch = !0, setMergeLocators(altMatch2, altMatch);
                                                     break;
                                                 }
-                                                if (isSubsetOf(altMatch, altMatch2)) {
+                                                if (isSubsetOf(altMatch, altMatch2, opts)) {
                                                     setMergeLocators(altMatch, altMatch2) && (dropMatch = !0, malternateMatches.splice(malternateMatches.indexOf(altMatch2), 0, altMatch));
                                                     break;
                                                 }
-                                                if (isSubsetOf(altMatch2, altMatch)) {
+                                                if (isSubsetOf(altMatch2, altMatch, opts)) {
                                                     setMergeLocators(altMatch2, altMatch);
                                                     break;
                                                 }
@@ -620,7 +620,8 @@
             value: !0
         }), exports.determineTestTemplate = determineTestTemplate, exports.getDecisionTaker = getDecisionTaker, 
         exports.getMaskTemplate = getMaskTemplate, exports.getPlaceholder = getPlaceholder, 
-        exports.getTest = getTest, exports.getTests = getTests, exports.getTestTemplate = getTestTemplate;
+        exports.getTest = getTest, exports.getTests = getTests, exports.getTestTemplate = getTestTemplate, 
+        exports.isSubsetOf = isSubsetOf;
     }, function(module, exports, __webpack_require__) {
         "use strict";
         Object.defineProperty(exports, "__esModule", {
@@ -837,7 +838,7 @@
         }
         function positionCanMatchDefinition(pos, testDefinition, opts) {
             for (var inputmask = this, maskset = this.maskset, valid = !1, tests = _validationTests.getTests.call(this, pos), tndx = 0; tndx < tests.length; tndx++) {
-                if (tests[tndx].match && (!(tests[tndx].match.nativeDef !== testDefinition.match[opts.shiftPositions ? "def" : "nativeDef"] || opts.shiftPositions && testDefinition.match.static) || tests[tndx].match.nativeDef === testDefinition.match.nativeDef)) {
+                if (tests[tndx].match && (tests[tndx].match.nativeDef === testDefinition.match[opts.shiftPositions ? "def" : "nativeDef"] && (!opts.shiftPositions || !testDefinition.match.static) || tests[tndx].match.nativeDef === testDefinition.match.nativeDef || opts.regex && !tests[tndx].match.static && tests[tndx].match.fn.test(testDefinition.input))) {
                     valid = !0;
                     break;
                 }
@@ -2325,17 +2326,11 @@
                 postValidation: function postValidation(buffer, pos, c, currentResult, opts, maskset, strict, fromCheckval) {
                     var inputmask = this, tokenMatch, validator;
                     if (strict) return !0;
-                    if (!1 === currentResult) return tokenMatch = getTokenMatch(pos + 1, opts), tokenMatch.targetMatch && tokenMatch.targetMatchIndex === pos && 1 < tokenMatch.targetMatch[0].length && void 0 !== formatCode[tokenMatch.targetMatch[0]] && (validator = formatCode[tokenMatch.targetMatch[0]][0], 
-                    new RegExp(validator).test("0" + c)) ? {
-                        insert: [ {
-                            pos: pos,
-                            c: "0"
-                        }, {
-                            pos: pos + 1,
-                            c: c
-                        } ],
-                        pos: pos + 1
-                    } : currentResult;
+                    if (!1 === currentResult && (tokenMatch = getTokenMatch(pos + 1, opts), tokenMatch.targetMatch && tokenMatch.targetMatchIndex === pos && 1 < tokenMatch.targetMatch[0].length && void 0 !== formatCode[tokenMatch.targetMatch[0]] && (validator = formatCode[tokenMatch.targetMatch[0]][0], 
+                    new RegExp(validator).test("0" + c) && (buffer[pos] = "0", buffer[pos + 1] = c, 
+                    currentResult = {
+                        pos: pos + 2
+                    })), !1 === currentResult)) return currentResult;
                     if (currentResult.fuzzy && (buffer = currentResult.buffer, pos = currentResult.pos), 
                     tokenMatch = getTokenMatch(pos, opts), tokenMatch.targetMatch && tokenMatch.targetMatch[0] && void 0 !== formatCode[tokenMatch.targetMatch[0]]) {
                         validator = formatCode[tokenMatch.targetMatch[0]][0];
@@ -2350,7 +2345,8 @@
                         refreshFromBuffer: {
                             start: pos,
                             end: currentResult.pos
-                        }
+                        },
+                        pos: currentResult.pos
                     } : result;
                 },
                 onKeyDown: function onKeyDown(e, buffer, caretPos, opts) {

File diff suppressed because it is too large
+ 2 - 2
dist/jquery.inputmask.min.js


+ 9 - 6
lib/extensions/inputmask.date.extensions.js

@@ -408,18 +408,20 @@ Inputmask.extendAliases({
 
 			if (strict) return true;
 			var tokenMatch, validator;
-			if (currentResult === false) {
+			if (currentResult === false) { //try some shifting
 				tokenMatch = getTokenMatch(pos + 1, opts);
 				if (tokenMatch.targetMatch && tokenMatch.targetMatchIndex === pos && tokenMatch.targetMatch[0].length > 1 && formatCode[tokenMatch.targetMatch[0]] !== undefined) {
 					validator = formatCode[tokenMatch.targetMatch[0]][0];
 					if (new RegExp(validator).test("0" + c)) {
-						return {
-							insert: [{pos: pos, c: "0"}, {pos: pos + 1, c: c}],
-							pos: pos + 1
+						buffer[pos] = "0";
+						buffer[pos + 1] = c;
+						currentResult = {
+							//insert: [{pos: pos, c: "0"}, {pos: pos + 1, c: c}],
+							pos: pos + 2 //this will triggeer a refreshfrombuffer
 						};
 					}
 				}
-				return currentResult;
+				if (currentResult === false) return currentResult;
 			}
 
 			if (currentResult.fuzzy) {
@@ -447,7 +449,8 @@ Inputmask.extendAliases({
 			if (pos && result && currentResult.pos !== pos) {
 				return {
 					buffer: parse(opts.inputFormat, dateParts, opts).split(""),
-					refreshFromBuffer: {start: pos, end: currentResult.pos}
+					refreshFromBuffer: {start: pos, end: currentResult.pos},
+					pos: currentResult.pos //correct caret position
 				};
 			}
 

+ 1 - 0
lib/inputHandling.js

@@ -225,6 +225,7 @@ function writeBuffer(input, buffer, caretPos, event, triggerEvents) {
 	if (input !== undefined) {
 		input.inputmask._valueSet(buffer.join(""));
 		if (caretPos !== undefined && (event === undefined || event.type !== "blur")) {
+			// console.log(caretPos);
 			caret.call(inputmask, input, caretPos, undefined, undefined, (event !== undefined && event.type === "keydown" && (event.keyCode === keyCode.DELETE || event.keyCode === keyCode.BACKSPACE)));
 		}
 		if (triggerEvents === true) {

+ 34 - 25
lib/validation-tests.js

@@ -1,4 +1,13 @@
-export {determineTestTemplate, getDecisionTaker, getMaskTemplate, getPlaceholder, getTest, getTests, getTestTemplate};
+export {
+	determineTestTemplate,
+	getDecisionTaker,
+	getMaskTemplate,
+	getPlaceholder,
+	getTest,
+	getTests,
+	getTestTemplate,
+	isSubsetOf
+};
 
 function getLocator(tst, align) { //need to align the locators to be correct
 	var locator = (tst.alternation != undefined ? tst.mloc[getDecisionTaker(tst)] : tst.locator).join("");
@@ -145,6 +154,28 @@ function getTest(pos, tests) {
 	return (tests || getTests.call(inputmask, pos))[0];
 }
 
+function isSubsetOf(source, target, opts) {
+	function expand(pattern) {
+		var expanded = [], start = -1, end;
+		for (var i = 0, l = pattern.length; i < l; i++) {
+			if (pattern.charAt(i) === "-") {
+				end = pattern.charCodeAt(i + 1);
+				while (++start < end) expanded.push(String.fromCharCode(start));
+			} else {
+				start = pattern.charCodeAt(i);
+				expanded.push(pattern.charAt(i));
+			}
+		}
+		return expanded.join("");
+	}
+
+	if (source.match.def === target.match.nativeDef) return true;
+	if ((opts.regex || (source.match.fn instanceof RegExp && target.match.fn instanceof RegExp)) && source.match.static !== true && target.match.static !== true) { //is regex a subset
+		return expand(target.match.fn.toString().replace(/[[\]/]/g, "")).indexOf(expand(source.match.fn.toString().replace(/[[\]/]/g, ""))) !== -1;
+	}
+	return false;
+}
+
 //tobe put on prototype?
 function getTests(pos, ndxIntlzr, tstPs) {
 	var inputmask = this,
@@ -205,28 +236,6 @@ function getTests(pos, ndxIntlzr, tstPs) {
 				}
 			}
 
-			function isSubsetOf(source, target) {
-				function expand(pattern) {
-					var expanded = [], start = -1, end;
-					for (var i = 0, l = pattern.length; i < l; i++) {
-						if (pattern.charAt(i) === "-") {
-							end = pattern.charCodeAt(i + 1);
-							while (++start < end) expanded.push(String.fromCharCode(start));
-						} else {
-							start = pattern.charCodeAt(i);
-							expanded.push(pattern.charAt(i));
-						}
-					}
-					return expanded.join("");
-				}
-
-				if (source.match.def === target.match.nativeDef) return true;
-				if ((opts.regex || (source.match.fn instanceof RegExp && target.match.fn instanceof RegExp)) && source.match.static !== true && target.match.static !== true) { //is regex a subset
-					return expand(target.match.fn.toString().replace(/[[\]/]/g, "")).indexOf(expand(source.match.fn.toString().replace(/[[\]/]/g, ""))) !== -1;
-				}
-				return false;
-			}
-
 			function staticCanMatchDefinition(source, target) {
 				return source.match.static === true && target.match.static !== true ? target.match.fn.test(source.match.def, maskset, pos, false, opts, false) : false;
 			}
@@ -382,13 +391,13 @@ function getTests(pos, ndxIntlzr, tstPs) {
 											dropMatch = true;
 											setMergeLocators(altMatch2, altMatch);
 											break;
-										} else if (isSubsetOf(altMatch, altMatch2)) {
+										} else if (isSubsetOf(altMatch, altMatch2, opts)) {
 											if (setMergeLocators(altMatch, altMatch2)) {
 												dropMatch = true;
 												malternateMatches.splice(malternateMatches.indexOf(altMatch2), 0, altMatch);
 											}
 											break;
-										} else if (isSubsetOf(altMatch2, altMatch)) {
+										} else if (isSubsetOf(altMatch2, altMatch, opts)) {
 											setMergeLocators(altMatch2, altMatch);
 											break;
 										} else if (staticCanMatchDefinition(altMatch, altMatch2)) {

+ 2 - 2
lib/validation.js

@@ -4,7 +4,7 @@ import {
 	getPlaceholder,
 	getTest,
 	getTests,
-	getTestTemplate
+	getTestTemplate, isSubsetOf
 } from "./validation-tests";
 import keyCode from "./keycode.json";
 import {
@@ -466,7 +466,7 @@ function positionCanMatchDefinition(pos, testDefinition, opts) {
 	for (var tndx = 0; tndx < tests.length; tndx++) {
 		if (tests[tndx].match &&
 			((tests[tndx].match["nativeDef"] === testDefinition.match[opts.shiftPositions ? "def" : "nativeDef"] && (!opts.shiftPositions || !testDefinition.match.static)) ||
-				tests[tndx].match["nativeDef"] === testDefinition.match["nativeDef"])) {
+				tests[tndx].match["nativeDef"] === testDefinition.match["nativeDef"] || (opts.regex && !tests[tndx].match.static && tests[tndx].match.fn.test(testDefinition.input)))) {
 			valid = true;
 			break;
 		} else if (tests[tndx].match && tests[tndx].match["def"] === testDefinition.match["nativeDef"]) {

+ 1 - 1
package.json

@@ -1,6 +1,6 @@
 {
   "name": "inputmask",
-  "version": "5.0.6-beta.25",
+  "version": "5.0.6-beta.26",
   "description": "Inputmask is a javascript library which creates an input mask.  Inputmask can run against vanilla javascript, jQuery and jqlite.",
   "main": "dist/inputmask.js",
   "files": [