diff --git a/bower.json b/bower.json index 0cfe6d1e..56f96b68 100644 --- a/bower.json +++ b/bower.json @@ -8,7 +8,7 @@ "form", "input" ], - "version": "1.3.8", + "version": "1.4.0", "author": { "name": "Max Huang", "email": "risonhuang@gmail.com", diff --git a/dist/cleave-angular.js b/dist/cleave-angular.js index 73f0ad71..6a69b084 100644 --- a/dist/cleave-angular.js +++ b/dist/cleave-angular.js @@ -87,7 +87,7 @@ return /******/ (function(modules) { // webpackBootstrap var owner = this, pps = owner.properties; // no need to use this lib - if (!pps.numeral && !pps.phone && !pps.creditCard && !pps.date && (pps.blocksLength === 0 && !pps.prefix)) { + if (!pps.numeral && !pps.phone && !pps.creditCard && !pps.time && !pps.date && (pps.blocksLength === 0 && !pps.prefix)) { owner.onInput(pps.initValue); return; @@ -113,6 +113,7 @@ return /******/ (function(modules) { // webpackBootstrap owner.initPhoneFormatter(); owner.initDateFormatter(); + owner.initTimeFormatter(); owner.initNumeralFormatter(); // avoid touch input field if value is null @@ -140,6 +141,19 @@ return /******/ (function(modules) { // webpackBootstrap ); }, + initTimeFormatter: function() { + var owner = this, pps = owner.properties; + + if (!pps.time) { + return; + } + + pps.timeFormatter = new Cleave.TimeFormatter(pps.timePattern); + pps.blocks = pps.timeFormatter.getBlocks(); + pps.blocksLength = pps.blocks.length; + pps.maxLength = Cleave.Util.getMaxLength(pps.blocks); + }, + initDateFormatter: function () { var owner = this, pps = owner.properties; @@ -282,6 +296,11 @@ return /******/ (function(modules) { // webpackBootstrap value = pps.dateFormatter.getValidatedDate(value); } + // time + if (pps.time) { + value = pps.timeFormatter.getValidatedTime(value); + } + // strip delimiters value = Util.stripDelimiters(value, pps.delimiter, pps.delimiters); @@ -464,10 +483,11 @@ return /******/ (function(modules) { // webpackBootstrap Cleave.NumeralFormatter = __webpack_require__(1); Cleave.DateFormatter = __webpack_require__(2); - Cleave.PhoneFormatter = __webpack_require__(3); - Cleave.CreditCardDetector = __webpack_require__(4); - Cleave.Util = __webpack_require__(5); - Cleave.DefaultProperties = __webpack_require__(6); + Cleave.TimeFormatter = __webpack_require__(3); + Cleave.PhoneFormatter = __webpack_require__(4); + Cleave.CreditCardDetector = __webpack_require__(5); + Cleave.Util = __webpack_require__(6); + Cleave.DefaultProperties = __webpack_require__(7); // for angular directive ((typeof global === 'object' && global) ? global : window)['Cleave'] = Cleave; @@ -815,6 +835,168 @@ return /******/ (function(modules) { // webpackBootstrap 'use strict'; + var TimeFormatter = function (timePattern) { + var owner = this; + + owner.time = []; + owner.blocks = []; + owner.timePattern = timePattern; + owner.initBlocks(); + }; + + TimeFormatter.prototype = { + initBlocks: function () { + var owner = this; + owner.timePattern.forEach(function () { + owner.blocks.push(2); + }); + }, + + getISOFormatTime: function () { + var owner = this, + time = owner.time; + + return time[2] ? ( + owner.addLeadingZero(time[0]) + ':' + owner.addLeadingZero(time[1]) + ':' + owner.addLeadingZero(time[2]) + ) : ''; + }, + + getBlocks: function () { + return this.blocks; + }, + + getValidatedTime: function (value) { + var owner = this, result = ''; + + value = value.replace(/[^\d]/g, ''); + + owner.blocks.forEach(function (length, index) { + if (value.length > 0) { + var sub = value.slice(0, length), + sub0 = sub.slice(0, 1), + rest = value.slice(length); + + switch (owner.timePattern[index]) { + + case 'h': + if (parseInt(sub0, 10) > 2) { + sub = '0' + sub0; + } else if (parseInt(sub, 10) > 23) { + sub = '23'; + } + + break; + + case 'm': + case 's': + if (parseInt(sub0, 10) > 5) { + sub = '0' + sub0; + } else if (parseInt(sub, 10) > 60) { + sub = '60'; + } + break; + } + + result += sub; + + // update remaining string + value = rest; + } + }); + + return this.getFixedTimeString(result); + }, + + getFixedTimeString: function (value) { + var owner = this, timePattern = owner.timePattern, time = [], + secondIndex = 0, minuteIndex = 0, hourIndex = 0, + secondStartIndex = 0, minuteStartIndex = 0, hourStartIndex = 0, + second, minute, hour; + + if (value.length === 6) { + timePattern.forEach(function (type, index) { + switch (type) { + case 's': + secondIndex = index * 2; + break; + case 'm': + minuteIndex = index * 2; + break; + case 'h': + hourIndex = index * 2; + break; + } + }); + + hourStartIndex = hourIndex; + minuteStartIndex = minuteIndex; + secondStartIndex = secondIndex; + + second = parseInt(value.slice(secondStartIndex, secondStartIndex + 2), 10); + minute = parseInt(value.slice(minuteStartIndex, minuteStartIndex + 2), 10); + hour = parseInt(value.slice(hourStartIndex, hourStartIndex + 2), 10); + + time = this.getFixedTime(hour, minute, second); + } + + if (value.length === 4 && owner.timePattern.indexOf('s') < 0) { + timePattern.forEach(function (type, index) { + switch (type) { + case 'm': + minuteIndex = index * 2; + break; + case 'h': + hourIndex = index * 2; + break; + } + }); + + hourStartIndex = hourIndex; + minuteStartIndex = minuteIndex; + + second = 0; + minute = parseInt(value.slice(minuteStartIndex, minuteStartIndex + 2), 10); + hour = parseInt(value.slice(hourStartIndex, hourStartIndex + 2), 10); + + time = this.getFixedTime(hour, minute, second); + } + + owner.time = time; + + return time.length === 0 ? value : timePattern.reduce(function (previous, current) { + switch (current) { + case 's': + return previous + owner.addLeadingZero(time[2]); + case 'm': + return previous + owner.addLeadingZero(time[1]); + case 'h': + return previous + owner.addLeadingZero(time[0]); + } + }, ''); + }, + + getFixedTime: function (hour, minute, second) { + second = Math.min(parseInt(second || 0, 10), 60); + minute = Math.min(minute, 60); + hour = Math.min(hour, 60); + + return [hour, minute, second]; + }, + + addLeadingZero: function (number) { + return (number < 10 ? '0' : '') + number; + } + }; + + module.exports = TimeFormatter; + + +/***/ }), +/* 4 */ +/***/ (function(module, exports) { + + 'use strict'; + var PhoneFormatter = function (formatter, delimiter) { var owner = this; @@ -874,7 +1056,7 @@ return /******/ (function(modules) { // webpackBootstrap /***/ }), -/* 4 */ +/* 5 */ /***/ (function(module, exports) { 'use strict'; @@ -977,7 +1159,7 @@ return /******/ (function(modules) { // webpackBootstrap /***/ }), -/* 5 */ +/* 6 */ /***/ (function(module, exports) { 'use strict'; @@ -1198,7 +1380,7 @@ return /******/ (function(modules) { // webpackBootstrap /***/ }), -/* 6 */ +/* 7 */ /***/ (function(module, exports) { /* WEBPACK VAR INJECTION */(function(global) {'use strict'; @@ -1226,6 +1408,11 @@ return /******/ (function(modules) { // webpackBootstrap target.phoneRegionCode = opts.phoneRegionCode || 'AU'; target.phoneFormatter = {}; + // time + target.time = !!opts.time; + target.timePattern = opts.timePattern || ['h', 'm']; + target.timeFormatter = {}; + // date target.date = !!opts.date; target.datePattern = opts.datePattern || ['d', 'm', 'Y']; @@ -1257,9 +1444,10 @@ return /******/ (function(modules) { // webpackBootstrap target.delimiter = (opts.delimiter || opts.delimiter === '') ? opts.delimiter : (opts.date ? '/' : - (opts.numeral ? ',' : - (opts.phone ? ' ' : - ' '))); + (opts.time ? ':' : + (opts.numeral ? ',' : + (opts.phone ? ' ' : + ' ')))); target.delimiterLength = target.delimiter.length; target.delimiterLazyShow = !!opts.delimiterLazyShow; target.delimiters = opts.delimiters || []; diff --git a/dist/cleave-angular.min.js b/dist/cleave-angular.min.js index aeab7e36..6efead59 100644 --- a/dist/cleave-angular.min.js +++ b/dist/cleave-angular.min.js @@ -1,8 +1,8 @@ /*! - * cleave.js - 1.3.8 + * cleave.js - 1.4.0 * https://github.com/nosir/cleave.js * Apache License Version 2.0 * * Copyright (C) 2012-2018 Max Huang https://github.com/nosir/ */ -!function(root,factory){"object"==typeof exports&&"object"==typeof module?module.exports=factory():"function"==typeof define&&define.amd?define([],factory):"object"==typeof exports?exports.Cleave=factory():root.Cleave=factory()}(this,function(){return function(modules){function __webpack_require__(moduleId){if(installedModules[moduleId])return installedModules[moduleId].exports;var module=installedModules[moduleId]={exports:{},id:moduleId,loaded:!1};return modules[moduleId].call(module.exports,module,module.exports,__webpack_require__),module.loaded=!0,module.exports}var installedModules={};return __webpack_require__.m=modules,__webpack_require__.c=installedModules,__webpack_require__.p="",__webpack_require__(0)}([function(module,exports,__webpack_require__){(function(global){"use strict";var Cleave=function(element,opts){var owner=this;if("string"==typeof element?owner.element=document.querySelector(element):owner.element="undefined"!=typeof element.length&&element.length>0?element[0]:element,!owner.element)throw new Error("[cleave.js] Please check the element");opts.initValue=owner.element.value,owner.properties=Cleave.DefaultProperties.assign({},opts),owner.init()};Cleave.prototype={init:function(){var owner=this,pps=owner.properties;return pps.numeral||pps.phone||pps.creditCard||pps.date||0!==pps.blocksLength||pps.prefix?(pps.maxLength=Cleave.Util.getMaxLength(pps.blocks),owner.isAndroid=Cleave.Util.isAndroid(),owner.lastInputValue="",owner.onChangeListener=owner.onChange.bind(owner),owner.onKeyDownListener=owner.onKeyDown.bind(owner),owner.onFocusListener=owner.onFocus.bind(owner),owner.onCutListener=owner.onCut.bind(owner),owner.onCopyListener=owner.onCopy.bind(owner),owner.element.addEventListener("input",owner.onChangeListener),owner.element.addEventListener("keydown",owner.onKeyDownListener),owner.element.addEventListener("focus",owner.onFocusListener),owner.element.addEventListener("cut",owner.onCutListener),owner.element.addEventListener("copy",owner.onCopyListener),owner.initPhoneFormatter(),owner.initDateFormatter(),owner.initNumeralFormatter(),void((pps.initValue||pps.prefix&&!pps.noImmediatePrefix)&&owner.onInput(pps.initValue))):void owner.onInput(pps.initValue)},initNumeralFormatter:function(){var owner=this,pps=owner.properties;pps.numeral&&(pps.numeralFormatter=new Cleave.NumeralFormatter(pps.numeralDecimalMark,pps.numeralIntegerScale,pps.numeralDecimalScale,pps.numeralThousandsGroupStyle,pps.numeralPositiveOnly,pps.stripLeadingZeroes,pps.delimiter))},initDateFormatter:function(){var owner=this,pps=owner.properties;pps.date&&(pps.dateFormatter=new Cleave.DateFormatter(pps.datePattern),pps.blocks=pps.dateFormatter.getBlocks(),pps.blocksLength=pps.blocks.length,pps.maxLength=Cleave.Util.getMaxLength(pps.blocks))},initPhoneFormatter:function(){var owner=this,pps=owner.properties;if(pps.phone)try{pps.phoneFormatter=new Cleave.PhoneFormatter(new pps.root.Cleave.AsYouTypeFormatter(pps.phoneRegionCode),pps.delimiter)}catch(ex){throw new Error("[cleave.js] Please include phone-type-formatter.{country}.js lib")}},onKeyDown:function(event){var owner=this,pps=owner.properties,charCode=event.which||event.keyCode,Util=Cleave.Util,currentValue=owner.element.value;return Util.isAndroidBackspaceKeydown(owner.lastInputValue,currentValue)&&(charCode=8),owner.lastInputValue=currentValue,8===charCode&&Util.isDelimiter(currentValue.slice(-pps.delimiterLength),pps.delimiter,pps.delimiters)?void(pps.backspace=!0):void(pps.backspace=!1)},onChange:function(){this.onInput(this.element.value)},onFocus:function(){var owner=this,pps=owner.properties;Cleave.Util.fixPrefixCursor(owner.element,pps.prefix,pps.delimiter,pps.delimiters)},onCut:function(e){this.copyClipboardData(e),this.onInput("")},onCopy:function(e){this.copyClipboardData(e)},copyClipboardData:function(e){var owner=this,pps=owner.properties,Util=Cleave.Util,inputValue=owner.element.value,textToCopy="";textToCopy=pps.copyDelimiter?inputValue:Util.stripDelimiters(inputValue,pps.delimiter,pps.delimiters);try{e.clipboardData?e.clipboardData.setData("Text",textToCopy):window.clipboardData.setData("Text",textToCopy),e.preventDefault()}catch(ex){}},onInput:function(value){var owner=this,pps=owner.properties,Util=Cleave.Util;return pps.numeral||!pps.backspace||Util.isDelimiter(value.slice(-pps.delimiterLength),pps.delimiter,pps.delimiters)||(value=Util.headStr(value,value.length-pps.delimiterLength)),pps.phone?(!pps.prefix||pps.noImmediatePrefix&&!value.length?pps.result=pps.phoneFormatter.format(value):pps.result=pps.prefix+pps.phoneFormatter.format(value).slice(pps.prefix.length),void owner.updateValueState()):pps.numeral?(!pps.prefix||pps.noImmediatePrefix&&!value.length?pps.result=pps.numeralFormatter.format(value):pps.result=pps.prefix+pps.numeralFormatter.format(value),void owner.updateValueState()):(pps.date&&(value=pps.dateFormatter.getValidatedDate(value)),value=Util.stripDelimiters(value,pps.delimiter,pps.delimiters),value=Util.getPrefixStrippedValue(value,pps.prefix,pps.prefixLength,pps.result),value=pps.numericOnly?Util.strip(value,/[^\d]/g):value,value=pps.uppercase?value.toUpperCase():value,value=pps.lowercase?value.toLowerCase():value,!pps.prefix||pps.noImmediatePrefix&&!value.length||(value=pps.prefix+value,0!==pps.blocksLength)?(pps.creditCard&&owner.updateCreditCardPropsByValue(value),value=Util.headStr(value,pps.maxLength),pps.result=Util.getFormattedValue(value,pps.blocks,pps.blocksLength,pps.delimiter,pps.delimiters,pps.delimiterLazyShow),void owner.updateValueState()):(pps.result=value,void owner.updateValueState()))},updateCreditCardPropsByValue:function(value){var creditCardInfo,owner=this,pps=owner.properties,Util=Cleave.Util;Util.headStr(pps.result,4)!==Util.headStr(value,4)&&(creditCardInfo=Cleave.CreditCardDetector.getInfo(value,pps.creditCardStrictMode),pps.blocks=creditCardInfo.blocks,pps.blocksLength=pps.blocks.length,pps.maxLength=Util.getMaxLength(pps.blocks),pps.creditCardType!==creditCardInfo.type&&(pps.creditCardType=creditCardInfo.type,pps.onCreditCardTypeChanged.call(owner,pps.creditCardType)))},updateValueState:function(){var owner=this,Util=Cleave.Util,pps=owner.properties;if(owner.element){var endPos=owner.element.selectionEnd,oldValue=owner.element.value,newValue=pps.result;if(endPos=Util.getNextCursorPosition(endPos,oldValue,newValue,pps.delimiter,pps.delimiters),owner.isAndroid)return void window.setTimeout(function(){owner.element.value=newValue,Util.setSelection(owner.element,endPos,pps.document,!1),owner.callOnValueChanged()},1);owner.element.value=newValue,Util.setSelection(owner.element,endPos,pps.document,!1),owner.callOnValueChanged()}},callOnValueChanged:function(){var owner=this,pps=owner.properties;pps.onValueChanged.call(owner,{target:{value:pps.result,rawValue:owner.getRawValue()}})},setPhoneRegionCode:function(phoneRegionCode){var owner=this,pps=owner.properties;pps.phoneRegionCode=phoneRegionCode,owner.initPhoneFormatter(),owner.onChange()},setRawValue:function(value){var owner=this,pps=owner.properties;value=void 0!==value&&null!==value?value.toString():"",pps.numeral&&(value=value.replace(".",pps.numeralDecimalMark)),pps.backspace=!1,owner.element.value=value,owner.onInput(value)},getRawValue:function(){var owner=this,pps=owner.properties,Util=Cleave.Util,rawValue=owner.element.value;return pps.rawValueTrimPrefix&&(rawValue=Util.getPrefixStrippedValue(rawValue,pps.prefix,pps.prefixLength,pps.result)),rawValue=pps.numeral?pps.numeralFormatter.getRawValue(rawValue):Util.stripDelimiters(rawValue,pps.delimiter,pps.delimiters)},getISOFormatDate:function(){var owner=this,pps=owner.properties;return pps.date?pps.dateFormatter.getISOFormatDate():""},getFormattedValue:function(){return this.element.value},destroy:function(){var owner=this;owner.element.removeEventListener("input",owner.onChangeListener),owner.element.removeEventListener("keydown",owner.onKeyDownListener),owner.element.removeEventListener("focus",owner.onFocusListener),owner.element.removeEventListener("cut",owner.onCutListener),owner.element.removeEventListener("copy",owner.onCopyListener)},toString:function(){return"[Cleave Object]"}},Cleave.NumeralFormatter=__webpack_require__(1),Cleave.DateFormatter=__webpack_require__(2),Cleave.PhoneFormatter=__webpack_require__(3),Cleave.CreditCardDetector=__webpack_require__(4),Cleave.Util=__webpack_require__(5),Cleave.DefaultProperties=__webpack_require__(6),("object"==typeof global&&global?global:window).Cleave=Cleave,module.exports=Cleave,angular.module("cleave.js",[]).directive("cleave",function(){return{restrict:"A",require:"ngModel",scope:{cleave:"&",onInit:"&?",onValueChange:"&?"},compile:function(){return{pre:function($scope,$element,attrs,ngModelCtrl){$scope.instance=new Cleave($element[0],$scope.cleave()),$scope.onInit&&$scope.onInit()($scope.instance),ngModelCtrl.$formatters.push(function(val){return $scope.instance.setRawValue(val),$scope.instance.getFormattedValue()}),ngModelCtrl.$parsers.push(function(newFormattedValue){return $scope.onValueChange&&$scope.onValueChange()(newFormattedValue),$scope.instance.getRawValue()}),$scope.$watch(function(){return $scope.cleave()},function(newOptions,oldOptions){$scope.instance.destroy(),$scope.instance=new Cleave($element[0],newOptions)},!0),$scope.$on("$destroy",function(){$scope.instance.destroy(),$scope.instance=null})}}}}})}).call(exports,function(){return this}())},function(module,exports){"use strict";var NumeralFormatter=function(numeralDecimalMark,numeralIntegerScale,numeralDecimalScale,numeralThousandsGroupStyle,numeralPositiveOnly,stripLeadingZeroes,delimiter){var owner=this;owner.numeralDecimalMark=numeralDecimalMark||".",owner.numeralIntegerScale=numeralIntegerScale>0?numeralIntegerScale:0,owner.numeralDecimalScale=numeralDecimalScale>=0?numeralDecimalScale:2,owner.numeralThousandsGroupStyle=numeralThousandsGroupStyle||NumeralFormatter.groupStyle.thousand,owner.numeralPositiveOnly=!!numeralPositiveOnly,owner.stripLeadingZeroes=stripLeadingZeroes!==!1,owner.delimiter=delimiter||""===delimiter?delimiter:",",owner.delimiterRE=delimiter?new RegExp("\\"+delimiter,"g"):""};NumeralFormatter.groupStyle={thousand:"thousand",lakh:"lakh",wan:"wan",none:"none"},NumeralFormatter.prototype={getRawValue:function(value){return value.replace(this.delimiterRE,"").replace(this.numeralDecimalMark,".")},format:function(value){var parts,partInteger,owner=this,partDecimal="";switch(value=value.replace(/[A-Za-z]/g,"").replace(owner.numeralDecimalMark,"M").replace(/[^\dM-]/g,"").replace(/^\-/,"N").replace(/\-/g,"").replace("N",owner.numeralPositiveOnly?"":"-").replace("M",owner.numeralDecimalMark),owner.stripLeadingZeroes&&(value=value.replace(/^(-)?0+(?=\d)/,"$1")),partInteger=value,value.indexOf(owner.numeralDecimalMark)>=0&&(parts=value.split(owner.numeralDecimalMark),partInteger=parts[0],partDecimal=owner.numeralDecimalMark+parts[1].slice(0,owner.numeralDecimalScale)),owner.numeralIntegerScale>0&&(partInteger=partInteger.slice(0,owner.numeralIntegerScale+("-"===value.slice(0,1)?1:0))),owner.numeralThousandsGroupStyle){case NumeralFormatter.groupStyle.lakh:partInteger=partInteger.replace(/(\d)(?=(\d\d)+\d$)/g,"$1"+owner.delimiter);break;case NumeralFormatter.groupStyle.wan:partInteger=partInteger.replace(/(\d)(?=(\d{4})+$)/g,"$1"+owner.delimiter);break;case NumeralFormatter.groupStyle.thousand:partInteger=partInteger.replace(/(\d)(?=(\d{3})+$)/g,"$1"+owner.delimiter)}return partInteger.toString()+(owner.numeralDecimalScale>0?partDecimal.toString():"")}},module.exports=NumeralFormatter},function(module,exports){"use strict";var DateFormatter=function(datePattern){var owner=this;owner.date=[],owner.blocks=[],owner.datePattern=datePattern,owner.initBlocks()};DateFormatter.prototype={initBlocks:function(){var owner=this;owner.datePattern.forEach(function(value){"Y"===value?owner.blocks.push(4):owner.blocks.push(2)})},getISOFormatDate:function(){var owner=this,date=owner.date;return date[2]?date[2]+"-"+owner.addLeadingZero(date[1])+"-"+owner.addLeadingZero(date[0]):""},getBlocks:function(){return this.blocks},getValidatedDate:function(value){var owner=this,result="";return value=value.replace(/[^\d]/g,""),owner.blocks.forEach(function(length,index){if(value.length>0){var sub=value.slice(0,length),sub0=sub.slice(0,1),rest=value.slice(length);switch(owner.datePattern[index]){case"d":"00"===sub?sub="01":parseInt(sub0,10)>3?sub="0"+sub0:parseInt(sub,10)>31&&(sub="31");break;case"m":"00"===sub?sub="01":parseInt(sub0,10)>1?sub="0"+sub0:parseInt(sub,10)>12&&(sub="12")}result+=sub,value=rest}}),this.getFixedDateString(result)},getFixedDateString:function(value){var day,month,year,owner=this,datePattern=owner.datePattern,date=[],dayIndex=0,monthIndex=0,yearIndex=0,dayStartIndex=0,monthStartIndex=0,yearStartIndex=0,fullYearDone=!1;return 4===value.length&&"y"!==datePattern[0].toLowerCase()&&"y"!==datePattern[1].toLowerCase()&&(dayStartIndex="d"===datePattern[0]?0:2,monthStartIndex=2-dayStartIndex,day=parseInt(value.slice(dayStartIndex,dayStartIndex+2),10),month=parseInt(value.slice(monthStartIndex,monthStartIndex+2),10),date=this.getFixedDate(day,month,0)),8===value.length&&(datePattern.forEach(function(type,index){switch(type){case"d":dayIndex=index;break;case"m":monthIndex=index;break;default:yearIndex=index}}),yearStartIndex=2*yearIndex,dayStartIndex=dayIndex<=yearIndex?2*dayIndex:2*dayIndex+2,monthStartIndex=monthIndex<=yearIndex?2*monthIndex:2*monthIndex+2,day=parseInt(value.slice(dayStartIndex,dayStartIndex+2),10),month=parseInt(value.slice(monthStartIndex,monthStartIndex+2),10),year=parseInt(value.slice(yearStartIndex,yearStartIndex+4),10),fullYearDone=4===value.slice(yearStartIndex,yearStartIndex+4).length,date=this.getFixedDate(day,month,year)),owner.date=date,0===date.length?value:datePattern.reduce(function(previous,current){switch(current){case"d":return previous+owner.addLeadingZero(date[0]);case"m":return previous+owner.addLeadingZero(date[1]);default:return previous+(fullYearDone?owner.addLeadingZeroForYear(date[2]):"")}},"")},getFixedDate:function(day,month,year){return day=Math.min(day,31),month=Math.min(month,12),year=parseInt(year||0,10),(month<7&&month%2===0||month>8&&month%2===1)&&(day=Math.min(day,2===month?this.isLeapYear(year)?29:28:30)),[day,month,year]},isLeapYear:function(year){return year%4===0&&year%100!==0||year%400===0},addLeadingZero:function(number){return(number<10?"0":"")+number},addLeadingZeroForYear:function(number){return(number<10?"000":number<100?"00":number<1e3?"0":"")+number}},module.exports=DateFormatter},function(module,exports){"use strict";var PhoneFormatter=function(formatter,delimiter){var owner=this;owner.delimiter=delimiter||""===delimiter?delimiter:" ",owner.delimiterRE=delimiter?new RegExp("\\"+delimiter,"g"):"",owner.formatter=formatter};PhoneFormatter.prototype={setFormatter:function(formatter){this.formatter=formatter},format:function(phoneNumber){var owner=this;owner.formatter.clear(),phoneNumber=phoneNumber.replace(/[^\d+]/g,""),phoneNumber=phoneNumber.replace(owner.delimiterRE,"");for(var current,result="",validated=!1,i=0,iMax=phoneNumber.length;iprefixLength?prevValue:prefix;else{var diffIndex=this.getFirstDiffIndex(prefix,value.slice(0,prefixLength));value=prefix+value.slice(diffIndex,diffIndex+1)+value.slice(prefixLength+1)}return value.slice(prefixLength)},getFirstDiffIndex:function(prev,current){for(var index=0;prev.charAt(index)===current.charAt(index);)if(""===prev.charAt(index++))return-1;return index},getFormattedValue:function(value,blocks,blocksLength,delimiter,delimiters,delimiterLazyShow){var currentDelimiter,result="",multipleDelimiters=delimiters.length>0;return 0===blocksLength?value:(blocks.forEach(function(length,index){if(value.length>0){var sub=value.slice(0,length),rest=value.slice(length);currentDelimiter=multipleDelimiters?delimiters[delimiterLazyShow?index-1:index]||currentDelimiter:delimiter,delimiterLazyShow?(index>0&&(result+=currentDelimiter),result+=sub):(result+=sub,sub.length===length&&index0?opts.numeralIntegerScale:0,target.numeralDecimalScale=opts.numeralDecimalScale>=0?opts.numeralDecimalScale:2,target.numeralDecimalMark=opts.numeralDecimalMark||".",target.numeralThousandsGroupStyle=opts.numeralThousandsGroupStyle||"thousand",target.numeralPositiveOnly=!!opts.numeralPositiveOnly,target.stripLeadingZeroes=opts.stripLeadingZeroes!==!1,target.numericOnly=target.creditCard||target.date||!!opts.numericOnly,target.uppercase=!!opts.uppercase,target.lowercase=!!opts.lowercase,target.prefix=target.creditCard||target.date?"":opts.prefix||"",target.noImmediatePrefix=!!opts.noImmediatePrefix,target.prefixLength=target.prefix.length,target.rawValueTrimPrefix=!!opts.rawValueTrimPrefix,target.copyDelimiter=!!opts.copyDelimiter,target.initValue=void 0!==opts.initValue&&null!==opts.initValue?opts.initValue.toString():"",target.delimiter=opts.delimiter||""===opts.delimiter?opts.delimiter:opts.date?"/":opts.numeral?",":(opts.phone," "),target.delimiterLength=target.delimiter.length,target.delimiterLazyShow=!!opts.delimiterLazyShow,target.delimiters=opts.delimiters||[],target.blocks=opts.blocks||[],target.blocksLength=target.blocks.length,target.root="object"==typeof global&&global?global:window,target.document=opts.document||target.root.document,target.maxLength=0,target.backspace=!1,target.result="",target.onValueChanged=opts.onValueChanged||function(){},target}};module.exports=DefaultProperties}).call(exports,function(){return this}())}])}); \ No newline at end of file +!function(root,factory){"object"==typeof exports&&"object"==typeof module?module.exports=factory():"function"==typeof define&&define.amd?define([],factory):"object"==typeof exports?exports.Cleave=factory():root.Cleave=factory()}(this,function(){return function(modules){function __webpack_require__(moduleId){if(installedModules[moduleId])return installedModules[moduleId].exports;var module=installedModules[moduleId]={exports:{},id:moduleId,loaded:!1};return modules[moduleId].call(module.exports,module,module.exports,__webpack_require__),module.loaded=!0,module.exports}var installedModules={};return __webpack_require__.m=modules,__webpack_require__.c=installedModules,__webpack_require__.p="",__webpack_require__(0)}([function(module,exports,__webpack_require__){(function(global){"use strict";var Cleave=function(element,opts){var owner=this;if("string"==typeof element?owner.element=document.querySelector(element):owner.element="undefined"!=typeof element.length&&element.length>0?element[0]:element,!owner.element)throw new Error("[cleave.js] Please check the element");opts.initValue=owner.element.value,owner.properties=Cleave.DefaultProperties.assign({},opts),owner.init()};Cleave.prototype={init:function(){var owner=this,pps=owner.properties;return pps.numeral||pps.phone||pps.creditCard||pps.time||pps.date||0!==pps.blocksLength||pps.prefix?(pps.maxLength=Cleave.Util.getMaxLength(pps.blocks),owner.isAndroid=Cleave.Util.isAndroid(),owner.lastInputValue="",owner.onChangeListener=owner.onChange.bind(owner),owner.onKeyDownListener=owner.onKeyDown.bind(owner),owner.onFocusListener=owner.onFocus.bind(owner),owner.onCutListener=owner.onCut.bind(owner),owner.onCopyListener=owner.onCopy.bind(owner),owner.element.addEventListener("input",owner.onChangeListener),owner.element.addEventListener("keydown",owner.onKeyDownListener),owner.element.addEventListener("focus",owner.onFocusListener),owner.element.addEventListener("cut",owner.onCutListener),owner.element.addEventListener("copy",owner.onCopyListener),owner.initPhoneFormatter(),owner.initDateFormatter(),owner.initTimeFormatter(),owner.initNumeralFormatter(),void((pps.initValue||pps.prefix&&!pps.noImmediatePrefix)&&owner.onInput(pps.initValue))):void owner.onInput(pps.initValue)},initNumeralFormatter:function(){var owner=this,pps=owner.properties;pps.numeral&&(pps.numeralFormatter=new Cleave.NumeralFormatter(pps.numeralDecimalMark,pps.numeralIntegerScale,pps.numeralDecimalScale,pps.numeralThousandsGroupStyle,pps.numeralPositiveOnly,pps.stripLeadingZeroes,pps.delimiter))},initTimeFormatter:function(){var owner=this,pps=owner.properties;pps.time&&(pps.timeFormatter=new Cleave.TimeFormatter(pps.timePattern),pps.blocks=pps.timeFormatter.getBlocks(),pps.blocksLength=pps.blocks.length,pps.maxLength=Cleave.Util.getMaxLength(pps.blocks))},initDateFormatter:function(){var owner=this,pps=owner.properties;pps.date&&(pps.dateFormatter=new Cleave.DateFormatter(pps.datePattern),pps.blocks=pps.dateFormatter.getBlocks(),pps.blocksLength=pps.blocks.length,pps.maxLength=Cleave.Util.getMaxLength(pps.blocks))},initPhoneFormatter:function(){var owner=this,pps=owner.properties;if(pps.phone)try{pps.phoneFormatter=new Cleave.PhoneFormatter(new pps.root.Cleave.AsYouTypeFormatter(pps.phoneRegionCode),pps.delimiter)}catch(ex){throw new Error("[cleave.js] Please include phone-type-formatter.{country}.js lib")}},onKeyDown:function(event){var owner=this,pps=owner.properties,charCode=event.which||event.keyCode,Util=Cleave.Util,currentValue=owner.element.value;return Util.isAndroidBackspaceKeydown(owner.lastInputValue,currentValue)&&(charCode=8),owner.lastInputValue=currentValue,8===charCode&&Util.isDelimiter(currentValue.slice(-pps.delimiterLength),pps.delimiter,pps.delimiters)?void(pps.backspace=!0):void(pps.backspace=!1)},onChange:function(){this.onInput(this.element.value)},onFocus:function(){var owner=this,pps=owner.properties;Cleave.Util.fixPrefixCursor(owner.element,pps.prefix,pps.delimiter,pps.delimiters)},onCut:function(e){this.copyClipboardData(e),this.onInput("")},onCopy:function(e){this.copyClipboardData(e)},copyClipboardData:function(e){var owner=this,pps=owner.properties,Util=Cleave.Util,inputValue=owner.element.value,textToCopy="";textToCopy=pps.copyDelimiter?inputValue:Util.stripDelimiters(inputValue,pps.delimiter,pps.delimiters);try{e.clipboardData?e.clipboardData.setData("Text",textToCopy):window.clipboardData.setData("Text",textToCopy),e.preventDefault()}catch(ex){}},onInput:function(value){var owner=this,pps=owner.properties,Util=Cleave.Util;return pps.numeral||!pps.backspace||Util.isDelimiter(value.slice(-pps.delimiterLength),pps.delimiter,pps.delimiters)||(value=Util.headStr(value,value.length-pps.delimiterLength)),pps.phone?(!pps.prefix||pps.noImmediatePrefix&&!value.length?pps.result=pps.phoneFormatter.format(value):pps.result=pps.prefix+pps.phoneFormatter.format(value).slice(pps.prefix.length),void owner.updateValueState()):pps.numeral?(!pps.prefix||pps.noImmediatePrefix&&!value.length?pps.result=pps.numeralFormatter.format(value):pps.result=pps.prefix+pps.numeralFormatter.format(value),void owner.updateValueState()):(pps.date&&(value=pps.dateFormatter.getValidatedDate(value)),pps.time&&(value=pps.timeFormatter.getValidatedTime(value)),value=Util.stripDelimiters(value,pps.delimiter,pps.delimiters),value=Util.getPrefixStrippedValue(value,pps.prefix,pps.prefixLength,pps.result),value=pps.numericOnly?Util.strip(value,/[^\d]/g):value,value=pps.uppercase?value.toUpperCase():value,value=pps.lowercase?value.toLowerCase():value,!pps.prefix||pps.noImmediatePrefix&&!value.length||(value=pps.prefix+value,0!==pps.blocksLength)?(pps.creditCard&&owner.updateCreditCardPropsByValue(value),value=Util.headStr(value,pps.maxLength),pps.result=Util.getFormattedValue(value,pps.blocks,pps.blocksLength,pps.delimiter,pps.delimiters,pps.delimiterLazyShow),void owner.updateValueState()):(pps.result=value,void owner.updateValueState()))},updateCreditCardPropsByValue:function(value){var creditCardInfo,owner=this,pps=owner.properties,Util=Cleave.Util;Util.headStr(pps.result,4)!==Util.headStr(value,4)&&(creditCardInfo=Cleave.CreditCardDetector.getInfo(value,pps.creditCardStrictMode),pps.blocks=creditCardInfo.blocks,pps.blocksLength=pps.blocks.length,pps.maxLength=Util.getMaxLength(pps.blocks),pps.creditCardType!==creditCardInfo.type&&(pps.creditCardType=creditCardInfo.type,pps.onCreditCardTypeChanged.call(owner,pps.creditCardType)))},updateValueState:function(){var owner=this,Util=Cleave.Util,pps=owner.properties;if(owner.element){var endPos=owner.element.selectionEnd,oldValue=owner.element.value,newValue=pps.result;if(endPos=Util.getNextCursorPosition(endPos,oldValue,newValue,pps.delimiter,pps.delimiters),owner.isAndroid)return void window.setTimeout(function(){owner.element.value=newValue,Util.setSelection(owner.element,endPos,pps.document,!1),owner.callOnValueChanged()},1);owner.element.value=newValue,Util.setSelection(owner.element,endPos,pps.document,!1),owner.callOnValueChanged()}},callOnValueChanged:function(){var owner=this,pps=owner.properties;pps.onValueChanged.call(owner,{target:{value:pps.result,rawValue:owner.getRawValue()}})},setPhoneRegionCode:function(phoneRegionCode){var owner=this,pps=owner.properties;pps.phoneRegionCode=phoneRegionCode,owner.initPhoneFormatter(),owner.onChange()},setRawValue:function(value){var owner=this,pps=owner.properties;value=void 0!==value&&null!==value?value.toString():"",pps.numeral&&(value=value.replace(".",pps.numeralDecimalMark)),pps.backspace=!1,owner.element.value=value,owner.onInput(value)},getRawValue:function(){var owner=this,pps=owner.properties,Util=Cleave.Util,rawValue=owner.element.value;return pps.rawValueTrimPrefix&&(rawValue=Util.getPrefixStrippedValue(rawValue,pps.prefix,pps.prefixLength,pps.result)),rawValue=pps.numeral?pps.numeralFormatter.getRawValue(rawValue):Util.stripDelimiters(rawValue,pps.delimiter,pps.delimiters)},getISOFormatDate:function(){var owner=this,pps=owner.properties;return pps.date?pps.dateFormatter.getISOFormatDate():""},getFormattedValue:function(){return this.element.value},destroy:function(){var owner=this;owner.element.removeEventListener("input",owner.onChangeListener),owner.element.removeEventListener("keydown",owner.onKeyDownListener),owner.element.removeEventListener("focus",owner.onFocusListener),owner.element.removeEventListener("cut",owner.onCutListener),owner.element.removeEventListener("copy",owner.onCopyListener)},toString:function(){return"[Cleave Object]"}},Cleave.NumeralFormatter=__webpack_require__(1),Cleave.DateFormatter=__webpack_require__(2),Cleave.TimeFormatter=__webpack_require__(3),Cleave.PhoneFormatter=__webpack_require__(4),Cleave.CreditCardDetector=__webpack_require__(5),Cleave.Util=__webpack_require__(6),Cleave.DefaultProperties=__webpack_require__(7),("object"==typeof global&&global?global:window).Cleave=Cleave,module.exports=Cleave,angular.module("cleave.js",[]).directive("cleave",function(){return{restrict:"A",require:"ngModel",scope:{cleave:"&",onInit:"&?",onValueChange:"&?"},compile:function(){return{pre:function($scope,$element,attrs,ngModelCtrl){$scope.instance=new Cleave($element[0],$scope.cleave()),$scope.onInit&&$scope.onInit()($scope.instance),ngModelCtrl.$formatters.push(function(val){return $scope.instance.setRawValue(val),$scope.instance.getFormattedValue()}),ngModelCtrl.$parsers.push(function(newFormattedValue){return $scope.onValueChange&&$scope.onValueChange()(newFormattedValue),$scope.instance.getRawValue()}),$scope.$watch(function(){return $scope.cleave()},function(newOptions,oldOptions){$scope.instance.destroy(),$scope.instance=new Cleave($element[0],newOptions)},!0),$scope.$on("$destroy",function(){$scope.instance.destroy(),$scope.instance=null})}}}}})}).call(exports,function(){return this}())},function(module,exports){"use strict";var NumeralFormatter=function(numeralDecimalMark,numeralIntegerScale,numeralDecimalScale,numeralThousandsGroupStyle,numeralPositiveOnly,stripLeadingZeroes,delimiter){var owner=this;owner.numeralDecimalMark=numeralDecimalMark||".",owner.numeralIntegerScale=numeralIntegerScale>0?numeralIntegerScale:0,owner.numeralDecimalScale=numeralDecimalScale>=0?numeralDecimalScale:2,owner.numeralThousandsGroupStyle=numeralThousandsGroupStyle||NumeralFormatter.groupStyle.thousand,owner.numeralPositiveOnly=!!numeralPositiveOnly,owner.stripLeadingZeroes=stripLeadingZeroes!==!1,owner.delimiter=delimiter||""===delimiter?delimiter:",",owner.delimiterRE=delimiter?new RegExp("\\"+delimiter,"g"):""};NumeralFormatter.groupStyle={thousand:"thousand",lakh:"lakh",wan:"wan",none:"none"},NumeralFormatter.prototype={getRawValue:function(value){return value.replace(this.delimiterRE,"").replace(this.numeralDecimalMark,".")},format:function(value){var parts,partInteger,owner=this,partDecimal="";switch(value=value.replace(/[A-Za-z]/g,"").replace(owner.numeralDecimalMark,"M").replace(/[^\dM-]/g,"").replace(/^\-/,"N").replace(/\-/g,"").replace("N",owner.numeralPositiveOnly?"":"-").replace("M",owner.numeralDecimalMark),owner.stripLeadingZeroes&&(value=value.replace(/^(-)?0+(?=\d)/,"$1")),partInteger=value,value.indexOf(owner.numeralDecimalMark)>=0&&(parts=value.split(owner.numeralDecimalMark),partInteger=parts[0],partDecimal=owner.numeralDecimalMark+parts[1].slice(0,owner.numeralDecimalScale)),owner.numeralIntegerScale>0&&(partInteger=partInteger.slice(0,owner.numeralIntegerScale+("-"===value.slice(0,1)?1:0))),owner.numeralThousandsGroupStyle){case NumeralFormatter.groupStyle.lakh:partInteger=partInteger.replace(/(\d)(?=(\d\d)+\d$)/g,"$1"+owner.delimiter);break;case NumeralFormatter.groupStyle.wan:partInteger=partInteger.replace(/(\d)(?=(\d{4})+$)/g,"$1"+owner.delimiter);break;case NumeralFormatter.groupStyle.thousand:partInteger=partInteger.replace(/(\d)(?=(\d{3})+$)/g,"$1"+owner.delimiter)}return partInteger.toString()+(owner.numeralDecimalScale>0?partDecimal.toString():"")}},module.exports=NumeralFormatter},function(module,exports){"use strict";var DateFormatter=function(datePattern){var owner=this;owner.date=[],owner.blocks=[],owner.datePattern=datePattern,owner.initBlocks()};DateFormatter.prototype={initBlocks:function(){var owner=this;owner.datePattern.forEach(function(value){"Y"===value?owner.blocks.push(4):owner.blocks.push(2)})},getISOFormatDate:function(){var owner=this,date=owner.date;return date[2]?date[2]+"-"+owner.addLeadingZero(date[1])+"-"+owner.addLeadingZero(date[0]):""},getBlocks:function(){return this.blocks},getValidatedDate:function(value){var owner=this,result="";return value=value.replace(/[^\d]/g,""),owner.blocks.forEach(function(length,index){if(value.length>0){var sub=value.slice(0,length),sub0=sub.slice(0,1),rest=value.slice(length);switch(owner.datePattern[index]){case"d":"00"===sub?sub="01":parseInt(sub0,10)>3?sub="0"+sub0:parseInt(sub,10)>31&&(sub="31");break;case"m":"00"===sub?sub="01":parseInt(sub0,10)>1?sub="0"+sub0:parseInt(sub,10)>12&&(sub="12")}result+=sub,value=rest}}),this.getFixedDateString(result)},getFixedDateString:function(value){var day,month,year,owner=this,datePattern=owner.datePattern,date=[],dayIndex=0,monthIndex=0,yearIndex=0,dayStartIndex=0,monthStartIndex=0,yearStartIndex=0,fullYearDone=!1;return 4===value.length&&"y"!==datePattern[0].toLowerCase()&&"y"!==datePattern[1].toLowerCase()&&(dayStartIndex="d"===datePattern[0]?0:2,monthStartIndex=2-dayStartIndex,day=parseInt(value.slice(dayStartIndex,dayStartIndex+2),10),month=parseInt(value.slice(monthStartIndex,monthStartIndex+2),10),date=this.getFixedDate(day,month,0)),8===value.length&&(datePattern.forEach(function(type,index){switch(type){case"d":dayIndex=index;break;case"m":monthIndex=index;break;default:yearIndex=index}}),yearStartIndex=2*yearIndex,dayStartIndex=dayIndex<=yearIndex?2*dayIndex:2*dayIndex+2,monthStartIndex=monthIndex<=yearIndex?2*monthIndex:2*monthIndex+2,day=parseInt(value.slice(dayStartIndex,dayStartIndex+2),10),month=parseInt(value.slice(monthStartIndex,monthStartIndex+2),10),year=parseInt(value.slice(yearStartIndex,yearStartIndex+4),10),fullYearDone=4===value.slice(yearStartIndex,yearStartIndex+4).length,date=this.getFixedDate(day,month,year)),owner.date=date,0===date.length?value:datePattern.reduce(function(previous,current){switch(current){case"d":return previous+owner.addLeadingZero(date[0]);case"m":return previous+owner.addLeadingZero(date[1]);default:return previous+(fullYearDone?owner.addLeadingZeroForYear(date[2]):"")}},"")},getFixedDate:function(day,month,year){return day=Math.min(day,31),month=Math.min(month,12),year=parseInt(year||0,10),(month<7&&month%2===0||month>8&&month%2===1)&&(day=Math.min(day,2===month?this.isLeapYear(year)?29:28:30)),[day,month,year]},isLeapYear:function(year){return year%4===0&&year%100!==0||year%400===0},addLeadingZero:function(number){return(number<10?"0":"")+number},addLeadingZeroForYear:function(number){return(number<10?"000":number<100?"00":number<1e3?"0":"")+number}},module.exports=DateFormatter},function(module,exports){"use strict";var TimeFormatter=function(timePattern){var owner=this;owner.time=[],owner.blocks=[],owner.timePattern=timePattern,owner.initBlocks()};TimeFormatter.prototype={initBlocks:function(){var owner=this;owner.timePattern.forEach(function(){owner.blocks.push(2)})},getISOFormatTime:function(){var owner=this,time=owner.time;return time[2]?owner.addLeadingZero(time[0])+":"+owner.addLeadingZero(time[1])+":"+owner.addLeadingZero(time[2]):""},getBlocks:function(){return this.blocks},getValidatedTime:function(value){var owner=this,result="";return value=value.replace(/[^\d]/g,""),owner.blocks.forEach(function(length,index){if(value.length>0){var sub=value.slice(0,length),sub0=sub.slice(0,1),rest=value.slice(length);switch(owner.timePattern[index]){case"h":parseInt(sub0,10)>2?sub="0"+sub0:parseInt(sub,10)>23&&(sub="23");break;case"m":case"s":parseInt(sub0,10)>5?sub="0"+sub0:parseInt(sub,10)>60&&(sub="60")}result+=sub,value=rest}}),this.getFixedTimeString(result)},getFixedTimeString:function(value){var second,minute,hour,owner=this,timePattern=owner.timePattern,time=[],secondIndex=0,minuteIndex=0,hourIndex=0,secondStartIndex=0,minuteStartIndex=0,hourStartIndex=0;return 6===value.length&&(timePattern.forEach(function(type,index){switch(type){case"s":secondIndex=2*index;break;case"m":minuteIndex=2*index;break;case"h":hourIndex=2*index}}),hourStartIndex=hourIndex,minuteStartIndex=minuteIndex,secondStartIndex=secondIndex,second=parseInt(value.slice(secondStartIndex,secondStartIndex+2),10),minute=parseInt(value.slice(minuteStartIndex,minuteStartIndex+2),10),hour=parseInt(value.slice(hourStartIndex,hourStartIndex+2),10),time=this.getFixedTime(hour,minute,second)),4===value.length&&owner.timePattern.indexOf("s")<0&&(timePattern.forEach(function(type,index){switch(type){case"m":minuteIndex=2*index;break;case"h":hourIndex=2*index}}),hourStartIndex=hourIndex,minuteStartIndex=minuteIndex,second=0,minute=parseInt(value.slice(minuteStartIndex,minuteStartIndex+2),10),hour=parseInt(value.slice(hourStartIndex,hourStartIndex+2),10),time=this.getFixedTime(hour,minute,second)),owner.time=time,0===time.length?value:timePattern.reduce(function(previous,current){switch(current){case"s":return previous+owner.addLeadingZero(time[2]);case"m":return previous+owner.addLeadingZero(time[1]);case"h":return previous+owner.addLeadingZero(time[0])}},"")},getFixedTime:function(hour,minute,second){return second=Math.min(parseInt(second||0,10),60),minute=Math.min(minute,60),hour=Math.min(hour,60),[hour,minute,second]},addLeadingZero:function(number){return(number<10?"0":"")+number}},module.exports=TimeFormatter},function(module,exports){"use strict";var PhoneFormatter=function(formatter,delimiter){var owner=this;owner.delimiter=delimiter||""===delimiter?delimiter:" ",owner.delimiterRE=delimiter?new RegExp("\\"+delimiter,"g"):"",owner.formatter=formatter};PhoneFormatter.prototype={setFormatter:function(formatter){this.formatter=formatter},format:function(phoneNumber){var owner=this;owner.formatter.clear(),phoneNumber=phoneNumber.replace(/[^\d+]/g,""),phoneNumber=phoneNumber.replace(owner.delimiterRE,"");for(var current,result="",validated=!1,i=0,iMax=phoneNumber.length;iprefixLength?prevValue:prefix;else{var diffIndex=this.getFirstDiffIndex(prefix,value.slice(0,prefixLength));value=prefix+value.slice(diffIndex,diffIndex+1)+value.slice(prefixLength+1)}return value.slice(prefixLength)},getFirstDiffIndex:function(prev,current){for(var index=0;prev.charAt(index)===current.charAt(index);)if(""===prev.charAt(index++))return-1;return index},getFormattedValue:function(value,blocks,blocksLength,delimiter,delimiters,delimiterLazyShow){var currentDelimiter,result="",multipleDelimiters=delimiters.length>0;return 0===blocksLength?value:(blocks.forEach(function(length,index){if(value.length>0){var sub=value.slice(0,length),rest=value.slice(length);currentDelimiter=multipleDelimiters?delimiters[delimiterLazyShow?index-1:index]||currentDelimiter:delimiter,delimiterLazyShow?(index>0&&(result+=currentDelimiter),result+=sub):(result+=sub,sub.length===length&&index0?opts.numeralIntegerScale:0,target.numeralDecimalScale=opts.numeralDecimalScale>=0?opts.numeralDecimalScale:2,target.numeralDecimalMark=opts.numeralDecimalMark||".",target.numeralThousandsGroupStyle=opts.numeralThousandsGroupStyle||"thousand",target.numeralPositiveOnly=!!opts.numeralPositiveOnly,target.stripLeadingZeroes=opts.stripLeadingZeroes!==!1,target.numericOnly=target.creditCard||target.date||!!opts.numericOnly,target.uppercase=!!opts.uppercase,target.lowercase=!!opts.lowercase,target.prefix=target.creditCard||target.date?"":opts.prefix||"",target.noImmediatePrefix=!!opts.noImmediatePrefix,target.prefixLength=target.prefix.length,target.rawValueTrimPrefix=!!opts.rawValueTrimPrefix,target.copyDelimiter=!!opts.copyDelimiter,target.initValue=void 0!==opts.initValue&&null!==opts.initValue?opts.initValue.toString():"",target.delimiter=opts.delimiter||""===opts.delimiter?opts.delimiter:opts.date?"/":opts.time?":":opts.numeral?",":(opts.phone," "),target.delimiterLength=target.delimiter.length,target.delimiterLazyShow=!!opts.delimiterLazyShow,target.delimiters=opts.delimiters||[],target.blocks=opts.blocks||[],target.blocksLength=target.blocks.length,target.root="object"==typeof global&&global?global:window,target.document=opts.document||target.root.document,target.maxLength=0,target.backspace=!1,target.result="",target.onValueChanged=opts.onValueChanged||function(){},target}};module.exports=DefaultProperties}).call(exports,function(){return this}())}])}); \ No newline at end of file diff --git a/dist/cleave-react-node.js b/dist/cleave-react-node.js index 29b10283..a7215259 100644 --- a/dist/cleave-react-node.js +++ b/dist/cleave-react-node.js @@ -65,10 +65,11 @@ return /******/ (function(modules) { // webpackBootstrap var NumeralFormatter = __webpack_require__(9); var DateFormatter = __webpack_require__(10); - var PhoneFormatter = __webpack_require__(11); - var CreditCardDetector = __webpack_require__(12); - var Util = __webpack_require__(13); - var DefaultProperties = __webpack_require__(14); + var TimeFormatter = __webpack_require__(11); + var PhoneFormatter = __webpack_require__(12); + var CreditCardDetector = __webpack_require__(13); + var Util = __webpack_require__(14); + var DefaultProperties = __webpack_require__(15); var cleaveReactClass = CreateReactClass({ componentDidMount: function componentDidMount() { @@ -156,6 +157,7 @@ return /******/ (function(modules) { // webpackBootstrap owner.initPhoneFormatter(); owner.initDateFormatter(); + owner.initTimeFormatter(); owner.initNumeralFormatter(); // avoid touch input field if value is null @@ -178,6 +180,20 @@ return /******/ (function(modules) { // webpackBootstrap pps.numeralFormatter = new NumeralFormatter(pps.numeralDecimalMark, pps.numeralIntegerScale, pps.numeralDecimalScale, pps.numeralThousandsGroupStyle, pps.numeralPositiveOnly, pps.stripLeadingZeroes, pps.delimiter); }, + initTimeFormatter: function initTimeFormatter() { + var owner = this, + pps = owner.properties; + + if (!pps.time) { + return; + } + + pps.timeFormatter = new TimeFormatter(pps.timePattern); + pps.blocks = pps.timeFormatter.getBlocks(); + pps.blocksLength = pps.blocks.length; + pps.maxLength = Util.getMaxLength(pps.blocks); + }, + initDateFormatter: function initDateFormatter() { var owner = this, pps = owner.properties; @@ -351,6 +367,11 @@ return /******/ (function(modules) { // webpackBootstrap value = pps.dateFormatter.getValidatedDate(value); } + // time + if (pps.time) { + value = pps.timeFormatter.getValidatedTime(value); + } + // strip delimiters value = Util.stripDelimiters(value, pps.delimiter, pps.delimiters); @@ -1974,6 +1995,174 @@ return /******/ (function(modules) { // webpackBootstrap 'use strict'; + var TimeFormatter = function TimeFormatter(timePattern) { + var owner = this; + + owner.time = []; + owner.blocks = []; + owner.timePattern = timePattern; + owner.initBlocks(); + }; + + TimeFormatter.prototype = { + initBlocks: function initBlocks() { + var owner = this; + owner.timePattern.forEach(function () { + owner.blocks.push(2); + }); + }, + + getISOFormatTime: function getISOFormatTime() { + var owner = this, + time = owner.time; + + return time[2] ? owner.addLeadingZero(time[0]) + ':' + owner.addLeadingZero(time[1]) + ':' + owner.addLeadingZero(time[2]) : ''; + }, + + getBlocks: function getBlocks() { + return this.blocks; + }, + + getValidatedTime: function getValidatedTime(value) { + var owner = this, + result = ''; + + value = value.replace(/[^\d]/g, ''); + + owner.blocks.forEach(function (length, index) { + if (value.length > 0) { + var sub = value.slice(0, length), + sub0 = sub.slice(0, 1), + rest = value.slice(length); + + switch (owner.timePattern[index]) { + + case 'h': + if (parseInt(sub0, 10) > 2) { + sub = '0' + sub0; + } else if (parseInt(sub, 10) > 23) { + sub = '23'; + } + + break; + + case 'm': + case 's': + if (parseInt(sub0, 10) > 5) { + sub = '0' + sub0; + } else if (parseInt(sub, 10) > 60) { + sub = '60'; + } + break; + } + + result += sub; + + // update remaining string + value = rest; + } + }); + + return this.getFixedTimeString(result); + }, + + getFixedTimeString: function getFixedTimeString(value) { + var owner = this, + timePattern = owner.timePattern, + time = [], + secondIndex = 0, + minuteIndex = 0, + hourIndex = 0, + secondStartIndex = 0, + minuteStartIndex = 0, + hourStartIndex = 0, + second, + minute, + hour; + + if (value.length === 6) { + timePattern.forEach(function (type, index) { + switch (type) { + case 's': + secondIndex = index * 2; + break; + case 'm': + minuteIndex = index * 2; + break; + case 'h': + hourIndex = index * 2; + break; + } + }); + + hourStartIndex = hourIndex; + minuteStartIndex = minuteIndex; + secondStartIndex = secondIndex; + + second = parseInt(value.slice(secondStartIndex, secondStartIndex + 2), 10); + minute = parseInt(value.slice(minuteStartIndex, minuteStartIndex + 2), 10); + hour = parseInt(value.slice(hourStartIndex, hourStartIndex + 2), 10); + + time = this.getFixedTime(hour, minute, second); + } + + if (value.length === 4 && owner.timePattern.indexOf('s') < 0) { + timePattern.forEach(function (type, index) { + switch (type) { + case 'm': + minuteIndex = index * 2; + break; + case 'h': + hourIndex = index * 2; + break; + } + }); + + hourStartIndex = hourIndex; + minuteStartIndex = minuteIndex; + + second = 0; + minute = parseInt(value.slice(minuteStartIndex, minuteStartIndex + 2), 10); + hour = parseInt(value.slice(hourStartIndex, hourStartIndex + 2), 10); + + time = this.getFixedTime(hour, minute, second); + } + + owner.time = time; + + return time.length === 0 ? value : timePattern.reduce(function (previous, current) { + switch (current) { + case 's': + return previous + owner.addLeadingZero(time[2]); + case 'm': + return previous + owner.addLeadingZero(time[1]); + case 'h': + return previous + owner.addLeadingZero(time[0]); + } + }, ''); + }, + + getFixedTime: function getFixedTime(hour, minute, second) { + second = Math.min(parseInt(second || 0, 10), 60); + minute = Math.min(minute, 60); + hour = Math.min(hour, 60); + + return [hour, minute, second]; + }, + + addLeadingZero: function addLeadingZero(number) { + return (number < 10 ? '0' : '') + number; + } + }; + + module.exports = TimeFormatter; + +/***/ }), +/* 12 */ +/***/ (function(module, exports) { + + 'use strict'; + var PhoneFormatter = function PhoneFormatter(formatter, delimiter) { var owner = this; @@ -2033,7 +2222,7 @@ return /******/ (function(modules) { // webpackBootstrap module.exports = PhoneFormatter; /***/ }), -/* 12 */ +/* 13 */ /***/ (function(module, exports) { 'use strict'; @@ -2135,7 +2324,7 @@ return /******/ (function(modules) { // webpackBootstrap module.exports = CreditCardDetector; /***/ }), -/* 13 */ +/* 14 */ /***/ (function(module, exports) { 'use strict'; @@ -2354,7 +2543,7 @@ return /******/ (function(modules) { // webpackBootstrap module.exports = Util; /***/ }), -/* 14 */ +/* 15 */ /***/ (function(module, exports) { 'use strict'; @@ -2385,6 +2574,11 @@ return /******/ (function(modules) { // webpackBootstrap target.phoneRegionCode = opts.phoneRegionCode || 'AU'; target.phoneFormatter = {}; + // time + target.time = !!opts.time; + target.timePattern = opts.timePattern || ['h', 'm']; + target.timeFormatter = {}; + // date target.date = !!opts.date; target.datePattern = opts.datePattern || ['d', 'm', 'Y']; @@ -2413,7 +2607,7 @@ return /******/ (function(modules) { // webpackBootstrap target.initValue = opts.initValue !== undefined && opts.initValue !== null ? opts.initValue.toString() : ''; - target.delimiter = opts.delimiter || opts.delimiter === '' ? opts.delimiter : opts.date ? '/' : opts.numeral ? ',' : opts.phone ? ' ' : ' '; + target.delimiter = opts.delimiter || opts.delimiter === '' ? opts.delimiter : opts.date ? '/' : opts.time ? ':' : opts.numeral ? ',' : opts.phone ? ' ' : ' '; target.delimiterLength = target.delimiter.length; target.delimiterLazyShow = !!opts.delimiterLazyShow; target.delimiters = opts.delimiters || []; diff --git a/dist/cleave-react-node.min.js b/dist/cleave-react-node.min.js index 1f1006ed..9d35f00a 100644 --- a/dist/cleave-react-node.min.js +++ b/dist/cleave-react-node.min.js @@ -1,8 +1,8 @@ /*! - * cleave.js - 1.3.8 + * cleave.js - 1.4.0 * https://github.com/nosir/cleave.js * Apache License Version 2.0 * * Copyright (C) 2012-2018 Max Huang https://github.com/nosir/ */ -!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t(require("react")):"function"==typeof define&&define.amd?define(["react"],t):"object"==typeof exports?exports.Cleave=t(require("react")):e.Cleave=t(e.React)}(this,function(e){return function(e){function t(r){if(n[r])return n[r].exports;var o=n[r]={exports:{},id:r,loaded:!1};return e[r].call(o.exports,o,o.exports,t),o.loaded=!0,o.exports}var n={};return t.m=e,t.c=n,t.p="",t(0)}([function(e,t,n){"use strict";function r(e,t){var n={};for(var r in e)t.indexOf(r)>=0||Object.prototype.hasOwnProperty.call(e,r)&&(n[r]=e[r]);return n}var o=Object.assign||function(e){for(var t=1;t0?p.headStr(e,r.maxLength):e,r.result=p.getFormattedValue(e,r.blocks,r.blocksLength,r.delimiter,r.delimiters,r.delimiterLazyShow),void n.updateValueState()):(r.result=e,void n.updateValueState()))},updateCreditCardPropsByValue:function(e){var t,n=this,r=n.properties;p.headStr(r.result,4)!==p.headStr(e,4)&&(t=u.getInfo(e,r.creditCardStrictMode),r.blocks=t.blocks,r.blocksLength=r.blocks.length,r.maxLength=p.getMaxLength(r.blocks),r.creditCardType!==t.type&&(r.creditCardType=t.type,r.onCreditCardTypeChanged.call(n,r.creditCardType)))},updateValueState:function(){var e=this,t=e.properties;e.element||e.setState({value:t.result});var n=e.element.selectionEnd,r=e.element.value,o=t.result;return e.lastInputValue=o,n=p.getNextCursorPosition(n,r,o,t.delimiter,t.delimiters),e.isAndroid?void window.setTimeout(function(){e.setState({value:o,cursorPosition:n})},1):void e.setState({value:o,cursorPosition:n})},render:function(){var e=this,t=e.props,n=(t.value,t.options,t.onKeyDown,t.onFocus,t.onBlur,t.onChange,t.onInit,t.htmlRef),a=r(t,["value","options","onKeyDown","onFocus","onBlur","onChange","onInit","htmlRef"]);return i.createElement("input",o({type:"text",ref:function(t){e.element=t,n&&n.apply(this,arguments)},value:e.state.value,onKeyDown:e.onKeyDown,onChange:e.onChange,onFocus:e.onFocus,onBlur:e.onBlur},a))}});e.exports=m},function(t,n){t.exports=e},function(e,t,n){"use strict";var r=n(1),o=n(3);if("undefined"==typeof r)throw Error("create-react-class could not find the React object. If you are using script tags, make sure that React is being loaded before create-react-class.");var i=(new r.Component).updater;e.exports=o(r.Component,r.isValidElement,i)},function(e,t,n){"use strict";function r(e){return e}function o(e,t,n){function o(e,t,n){for(var r in t)t.hasOwnProperty(r)&&"production"!==process.env.NODE_ENV&&c("function"==typeof t[r],"%s: %s type `%s` is invalid; it must be a function, usually from React.PropTypes.",e.displayName||"ReactClass",l[n],r)}function p(e,t){var n=b.hasOwnProperty(t)?b[t]:null;C.hasOwnProperty(t)&&s("OVERRIDE_BASE"===n,"ReactClassInterface: You are attempting to override `%s` from your class specification. Ensure that your method names do not overlap with React methods.",t),e&&s("DEFINE_MANY"===n||"DEFINE_MANY_MERGED"===n,"ReactClassInterface: You are attempting to define `%s` on your component more than once. This conflict may be due to a mixin.",t)}function d(e,n){if(n){s("function"!=typeof n,"ReactClass: You're attempting to use a component class or function as a mixin. Instead, just use a regular object."),s(!t(n),"ReactClass: You're attempting to use a component as a mixin. Instead, just use a regular object.");var r=e.prototype,o=r.__reactAutoBindPairs;n.hasOwnProperty(u)&&x.mixins(e,n.mixins);for(var i in n)if(n.hasOwnProperty(i)&&i!==u){var a=n[i],l=r.hasOwnProperty(i);if(p(l,i),x.hasOwnProperty(i))x[i](e,a);else{var d=b.hasOwnProperty(i),m="function"==typeof a,f=m&&!d&&!l&&n.autobind!==!1;if(f)o.push(i,a),r[i]=a;else if(l){var y=b[i];s(d&&("DEFINE_MANY_MERGED"===y||"DEFINE_MANY"===y),"ReactClass: Unexpected spec policy %s for key %s when mixing in component specs.",y,i),"DEFINE_MANY_MERGED"===y?r[i]=h(r[i],a):"DEFINE_MANY"===y&&(r[i]=g(r[i],a))}else r[i]=a,"production"!==process.env.NODE_ENV&&"function"==typeof a&&n.displayName&&(r[i].displayName=n.displayName+"_"+i)}}}else if("production"!==process.env.NODE_ENV){var v=typeof n,E="object"===v&&null!==n;"production"!==process.env.NODE_ENV&&c(E,"%s: You're attempting to include a mixin that is either null or not an object. Check the mixins included by the component, as well as any mixins they include themselves. Expected object but got %s.",e.displayName||"ReactClass",null===n?null:v)}}function m(e,t){if(t)for(var n in t){var r=t[n];if(t.hasOwnProperty(n)){var o=n in x;s(!o,'ReactClass: You are attempting to define a reserved property, `%s`, that shouldn\'t be on the "statics" key. Define it as an instance property instead; it will still be accessible on the constructor.',n);var i=n in e;s(!i,"ReactClass: You are attempting to define `%s` on your component more than once. This conflict may be due to a mixin.",n),e[n]=r}}}function f(e,t){s(e&&t&&"object"==typeof e&&"object"==typeof t,"mergeIntoWithNoDuplicateKeys(): Cannot merge non-objects.");for(var n in t)t.hasOwnProperty(n)&&(s(void 0===e[n],"mergeIntoWithNoDuplicateKeys(): Tried to merge two objects with the same key: `%s`. This conflict may be due to a mixin; in particular, this may be caused by two getInitialState() or getDefaultProps() methods returning objects with clashing keys.",n),e[n]=t[n]);return e}function h(e,t){return function(){var n=e.apply(this,arguments),r=t.apply(this,arguments);if(null==n)return r;if(null==r)return n;var o={};return f(o,n),f(o,r),o}}function g(e,t){return function(){e.apply(this,arguments),t.apply(this,arguments)}}function y(e,t){var n=t.bind(e);if("production"!==process.env.NODE_ENV){n.__reactBoundContext=e,n.__reactBoundMethod=t,n.__reactBoundArguments=null;var r=e.constructor.displayName,o=n.bind;n.bind=function(i){for(var a=arguments.length,s=Array(a>1?a-1:0),l=1;l1?t-1:0),r=1;r2?n-2:0),o=2;o0?t:0,c.numeralDecimalScale=n>=0?n:2,c.numeralThousandsGroupStyle=o||r.groupStyle.thousand,c.numeralPositiveOnly=!!i,c.stripLeadingZeroes=a!==!1,c.delimiter=s||""===s?s:",",c.delimiterRE=s?new RegExp("\\"+s,"g"):""};n.groupStyle={thousand:"thousand",lakh:"lakh",wan:"wan",none:"none"},n.prototype={getRawValue:function(e){return e.replace(this.delimiterRE,"").replace(this.numeralDecimalMark,".")},format:function(e){var t,r,o=this,i="";switch(e=e.replace(/[A-Za-z]/g,"").replace(o.numeralDecimalMark,"M").replace(/[^\dM-]/g,"").replace(/^\-/,"N").replace(/\-/g,"").replace("N",o.numeralPositiveOnly?"":"-").replace("M",o.numeralDecimalMark),o.stripLeadingZeroes&&(e=e.replace(/^(-)?0+(?=\d)/,"$1")),r=e,e.indexOf(o.numeralDecimalMark)>=0&&(t=e.split(o.numeralDecimalMark),r=t[0],i=o.numeralDecimalMark+t[1].slice(0,o.numeralDecimalScale)),o.numeralIntegerScale>0&&(r=r.slice(0,o.numeralIntegerScale+("-"===e.slice(0,1)?1:0))),o.numeralThousandsGroupStyle){case n.groupStyle.lakh:r=r.replace(/(\d)(?=(\d\d)+\d$)/g,"$1"+o.delimiter);break;case n.groupStyle.wan:r=r.replace(/(\d)(?=(\d{4})+$)/g,"$1"+o.delimiter);break;case n.groupStyle.thousand:r=r.replace(/(\d)(?=(\d{3})+$)/g,"$1"+o.delimiter)}return r.toString()+(o.numeralDecimalScale>0?i.toString():"")}},e.exports=n},function(e,t){"use strict";var n=function(e){var t=this;t.date=[],t.blocks=[],t.datePattern=e,t.initBlocks()};n.prototype={initBlocks:function(){var e=this;e.datePattern.forEach(function(t){"Y"===t?e.blocks.push(4):e.blocks.push(2)})},getISOFormatDate:function(){var e=this,t=e.date;return t[2]?t[2]+"-"+e.addLeadingZero(t[1])+"-"+e.addLeadingZero(t[0]):""},getBlocks:function(){return this.blocks},getValidatedDate:function(e){var t=this,n="";return e=e.replace(/[^\d]/g,""),t.blocks.forEach(function(r,o){if(e.length>0){var i=e.slice(0,r),a=i.slice(0,1),s=e.slice(r);switch(t.datePattern[o]){case"d":"00"===i?i="01":parseInt(a,10)>3?i="0"+a:parseInt(i,10)>31&&(i="31");break;case"m":"00"===i?i="01":parseInt(a,10)>1?i="0"+a:parseInt(i,10)>12&&(i="12")}n+=i,e=s}}),this.getFixedDateString(n)},getFixedDateString:function(e){var t,n,r,o=this,i=o.datePattern,a=[],s=0,c=0,l=0,u=0,p=0,d=0,m=!1;return 4===e.length&&"y"!==i[0].toLowerCase()&&"y"!==i[1].toLowerCase()&&(u="d"===i[0]?0:2,p=2-u,t=parseInt(e.slice(u,u+2),10),n=parseInt(e.slice(p,p+2),10),a=this.getFixedDate(t,n,0)),8===e.length&&(i.forEach(function(e,t){switch(e){case"d":s=t;break;case"m":c=t;break;default:l=t}}),d=2*l,u=s<=l?2*s:2*s+2,p=c<=l?2*c:2*c+2,t=parseInt(e.slice(u,u+2),10),n=parseInt(e.slice(p,p+2),10),r=parseInt(e.slice(d,d+4),10),m=4===e.slice(d,d+4).length,a=this.getFixedDate(t,n,r)),o.date=a,0===a.length?e:i.reduce(function(e,t){switch(t){case"d":return e+o.addLeadingZero(a[0]);case"m":return e+o.addLeadingZero(a[1]);default:return e+(m?o.addLeadingZeroForYear(a[2]):"")}},"")},getFixedDate:function(e,t,n){return e=Math.min(e,31),t=Math.min(t,12),n=parseInt(n||0,10),(t<7&&t%2===0||t>8&&t%2===1)&&(e=Math.min(e,2===t?this.isLeapYear(n)?29:28:30)),[e,t,n]},isLeapYear:function(e){return e%4===0&&e%100!==0||e%400===0},addLeadingZero:function(e){return(e<10?"0":"")+e},addLeadingZeroForYear:function(e){return(e<10?"000":e<100?"00":e<1e3?"0":"")+e}},e.exports=n},function(e,t){"use strict";var n=function(e,t){var n=this;n.delimiter=t||""===t?t:" ",n.delimiterRE=t?new RegExp("\\"+t,"g"):"",n.formatter=e};n.prototype={setFormatter:function(e){this.formatter=e},format:function(e){var t=this;t.formatter.clear(),e=e.replace(/[^\d+]/g,""),e=e.replace(t.delimiterRE,"");for(var n,r="",o=!1,i=0,a=e.length;in?r:t;else{var o=this.getFirstDiffIndex(t,e.slice(0,n));e=t+e.slice(o,o+1)+e.slice(n+1)}return e.slice(n)},getFirstDiffIndex:function(e,t){for(var n=0;e.charAt(n)===t.charAt(n);)if(""===e.charAt(n++))return-1;return n},getFormattedValue:function(e,t,n,r,o,i){var a,s="",c=o.length>0;return 0===n?e:(t.forEach(function(t,l){if(e.length>0){var u=e.slice(0,t),p=e.slice(t);a=c?o[i?l-1:l]||a:r,i?(l>0&&(s+=a),s+=u):(s+=u,u.length===t&&l0?t.numeralIntegerScale:0,e.numeralDecimalScale=t.numeralDecimalScale>=0?t.numeralDecimalScale:2,e.numeralDecimalMark=t.numeralDecimalMark||".",e.numeralThousandsGroupStyle=t.numeralThousandsGroupStyle||"thousand",e.numeralPositiveOnly=!!t.numeralPositiveOnly,e.stripLeadingZeroes=t.stripLeadingZeroes!==!1,e.numericOnly=e.creditCard||e.date||!!t.numericOnly,e.uppercase=!!t.uppercase,e.lowercase=!!t.lowercase,e.prefix=e.creditCard||e.date?"":t.prefix||"",e.noImmediatePrefix=!!t.noImmediatePrefix,e.prefixLength=e.prefix.length,e.rawValueTrimPrefix=!!t.rawValueTrimPrefix,e.copyDelimiter=!!t.copyDelimiter,e.initValue=void 0!==t.initValue&&null!==t.initValue?t.initValue.toString():"",e.delimiter=t.delimiter||""===t.delimiter?t.delimiter:t.date?"/":t.numeral?",":(t.phone," "),e.delimiterLength=e.delimiter.length,e.delimiterLazyShow=!!t.delimiterLazyShow,e.delimiters=t.delimiters||[],e.blocks=t.blocks||[],e.blocksLength=e.blocks.length,e.root="object"===("undefined"==typeof global?"undefined":n(global))&&global?global:window,e.document=t.document||e.root.document,e.maxLength=0,e.backspace=!1,e.result="",e.onValueChanged=t.onValueChanged||function(){},e}};e.exports=r}])}); \ No newline at end of file +!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t(require("react")):"function"==typeof define&&define.amd?define(["react"],t):"object"==typeof exports?exports.Cleave=t(require("react")):e.Cleave=t(e.React)}(this,function(e){return function(e){function t(r){if(n[r])return n[r].exports;var i=n[r]={exports:{},id:r,loaded:!1};return e[r].call(i.exports,i,i.exports,t),i.loaded=!0,i.exports}var n={};return t.m=e,t.c=n,t.p="",t(0)}([function(e,t,n){"use strict";function r(e,t){var n={};for(var r in e)t.indexOf(r)>=0||Object.prototype.hasOwnProperty.call(e,r)&&(n[r]=e[r]);return n}var i=Object.assign||function(e){for(var t=1;t0?d.headStr(e,r.maxLength):e,r.result=d.getFormattedValue(e,r.blocks,r.blocksLength,r.delimiter,r.delimiters,r.delimiterLazyShow),void n.updateValueState()):(r.result=e,void n.updateValueState()))},updateCreditCardPropsByValue:function(e){var t,n=this,r=n.properties;d.headStr(r.result,4)!==d.headStr(e,4)&&(t=p.getInfo(e,r.creditCardStrictMode),r.blocks=t.blocks,r.blocksLength=r.blocks.length,r.maxLength=d.getMaxLength(r.blocks),r.creditCardType!==t.type&&(r.creditCardType=t.type,r.onCreditCardTypeChanged.call(n,r.creditCardType)))},updateValueState:function(){var e=this,t=e.properties;e.element||e.setState({value:t.result});var n=e.element.selectionEnd,r=e.element.value,i=t.result;return e.lastInputValue=i,n=d.getNextCursorPosition(n,r,i,t.delimiter,t.delimiters),e.isAndroid?void window.setTimeout(function(){e.setState({value:i,cursorPosition:n})},1):void e.setState({value:i,cursorPosition:n})},render:function(){var e=this,t=e.props,n=(t.value,t.options,t.onKeyDown,t.onFocus,t.onBlur,t.onChange,t.onInit,t.htmlRef),a=r(t,["value","options","onKeyDown","onFocus","onBlur","onChange","onInit","htmlRef"]);return o.createElement("input",i({type:"text",ref:function(t){e.element=t,n&&n.apply(this,arguments)},value:e.state.value,onKeyDown:e.onKeyDown,onChange:e.onChange,onFocus:e.onFocus,onBlur:e.onBlur},a))}});e.exports=f},function(t,n){t.exports=e},function(e,t,n){"use strict";var r=n(1),i=n(3);if("undefined"==typeof r)throw Error("create-react-class could not find the React object. If you are using script tags, make sure that React is being loaded before create-react-class.");var o=(new r.Component).updater;e.exports=i(r.Component,r.isValidElement,o)},function(e,t,n){"use strict";function r(e){return e}function i(e,t,n){function i(e,t,n){for(var r in t)t.hasOwnProperty(r)&&"production"!==process.env.NODE_ENV&&c("function"==typeof t[r],"%s: %s type `%s` is invalid; it must be a function, usually from React.PropTypes.",e.displayName||"ReactClass",l[n],r)}function p(e,t){var n=D.hasOwnProperty(t)?D[t]:null;I.hasOwnProperty(t)&&s("OVERRIDE_BASE"===n,"ReactClassInterface: You are attempting to override `%s` from your class specification. Ensure that your method names do not overlap with React methods.",t),e&&s("DEFINE_MANY"===n||"DEFINE_MANY_MERGED"===n,"ReactClassInterface: You are attempting to define `%s` on your component more than once. This conflict may be due to a mixin.",t)}function d(e,n){if(n){s("function"!=typeof n,"ReactClass: You're attempting to use a component class or function as a mixin. Instead, just use a regular object."),s(!t(n),"ReactClass: You're attempting to use a component as a mixin. Instead, just use a regular object.");var r=e.prototype,i=r.__reactAutoBindPairs;n.hasOwnProperty(u)&&x.mixins(e,n.mixins);for(var o in n)if(n.hasOwnProperty(o)&&o!==u){var a=n[o],l=r.hasOwnProperty(o);if(p(l,o),x.hasOwnProperty(o))x[o](e,a);else{var d=D.hasOwnProperty(o),m="function"==typeof a,f=m&&!d&&!l&&n.autobind!==!1;if(f)i.push(o,a),r[o]=a;else if(l){var v=D[o];s(d&&("DEFINE_MANY_MERGED"===v||"DEFINE_MANY"===v),"ReactClass: Unexpected spec policy %s for key %s when mixing in component specs.",v,o),"DEFINE_MANY_MERGED"===v?r[o]=h(r[o],a):"DEFINE_MANY"===v&&(r[o]=g(r[o],a))}else r[o]=a,"production"!==process.env.NODE_ENV&&"function"==typeof a&&n.displayName&&(r[o].displayName=n.displayName+"_"+o)}}}else if("production"!==process.env.NODE_ENV){var y=typeof n,E="object"===y&&null!==n;"production"!==process.env.NODE_ENV&&c(E,"%s: You're attempting to include a mixin that is either null or not an object. Check the mixins included by the component, as well as any mixins they include themselves. Expected object but got %s.",e.displayName||"ReactClass",null===n?null:y)}}function m(e,t){if(t)for(var n in t){var r=t[n];if(t.hasOwnProperty(n)){var i=n in x;s(!i,'ReactClass: You are attempting to define a reserved property, `%s`, that shouldn\'t be on the "statics" key. Define it as an instance property instead; it will still be accessible on the constructor.',n);var o=n in e;s(!o,"ReactClass: You are attempting to define `%s` on your component more than once. This conflict may be due to a mixin.",n),e[n]=r}}}function f(e,t){s(e&&t&&"object"==typeof e&&"object"==typeof t,"mergeIntoWithNoDuplicateKeys(): Cannot merge non-objects.");for(var n in t)t.hasOwnProperty(n)&&(s(void 0===e[n],"mergeIntoWithNoDuplicateKeys(): Tried to merge two objects with the same key: `%s`. This conflict may be due to a mixin; in particular, this may be caused by two getInitialState() or getDefaultProps() methods returning objects with clashing keys.",n),e[n]=t[n]);return e}function h(e,t){return function(){var n=e.apply(this,arguments),r=t.apply(this,arguments);if(null==n)return r;if(null==r)return n;var i={};return f(i,n),f(i,r),i}}function g(e,t){return function(){e.apply(this,arguments),t.apply(this,arguments)}}function v(e,t){var n=t.bind(e);if("production"!==process.env.NODE_ENV){n.__reactBoundContext=e,n.__reactBoundMethod=t,n.__reactBoundArguments=null;var r=e.constructor.displayName,i=n.bind;n.bind=function(o){for(var a=arguments.length,s=Array(a>1?a-1:0),l=1;l1?t-1:0),r=1;r2?n-2:0),i=2;i0?t:0,c.numeralDecimalScale=n>=0?n:2,c.numeralThousandsGroupStyle=i||r.groupStyle.thousand,c.numeralPositiveOnly=!!o,c.stripLeadingZeroes=a!==!1,c.delimiter=s||""===s?s:",",c.delimiterRE=s?new RegExp("\\"+s,"g"):""};n.groupStyle={thousand:"thousand",lakh:"lakh",wan:"wan",none:"none"},n.prototype={getRawValue:function(e){return e.replace(this.delimiterRE,"").replace(this.numeralDecimalMark,".")},format:function(e){var t,r,i=this,o="";switch(e=e.replace(/[A-Za-z]/g,"").replace(i.numeralDecimalMark,"M").replace(/[^\dM-]/g,"").replace(/^\-/,"N").replace(/\-/g,"").replace("N",i.numeralPositiveOnly?"":"-").replace("M",i.numeralDecimalMark),i.stripLeadingZeroes&&(e=e.replace(/^(-)?0+(?=\d)/,"$1")),r=e,e.indexOf(i.numeralDecimalMark)>=0&&(t=e.split(i.numeralDecimalMark),r=t[0],o=i.numeralDecimalMark+t[1].slice(0,i.numeralDecimalScale)),i.numeralIntegerScale>0&&(r=r.slice(0,i.numeralIntegerScale+("-"===e.slice(0,1)?1:0))),i.numeralThousandsGroupStyle){case n.groupStyle.lakh:r=r.replace(/(\d)(?=(\d\d)+\d$)/g,"$1"+i.delimiter);break;case n.groupStyle.wan:r=r.replace(/(\d)(?=(\d{4})+$)/g,"$1"+i.delimiter);break;case n.groupStyle.thousand:r=r.replace(/(\d)(?=(\d{3})+$)/g,"$1"+i.delimiter)}return r.toString()+(i.numeralDecimalScale>0?o.toString():"")}},e.exports=n},function(e,t){"use strict";var n=function(e){var t=this;t.date=[],t.blocks=[],t.datePattern=e,t.initBlocks()};n.prototype={initBlocks:function(){var e=this;e.datePattern.forEach(function(t){"Y"===t?e.blocks.push(4):e.blocks.push(2)})},getISOFormatDate:function(){var e=this,t=e.date;return t[2]?t[2]+"-"+e.addLeadingZero(t[1])+"-"+e.addLeadingZero(t[0]):""},getBlocks:function(){return this.blocks},getValidatedDate:function(e){var t=this,n="";return e=e.replace(/[^\d]/g,""),t.blocks.forEach(function(r,i){if(e.length>0){var o=e.slice(0,r),a=o.slice(0,1),s=e.slice(r);switch(t.datePattern[i]){case"d":"00"===o?o="01":parseInt(a,10)>3?o="0"+a:parseInt(o,10)>31&&(o="31");break;case"m":"00"===o?o="01":parseInt(a,10)>1?o="0"+a:parseInt(o,10)>12&&(o="12")}n+=o,e=s}}),this.getFixedDateString(n)},getFixedDateString:function(e){var t,n,r,i=this,o=i.datePattern,a=[],s=0,c=0,l=0,u=0,p=0,d=0,m=!1;return 4===e.length&&"y"!==o[0].toLowerCase()&&"y"!==o[1].toLowerCase()&&(u="d"===o[0]?0:2,p=2-u,t=parseInt(e.slice(u,u+2),10),n=parseInt(e.slice(p,p+2),10),a=this.getFixedDate(t,n,0)),8===e.length&&(o.forEach(function(e,t){switch(e){case"d":s=t;break;case"m":c=t;break;default:l=t}}),d=2*l,u=s<=l?2*s:2*s+2,p=c<=l?2*c:2*c+2,t=parseInt(e.slice(u,u+2),10),n=parseInt(e.slice(p,p+2),10),r=parseInt(e.slice(d,d+4),10),m=4===e.slice(d,d+4).length,a=this.getFixedDate(t,n,r)),i.date=a,0===a.length?e:o.reduce(function(e,t){switch(t){case"d":return e+i.addLeadingZero(a[0]);case"m":return e+i.addLeadingZero(a[1]);default:return e+(m?i.addLeadingZeroForYear(a[2]):"")}},"")},getFixedDate:function(e,t,n){return e=Math.min(e,31),t=Math.min(t,12),n=parseInt(n||0,10),(t<7&&t%2===0||t>8&&t%2===1)&&(e=Math.min(e,2===t?this.isLeapYear(n)?29:28:30)),[e,t,n]},isLeapYear:function(e){return e%4===0&&e%100!==0||e%400===0},addLeadingZero:function(e){return(e<10?"0":"")+e},addLeadingZeroForYear:function(e){return(e<10?"000":e<100?"00":e<1e3?"0":"")+e}},e.exports=n},function(e,t){"use strict";var n=function(e){var t=this;t.time=[],t.blocks=[],t.timePattern=e,t.initBlocks()};n.prototype={initBlocks:function(){var e=this;e.timePattern.forEach(function(){e.blocks.push(2)})},getISOFormatTime:function(){var e=this,t=e.time;return t[2]?e.addLeadingZero(t[0])+":"+e.addLeadingZero(t[1])+":"+e.addLeadingZero(t[2]):""},getBlocks:function(){return this.blocks},getValidatedTime:function(e){var t=this,n="";return e=e.replace(/[^\d]/g,""),t.blocks.forEach(function(r,i){if(e.length>0){var o=e.slice(0,r),a=o.slice(0,1),s=e.slice(r);switch(t.timePattern[i]){case"h":parseInt(a,10)>2?o="0"+a:parseInt(o,10)>23&&(o="23");break;case"m":case"s":parseInt(a,10)>5?o="0"+a:parseInt(o,10)>60&&(o="60")}n+=o,e=s}}),this.getFixedTimeString(n)},getFixedTimeString:function(e){var t,n,r,i=this,o=i.timePattern,a=[],s=0,c=0,l=0,u=0,p=0,d=0;return 6===e.length&&(o.forEach(function(e,t){switch(e){case"s":s=2*t;break;case"m":c=2*t;break;case"h":l=2*t}}),d=l,p=c,u=s,t=parseInt(e.slice(u,u+2),10),n=parseInt(e.slice(p,p+2),10),r=parseInt(e.slice(d,d+2),10),a=this.getFixedTime(r,n,t)),4===e.length&&i.timePattern.indexOf("s")<0&&(o.forEach(function(e,t){switch(e){case"m":c=2*t;break;case"h":l=2*t}}),d=l,p=c,t=0,n=parseInt(e.slice(p,p+2),10),r=parseInt(e.slice(d,d+2),10),a=this.getFixedTime(r,n,t)),i.time=a,0===a.length?e:o.reduce(function(e,t){switch(t){case"s":return e+i.addLeadingZero(a[2]);case"m":return e+i.addLeadingZero(a[1]);case"h":return e+i.addLeadingZero(a[0])}},"")},getFixedTime:function(e,t,n){return n=Math.min(parseInt(n||0,10),60),t=Math.min(t,60),e=Math.min(e,60),[e,t,n]},addLeadingZero:function(e){return(e<10?"0":"")+e}},e.exports=n},function(e,t){"use strict";var n=function(e,t){var n=this;n.delimiter=t||""===t?t:" ",n.delimiterRE=t?new RegExp("\\"+t,"g"):"",n.formatter=e};n.prototype={setFormatter:function(e){this.formatter=e},format:function(e){var t=this;t.formatter.clear(),e=e.replace(/[^\d+]/g,""),e=e.replace(t.delimiterRE,"");for(var n,r="",i=!1,o=0,a=e.length;on?r:t;else{var i=this.getFirstDiffIndex(t,e.slice(0,n));e=t+e.slice(i,i+1)+e.slice(n+1)}return e.slice(n)},getFirstDiffIndex:function(e,t){for(var n=0;e.charAt(n)===t.charAt(n);)if(""===e.charAt(n++))return-1;return n},getFormattedValue:function(e,t,n,r,i,o){var a,s="",c=i.length>0;return 0===n?e:(t.forEach(function(t,l){if(e.length>0){var u=e.slice(0,t),p=e.slice(t);a=c?i[o?l-1:l]||a:r,o?(l>0&&(s+=a),s+=u):(s+=u,u.length===t&&l0?t.numeralIntegerScale:0,e.numeralDecimalScale=t.numeralDecimalScale>=0?t.numeralDecimalScale:2,e.numeralDecimalMark=t.numeralDecimalMark||".",e.numeralThousandsGroupStyle=t.numeralThousandsGroupStyle||"thousand",e.numeralPositiveOnly=!!t.numeralPositiveOnly,e.stripLeadingZeroes=t.stripLeadingZeroes!==!1,e.numericOnly=e.creditCard||e.date||!!t.numericOnly,e.uppercase=!!t.uppercase,e.lowercase=!!t.lowercase,e.prefix=e.creditCard||e.date?"":t.prefix||"",e.noImmediatePrefix=!!t.noImmediatePrefix,e.prefixLength=e.prefix.length,e.rawValueTrimPrefix=!!t.rawValueTrimPrefix,e.copyDelimiter=!!t.copyDelimiter,e.initValue=void 0!==t.initValue&&null!==t.initValue?t.initValue.toString():"",e.delimiter=t.delimiter||""===t.delimiter?t.delimiter:t.date?"/":t.time?":":t.numeral?",":(t.phone," "),e.delimiterLength=e.delimiter.length,e.delimiterLazyShow=!!t.delimiterLazyShow,e.delimiters=t.delimiters||[],e.blocks=t.blocks||[],e.blocksLength=e.blocks.length,e.root="object"===("undefined"==typeof global?"undefined":n(global))&&global?global:window,e.document=t.document||e.root.document,e.maxLength=0,e.backspace=!1,e.result="",e.onValueChanged=t.onValueChanged||function(){},e}};e.exports=r}])}); \ No newline at end of file diff --git a/dist/cleave-react.js b/dist/cleave-react.js index d23db293..dd1e79e6 100644 --- a/dist/cleave-react.js +++ b/dist/cleave-react.js @@ -65,10 +65,11 @@ return /******/ (function(modules) { // webpackBootstrap var NumeralFormatter = __webpack_require__(10); var DateFormatter = __webpack_require__(11); - var PhoneFormatter = __webpack_require__(12); - var CreditCardDetector = __webpack_require__(13); - var Util = __webpack_require__(14); - var DefaultProperties = __webpack_require__(15); + var TimeFormatter = __webpack_require__(12); + var PhoneFormatter = __webpack_require__(13); + var CreditCardDetector = __webpack_require__(14); + var Util = __webpack_require__(15); + var DefaultProperties = __webpack_require__(16); var cleaveReactClass = CreateReactClass({ componentDidMount: function componentDidMount() { @@ -156,6 +157,7 @@ return /******/ (function(modules) { // webpackBootstrap owner.initPhoneFormatter(); owner.initDateFormatter(); + owner.initTimeFormatter(); owner.initNumeralFormatter(); // avoid touch input field if value is null @@ -178,6 +180,20 @@ return /******/ (function(modules) { // webpackBootstrap pps.numeralFormatter = new NumeralFormatter(pps.numeralDecimalMark, pps.numeralIntegerScale, pps.numeralDecimalScale, pps.numeralThousandsGroupStyle, pps.numeralPositiveOnly, pps.stripLeadingZeroes, pps.delimiter); }, + initTimeFormatter: function initTimeFormatter() { + var owner = this, + pps = owner.properties; + + if (!pps.time) { + return; + } + + pps.timeFormatter = new TimeFormatter(pps.timePattern); + pps.blocks = pps.timeFormatter.getBlocks(); + pps.blocksLength = pps.blocks.length; + pps.maxLength = Util.getMaxLength(pps.blocks); + }, + initDateFormatter: function initDateFormatter() { var owner = this, pps = owner.properties; @@ -351,6 +367,11 @@ return /******/ (function(modules) { // webpackBootstrap value = pps.dateFormatter.getValidatedDate(value); } + // time + if (pps.time) { + value = pps.timeFormatter.getValidatedTime(value); + } + // strip delimiters value = Util.stripDelimiters(value, pps.delimiter, pps.delimiters); @@ -2168,6 +2189,174 @@ return /******/ (function(modules) { // webpackBootstrap 'use strict'; + var TimeFormatter = function TimeFormatter(timePattern) { + var owner = this; + + owner.time = []; + owner.blocks = []; + owner.timePattern = timePattern; + owner.initBlocks(); + }; + + TimeFormatter.prototype = { + initBlocks: function initBlocks() { + var owner = this; + owner.timePattern.forEach(function () { + owner.blocks.push(2); + }); + }, + + getISOFormatTime: function getISOFormatTime() { + var owner = this, + time = owner.time; + + return time[2] ? owner.addLeadingZero(time[0]) + ':' + owner.addLeadingZero(time[1]) + ':' + owner.addLeadingZero(time[2]) : ''; + }, + + getBlocks: function getBlocks() { + return this.blocks; + }, + + getValidatedTime: function getValidatedTime(value) { + var owner = this, + result = ''; + + value = value.replace(/[^\d]/g, ''); + + owner.blocks.forEach(function (length, index) { + if (value.length > 0) { + var sub = value.slice(0, length), + sub0 = sub.slice(0, 1), + rest = value.slice(length); + + switch (owner.timePattern[index]) { + + case 'h': + if (parseInt(sub0, 10) > 2) { + sub = '0' + sub0; + } else if (parseInt(sub, 10) > 23) { + sub = '23'; + } + + break; + + case 'm': + case 's': + if (parseInt(sub0, 10) > 5) { + sub = '0' + sub0; + } else if (parseInt(sub, 10) > 60) { + sub = '60'; + } + break; + } + + result += sub; + + // update remaining string + value = rest; + } + }); + + return this.getFixedTimeString(result); + }, + + getFixedTimeString: function getFixedTimeString(value) { + var owner = this, + timePattern = owner.timePattern, + time = [], + secondIndex = 0, + minuteIndex = 0, + hourIndex = 0, + secondStartIndex = 0, + minuteStartIndex = 0, + hourStartIndex = 0, + second, + minute, + hour; + + if (value.length === 6) { + timePattern.forEach(function (type, index) { + switch (type) { + case 's': + secondIndex = index * 2; + break; + case 'm': + minuteIndex = index * 2; + break; + case 'h': + hourIndex = index * 2; + break; + } + }); + + hourStartIndex = hourIndex; + minuteStartIndex = minuteIndex; + secondStartIndex = secondIndex; + + second = parseInt(value.slice(secondStartIndex, secondStartIndex + 2), 10); + minute = parseInt(value.slice(minuteStartIndex, minuteStartIndex + 2), 10); + hour = parseInt(value.slice(hourStartIndex, hourStartIndex + 2), 10); + + time = this.getFixedTime(hour, minute, second); + } + + if (value.length === 4 && owner.timePattern.indexOf('s') < 0) { + timePattern.forEach(function (type, index) { + switch (type) { + case 'm': + minuteIndex = index * 2; + break; + case 'h': + hourIndex = index * 2; + break; + } + }); + + hourStartIndex = hourIndex; + minuteStartIndex = minuteIndex; + + second = 0; + minute = parseInt(value.slice(minuteStartIndex, minuteStartIndex + 2), 10); + hour = parseInt(value.slice(hourStartIndex, hourStartIndex + 2), 10); + + time = this.getFixedTime(hour, minute, second); + } + + owner.time = time; + + return time.length === 0 ? value : timePattern.reduce(function (previous, current) { + switch (current) { + case 's': + return previous + owner.addLeadingZero(time[2]); + case 'm': + return previous + owner.addLeadingZero(time[1]); + case 'h': + return previous + owner.addLeadingZero(time[0]); + } + }, ''); + }, + + getFixedTime: function getFixedTime(hour, minute, second) { + second = Math.min(parseInt(second || 0, 10), 60); + minute = Math.min(minute, 60); + hour = Math.min(hour, 60); + + return [hour, minute, second]; + }, + + addLeadingZero: function addLeadingZero(number) { + return (number < 10 ? '0' : '') + number; + } + }; + + module.exports = TimeFormatter; + +/***/ }), +/* 13 */ +/***/ (function(module, exports) { + + 'use strict'; + var PhoneFormatter = function PhoneFormatter(formatter, delimiter) { var owner = this; @@ -2227,7 +2416,7 @@ return /******/ (function(modules) { // webpackBootstrap module.exports = PhoneFormatter; /***/ }), -/* 13 */ +/* 14 */ /***/ (function(module, exports) { 'use strict'; @@ -2329,7 +2518,7 @@ return /******/ (function(modules) { // webpackBootstrap module.exports = CreditCardDetector; /***/ }), -/* 14 */ +/* 15 */ /***/ (function(module, exports) { 'use strict'; @@ -2548,7 +2737,7 @@ return /******/ (function(modules) { // webpackBootstrap module.exports = Util; /***/ }), -/* 15 */ +/* 16 */ /***/ (function(module, exports) { /* WEBPACK VAR INJECTION */(function(global) {'use strict'; @@ -2579,6 +2768,11 @@ return /******/ (function(modules) { // webpackBootstrap target.phoneRegionCode = opts.phoneRegionCode || 'AU'; target.phoneFormatter = {}; + // time + target.time = !!opts.time; + target.timePattern = opts.timePattern || ['h', 'm']; + target.timeFormatter = {}; + // date target.date = !!opts.date; target.datePattern = opts.datePattern || ['d', 'm', 'Y']; @@ -2607,7 +2801,7 @@ return /******/ (function(modules) { // webpackBootstrap target.initValue = opts.initValue !== undefined && opts.initValue !== null ? opts.initValue.toString() : ''; - target.delimiter = opts.delimiter || opts.delimiter === '' ? opts.delimiter : opts.date ? '/' : opts.numeral ? ',' : opts.phone ? ' ' : ' '; + target.delimiter = opts.delimiter || opts.delimiter === '' ? opts.delimiter : opts.date ? '/' : opts.time ? ':' : opts.numeral ? ',' : opts.phone ? ' ' : ' '; target.delimiterLength = target.delimiter.length; target.delimiterLazyShow = !!opts.delimiterLazyShow; target.delimiters = opts.delimiters || []; diff --git a/dist/cleave-react.min.js b/dist/cleave-react.min.js index ae28bb7f..04b565fc 100644 --- a/dist/cleave-react.min.js +++ b/dist/cleave-react.min.js @@ -1,8 +1,8 @@ /*! - * cleave.js - 1.3.8 + * cleave.js - 1.4.0 * https://github.com/nosir/cleave.js * Apache License Version 2.0 * * Copyright (C) 2012-2018 Max Huang https://github.com/nosir/ */ -!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t(require("react")):"function"==typeof define&&define.amd?define(["react"],t):"object"==typeof exports?exports.Cleave=t(require("react")):e.Cleave=t(e.React)}(this,function(e){return function(e){function t(r){if(n[r])return n[r].exports;var o=n[r]={exports:{},id:r,loaded:!1};return e[r].call(o.exports,o,o.exports,t),o.loaded=!0,o.exports}var n={};return t.m=e,t.c=n,t.p="",t(0)}([function(e,t,n){"use strict";function r(e,t){var n={};for(var r in e)t.indexOf(r)>=0||Object.prototype.hasOwnProperty.call(e,r)&&(n[r]=e[r]);return n}var o=Object.assign||function(e){for(var t=1;t0?p.headStr(e,r.maxLength):e,r.result=p.getFormattedValue(e,r.blocks,r.blocksLength,r.delimiter,r.delimiters,r.delimiterLazyShow),void n.updateValueState()):(r.result=e,void n.updateValueState()))},updateCreditCardPropsByValue:function(e){var t,n=this,r=n.properties;p.headStr(r.result,4)!==p.headStr(e,4)&&(t=l.getInfo(e,r.creditCardStrictMode),r.blocks=t.blocks,r.blocksLength=r.blocks.length,r.maxLength=p.getMaxLength(r.blocks),r.creditCardType!==t.type&&(r.creditCardType=t.type,r.onCreditCardTypeChanged.call(n,r.creditCardType)))},updateValueState:function(){var e=this,t=e.properties;e.element||e.setState({value:t.result});var n=e.element.selectionEnd,r=e.element.value,o=t.result;return e.lastInputValue=o,n=p.getNextCursorPosition(n,r,o,t.delimiter,t.delimiters),e.isAndroid?void window.setTimeout(function(){e.setState({value:o,cursorPosition:n})},1):void e.setState({value:o,cursorPosition:n})},render:function(){var e=this,t=e.props,n=(t.value,t.options,t.onKeyDown,t.onFocus,t.onBlur,t.onChange,t.onInit,t.htmlRef),a=r(t,["value","options","onKeyDown","onFocus","onBlur","onChange","onInit","htmlRef"]);return i.createElement("input",o({type:"text",ref:function(t){e.element=t,n&&n.apply(this,arguments)},value:e.state.value,onKeyDown:e.onKeyDown,onChange:e.onChange,onFocus:e.onFocus,onBlur:e.onBlur},a))}});e.exports=m},function(t,n){t.exports=e},function(e,t,n){"use strict";var r=n(1),o=n(3);if("undefined"==typeof r)throw Error("create-react-class could not find the React object. If you are using script tags, make sure that React is being loaded before create-react-class.");var i=(new r.Component).updater;e.exports=o(r.Component,r.isValidElement,i)},function(e,t,n){(function(t){"use strict";function r(e){return e}function o(e,n,o){function p(e,n,r){for(var o in n)n.hasOwnProperty(o)&&"production"!==t.env.NODE_ENV&&u("function"==typeof n[o],"%s: %s type `%s` is invalid; it must be a function, usually from React.PropTypes.",e.displayName||"ReactClass",c[r],o)}function d(e,t){var n=x.hasOwnProperty(t)?x[t]:null;S.hasOwnProperty(t)&&s("OVERRIDE_BASE"===n,"ReactClassInterface: You are attempting to override `%s` from your class specification. Ensure that your method names do not overlap with React methods.",t),e&&s("DEFINE_MANY"===n||"DEFINE_MANY_MERGED"===n,"ReactClassInterface: You are attempting to define `%s` on your component more than once. This conflict may be due to a mixin.",t)}function m(e,r){if(r){s("function"!=typeof r,"ReactClass: You're attempting to use a component class or function as a mixin. Instead, just use a regular object."),s(!n(r),"ReactClass: You're attempting to use a component as a mixin. Instead, just use a regular object.");var o=e.prototype,i=o.__reactAutoBindPairs;r.hasOwnProperty(l)&&w.mixins(e,r.mixins);for(var a in r)if(r.hasOwnProperty(a)&&a!==l){var c=r[a],p=o.hasOwnProperty(a);if(d(p,a),w.hasOwnProperty(a))w[a](e,c);else{var m=x.hasOwnProperty(a),f="function"==typeof c,h=f&&!m&&!p&&r.autobind!==!1;if(h)i.push(a,c),o[a]=c;else if(p){var v=x[a];s(m&&("DEFINE_MANY_MERGED"===v||"DEFINE_MANY"===v),"ReactClass: Unexpected spec policy %s for key %s when mixing in component specs.",v,a),"DEFINE_MANY_MERGED"===v?o[a]=g(o[a],c):"DEFINE_MANY"===v&&(o[a]=y(o[a],c))}else o[a]=c,"production"!==t.env.NODE_ENV&&"function"==typeof c&&r.displayName&&(o[a].displayName=r.displayName+"_"+a)}}}else if("production"!==t.env.NODE_ENV){var E=typeof r,D="object"===E&&null!==r;"production"!==t.env.NODE_ENV&&u(D,"%s: You're attempting to include a mixin that is either null or not an object. Check the mixins included by the component, as well as any mixins they include themselves. Expected object but got %s.",e.displayName||"ReactClass",null===r?null:E)}}function f(e,t){if(t)for(var n in t){var r=t[n];if(t.hasOwnProperty(n)){var o=n in w;s(!o,'ReactClass: You are attempting to define a reserved property, `%s`, that shouldn\'t be on the "statics" key. Define it as an instance property instead; it will still be accessible on the constructor.',n);var i=n in e;s(!i,"ReactClass: You are attempting to define `%s` on your component more than once. This conflict may be due to a mixin.",n),e[n]=r}}}function h(e,t){s(e&&t&&"object"==typeof e&&"object"==typeof t,"mergeIntoWithNoDuplicateKeys(): Cannot merge non-objects.");for(var n in t)t.hasOwnProperty(n)&&(s(void 0===e[n],"mergeIntoWithNoDuplicateKeys(): Tried to merge two objects with the same key: `%s`. This conflict may be due to a mixin; in particular, this may be caused by two getInitialState() or getDefaultProps() methods returning objects with clashing keys.",n),e[n]=t[n]);return e}function g(e,t){return function(){var n=e.apply(this,arguments),r=t.apply(this,arguments);if(null==n)return r;if(null==r)return n;var o={};return h(o,n),h(o,r),o}}function y(e,t){return function(){e.apply(this,arguments),t.apply(this,arguments)}}function v(e,n){var r=n.bind(e);if("production"!==t.env.NODE_ENV){r.__reactBoundContext=e,r.__reactBoundMethod=n,r.__reactBoundArguments=null;var o=e.constructor.displayName,i=r.bind;r.bind=function(a){for(var s=arguments.length,c=Array(s>1?s-1:0),l=1;l1)for(var n=1;n1?t-1:0),r=1;r2?n-2:0),o=2;o0?t:0,u.numeralDecimalScale=n>=0?n:2,u.numeralThousandsGroupStyle=o||r.groupStyle.thousand,u.numeralPositiveOnly=!!i,u.stripLeadingZeroes=a!==!1,u.delimiter=s||""===s?s:",",u.delimiterRE=s?new RegExp("\\"+s,"g"):""};n.groupStyle={thousand:"thousand",lakh:"lakh",wan:"wan",none:"none"},n.prototype={getRawValue:function(e){return e.replace(this.delimiterRE,"").replace(this.numeralDecimalMark,".")},format:function(e){var t,r,o=this,i="";switch(e=e.replace(/[A-Za-z]/g,"").replace(o.numeralDecimalMark,"M").replace(/[^\dM-]/g,"").replace(/^\-/,"N").replace(/\-/g,"").replace("N",o.numeralPositiveOnly?"":"-").replace("M",o.numeralDecimalMark),o.stripLeadingZeroes&&(e=e.replace(/^(-)?0+(?=\d)/,"$1")),r=e,e.indexOf(o.numeralDecimalMark)>=0&&(t=e.split(o.numeralDecimalMark),r=t[0],i=o.numeralDecimalMark+t[1].slice(0,o.numeralDecimalScale)),o.numeralIntegerScale>0&&(r=r.slice(0,o.numeralIntegerScale+("-"===e.slice(0,1)?1:0))),o.numeralThousandsGroupStyle){case n.groupStyle.lakh:r=r.replace(/(\d)(?=(\d\d)+\d$)/g,"$1"+o.delimiter);break;case n.groupStyle.wan:r=r.replace(/(\d)(?=(\d{4})+$)/g,"$1"+o.delimiter);break;case n.groupStyle.thousand:r=r.replace(/(\d)(?=(\d{3})+$)/g,"$1"+o.delimiter)}return r.toString()+(o.numeralDecimalScale>0?i.toString():"")}},e.exports=n},function(e,t){"use strict";var n=function(e){var t=this;t.date=[],t.blocks=[],t.datePattern=e,t.initBlocks()};n.prototype={initBlocks:function(){var e=this;e.datePattern.forEach(function(t){"Y"===t?e.blocks.push(4):e.blocks.push(2)})},getISOFormatDate:function(){var e=this,t=e.date;return t[2]?t[2]+"-"+e.addLeadingZero(t[1])+"-"+e.addLeadingZero(t[0]):""},getBlocks:function(){return this.blocks},getValidatedDate:function(e){var t=this,n="";return e=e.replace(/[^\d]/g,""),t.blocks.forEach(function(r,o){if(e.length>0){var i=e.slice(0,r),a=i.slice(0,1),s=e.slice(r);switch(t.datePattern[o]){case"d":"00"===i?i="01":parseInt(a,10)>3?i="0"+a:parseInt(i,10)>31&&(i="31");break;case"m":"00"===i?i="01":parseInt(a,10)>1?i="0"+a:parseInt(i,10)>12&&(i="12")}n+=i,e=s}}),this.getFixedDateString(n)},getFixedDateString:function(e){var t,n,r,o=this,i=o.datePattern,a=[],s=0,u=0,c=0,l=0,p=0,d=0,m=!1;return 4===e.length&&"y"!==i[0].toLowerCase()&&"y"!==i[1].toLowerCase()&&(l="d"===i[0]?0:2,p=2-l,t=parseInt(e.slice(l,l+2),10),n=parseInt(e.slice(p,p+2),10),a=this.getFixedDate(t,n,0)),8===e.length&&(i.forEach(function(e,t){switch(e){case"d":s=t;break;case"m":u=t;break;default:c=t}}),d=2*c,l=s<=c?2*s:2*s+2,p=u<=c?2*u:2*u+2,t=parseInt(e.slice(l,l+2),10),n=parseInt(e.slice(p,p+2),10),r=parseInt(e.slice(d,d+4),10),m=4===e.slice(d,d+4).length,a=this.getFixedDate(t,n,r)),o.date=a,0===a.length?e:i.reduce(function(e,t){switch(t){case"d":return e+o.addLeadingZero(a[0]);case"m":return e+o.addLeadingZero(a[1]);default:return e+(m?o.addLeadingZeroForYear(a[2]):"")}},"")},getFixedDate:function(e,t,n){return e=Math.min(e,31),t=Math.min(t,12),n=parseInt(n||0,10),(t<7&&t%2===0||t>8&&t%2===1)&&(e=Math.min(e,2===t?this.isLeapYear(n)?29:28:30)),[e,t,n]},isLeapYear:function(e){return e%4===0&&e%100!==0||e%400===0},addLeadingZero:function(e){return(e<10?"0":"")+e},addLeadingZeroForYear:function(e){return(e<10?"000":e<100?"00":e<1e3?"0":"")+e}},e.exports=n},function(e,t){"use strict";var n=function(e,t){var n=this;n.delimiter=t||""===t?t:" ",n.delimiterRE=t?new RegExp("\\"+t,"g"):"",n.formatter=e};n.prototype={setFormatter:function(e){this.formatter=e},format:function(e){var t=this;t.formatter.clear(),e=e.replace(/[^\d+]/g,""),e=e.replace(t.delimiterRE,"");for(var n,r="",o=!1,i=0,a=e.length;in?r:t;else{var o=this.getFirstDiffIndex(t,e.slice(0,n));e=t+e.slice(o,o+1)+e.slice(n+1)}return e.slice(n)},getFirstDiffIndex:function(e,t){for(var n=0;e.charAt(n)===t.charAt(n);)if(""===e.charAt(n++))return-1;return n},getFormattedValue:function(e,t,n,r,o,i){var a,s="",u=o.length>0;return 0===n?e:(t.forEach(function(t,c){if(e.length>0){var l=e.slice(0,t),p=e.slice(t);a=u?o[i?c-1:c]||a:r,i?(c>0&&(s+=a),s+=l):(s+=l,l.length===t&&c0?r.numeralIntegerScale:0,e.numeralDecimalScale=r.numeralDecimalScale>=0?r.numeralDecimalScale:2,e.numeralDecimalMark=r.numeralDecimalMark||".",e.numeralThousandsGroupStyle=r.numeralThousandsGroupStyle||"thousand",e.numeralPositiveOnly=!!r.numeralPositiveOnly,e.stripLeadingZeroes=r.stripLeadingZeroes!==!1,e.numericOnly=e.creditCard||e.date||!!r.numericOnly,e.uppercase=!!r.uppercase,e.lowercase=!!r.lowercase,e.prefix=e.creditCard||e.date?"":r.prefix||"",e.noImmediatePrefix=!!r.noImmediatePrefix,e.prefixLength=e.prefix.length,e.rawValueTrimPrefix=!!r.rawValueTrimPrefix,e.copyDelimiter=!!r.copyDelimiter,e.initValue=void 0!==r.initValue&&null!==r.initValue?r.initValue.toString():"",e.delimiter=r.delimiter||""===r.delimiter?r.delimiter:r.date?"/":r.numeral?",":(r.phone," "),e.delimiterLength=e.delimiter.length,e.delimiterLazyShow=!!r.delimiterLazyShow,e.delimiters=r.delimiters||[],e.blocks=r.blocks||[],e.blocksLength=e.blocks.length,e.root="object"===("undefined"==typeof t?"undefined":n(t))&&t?t:window,e.document=r.document||e.root.document,e.maxLength=0,e.backspace=!1,e.result="",e.onValueChanged=r.onValueChanged||function(){},e}};e.exports=r}).call(t,function(){return this}())}])}); \ No newline at end of file +!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t(require("react")):"function"==typeof define&&define.amd?define(["react"],t):"object"==typeof exports?exports.Cleave=t(require("react")):e.Cleave=t(e.React)}(this,function(e){return function(e){function t(r){if(n[r])return n[r].exports;var i=n[r]={exports:{},id:r,loaded:!1};return e[r].call(i.exports,i,i.exports,t),i.loaded=!0,i.exports}var n={};return t.m=e,t.c=n,t.p="",t(0)}([function(e,t,n){"use strict";function r(e,t){var n={};for(var r in e)t.indexOf(r)>=0||Object.prototype.hasOwnProperty.call(e,r)&&(n[r]=e[r]);return n}var i=Object.assign||function(e){for(var t=1;t0?d.headStr(e,r.maxLength):e,r.result=d.getFormattedValue(e,r.blocks,r.blocksLength,r.delimiter,r.delimiters,r.delimiterLazyShow),void n.updateValueState()):(r.result=e,void n.updateValueState()))},updateCreditCardPropsByValue:function(e){var t,n=this,r=n.properties;d.headStr(r.result,4)!==d.headStr(e,4)&&(t=p.getInfo(e,r.creditCardStrictMode),r.blocks=t.blocks,r.blocksLength=r.blocks.length,r.maxLength=d.getMaxLength(r.blocks),r.creditCardType!==t.type&&(r.creditCardType=t.type,r.onCreditCardTypeChanged.call(n,r.creditCardType)))},updateValueState:function(){var e=this,t=e.properties;e.element||e.setState({value:t.result});var n=e.element.selectionEnd,r=e.element.value,i=t.result;return e.lastInputValue=i,n=d.getNextCursorPosition(n,r,i,t.delimiter,t.delimiters),e.isAndroid?void window.setTimeout(function(){e.setState({value:i,cursorPosition:n})},1):void e.setState({value:i,cursorPosition:n})},render:function(){var e=this,t=e.props,n=(t.value,t.options,t.onKeyDown,t.onFocus,t.onBlur,t.onChange,t.onInit,t.htmlRef),a=r(t,["value","options","onKeyDown","onFocus","onBlur","onChange","onInit","htmlRef"]);return o.createElement("input",i({type:"text",ref:function(t){e.element=t,n&&n.apply(this,arguments)},value:e.state.value,onKeyDown:e.onKeyDown,onChange:e.onChange,onFocus:e.onFocus,onBlur:e.onBlur},a))}});e.exports=f},function(t,n){t.exports=e},function(e,t,n){"use strict";var r=n(1),i=n(3);if("undefined"==typeof r)throw Error("create-react-class could not find the React object. If you are using script tags, make sure that React is being loaded before create-react-class.");var o=(new r.Component).updater;e.exports=i(r.Component,r.isValidElement,o)},function(e,t,n){(function(t){"use strict";function r(e){return e}function i(e,n,i){function p(e,n,r){for(var i in n)n.hasOwnProperty(i)&&"production"!==t.env.NODE_ENV&&c("function"==typeof n[i],"%s: %s type `%s` is invalid; it must be a function, usually from React.PropTypes.",e.displayName||"ReactClass",u[r],i)}function d(e,t){var n=D.hasOwnProperty(t)?D[t]:null;S.hasOwnProperty(t)&&s("OVERRIDE_BASE"===n,"ReactClassInterface: You are attempting to override `%s` from your class specification. Ensure that your method names do not overlap with React methods.",t),e&&s("DEFINE_MANY"===n||"DEFINE_MANY_MERGED"===n,"ReactClassInterface: You are attempting to define `%s` on your component more than once. This conflict may be due to a mixin.",t)}function m(e,r){if(r){s("function"!=typeof r,"ReactClass: You're attempting to use a component class or function as a mixin. Instead, just use a regular object."),s(!n(r),"ReactClass: You're attempting to use a component as a mixin. Instead, just use a regular object.");var i=e.prototype,o=i.__reactAutoBindPairs;r.hasOwnProperty(l)&&w.mixins(e,r.mixins);for(var a in r)if(r.hasOwnProperty(a)&&a!==l){var u=r[a],p=i.hasOwnProperty(a);if(d(p,a),w.hasOwnProperty(a))w[a](e,u);else{var m=D.hasOwnProperty(a),f="function"==typeof u,h=f&&!m&&!p&&r.autobind!==!1;if(h)o.push(a,u),i[a]=u;else if(p){var y=D[a];s(m&&("DEFINE_MANY_MERGED"===y||"DEFINE_MANY"===y),"ReactClass: Unexpected spec policy %s for key %s when mixing in component specs.",y,a),"DEFINE_MANY_MERGED"===y?i[a]=g(i[a],u):"DEFINE_MANY"===y&&(i[a]=v(i[a],u))}else i[a]=u,"production"!==t.env.NODE_ENV&&"function"==typeof u&&r.displayName&&(i[a].displayName=r.displayName+"_"+a)}}}else if("production"!==t.env.NODE_ENV){var E=typeof r,b="object"===E&&null!==r;"production"!==t.env.NODE_ENV&&c(b,"%s: You're attempting to include a mixin that is either null or not an object. Check the mixins included by the component, as well as any mixins they include themselves. Expected object but got %s.",e.displayName||"ReactClass",null===r?null:E)}}function f(e,t){if(t)for(var n in t){var r=t[n];if(t.hasOwnProperty(n)){var i=n in w;s(!i,'ReactClass: You are attempting to define a reserved property, `%s`, that shouldn\'t be on the "statics" key. Define it as an instance property instead; it will still be accessible on the constructor.',n);var o=n in e;s(!o,"ReactClass: You are attempting to define `%s` on your component more than once. This conflict may be due to a mixin.",n),e[n]=r}}}function h(e,t){s(e&&t&&"object"==typeof e&&"object"==typeof t,"mergeIntoWithNoDuplicateKeys(): Cannot merge non-objects.");for(var n in t)t.hasOwnProperty(n)&&(s(void 0===e[n],"mergeIntoWithNoDuplicateKeys(): Tried to merge two objects with the same key: `%s`. This conflict may be due to a mixin; in particular, this may be caused by two getInitialState() or getDefaultProps() methods returning objects with clashing keys.",n),e[n]=t[n]);return e}function g(e,t){return function(){var n=e.apply(this,arguments),r=t.apply(this,arguments);if(null==n)return r;if(null==r)return n;var i={};return h(i,n),h(i,r),i}}function v(e,t){return function(){e.apply(this,arguments),t.apply(this,arguments)}}function y(e,n){var r=n.bind(e);if("production"!==t.env.NODE_ENV){r.__reactBoundContext=e,r.__reactBoundMethod=n,r.__reactBoundArguments=null;var i=e.constructor.displayName,o=r.bind;r.bind=function(a){for(var s=arguments.length,u=Array(s>1?s-1:0),l=1;l1)for(var n=1;n1?t-1:0),r=1;r2?n-2:0),i=2;i0?t:0,c.numeralDecimalScale=n>=0?n:2,c.numeralThousandsGroupStyle=i||r.groupStyle.thousand,c.numeralPositiveOnly=!!o,c.stripLeadingZeroes=a!==!1,c.delimiter=s||""===s?s:",",c.delimiterRE=s?new RegExp("\\"+s,"g"):""};n.groupStyle={thousand:"thousand",lakh:"lakh",wan:"wan",none:"none"},n.prototype={getRawValue:function(e){return e.replace(this.delimiterRE,"").replace(this.numeralDecimalMark,".")},format:function(e){var t,r,i=this,o="";switch(e=e.replace(/[A-Za-z]/g,"").replace(i.numeralDecimalMark,"M").replace(/[^\dM-]/g,"").replace(/^\-/,"N").replace(/\-/g,"").replace("N",i.numeralPositiveOnly?"":"-").replace("M",i.numeralDecimalMark),i.stripLeadingZeroes&&(e=e.replace(/^(-)?0+(?=\d)/,"$1")),r=e,e.indexOf(i.numeralDecimalMark)>=0&&(t=e.split(i.numeralDecimalMark),r=t[0],o=i.numeralDecimalMark+t[1].slice(0,i.numeralDecimalScale)),i.numeralIntegerScale>0&&(r=r.slice(0,i.numeralIntegerScale+("-"===e.slice(0,1)?1:0))),i.numeralThousandsGroupStyle){case n.groupStyle.lakh:r=r.replace(/(\d)(?=(\d\d)+\d$)/g,"$1"+i.delimiter);break;case n.groupStyle.wan:r=r.replace(/(\d)(?=(\d{4})+$)/g,"$1"+i.delimiter);break;case n.groupStyle.thousand:r=r.replace(/(\d)(?=(\d{3})+$)/g,"$1"+i.delimiter)}return r.toString()+(i.numeralDecimalScale>0?o.toString():"")}},e.exports=n},function(e,t){"use strict";var n=function(e){var t=this;t.date=[],t.blocks=[],t.datePattern=e,t.initBlocks()};n.prototype={initBlocks:function(){var e=this;e.datePattern.forEach(function(t){"Y"===t?e.blocks.push(4):e.blocks.push(2)})},getISOFormatDate:function(){var e=this,t=e.date;return t[2]?t[2]+"-"+e.addLeadingZero(t[1])+"-"+e.addLeadingZero(t[0]):""},getBlocks:function(){return this.blocks},getValidatedDate:function(e){var t=this,n="";return e=e.replace(/[^\d]/g,""),t.blocks.forEach(function(r,i){if(e.length>0){var o=e.slice(0,r),a=o.slice(0,1),s=e.slice(r);switch(t.datePattern[i]){case"d":"00"===o?o="01":parseInt(a,10)>3?o="0"+a:parseInt(o,10)>31&&(o="31");break;case"m":"00"===o?o="01":parseInt(a,10)>1?o="0"+a:parseInt(o,10)>12&&(o="12")}n+=o,e=s}}),this.getFixedDateString(n)},getFixedDateString:function(e){var t,n,r,i=this,o=i.datePattern,a=[],s=0,c=0,u=0,l=0,p=0,d=0,m=!1;return 4===e.length&&"y"!==o[0].toLowerCase()&&"y"!==o[1].toLowerCase()&&(l="d"===o[0]?0:2,p=2-l,t=parseInt(e.slice(l,l+2),10),n=parseInt(e.slice(p,p+2),10),a=this.getFixedDate(t,n,0)),8===e.length&&(o.forEach(function(e,t){switch(e){case"d":s=t;break;case"m":c=t;break;default:u=t}}),d=2*u,l=s<=u?2*s:2*s+2,p=c<=u?2*c:2*c+2,t=parseInt(e.slice(l,l+2),10),n=parseInt(e.slice(p,p+2),10),r=parseInt(e.slice(d,d+4),10),m=4===e.slice(d,d+4).length,a=this.getFixedDate(t,n,r)),i.date=a,0===a.length?e:o.reduce(function(e,t){switch(t){case"d":return e+i.addLeadingZero(a[0]);case"m":return e+i.addLeadingZero(a[1]);default:return e+(m?i.addLeadingZeroForYear(a[2]):"")}},"")},getFixedDate:function(e,t,n){return e=Math.min(e,31),t=Math.min(t,12),n=parseInt(n||0,10),(t<7&&t%2===0||t>8&&t%2===1)&&(e=Math.min(e,2===t?this.isLeapYear(n)?29:28:30)),[e,t,n]},isLeapYear:function(e){return e%4===0&&e%100!==0||e%400===0},addLeadingZero:function(e){return(e<10?"0":"")+e},addLeadingZeroForYear:function(e){return(e<10?"000":e<100?"00":e<1e3?"0":"")+e}},e.exports=n},function(e,t){"use strict";var n=function(e){var t=this;t.time=[],t.blocks=[],t.timePattern=e,t.initBlocks()};n.prototype={initBlocks:function(){var e=this;e.timePattern.forEach(function(){e.blocks.push(2)})},getISOFormatTime:function(){var e=this,t=e.time;return t[2]?e.addLeadingZero(t[0])+":"+e.addLeadingZero(t[1])+":"+e.addLeadingZero(t[2]):""},getBlocks:function(){return this.blocks},getValidatedTime:function(e){var t=this,n="";return e=e.replace(/[^\d]/g,""),t.blocks.forEach(function(r,i){if(e.length>0){var o=e.slice(0,r),a=o.slice(0,1),s=e.slice(r);switch(t.timePattern[i]){case"h":parseInt(a,10)>2?o="0"+a:parseInt(o,10)>23&&(o="23");break;case"m":case"s":parseInt(a,10)>5?o="0"+a:parseInt(o,10)>60&&(o="60")}n+=o,e=s}}),this.getFixedTimeString(n)},getFixedTimeString:function(e){var t,n,r,i=this,o=i.timePattern,a=[],s=0,c=0,u=0,l=0,p=0,d=0;return 6===e.length&&(o.forEach(function(e,t){switch(e){case"s":s=2*t;break;case"m":c=2*t;break;case"h":u=2*t}}),d=u,p=c,l=s,t=parseInt(e.slice(l,l+2),10),n=parseInt(e.slice(p,p+2),10),r=parseInt(e.slice(d,d+2),10),a=this.getFixedTime(r,n,t)),4===e.length&&i.timePattern.indexOf("s")<0&&(o.forEach(function(e,t){switch(e){case"m":c=2*t;break;case"h":u=2*t}}),d=u,p=c,t=0,n=parseInt(e.slice(p,p+2),10),r=parseInt(e.slice(d,d+2),10),a=this.getFixedTime(r,n,t)),i.time=a,0===a.length?e:o.reduce(function(e,t){switch(t){case"s":return e+i.addLeadingZero(a[2]);case"m":return e+i.addLeadingZero(a[1]);case"h":return e+i.addLeadingZero(a[0])}},"")},getFixedTime:function(e,t,n){return n=Math.min(parseInt(n||0,10),60),t=Math.min(t,60),e=Math.min(e,60),[e,t,n]},addLeadingZero:function(e){return(e<10?"0":"")+e}},e.exports=n},function(e,t){"use strict";var n=function(e,t){var n=this;n.delimiter=t||""===t?t:" ",n.delimiterRE=t?new RegExp("\\"+t,"g"):"",n.formatter=e};n.prototype={setFormatter:function(e){this.formatter=e},format:function(e){var t=this;t.formatter.clear(),e=e.replace(/[^\d+]/g,""),e=e.replace(t.delimiterRE,"");for(var n,r="",i=!1,o=0,a=e.length;on?r:t;else{var i=this.getFirstDiffIndex(t,e.slice(0,n));e=t+e.slice(i,i+1)+e.slice(n+1)}return e.slice(n)},getFirstDiffIndex:function(e,t){for(var n=0;e.charAt(n)===t.charAt(n);)if(""===e.charAt(n++))return-1;return n},getFormattedValue:function(e,t,n,r,i,o){var a,s="",c=i.length>0;return 0===n?e:(t.forEach(function(t,u){if(e.length>0){var l=e.slice(0,t),p=e.slice(t);a=c?i[o?u-1:u]||a:r,o?(u>0&&(s+=a),s+=l):(s+=l,l.length===t&&u0?r.numeralIntegerScale:0,e.numeralDecimalScale=r.numeralDecimalScale>=0?r.numeralDecimalScale:2,e.numeralDecimalMark=r.numeralDecimalMark||".",e.numeralThousandsGroupStyle=r.numeralThousandsGroupStyle||"thousand",e.numeralPositiveOnly=!!r.numeralPositiveOnly,e.stripLeadingZeroes=r.stripLeadingZeroes!==!1,e.numericOnly=e.creditCard||e.date||!!r.numericOnly,e.uppercase=!!r.uppercase,e.lowercase=!!r.lowercase,e.prefix=e.creditCard||e.date?"":r.prefix||"",e.noImmediatePrefix=!!r.noImmediatePrefix,e.prefixLength=e.prefix.length,e.rawValueTrimPrefix=!!r.rawValueTrimPrefix,e.copyDelimiter=!!r.copyDelimiter,e.initValue=void 0!==r.initValue&&null!==r.initValue?r.initValue.toString():"",e.delimiter=r.delimiter||""===r.delimiter?r.delimiter:r.date?"/":r.time?":":r.numeral?",":(r.phone," "),e.delimiterLength=e.delimiter.length,e.delimiterLazyShow=!!r.delimiterLazyShow,e.delimiters=r.delimiters||[],e.blocks=r.blocks||[],e.blocksLength=e.blocks.length,e.root="object"===("undefined"==typeof t?"undefined":n(t))&&t?t:window,e.document=r.document||e.root.document,e.maxLength=0,e.backspace=!1,e.result="",e.onValueChanged=r.onValueChanged||function(){},e}};e.exports=r}).call(t,function(){return this}())}])}); \ No newline at end of file diff --git a/dist/cleave.js b/dist/cleave.js index 392c8143..834a1503 100644 --- a/dist/cleave.js +++ b/dist/cleave.js @@ -87,7 +87,7 @@ return /******/ (function(modules) { // webpackBootstrap var owner = this, pps = owner.properties; // no need to use this lib - if (!pps.numeral && !pps.phone && !pps.creditCard && !pps.date && (pps.blocksLength === 0 && !pps.prefix)) { + if (!pps.numeral && !pps.phone && !pps.creditCard && !pps.time && !pps.date && (pps.blocksLength === 0 && !pps.prefix)) { owner.onInput(pps.initValue); return; @@ -113,6 +113,7 @@ return /******/ (function(modules) { // webpackBootstrap owner.initPhoneFormatter(); owner.initDateFormatter(); + owner.initTimeFormatter(); owner.initNumeralFormatter(); // avoid touch input field if value is null @@ -140,6 +141,19 @@ return /******/ (function(modules) { // webpackBootstrap ); }, + initTimeFormatter: function() { + var owner = this, pps = owner.properties; + + if (!pps.time) { + return; + } + + pps.timeFormatter = new Cleave.TimeFormatter(pps.timePattern); + pps.blocks = pps.timeFormatter.getBlocks(); + pps.blocksLength = pps.blocks.length; + pps.maxLength = Cleave.Util.getMaxLength(pps.blocks); + }, + initDateFormatter: function () { var owner = this, pps = owner.properties; @@ -282,6 +296,11 @@ return /******/ (function(modules) { // webpackBootstrap value = pps.dateFormatter.getValidatedDate(value); } + // time + if (pps.time) { + value = pps.timeFormatter.getValidatedTime(value); + } + // strip delimiters value = Util.stripDelimiters(value, pps.delimiter, pps.delimiters); @@ -464,10 +483,11 @@ return /******/ (function(modules) { // webpackBootstrap Cleave.NumeralFormatter = __webpack_require__(1); Cleave.DateFormatter = __webpack_require__(2); - Cleave.PhoneFormatter = __webpack_require__(3); - Cleave.CreditCardDetector = __webpack_require__(4); - Cleave.Util = __webpack_require__(5); - Cleave.DefaultProperties = __webpack_require__(6); + Cleave.TimeFormatter = __webpack_require__(3); + Cleave.PhoneFormatter = __webpack_require__(4); + Cleave.CreditCardDetector = __webpack_require__(5); + Cleave.Util = __webpack_require__(6); + Cleave.DefaultProperties = __webpack_require__(7); // for angular directive ((typeof global === 'object' && global) ? global : window)['Cleave'] = Cleave; @@ -758,6 +778,168 @@ return /******/ (function(modules) { // webpackBootstrap 'use strict'; + var TimeFormatter = function (timePattern) { + var owner = this; + + owner.time = []; + owner.blocks = []; + owner.timePattern = timePattern; + owner.initBlocks(); + }; + + TimeFormatter.prototype = { + initBlocks: function () { + var owner = this; + owner.timePattern.forEach(function () { + owner.blocks.push(2); + }); + }, + + getISOFormatTime: function () { + var owner = this, + time = owner.time; + + return time[2] ? ( + owner.addLeadingZero(time[0]) + ':' + owner.addLeadingZero(time[1]) + ':' + owner.addLeadingZero(time[2]) + ) : ''; + }, + + getBlocks: function () { + return this.blocks; + }, + + getValidatedTime: function (value) { + var owner = this, result = ''; + + value = value.replace(/[^\d]/g, ''); + + owner.blocks.forEach(function (length, index) { + if (value.length > 0) { + var sub = value.slice(0, length), + sub0 = sub.slice(0, 1), + rest = value.slice(length); + + switch (owner.timePattern[index]) { + + case 'h': + if (parseInt(sub0, 10) > 2) { + sub = '0' + sub0; + } else if (parseInt(sub, 10) > 23) { + sub = '23'; + } + + break; + + case 'm': + case 's': + if (parseInt(sub0, 10) > 5) { + sub = '0' + sub0; + } else if (parseInt(sub, 10) > 60) { + sub = '60'; + } + break; + } + + result += sub; + + // update remaining string + value = rest; + } + }); + + return this.getFixedTimeString(result); + }, + + getFixedTimeString: function (value) { + var owner = this, timePattern = owner.timePattern, time = [], + secondIndex = 0, minuteIndex = 0, hourIndex = 0, + secondStartIndex = 0, minuteStartIndex = 0, hourStartIndex = 0, + second, minute, hour; + + if (value.length === 6) { + timePattern.forEach(function (type, index) { + switch (type) { + case 's': + secondIndex = index * 2; + break; + case 'm': + minuteIndex = index * 2; + break; + case 'h': + hourIndex = index * 2; + break; + } + }); + + hourStartIndex = hourIndex; + minuteStartIndex = minuteIndex; + secondStartIndex = secondIndex; + + second = parseInt(value.slice(secondStartIndex, secondStartIndex + 2), 10); + minute = parseInt(value.slice(minuteStartIndex, minuteStartIndex + 2), 10); + hour = parseInt(value.slice(hourStartIndex, hourStartIndex + 2), 10); + + time = this.getFixedTime(hour, minute, second); + } + + if (value.length === 4 && owner.timePattern.indexOf('s') < 0) { + timePattern.forEach(function (type, index) { + switch (type) { + case 'm': + minuteIndex = index * 2; + break; + case 'h': + hourIndex = index * 2; + break; + } + }); + + hourStartIndex = hourIndex; + minuteStartIndex = minuteIndex; + + second = 0; + minute = parseInt(value.slice(minuteStartIndex, minuteStartIndex + 2), 10); + hour = parseInt(value.slice(hourStartIndex, hourStartIndex + 2), 10); + + time = this.getFixedTime(hour, minute, second); + } + + owner.time = time; + + return time.length === 0 ? value : timePattern.reduce(function (previous, current) { + switch (current) { + case 's': + return previous + owner.addLeadingZero(time[2]); + case 'm': + return previous + owner.addLeadingZero(time[1]); + case 'h': + return previous + owner.addLeadingZero(time[0]); + } + }, ''); + }, + + getFixedTime: function (hour, minute, second) { + second = Math.min(parseInt(second || 0, 10), 60); + minute = Math.min(minute, 60); + hour = Math.min(hour, 60); + + return [hour, minute, second]; + }, + + addLeadingZero: function (number) { + return (number < 10 ? '0' : '') + number; + } + }; + + module.exports = TimeFormatter; + + +/***/ }), +/* 4 */ +/***/ (function(module, exports) { + + 'use strict'; + var PhoneFormatter = function (formatter, delimiter) { var owner = this; @@ -817,7 +999,7 @@ return /******/ (function(modules) { // webpackBootstrap /***/ }), -/* 4 */ +/* 5 */ /***/ (function(module, exports) { 'use strict'; @@ -920,7 +1102,7 @@ return /******/ (function(modules) { // webpackBootstrap /***/ }), -/* 5 */ +/* 6 */ /***/ (function(module, exports) { 'use strict'; @@ -1141,7 +1323,7 @@ return /******/ (function(modules) { // webpackBootstrap /***/ }), -/* 6 */ +/* 7 */ /***/ (function(module, exports) { /* WEBPACK VAR INJECTION */(function(global) {'use strict'; @@ -1169,6 +1351,11 @@ return /******/ (function(modules) { // webpackBootstrap target.phoneRegionCode = opts.phoneRegionCode || 'AU'; target.phoneFormatter = {}; + // time + target.time = !!opts.time; + target.timePattern = opts.timePattern || ['h', 'm']; + target.timeFormatter = {}; + // date target.date = !!opts.date; target.datePattern = opts.datePattern || ['d', 'm', 'Y']; @@ -1200,9 +1387,10 @@ return /******/ (function(modules) { // webpackBootstrap target.delimiter = (opts.delimiter || opts.delimiter === '') ? opts.delimiter : (opts.date ? '/' : - (opts.numeral ? ',' : - (opts.phone ? ' ' : - ' '))); + (opts.time ? ':' : + (opts.numeral ? ',' : + (opts.phone ? ' ' : + ' ')))); target.delimiterLength = target.delimiter.length; target.delimiterLazyShow = !!opts.delimiterLazyShow; target.delimiters = opts.delimiters || []; diff --git a/dist/cleave.min.js b/dist/cleave.min.js index 8ff167ec..2f737098 100644 --- a/dist/cleave.min.js +++ b/dist/cleave.min.js @@ -1,8 +1,8 @@ /*! - * cleave.js - 1.3.8 + * cleave.js - 1.4.0 * https://github.com/nosir/cleave.js * Apache License Version 2.0 * * Copyright (C) 2012-2018 Max Huang https://github.com/nosir/ */ -!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.Cleave=t():e.Cleave=t()}(this,function(){return function(e){function t(n){if(r[n])return r[n].exports;var i=r[n]={exports:{},id:n,loaded:!1};return e[n].call(i.exports,i,i.exports,t),i.loaded=!0,i.exports}var r={};return t.m=e,t.c=r,t.p="",t(0)}([function(e,t,r){(function(t){"use strict";var n=function(e,t){var r=this;if("string"==typeof e?r.element=document.querySelector(e):r.element="undefined"!=typeof e.length&&e.length>0?e[0]:e,!r.element)throw new Error("[cleave.js] Please check the element");t.initValue=r.element.value,r.properties=n.DefaultProperties.assign({},t),r.init()};n.prototype={init:function(){var e=this,t=e.properties;return t.numeral||t.phone||t.creditCard||t.date||0!==t.blocksLength||t.prefix?(t.maxLength=n.Util.getMaxLength(t.blocks),e.isAndroid=n.Util.isAndroid(),e.lastInputValue="",e.onChangeListener=e.onChange.bind(e),e.onKeyDownListener=e.onKeyDown.bind(e),e.onFocusListener=e.onFocus.bind(e),e.onCutListener=e.onCut.bind(e),e.onCopyListener=e.onCopy.bind(e),e.element.addEventListener("input",e.onChangeListener),e.element.addEventListener("keydown",e.onKeyDownListener),e.element.addEventListener("focus",e.onFocusListener),e.element.addEventListener("cut",e.onCutListener),e.element.addEventListener("copy",e.onCopyListener),e.initPhoneFormatter(),e.initDateFormatter(),e.initNumeralFormatter(),void((t.initValue||t.prefix&&!t.noImmediatePrefix)&&e.onInput(t.initValue))):void e.onInput(t.initValue)},initNumeralFormatter:function(){var e=this,t=e.properties;t.numeral&&(t.numeralFormatter=new n.NumeralFormatter(t.numeralDecimalMark,t.numeralIntegerScale,t.numeralDecimalScale,t.numeralThousandsGroupStyle,t.numeralPositiveOnly,t.stripLeadingZeroes,t.delimiter))},initDateFormatter:function(){var e=this,t=e.properties;t.date&&(t.dateFormatter=new n.DateFormatter(t.datePattern),t.blocks=t.dateFormatter.getBlocks(),t.blocksLength=t.blocks.length,t.maxLength=n.Util.getMaxLength(t.blocks))},initPhoneFormatter:function(){var e=this,t=e.properties;if(t.phone)try{t.phoneFormatter=new n.PhoneFormatter(new t.root.Cleave.AsYouTypeFormatter(t.phoneRegionCode),t.delimiter)}catch(r){throw new Error("[cleave.js] Please include phone-type-formatter.{country}.js lib")}},onKeyDown:function(e){var t=this,r=t.properties,i=e.which||e.keyCode,a=n.Util,o=t.element.value;return a.isAndroidBackspaceKeydown(t.lastInputValue,o)&&(i=8),t.lastInputValue=o,8===i&&a.isDelimiter(o.slice(-r.delimiterLength),r.delimiter,r.delimiters)?void(r.backspace=!0):void(r.backspace=!1)},onChange:function(){this.onInput(this.element.value)},onFocus:function(){var e=this,t=e.properties;n.Util.fixPrefixCursor(e.element,t.prefix,t.delimiter,t.delimiters)},onCut:function(e){this.copyClipboardData(e),this.onInput("")},onCopy:function(e){this.copyClipboardData(e)},copyClipboardData:function(e){var t=this,r=t.properties,i=n.Util,a=t.element.value,o="";o=r.copyDelimiter?a:i.stripDelimiters(a,r.delimiter,r.delimiters);try{e.clipboardData?e.clipboardData.setData("Text",o):window.clipboardData.setData("Text",o),e.preventDefault()}catch(l){}},onInput:function(e){var t=this,r=t.properties,i=n.Util;return r.numeral||!r.backspace||i.isDelimiter(e.slice(-r.delimiterLength),r.delimiter,r.delimiters)||(e=i.headStr(e,e.length-r.delimiterLength)),r.phone?(!r.prefix||r.noImmediatePrefix&&!e.length?r.result=r.phoneFormatter.format(e):r.result=r.prefix+r.phoneFormatter.format(e).slice(r.prefix.length),void t.updateValueState()):r.numeral?(!r.prefix||r.noImmediatePrefix&&!e.length?r.result=r.numeralFormatter.format(e):r.result=r.prefix+r.numeralFormatter.format(e),void t.updateValueState()):(r.date&&(e=r.dateFormatter.getValidatedDate(e)),e=i.stripDelimiters(e,r.delimiter,r.delimiters),e=i.getPrefixStrippedValue(e,r.prefix,r.prefixLength,r.result),e=r.numericOnly?i.strip(e,/[^\d]/g):e,e=r.uppercase?e.toUpperCase():e,e=r.lowercase?e.toLowerCase():e,!r.prefix||r.noImmediatePrefix&&!e.length||(e=r.prefix+e,0!==r.blocksLength)?(r.creditCard&&t.updateCreditCardPropsByValue(e),e=i.headStr(e,r.maxLength),r.result=i.getFormattedValue(e,r.blocks,r.blocksLength,r.delimiter,r.delimiters,r.delimiterLazyShow),void t.updateValueState()):(r.result=e,void t.updateValueState()))},updateCreditCardPropsByValue:function(e){var t,r=this,i=r.properties,a=n.Util;a.headStr(i.result,4)!==a.headStr(e,4)&&(t=n.CreditCardDetector.getInfo(e,i.creditCardStrictMode),i.blocks=t.blocks,i.blocksLength=i.blocks.length,i.maxLength=a.getMaxLength(i.blocks),i.creditCardType!==t.type&&(i.creditCardType=t.type,i.onCreditCardTypeChanged.call(r,i.creditCardType)))},updateValueState:function(){var e=this,t=n.Util,r=e.properties;if(e.element){var i=e.element.selectionEnd,a=e.element.value,o=r.result;if(i=t.getNextCursorPosition(i,a,o,r.delimiter,r.delimiters),e.isAndroid)return void window.setTimeout(function(){e.element.value=o,t.setSelection(e.element,i,r.document,!1),e.callOnValueChanged()},1);e.element.value=o,t.setSelection(e.element,i,r.document,!1),e.callOnValueChanged()}},callOnValueChanged:function(){var e=this,t=e.properties;t.onValueChanged.call(e,{target:{value:t.result,rawValue:e.getRawValue()}})},setPhoneRegionCode:function(e){var t=this,r=t.properties;r.phoneRegionCode=e,t.initPhoneFormatter(),t.onChange()},setRawValue:function(e){var t=this,r=t.properties;e=void 0!==e&&null!==e?e.toString():"",r.numeral&&(e=e.replace(".",r.numeralDecimalMark)),r.backspace=!1,t.element.value=e,t.onInput(e)},getRawValue:function(){var e=this,t=e.properties,r=n.Util,i=e.element.value;return t.rawValueTrimPrefix&&(i=r.getPrefixStrippedValue(i,t.prefix,t.prefixLength,t.result)),i=t.numeral?t.numeralFormatter.getRawValue(i):r.stripDelimiters(i,t.delimiter,t.delimiters)},getISOFormatDate:function(){var e=this,t=e.properties;return t.date?t.dateFormatter.getISOFormatDate():""},getFormattedValue:function(){return this.element.value},destroy:function(){var e=this;e.element.removeEventListener("input",e.onChangeListener),e.element.removeEventListener("keydown",e.onKeyDownListener),e.element.removeEventListener("focus",e.onFocusListener),e.element.removeEventListener("cut",e.onCutListener),e.element.removeEventListener("copy",e.onCopyListener)},toString:function(){return"[Cleave Object]"}},n.NumeralFormatter=r(1),n.DateFormatter=r(2),n.PhoneFormatter=r(3),n.CreditCardDetector=r(4),n.Util=r(5),n.DefaultProperties=r(6),("object"==typeof t&&t?t:window).Cleave=n,e.exports=n}).call(t,function(){return this}())},function(e,t){"use strict";var r=function(e,t,n,i,a,o,l){var s=this;s.numeralDecimalMark=e||".",s.numeralIntegerScale=t>0?t:0,s.numeralDecimalScale=n>=0?n:2,s.numeralThousandsGroupStyle=i||r.groupStyle.thousand,s.numeralPositiveOnly=!!a,s.stripLeadingZeroes=o!==!1,s.delimiter=l||""===l?l:",",s.delimiterRE=l?new RegExp("\\"+l,"g"):""};r.groupStyle={thousand:"thousand",lakh:"lakh",wan:"wan",none:"none"},r.prototype={getRawValue:function(e){return e.replace(this.delimiterRE,"").replace(this.numeralDecimalMark,".")},format:function(e){var t,n,i=this,a="";switch(e=e.replace(/[A-Za-z]/g,"").replace(i.numeralDecimalMark,"M").replace(/[^\dM-]/g,"").replace(/^\-/,"N").replace(/\-/g,"").replace("N",i.numeralPositiveOnly?"":"-").replace("M",i.numeralDecimalMark),i.stripLeadingZeroes&&(e=e.replace(/^(-)?0+(?=\d)/,"$1")),n=e,e.indexOf(i.numeralDecimalMark)>=0&&(t=e.split(i.numeralDecimalMark),n=t[0],a=i.numeralDecimalMark+t[1].slice(0,i.numeralDecimalScale)),i.numeralIntegerScale>0&&(n=n.slice(0,i.numeralIntegerScale+("-"===e.slice(0,1)?1:0))),i.numeralThousandsGroupStyle){case r.groupStyle.lakh:n=n.replace(/(\d)(?=(\d\d)+\d$)/g,"$1"+i.delimiter);break;case r.groupStyle.wan:n=n.replace(/(\d)(?=(\d{4})+$)/g,"$1"+i.delimiter);break;case r.groupStyle.thousand:n=n.replace(/(\d)(?=(\d{3})+$)/g,"$1"+i.delimiter)}return n.toString()+(i.numeralDecimalScale>0?a.toString():"")}},e.exports=r},function(e,t){"use strict";var r=function(e){var t=this;t.date=[],t.blocks=[],t.datePattern=e,t.initBlocks()};r.prototype={initBlocks:function(){var e=this;e.datePattern.forEach(function(t){"Y"===t?e.blocks.push(4):e.blocks.push(2)})},getISOFormatDate:function(){var e=this,t=e.date;return t[2]?t[2]+"-"+e.addLeadingZero(t[1])+"-"+e.addLeadingZero(t[0]):""},getBlocks:function(){return this.blocks},getValidatedDate:function(e){var t=this,r="";return e=e.replace(/[^\d]/g,""),t.blocks.forEach(function(n,i){if(e.length>0){var a=e.slice(0,n),o=a.slice(0,1),l=e.slice(n);switch(t.datePattern[i]){case"d":"00"===a?a="01":parseInt(o,10)>3?a="0"+o:parseInt(a,10)>31&&(a="31");break;case"m":"00"===a?a="01":parseInt(o,10)>1?a="0"+o:parseInt(a,10)>12&&(a="12")}r+=a,e=l}}),this.getFixedDateString(r)},getFixedDateString:function(e){var t,r,n,i=this,a=i.datePattern,o=[],l=0,s=0,u=0,c=0,d=0,m=0,p=!1;return 4===e.length&&"y"!==a[0].toLowerCase()&&"y"!==a[1].toLowerCase()&&(c="d"===a[0]?0:2,d=2-c,t=parseInt(e.slice(c,c+2),10),r=parseInt(e.slice(d,d+2),10),o=this.getFixedDate(t,r,0)),8===e.length&&(a.forEach(function(e,t){switch(e){case"d":l=t;break;case"m":s=t;break;default:u=t}}),m=2*u,c=l<=u?2*l:2*l+2,d=s<=u?2*s:2*s+2,t=parseInt(e.slice(c,c+2),10),r=parseInt(e.slice(d,d+2),10),n=parseInt(e.slice(m,m+4),10),p=4===e.slice(m,m+4).length,o=this.getFixedDate(t,r,n)),i.date=o,0===o.length?e:a.reduce(function(e,t){switch(t){case"d":return e+i.addLeadingZero(o[0]);case"m":return e+i.addLeadingZero(o[1]);default:return e+(p?i.addLeadingZeroForYear(o[2]):"")}},"")},getFixedDate:function(e,t,r){return e=Math.min(e,31),t=Math.min(t,12),r=parseInt(r||0,10),(t<7&&t%2===0||t>8&&t%2===1)&&(e=Math.min(e,2===t?this.isLeapYear(r)?29:28:30)),[e,t,r]},isLeapYear:function(e){return e%4===0&&e%100!==0||e%400===0},addLeadingZero:function(e){return(e<10?"0":"")+e},addLeadingZeroForYear:function(e){return(e<10?"000":e<100?"00":e<1e3?"0":"")+e}},e.exports=r},function(e,t){"use strict";var r=function(e,t){var r=this;r.delimiter=t||""===t?t:" ",r.delimiterRE=t?new RegExp("\\"+t,"g"):"",r.formatter=e};r.prototype={setFormatter:function(e){this.formatter=e},format:function(e){var t=this;t.formatter.clear(),e=e.replace(/[^\d+]/g,""),e=e.replace(t.delimiterRE,"");for(var r,n="",i=!1,a=0,o=e.length;ar?n:t;else{var i=this.getFirstDiffIndex(t,e.slice(0,r));e=t+e.slice(i,i+1)+e.slice(r+1)}return e.slice(r)},getFirstDiffIndex:function(e,t){for(var r=0;e.charAt(r)===t.charAt(r);)if(""===e.charAt(r++))return-1;return r},getFormattedValue:function(e,t,r,n,i,a){var o,l="",s=i.length>0;return 0===r?e:(t.forEach(function(t,u){if(e.length>0){var c=e.slice(0,t),d=e.slice(t);o=s?i[a?u-1:u]||o:n,a?(u>0&&(l+=o),l+=c):(l+=c,c.length===t&&u0?r.numeralIntegerScale:0,e.numeralDecimalScale=r.numeralDecimalScale>=0?r.numeralDecimalScale:2,e.numeralDecimalMark=r.numeralDecimalMark||".",e.numeralThousandsGroupStyle=r.numeralThousandsGroupStyle||"thousand",e.numeralPositiveOnly=!!r.numeralPositiveOnly,e.stripLeadingZeroes=r.stripLeadingZeroes!==!1,e.numericOnly=e.creditCard||e.date||!!r.numericOnly,e.uppercase=!!r.uppercase,e.lowercase=!!r.lowercase,e.prefix=e.creditCard||e.date?"":r.prefix||"",e.noImmediatePrefix=!!r.noImmediatePrefix,e.prefixLength=e.prefix.length,e.rawValueTrimPrefix=!!r.rawValueTrimPrefix,e.copyDelimiter=!!r.copyDelimiter,e.initValue=void 0!==r.initValue&&null!==r.initValue?r.initValue.toString():"",e.delimiter=r.delimiter||""===r.delimiter?r.delimiter:r.date?"/":r.numeral?",":(r.phone," "),e.delimiterLength=e.delimiter.length,e.delimiterLazyShow=!!r.delimiterLazyShow,e.delimiters=r.delimiters||[],e.blocks=r.blocks||[],e.blocksLength=e.blocks.length,e.root="object"==typeof t&&t?t:window,e.document=r.document||e.root.document,e.maxLength=0,e.backspace=!1,e.result="",e.onValueChanged=r.onValueChanged||function(){},e}};e.exports=r}).call(t,function(){return this}())}])}); \ No newline at end of file +!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.Cleave=t():e.Cleave=t()}(this,function(){return function(e){function t(n){if(r[n])return r[n].exports;var i=r[n]={exports:{},id:n,loaded:!1};return e[n].call(i.exports,i,i.exports,t),i.loaded=!0,i.exports}var r={};return t.m=e,t.c=r,t.p="",t(0)}([function(e,t,r){(function(t){"use strict";var n=function(e,t){var r=this;if("string"==typeof e?r.element=document.querySelector(e):r.element="undefined"!=typeof e.length&&e.length>0?e[0]:e,!r.element)throw new Error("[cleave.js] Please check the element");t.initValue=r.element.value,r.properties=n.DefaultProperties.assign({},t),r.init()};n.prototype={init:function(){var e=this,t=e.properties;return t.numeral||t.phone||t.creditCard||t.time||t.date||0!==t.blocksLength||t.prefix?(t.maxLength=n.Util.getMaxLength(t.blocks),e.isAndroid=n.Util.isAndroid(),e.lastInputValue="",e.onChangeListener=e.onChange.bind(e),e.onKeyDownListener=e.onKeyDown.bind(e),e.onFocusListener=e.onFocus.bind(e),e.onCutListener=e.onCut.bind(e),e.onCopyListener=e.onCopy.bind(e),e.element.addEventListener("input",e.onChangeListener),e.element.addEventListener("keydown",e.onKeyDownListener),e.element.addEventListener("focus",e.onFocusListener),e.element.addEventListener("cut",e.onCutListener),e.element.addEventListener("copy",e.onCopyListener),e.initPhoneFormatter(),e.initDateFormatter(),e.initTimeFormatter(),e.initNumeralFormatter(),void((t.initValue||t.prefix&&!t.noImmediatePrefix)&&e.onInput(t.initValue))):void e.onInput(t.initValue)},initNumeralFormatter:function(){var e=this,t=e.properties;t.numeral&&(t.numeralFormatter=new n.NumeralFormatter(t.numeralDecimalMark,t.numeralIntegerScale,t.numeralDecimalScale,t.numeralThousandsGroupStyle,t.numeralPositiveOnly,t.stripLeadingZeroes,t.delimiter))},initTimeFormatter:function(){var e=this,t=e.properties;t.time&&(t.timeFormatter=new n.TimeFormatter(t.timePattern),t.blocks=t.timeFormatter.getBlocks(),t.blocksLength=t.blocks.length,t.maxLength=n.Util.getMaxLength(t.blocks))},initDateFormatter:function(){var e=this,t=e.properties;t.date&&(t.dateFormatter=new n.DateFormatter(t.datePattern),t.blocks=t.dateFormatter.getBlocks(),t.blocksLength=t.blocks.length,t.maxLength=n.Util.getMaxLength(t.blocks))},initPhoneFormatter:function(){var e=this,t=e.properties;if(t.phone)try{t.phoneFormatter=new n.PhoneFormatter(new t.root.Cleave.AsYouTypeFormatter(t.phoneRegionCode),t.delimiter)}catch(r){throw new Error("[cleave.js] Please include phone-type-formatter.{country}.js lib")}},onKeyDown:function(e){var t=this,r=t.properties,i=e.which||e.keyCode,a=n.Util,o=t.element.value;return a.isAndroidBackspaceKeydown(t.lastInputValue,o)&&(i=8),t.lastInputValue=o,8===i&&a.isDelimiter(o.slice(-r.delimiterLength),r.delimiter,r.delimiters)?void(r.backspace=!0):void(r.backspace=!1)},onChange:function(){this.onInput(this.element.value)},onFocus:function(){var e=this,t=e.properties;n.Util.fixPrefixCursor(e.element,t.prefix,t.delimiter,t.delimiters)},onCut:function(e){this.copyClipboardData(e),this.onInput("")},onCopy:function(e){this.copyClipboardData(e)},copyClipboardData:function(e){var t=this,r=t.properties,i=n.Util,a=t.element.value,o="";o=r.copyDelimiter?a:i.stripDelimiters(a,r.delimiter,r.delimiters);try{e.clipboardData?e.clipboardData.setData("Text",o):window.clipboardData.setData("Text",o),e.preventDefault()}catch(l){}},onInput:function(e){var t=this,r=t.properties,i=n.Util;return r.numeral||!r.backspace||i.isDelimiter(e.slice(-r.delimiterLength),r.delimiter,r.delimiters)||(e=i.headStr(e,e.length-r.delimiterLength)),r.phone?(!r.prefix||r.noImmediatePrefix&&!e.length?r.result=r.phoneFormatter.format(e):r.result=r.prefix+r.phoneFormatter.format(e).slice(r.prefix.length),void t.updateValueState()):r.numeral?(!r.prefix||r.noImmediatePrefix&&!e.length?r.result=r.numeralFormatter.format(e):r.result=r.prefix+r.numeralFormatter.format(e),void t.updateValueState()):(r.date&&(e=r.dateFormatter.getValidatedDate(e)),r.time&&(e=r.timeFormatter.getValidatedTime(e)),e=i.stripDelimiters(e,r.delimiter,r.delimiters),e=i.getPrefixStrippedValue(e,r.prefix,r.prefixLength,r.result),e=r.numericOnly?i.strip(e,/[^\d]/g):e,e=r.uppercase?e.toUpperCase():e,e=r.lowercase?e.toLowerCase():e,!r.prefix||r.noImmediatePrefix&&!e.length||(e=r.prefix+e,0!==r.blocksLength)?(r.creditCard&&t.updateCreditCardPropsByValue(e),e=i.headStr(e,r.maxLength),r.result=i.getFormattedValue(e,r.blocks,r.blocksLength,r.delimiter,r.delimiters,r.delimiterLazyShow),void t.updateValueState()):(r.result=e,void t.updateValueState()))},updateCreditCardPropsByValue:function(e){var t,r=this,i=r.properties,a=n.Util;a.headStr(i.result,4)!==a.headStr(e,4)&&(t=n.CreditCardDetector.getInfo(e,i.creditCardStrictMode),i.blocks=t.blocks,i.blocksLength=i.blocks.length,i.maxLength=a.getMaxLength(i.blocks),i.creditCardType!==t.type&&(i.creditCardType=t.type,i.onCreditCardTypeChanged.call(r,i.creditCardType)))},updateValueState:function(){var e=this,t=n.Util,r=e.properties;if(e.element){var i=e.element.selectionEnd,a=e.element.value,o=r.result;if(i=t.getNextCursorPosition(i,a,o,r.delimiter,r.delimiters),e.isAndroid)return void window.setTimeout(function(){e.element.value=o,t.setSelection(e.element,i,r.document,!1),e.callOnValueChanged()},1);e.element.value=o,t.setSelection(e.element,i,r.document,!1),e.callOnValueChanged()}},callOnValueChanged:function(){var e=this,t=e.properties;t.onValueChanged.call(e,{target:{value:t.result,rawValue:e.getRawValue()}})},setPhoneRegionCode:function(e){var t=this,r=t.properties;r.phoneRegionCode=e,t.initPhoneFormatter(),t.onChange()},setRawValue:function(e){var t=this,r=t.properties;e=void 0!==e&&null!==e?e.toString():"",r.numeral&&(e=e.replace(".",r.numeralDecimalMark)),r.backspace=!1,t.element.value=e,t.onInput(e)},getRawValue:function(){var e=this,t=e.properties,r=n.Util,i=e.element.value;return t.rawValueTrimPrefix&&(i=r.getPrefixStrippedValue(i,t.prefix,t.prefixLength,t.result)),i=t.numeral?t.numeralFormatter.getRawValue(i):r.stripDelimiters(i,t.delimiter,t.delimiters)},getISOFormatDate:function(){var e=this,t=e.properties;return t.date?t.dateFormatter.getISOFormatDate():""},getFormattedValue:function(){return this.element.value},destroy:function(){var e=this;e.element.removeEventListener("input",e.onChangeListener),e.element.removeEventListener("keydown",e.onKeyDownListener),e.element.removeEventListener("focus",e.onFocusListener),e.element.removeEventListener("cut",e.onCutListener),e.element.removeEventListener("copy",e.onCopyListener)},toString:function(){return"[Cleave Object]"}},n.NumeralFormatter=r(1),n.DateFormatter=r(2),n.TimeFormatter=r(3),n.PhoneFormatter=r(4),n.CreditCardDetector=r(5),n.Util=r(6),n.DefaultProperties=r(7),("object"==typeof t&&t?t:window).Cleave=n,e.exports=n}).call(t,function(){return this}())},function(e,t){"use strict";var r=function(e,t,n,i,a,o,l){var s=this;s.numeralDecimalMark=e||".",s.numeralIntegerScale=t>0?t:0,s.numeralDecimalScale=n>=0?n:2,s.numeralThousandsGroupStyle=i||r.groupStyle.thousand,s.numeralPositiveOnly=!!a,s.stripLeadingZeroes=o!==!1,s.delimiter=l||""===l?l:",",s.delimiterRE=l?new RegExp("\\"+l,"g"):""};r.groupStyle={thousand:"thousand",lakh:"lakh",wan:"wan",none:"none"},r.prototype={getRawValue:function(e){return e.replace(this.delimiterRE,"").replace(this.numeralDecimalMark,".")},format:function(e){var t,n,i=this,a="";switch(e=e.replace(/[A-Za-z]/g,"").replace(i.numeralDecimalMark,"M").replace(/[^\dM-]/g,"").replace(/^\-/,"N").replace(/\-/g,"").replace("N",i.numeralPositiveOnly?"":"-").replace("M",i.numeralDecimalMark),i.stripLeadingZeroes&&(e=e.replace(/^(-)?0+(?=\d)/,"$1")),n=e,e.indexOf(i.numeralDecimalMark)>=0&&(t=e.split(i.numeralDecimalMark),n=t[0],a=i.numeralDecimalMark+t[1].slice(0,i.numeralDecimalScale)),i.numeralIntegerScale>0&&(n=n.slice(0,i.numeralIntegerScale+("-"===e.slice(0,1)?1:0))),i.numeralThousandsGroupStyle){case r.groupStyle.lakh:n=n.replace(/(\d)(?=(\d\d)+\d$)/g,"$1"+i.delimiter);break;case r.groupStyle.wan:n=n.replace(/(\d)(?=(\d{4})+$)/g,"$1"+i.delimiter);break;case r.groupStyle.thousand:n=n.replace(/(\d)(?=(\d{3})+$)/g,"$1"+i.delimiter)}return n.toString()+(i.numeralDecimalScale>0?a.toString():"")}},e.exports=r},function(e,t){"use strict";var r=function(e){var t=this;t.date=[],t.blocks=[],t.datePattern=e,t.initBlocks()};r.prototype={initBlocks:function(){var e=this;e.datePattern.forEach(function(t){"Y"===t?e.blocks.push(4):e.blocks.push(2)})},getISOFormatDate:function(){var e=this,t=e.date;return t[2]?t[2]+"-"+e.addLeadingZero(t[1])+"-"+e.addLeadingZero(t[0]):""},getBlocks:function(){return this.blocks},getValidatedDate:function(e){var t=this,r="";return e=e.replace(/[^\d]/g,""),t.blocks.forEach(function(n,i){if(e.length>0){var a=e.slice(0,n),o=a.slice(0,1),l=e.slice(n);switch(t.datePattern[i]){case"d":"00"===a?a="01":parseInt(o,10)>3?a="0"+o:parseInt(a,10)>31&&(a="31");break;case"m":"00"===a?a="01":parseInt(o,10)>1?a="0"+o:parseInt(a,10)>12&&(a="12")}r+=a,e=l}}),this.getFixedDateString(r)},getFixedDateString:function(e){var t,r,n,i=this,a=i.datePattern,o=[],l=0,s=0,c=0,u=0,d=0,m=0,p=!1;return 4===e.length&&"y"!==a[0].toLowerCase()&&"y"!==a[1].toLowerCase()&&(u="d"===a[0]?0:2,d=2-u,t=parseInt(e.slice(u,u+2),10),r=parseInt(e.slice(d,d+2),10),o=this.getFixedDate(t,r,0)),8===e.length&&(a.forEach(function(e,t){switch(e){case"d":l=t;break;case"m":s=t;break;default:c=t}}),m=2*c,u=l<=c?2*l:2*l+2,d=s<=c?2*s:2*s+2,t=parseInt(e.slice(u,u+2),10),r=parseInt(e.slice(d,d+2),10),n=parseInt(e.slice(m,m+4),10),p=4===e.slice(m,m+4).length,o=this.getFixedDate(t,r,n)),i.date=o,0===o.length?e:a.reduce(function(e,t){switch(t){case"d":return e+i.addLeadingZero(o[0]);case"m":return e+i.addLeadingZero(o[1]);default:return e+(p?i.addLeadingZeroForYear(o[2]):"")}},"")},getFixedDate:function(e,t,r){return e=Math.min(e,31),t=Math.min(t,12),r=parseInt(r||0,10),(t<7&&t%2===0||t>8&&t%2===1)&&(e=Math.min(e,2===t?this.isLeapYear(r)?29:28:30)),[e,t,r]},isLeapYear:function(e){return e%4===0&&e%100!==0||e%400===0},addLeadingZero:function(e){return(e<10?"0":"")+e},addLeadingZeroForYear:function(e){return(e<10?"000":e<100?"00":e<1e3?"0":"")+e}},e.exports=r},function(e,t){"use strict";var r=function(e){var t=this;t.time=[],t.blocks=[],t.timePattern=e,t.initBlocks()};r.prototype={initBlocks:function(){var e=this;e.timePattern.forEach(function(){e.blocks.push(2)})},getISOFormatTime:function(){var e=this,t=e.time;return t[2]?e.addLeadingZero(t[0])+":"+e.addLeadingZero(t[1])+":"+e.addLeadingZero(t[2]):""},getBlocks:function(){return this.blocks},getValidatedTime:function(e){var t=this,r="";return e=e.replace(/[^\d]/g,""),t.blocks.forEach(function(n,i){if(e.length>0){var a=e.slice(0,n),o=a.slice(0,1),l=e.slice(n);switch(t.timePattern[i]){case"h":parseInt(o,10)>2?a="0"+o:parseInt(a,10)>23&&(a="23");break;case"m":case"s":parseInt(o,10)>5?a="0"+o:parseInt(a,10)>60&&(a="60")}r+=a,e=l}}),this.getFixedTimeString(r)},getFixedTimeString:function(e){var t,r,n,i=this,a=i.timePattern,o=[],l=0,s=0,c=0,u=0,d=0,m=0;return 6===e.length&&(a.forEach(function(e,t){switch(e){case"s":l=2*t;break;case"m":s=2*t;break;case"h":c=2*t}}),m=c,d=s,u=l,t=parseInt(e.slice(u,u+2),10),r=parseInt(e.slice(d,d+2),10),n=parseInt(e.slice(m,m+2),10),o=this.getFixedTime(n,r,t)),4===e.length&&i.timePattern.indexOf("s")<0&&(a.forEach(function(e,t){switch(e){case"m":s=2*t;break;case"h":c=2*t}}),m=c,d=s,t=0,r=parseInt(e.slice(d,d+2),10),n=parseInt(e.slice(m,m+2),10),o=this.getFixedTime(n,r,t)),i.time=o,0===o.length?e:a.reduce(function(e,t){switch(t){case"s":return e+i.addLeadingZero(o[2]);case"m":return e+i.addLeadingZero(o[1]);case"h":return e+i.addLeadingZero(o[0])}},"")},getFixedTime:function(e,t,r){return r=Math.min(parseInt(r||0,10),60),t=Math.min(t,60),e=Math.min(e,60),[e,t,r]},addLeadingZero:function(e){return(e<10?"0":"")+e}},e.exports=r},function(e,t){"use strict";var r=function(e,t){var r=this;r.delimiter=t||""===t?t:" ",r.delimiterRE=t?new RegExp("\\"+t,"g"):"",r.formatter=e};r.prototype={setFormatter:function(e){this.formatter=e},format:function(e){var t=this;t.formatter.clear(),e=e.replace(/[^\d+]/g,""),e=e.replace(t.delimiterRE,"");for(var r,n="",i=!1,a=0,o=e.length;ar?n:t;else{var i=this.getFirstDiffIndex(t,e.slice(0,r));e=t+e.slice(i,i+1)+e.slice(r+1)}return e.slice(r)},getFirstDiffIndex:function(e,t){for(var r=0;e.charAt(r)===t.charAt(r);)if(""===e.charAt(r++))return-1;return r},getFormattedValue:function(e,t,r,n,i,a){var o,l="",s=i.length>0;return 0===r?e:(t.forEach(function(t,c){if(e.length>0){var u=e.slice(0,t),d=e.slice(t);o=s?i[a?c-1:c]||o:n,a?(c>0&&(l+=o),l+=u):(l+=u,u.length===t&&c0?r.numeralIntegerScale:0,e.numeralDecimalScale=r.numeralDecimalScale>=0?r.numeralDecimalScale:2,e.numeralDecimalMark=r.numeralDecimalMark||".",e.numeralThousandsGroupStyle=r.numeralThousandsGroupStyle||"thousand",e.numeralPositiveOnly=!!r.numeralPositiveOnly,e.stripLeadingZeroes=r.stripLeadingZeroes!==!1,e.numericOnly=e.creditCard||e.date||!!r.numericOnly,e.uppercase=!!r.uppercase,e.lowercase=!!r.lowercase,e.prefix=e.creditCard||e.date?"":r.prefix||"",e.noImmediatePrefix=!!r.noImmediatePrefix,e.prefixLength=e.prefix.length,e.rawValueTrimPrefix=!!r.rawValueTrimPrefix,e.copyDelimiter=!!r.copyDelimiter,e.initValue=void 0!==r.initValue&&null!==r.initValue?r.initValue.toString():"",e.delimiter=r.delimiter||""===r.delimiter?r.delimiter:r.date?"/":r.time?":":r.numeral?",":(r.phone," "),e.delimiterLength=e.delimiter.length,e.delimiterLazyShow=!!r.delimiterLazyShow,e.delimiters=r.delimiters||[],e.blocks=r.blocks||[],e.blocksLength=e.blocks.length,e.root="object"==typeof t&&t?t:window,e.document=r.document||e.root.document,e.maxLength=0,e.backspace=!1,e.result="",e.onValueChanged=r.onValueChanged||function(){},e}};e.exports=r}).call(t,function(){return this}())}])}); \ No newline at end of file diff --git a/doc/options.md b/doc/options.md index aa84413e..cb676ae8 100644 --- a/doc/options.md +++ b/doc/options.md @@ -12,6 +12,9 @@ - Date: - [date](#date) - [datePattern](#datepattern) +- Time: + - [time](#time) + - [timePattern](#timepattern) - Numerals: - [numeral](#numeral) - [numeralThousandsGroupStyle](#numeralthousandsgroupstyle) @@ -161,6 +164,37 @@ new Cleave('.my-input', { You can also custom the [delimiter](#delimiter) for date +## Time + +### `time` + +A `Boolean` value indicates if this is a time input field. Enable to trigger time shortcut mode. + +**Default value**: `false` + +### `timePattern` + +An `Array` value indicates the time pattern. + +Since it's an input field, leading `0` before hour, minute and second is required. To indicate what patterns it should apply, you can use: 'h', 'm' and 's'. + +**Default value**: `['h', 'm', 's']` + +```js +new Cleave('.my-input', { + time: true, + timePattern: ['h', 'm'] +}); +``` + +```js +['h', 'm', 's']: 14:56:37 +['h', 'm']: 21:56 +['s', 'm', 'h']: 37:56:14 +``` + +You can also custom the [delimiter](#delimiter) for time + ## Numerals ### `numeral` @@ -402,7 +436,7 @@ new Cleave('.my-input', { ```js // before input -// +// // after input of 5 // $5 ``` diff --git a/package.json b/package.json index 771f7805..82630846 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "form", "input" ], - "version": "1.3.8", + "version": "1.4.0", "files": [ "src", "dist", diff --git a/src/Cleave.js b/src/Cleave.js index 52ab6363..aaac94eb 100644 --- a/src/Cleave.js +++ b/src/Cleave.js @@ -31,7 +31,7 @@ Cleave.prototype = { var owner = this, pps = owner.properties; // no need to use this lib - if (!pps.numeral && !pps.phone && !pps.creditCard && !pps.time &&!pps.date && (pps.blocksLength === 0 && !pps.prefix)) { + if (!pps.numeral && !pps.phone && !pps.creditCard && !pps.time && !pps.date && (pps.blocksLength === 0 && !pps.prefix)) { owner.onInput(pps.initValue); return; diff --git a/src/Cleave.react.js b/src/Cleave.react.js index b8db7c21..90ce333f 100644 --- a/src/Cleave.react.js +++ b/src/Cleave.react.js @@ -5,6 +5,7 @@ var CreateReactClass = require('create-react-class'); var NumeralFormatter = require('./shortcuts/NumeralFormatter'); var DateFormatter = require('./shortcuts/DateFormatter'); +var TimeFormatter = require('./shortcuts/TimeFormatter'); var PhoneFormatter = require('./shortcuts/PhoneFormatter'); var CreditCardDetector = require('./shortcuts/CreditCardDetector'); var Util = require('./utils/Util'); @@ -75,7 +76,7 @@ var cleaveReactClass = CreateReactClass({ pps = owner.properties; // so no need for this lib at all - if (!pps.numeral && !pps.phone && !pps.creditCard && !pps.date && (pps.blocksLength === 0 && !pps.prefix)) { + if (!pps.numeral && !pps.phone && !pps.creditCard && !pps.time && !pps.date && (pps.blocksLength === 0 && !pps.prefix)) { owner.onInput(pps.initValue); owner.registeredEvents.onInit(owner); @@ -88,6 +89,7 @@ var cleaveReactClass = CreateReactClass({ owner.initPhoneFormatter(); owner.initDateFormatter(); + owner.initTimeFormatter(); owner.initNumeralFormatter(); // avoid touch input field if value is null @@ -118,6 +120,20 @@ var cleaveReactClass = CreateReactClass({ ); }, + initTimeFormatter: function () { + var owner = this, + pps = owner.properties; + + if (!pps.time) { + return; + } + + pps.timeFormatter = new TimeFormatter(pps.timePattern); + pps.blocks = pps.timeFormatter.getBlocks(); + pps.blocksLength = pps.blocks.length; + pps.maxLength = Util.getMaxLength(pps.blocks); + }, + initDateFormatter: function () { var owner = this, pps = owner.properties; @@ -291,6 +307,11 @@ var cleaveReactClass = CreateReactClass({ value = pps.dateFormatter.getValidatedDate(value); } + // time + if (pps.time) { + value = pps.timeFormatter.getValidatedTime(value); + } + // strip delimiters value = Util.stripDelimiters(value, pps.delimiter, pps.delimiters); diff --git a/src/common/DefaultProperties.js b/src/common/DefaultProperties.js index a10ad491..bd281bb8 100644 --- a/src/common/DefaultProperties.js +++ b/src/common/DefaultProperties.js @@ -59,9 +59,10 @@ var DefaultProperties = { target.delimiter = (opts.delimiter || opts.delimiter === '') ? opts.delimiter : (opts.date ? '/' : - (opts.numeral ? ',' : - (opts.phone ? ' ' : - ' '))); + (opts.time ? ':' : + (opts.numeral ? ',' : + (opts.phone ? ' ' : + ' ')))); target.delimiterLength = target.delimiter.length; target.delimiterLazyShow = !!opts.delimiterLazyShow; target.delimiters = opts.delimiters || []; diff --git a/src/shortcuts/TimeFormatter.js b/src/shortcuts/TimeFormatter.js index eef2f07b..935016bf 100644 --- a/src/shortcuts/TimeFormatter.js +++ b/src/shortcuts/TimeFormatter.js @@ -12,7 +12,7 @@ var TimeFormatter = function (timePattern) { TimeFormatter.prototype = { initBlocks: function () { var owner = this; - owner.timePattern.forEach(function (value) { + owner.timePattern.forEach(function () { owner.blocks.push(2); }); }, @@ -154,4 +154,3 @@ TimeFormatter.prototype = { }; module.exports = TimeFormatter; - diff --git a/test/browser/runner.html b/test/browser/runner.html index 78686b5a..0d934315 100644 --- a/test/browser/runner.html +++ b/test/browser/runner.html @@ -21,6 +21,7 @@ + @@ -28,6 +29,7 @@ + diff --git a/test/browser/time.js b/test/browser/time.js new file mode 100644 index 00000000..1ae45317 --- /dev/null +++ b/test/browser/time.js @@ -0,0 +1,62 @@ +describe('Time input field', function () { + var field = document.querySelector('.input-time'); + var cleave = new Cleave(field, { + time: true + }); + + it('should format fully matched input value', function () { + cleave.setRawValue('231515'); + assert.equal(field.value, '23:15:15'); + }); + + it('should format partially matched input value', function () { + cleave.setRawValue('2315'); + assert.equal(field.value, '23:15:'); + }); + + it('should correct large time hour to 23', function () { + cleave.setRawValue('25'); + assert.equal(field.value, '23:'); + }); + + it('should correct large time hour to add leading 0', function () { + cleave.setRawValue('4'); + assert.equal(field.value, '02:'); + }); + + it('should correct large min to add leading 0', function () { + cleave.setRawValue('147'); + assert.equal(field.value, '14:07:'); + }); + + it('should correct large sec to add leading 0', function () { + cleave.setRawValue('14147'); + assert.equal(field.value, '14:14:07'); + }); +}); + +describe('Date input field with pattern', function () { + var field = document.querySelector('.input-time'); + var cleave = new Cleave(field, { + time: true, + timePattern: ['m': 's'] + }); + + it('should format fully matched input value', function () { + cleave.setRawValue('5555'); + assert.equal(field.value, '55:55'); + }); +}); + +describe('ISO time', function () { + var field = document.querySelector('.input-time'); + var cleave = new Cleave(field, { + time: true, + timePattern: ['h', 'm', 's'] + }); + + it('should get correct ISO time', function () { + cleave.setRawValue('808080'); + assert.equal(cleave.getISOFormatTime(), '08:08:08'); + }); +}); diff --git a/test/fixtures/time.json b/test/fixtures/time.json new file mode 100644 index 00000000..b6495621 --- /dev/null +++ b/test/fixtures/time.json @@ -0,0 +1,104 @@ +[ + { + "timePattern": [ + "h", + "m", + "s" + ], + "time": [ + [ + "232323", + "232323" + ], + [ + "236161", + "230606" + ], + [ + "250101", + "230101" + ], + [ + "232323000", + "232323" + ], + [ + "3", + "03" + ], + [ + "037", + "0307" + ], + [ + "03077", + "030707" + ] + ] + }, + { + "timePattern": [ + "s", + "m", + "h" + ], + "time": [ + [ + "232323", + "232323" + ], + [ + "616123", + "060623" + ], + [ + "010125", + "010123" + ], + [ + "232323000", + "232323" + ], + [ + "7", + "07" + ], + [ + "077", + "0707" + ], + [ + "07073", + "070703" + ] + ] + }, + { + "timePattern": [ + "h", + "m" + ], + "time": [ + [ + "0718", + "0718" + ], + [ + "2518", + "2318" + ], + [ + "071899", + "0718" + ], + [ + "0008", + "0008" + ], + [ + "9018", + "0918" + ] + ] + } +] diff --git a/test/unit/TimeFormatter_spec.js b/test/unit/TimeFormatter_spec.js new file mode 100644 index 00000000..43125886 --- /dev/null +++ b/test/unit/TimeFormatter_spec.js @@ -0,0 +1,17 @@ +var _ = require('underscore'); +var TimeFormatter = require('../../src/shortcuts/TimeFormatter'); +var timeGroups = require('../fixtures/time.json'); + +describe('TimeFormatter', function () { + _.each(timeGroups, function (timeGroup) { + describe('pattern: ' + timeGroup.timePattern.join(', '), function () { + var timeFormatter = new TimeFormatter(timeGroup.timePattern); + + _.each(timeGroup.time, function (time) { + it('should convert time ' + time[0] + ' to ' + time[1], function () { + timeFormatter.getValidatedTime(time[0]).should.eql(time[1]); + }); + }); + }); + }); +});