extra-utilities
Advanced tools
Comparing version 1.2.16 to 1.2.17
@@ -257,11 +257,3 @@ (function(global, factory) { | ||
utilities.isEmptyArray = function(value) { | ||
if(utilities.isInvalid(value)) { | ||
return true; | ||
} | ||
if(!Array.isArray(value)) { | ||
return false; | ||
} | ||
return value.length === 0; | ||
return Array.isArray(value) ? value.length === 0 : true; | ||
}; | ||
@@ -268,0 +260,0 @@ |
@@ -1,1 +0,1 @@ | ||
!function(global,factory){"object"==typeof exports&&"undefined"!=typeof module?module.exports=factory():"function"==typeof define&&define.amd?define(factory):global.utilities=factory()}(this,function(){"use strict";"undefined"!=typeof require&&("undefined"==typeof validator&&(global.validator=require("validator")),"undefined"==typeof changeCase&&(global.changeCase=require("change-case-bundled")));var utilities={},postalCodeValidators={UK:/^([A-Z]){1}([0-9][0-9]|[0-9]|[A-Z][0-9][A-Z]|[A-Z][0-9][0-9]|[A-Z][0-9]|[0-9][A-Z]){1}([ ])?([0-9][A-z][A-z]){1}$/i,JE:/^JE\d[\dA-Z]?[ ]?\d[ABD-HJLN-UW-Z]{2}$/,GG:/^GY\d[\dA-Z]?[ ]?\d[ABD-HJLN-UW-Z]{2}$/,IM:/^IM\d[\dA-Z]?[ ]?\d[ABD-HJLN-UW-Z]{2}$/,US:/^([0-9]{5})(?:[-\s]*([0-9]{4}))?$/,CA:/^([A-Z][0-9][A-Z])\s*([0-9][A-Z][0-9])$/i,DE:/^\d{5}$/,JP:/^\d{3}-\d{4}$/,FR:/^\d{2}[ ]?\d{3}$/,AU:/^\d{4}$/,IT:/^\d{5}$/,CH:/^\d{4}$/,AT:/^\d{4}$/,ES:/^\d{5}$/,NL:/^\d{4}[ ]?[A-Z]{2}$/,BE:/^\d{4}$/,DK:/^\d{4}$/,SE:/^\d{3}[ ]?\d{2}$/,NO:/^\d{4}$/,BR:/^\d{5}[\-]?\d{3}$/,PT:/^\d{4}([\-]\d{3})?$/,FI:/^\d{5}$/,AX:/^22\d{3}$/,KR:/^\d{3}[\-]\d{3}$/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) 1ZZ$/,SJ:/^\d{4}$/,SO:/^\d{5}$/,SZ:/^[HLMS]\d{3}$/,TC:/^TKCA 1ZZ$/,WF:/^986\d{2}$/,XK:/^\d{5}$/,YT:/^976\d{2}$/,INTL:/^(?:[A-Z0-9]+([- ]?[A-Z0-9]+)*)?$/i},regExpFlags={global:"g",multiline:"m",ignoreCase:"i",sticky:"y",unicode:"u"},formatTypes=["boolean","bool","integer","int","float","number","string","object","array","date","regex","regexp","regularexpression","function","func"],stringCaseFunctions={camel:changeCase.camelCase,constant:changeCase.constantCase,dot:changeCase.dotCase,header:changeCase.headerCase,lower:changeCase.lowerCase,lowerFirst:changeCase.lowerCaseFirst,no:changeCase.noCase,param:changeCase.paramCase,pascal:changeCase.pascalCase,path:changeCase.pathCase,sentence:changeCase.sentenceCase,snake:changeCase.snakeCase,swap:changeCase.swapCase,title:changeCase.titleCase,upper:changeCase.upperCase,upperFirst:changeCase.upperCaseFirst};return utilities.isValid=function(value){return null!=value},utilities.isInvalid=function(value){return null==value},utilities.isBoolean=function(value,allowObjects){return!0===value||!1===value||!!allowObjects&&value instanceof Boolean},utilities.isValidNumber=function(value){return"number"==typeof value&&!isNaN(value)&&value!==-1/0&&value!==1/0},utilities.isInvalidNumber=function(value){return"number"!=typeof value||isNaN(value)||value===-1/0||value===1/0},utilities.isEmptyString=function(value,trim){return"string"!=typeof value||(utilities.parseBoolean(trim,!0)?0===value.trim().length:0===value.length)},utilities.isNonEmptyString=function(value,trim){return"string"==typeof value&&(utilities.parseBoolean(trim,!0)?0!==value.trim().length:0!==value.length)},utilities.isObject=function(value,strict){return void 0!==value&&(strict?null!==value&&value.constructor===Object:value instanceof Object&&!(value instanceof Function))},utilities.isObjectStrict=function(value){return null!=value&&value.constructor===Object},utilities.isEmptyObject=function(value){return null!=value&&value.constructor===Object&&0===Object.keys(value).length},utilities.isNonEmptyObject=function(value){return null!=value&&value.constructor===Object&&0!==Object.keys(value).length},utilities.isEmptyArray=function(value){return!!utilities.isInvalid(value)||!!Array.isArray(value)&&0===value.length},utilities.isNonEmptyArray=function(value){return Array.isArray(value)&&0!==value.length},utilities.isDate=function(value){return value instanceof Date},utilities.isError=function(value){return value instanceof Error},utilities.isRegularExpression=function(value){return value instanceof RegExp},utilities.isFunction=function(value){return value instanceof Function},utilities.isComment=function(value,comment){if(utilities.isEmptyString(value))return!1;utilities.isEmptyString(comment)&&(comment="//");for(var commentStartIndex=-1,i=0;i<value.length;i++)if(" "!==value[i]&&"\t"!=value[i]){if(value[i]==comment[0]){commentStartIndex=i;break}return!1}if(commentStartIndex<0||value.length-commentStartIndex<comment.length)return!1;for(i=commentStartIndex;i<value.length&&!(i-commentStartIndex>=comment.length);i++)if(value[i]!=comment[i-commentStartIndex])return!1;return!0},utilities.isVisible=function(element){return!!utilities.isObject(element)&&(utilities.isBoolean(element.visible)?element.visible:utilities.isFunction(element.visible)?element.visible():utilities.isBoolean(element.hidden)?!element.hidden:!utilities.isFunction(element.hidden)||!element.hidden())},utilities.isHidden=function(element){return!utilities.isObject(element)||(utilities.isBoolean(element.visible)?!element.visible:utilities.isFunction(element.visible)?!element.visible():utilities.isBoolean(element.hidden)?element.hidden:!!utilities.isFunction(element.hidden)&&element.hidden())},utilities.isEnabled=function(element){return!!utilities.isObject(element)&&(utilities.isBoolean(element.enabled)?element.enabled:utilities.isFunction(element.enabled)?element.enabled():utilities.isBoolean(element.disabled)?!element.disabled:!utilities.isFunction(element.disabled)||!element.disabled())},utilities.isDisabled=function(element){return!utilities.isObject(element)||(utilities.isBoolean(element.enabled)?!element.enabled:utilities.isFunction(element.enabled)?!element.enabled():utilities.isBoolean(element.disabled)?element.disabled:!!utilities.isFunction(element.disabled)&&element.disabled())},utilities.parseBoolean=function(value,defaultValue){if(utilities.isBoolean(value))return value;if(utilities.isBoolean(value,!0))return value.valueOf();if(utilities.isBoolean(defaultValue)||(defaultValue=null),utilities.isInvalid(value))return defaultValue;if(0===value)return!1;if(1===value)return!0;if("string"!=typeof value)return defaultValue;var formattedValue=value.trim().toLowerCase();if(0===formattedValue.length)return defaultValue;if(1===formattedValue.length){var character=formattedValue.charAt(0);return"t"===character||"y"===character||"f"!==character&&"n"!==character&&("0"!==character&&("1"===character||defaultValue))}return"true"===formattedValue||"yes"===formattedValue||"on"===formattedValue||"false"!==formattedValue&&"no"!==formattedValue&&"off"!==formattedValue&&defaultValue},utilities.parseInteger=function(value,defaultValue){var newValue=NaN;return"number"==typeof value?newValue=parseInt(value):"string"==typeof value&&validator.isFloat(value)&&(newValue=parseInt(value)),utilities.isInvalidNumber(newValue)?(defaultValue=parseInt(defaultValue),utilities.isValidNumber(defaultValue)?defaultValue:NaN):newValue},utilities.parseFloatingPointNumber=function(value,defaultValue){var newValue=NaN;return"number"==typeof value?newValue=value:"string"==typeof value&&validator.isFloat(value)&&(newValue=parseFloat(value)),utilities.isInvalidNumber(newValue)?utilities.isValidNumber(defaultValue)?defaultValue:NaN:newValue},utilities.parseDate=function(value,defaultValue){if(utilities.isDate(defaultValue)||(defaultValue=null),"number"==typeof value)return utilities.isInvalidNumber(value)||!Number.isInteger(value)?defaultValue:new Date(parseInt(value));if("string"==typeof value){var formattedValue=value.trim();if(0===formattedValue.length)return defaultValue;var timestamp=null;return timestamp=validator.isInt(formattedValue)?parseInt(formattedValue):Date.parse(formattedValue),utilities.isInvalidNumber(timestamp)?defaultValue:new Date(timestamp)}return value instanceof Date?value:defaultValue},utilities.parseTime=function(value,throwErrors){if(throwErrors=utilities.parseBoolean(throwErrors),utilities.isEmptyString(value)){if(throwErrors)throw new Error("Invalid or empty time value.");return null}var formattedValue=value.trim(),hour=null,minutes=null,regularTime=formattedValue.match(/^[ \t]*(([2-9]|[1][0-2]?)(:([0-5][0-9]))?[ \t]*([ap]m))[ \t]*$/i);if(regularTime){var regularHour=utilities.parseInteger(utilities.trimLeadingZeroes(regularTime[2]));if(utilities.isInvalidNumber(regularHour)){if(throwErrors)throw new Error('Invalid regular hour: "'+regularTime[2]+'".');return null}minutes=utilities.parseInteger(utilities.trimLeadingZeroes(regularTime[4])),utilities.isInvalidNumber(minutes)&&(minutes=0);var morning="AM"===(period=regularTime[5].toUpperCase())||"PM"!==period&&null;if(null===morning){if(throwErrors)throw new Error('Invalid period: "'+regularTime[5]+'".');return null}24===(hour=morning?regularHour+(12===regularHour?12:0):regularHour+(12===regularHour?0:12))&&(hour=0)}else{var militaryTime=formattedValue.match(/^[ \t]*(((([0-1][0-9])|(2[0-3])):?([0-5][0-9]))|((24):?(00)))[ \t]*$/i);if(!militaryTime){if(throwErrors)throw new Error('Invalid time: "'+formattedValue+'".');return null}var militaryHour=militaryTime[3],militaryMinutes=militaryTime[6];if((utilities.isInvalid(militaryHour)||utilities.isInvalid(militaryMinutes))&&(militaryHour=militaryTime[8],utilities.isNonEmptyString(militaryTime[9])&&(militaryMinutes=militaryTime[9])),utilities.isInvalid(militaryHour)||utilities.isInvalid(militaryMinutes)){if(throwErrors)throw new Error('Invalid military time: "'+formattedValue+'".');return null}if(hour=utilities.parseInteger(utilities.trimLeadingZeroes(militaryHour)),utilities.isInvalidNumber(hour)){if(throwErrors)throw new Error('Invalid military time hour: "'+militaryHour+'".');return null}if(minutes=utilities.parseInteger(utilities.trimLeadingZeroes(militaryMinutes)),utilities.isInvalidNumber(minutes)){if(throwErrors)throw new Error('Invalid military time minutes: "'+militaryMinutes+'".');return null}24===hour&&0===minutes&&(hour=0)}if(hour<0||23<hour){if(throwErrors)throw new Error('Time hour is out of range (0 - 23): "'+hour+'".');return null}if(minutes<0||59<minutes){if(throwErrors)throw new Error('Time minutes is out of range (0 - 59): "'+minutes+'".');return null}var period=hour<12?"AM":"PM";return{regular:{raw:(regularHour=0===hour?12:hour<=12?hour:hour-12)+":"+utilities.addLeadingZeroes(minutes,2)+" "+period,hour:regularHour,minutes:minutes,period:period,morning:hour<12},military:{raw:utilities.addLeadingZeroes(hour,2)+utilities.addLeadingZeroes(minutes,2),hour:hour,minutes:minutes}}},utilities.parsePostalCode=function(value){if(utilities.isEmptyString(value))return null;var postalCodeData=value.match(/[ \t]*([A-Z][0-9][A-Z])[_\- \t]?([0-9][A-Z][0-9])[ \t]*/i);return postalCodeData?(postalCodeData[1]+postalCodeData[2]).toUpperCase():null},utilities.parseEmail=function(value){if(utilities.isEmptyString(value))return null;var emailData=value.trim().toLowerCase().match(/([^+@]+)(\+.*)?(@.+\..+)/);return utilities.isInvalid(emailData)||emailData.length<4?null:emailData[1]+emailData[3]},utilities.parseEmailDomain=function(value){if(utilities.isEmptyString(value))return null;var emailDomainData=value.trim().toLowerCase().match(/([^+@]+)(\+.*)?@(.+\..+)/);return utilities.isInvalid(emailDomainData)||emailDomainData.length<4?null:emailDomainData[3]},utilities.parseStringList=function(value){if("string"!=typeof value)return null;if(0===value.length)return[];for(var data=value.split(/[;,]+/),formattedList=[],formattedValue=null,i=0;i<data.length;i++)0!==(formattedValue=data[i].trim()).length&&formattedList.push(formattedValue);return formattedList},utilities.parseRegularExpression=function(value,throwErrors){if(throwErrors=utilities.parseBoolean(throwErrors,!1),utilities.isRegularExpression(value))return value;if(utilities.isEmptyString(value)){if(throwErrors)throw new Error("Regular expression cannot be empty.");return null}var regExpData=value.match(/\s*\/(.*)\/(.*)\s*/);if(!regExpData){if(throwErrors)throw new Error("Invalid regular expression value.");return null}if(throwErrors)return new RegExp(regExpData[1],regExpData[2]);try{return new RegExp(regExpData[1],regExpData[2])}catch(error){return null}},utilities.parseYouTubeLink=function(value){if(utilities.isEmptyString(value))return null;var formattedValue=value.trim(),linkData=formattedValue.match(/.*(?:youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=)([^#\&\?]*).*/i);return linkData&&11<=linkData[1].length?linkData[1]:formattedValue.match(/[A-Z0-9_-]{11,}/i)?formattedValue:null},utilities.formatValue=function(value,format,options){if(utilities.isObjectStrict(options)?(options={throwErrors:utilities.parseBoolean(options.throwErrors,!1)}).verbose=utilities.parseBoolean(options.verbose,!options.throwErrors):options={throwErrors:!1,verbose:!0},!utilities.isObjectStrict(format))return utilities.clone(value);format=utilities.clone(format);var errorMessage=null;if(utilities.isEmptyString(format.type))errorMessage="Invalid required type format value - expected non-empty string.";else{var originalType=format.type;format.type=format.type.trim().toLowerCase();for(var validFormatType=!1,i=0;i<formatTypes.length;i++)if(format.type===formatTypes[i]){validFormatType=!0;break}if(validFormatType){if("bool"===format.type)format.type="boolean";else if("int"===format.type)format.type="integer";else if("number"===format.type)format.type="float";else if("string"===format.type){if(void 0!==format.trim){var originalTrim=format.trim;format.trim=utilities.parseBoolean(format.trim),null===format.trim&&(errorMessage='Invalid optional trim string format value - expected boolean, received "'+utilities.toString(originalTrim)+'".')}if(utilities.isInvalid(errorMessage)&&void 0!==format.case)if(utilities.isEmptyString(format.case))errorMessage="Invalid optional string case format value - expected non-empty string.";else{var originalCase=format.case;format.case=changeCase.camel(format.case.trim()),utilities.isFunction(stringCaseFunctions[format.case])||(errorMessage="Invalid optional case format value - expected one of "+Object.keys(stringCaseFunctions).join(", ")+', received "'+utilities.toString(originalCase)+'".')}}else if("object"===format.type){if(void 0!==format.strict){var originalStrict=format.strict;format.strict=utilities.parseBoolean(format.strict),null===format.strict&&(errorMessage='Invalid optional strict object format value - expected boolean, received "'+utilities.toString(originalStrict)+'".')}if(utilities.isInvalid(errorMessage)&&void 0!==format.autopopulate){var originalAutopopulate=format.autopopulate;format.autopopulate=utilities.parseBoolean(format.autopopulate),null===format.autopopulate&&(errorMessage='Invalid optional autopopulate object format value - expected boolean, received "'+utilities.toString(originalAutopopulate)+'".')}if(utilities.isInvalid(errorMessage)&&void 0!==format.order){var originalOrder=format.order;format.order=utilities.parseBoolean(format.order),null===format.order&&(errorMessage='Invalid optional order object format value - expected boolean, received "'+utilities.toString(originalOrder)+'".')}if(utilities.isInvalid(errorMessage)&&void 0!==format.removeExtra){var originalRemoveExtra=format.removeExtra;format.removeExtra=utilities.parseBoolean(format.removeExtra),null===format.removeExtra&&(errorMessage='Invalid optional removeExtra object format value - expected boolean, received "'+utilities.toString(originalRemoveExtra)+'".')}}else"regexp"===format.type||"regularexpression"===format.type?format.type="regex":"func"===format.type&&(format.type="function");if(utilities.isInvalid(errorMessage)&&("object"!==format.type&&"array"!==format.type||void 0===format.format||utilities.isObjectStrict(format.format)||(errorMessage='Invalid optional format format value - expected object, received "'+utilities.toString(format.format)+'".')),utilities.isInvalid(errorMessage)&&("string"===format.type||"object"===format.type||"array"===format.type)&&void 0!==format.nonEmpty){var originalNonEmpty=format.nonEmpty;format.nonEmpty=utilities.parseBoolean(format.nonEmpty),null===format.nonEmpty&&(errorMessage='Invalid optional nonEmpty format value - expected boolean, received "'+utilities.toString(originalNonEmpty)+'".')}}else errorMessage="Invalid required type format value - expected one of "+formatTypes.join(", ")+', received "'+utilities.toString(originalType)+'".'}if(utilities.isInvalid(errorMessage)&&void 0!==format.nullable){var originalNullable=format.nullable;format.nullable=utilities.parseBoolean(format.nullable),null===format.nullable&&(errorMessage='Invalid optional nullable format value - expected boolean, received "'+utilities.toString(originalNullable)+'".')}if(utilities.isInvalid(errorMessage)&&void 0!==format.required){var originalRequired=format.required;format.required=utilities.parseBoolean(format.required),null===format.required&&(errorMessage='Invalid optional required format value - expected boolean, received "'+utilities.toString(originalRequired)+'".')}if(utilities.isInvalid(errorMessage)&&void 0!==format.parser&&(utilities.isFunction(format.parser)||(errorMessage='Invalid optional parser format value - expected function, received "'+utilities.toString(format.parser)+'".')),utilities.isInvalid(errorMessage)&&void 0!==format.validator&&(utilities.isFunction(format.validator)||(errorMessage='Invalid optional validator format value - expected function, received "'+utilities.toString(format.validator)+'".')),utilities.isInvalid(errorMessage)&&void 0!==format.formatter&&(utilities.isFunction(format.formatter)||(errorMessage='Invalid optional formatter format value - expected function, received "'+utilities.toString(format.formatter)+'".')),utilities.isValid(errorMessage)){if(options.throwErrors)throw new Error(errorMessage);return options.verbose&&console.error(errorMessage),null}if(utilities.isFunction(format.parser))if(options.throwErrors)value=format.parser(value,format,options);else try{value=format.parser(value,format,options)}catch(error){return options.verbose&&console.error(error.message),null}var formattedValue=void 0;if(void 0===value)formattedValue=utilities.clone(format.default);else if(null===value)format.nullable?formattedValue=null:void 0!==format.default?formattedValue=utilities.clone(format.default):errorMessage="Value cannot be null!";else if("boolean"===format.type)null===(formattedValue=utilities.parseBoolean(value))&&(errorMessage='Invalid boolean value: "'+utilities.toString(value)+'".');else if("integer"===format.type)formattedValue=utilities.parseInteger(value),utilities.isInvalidNumber(formattedValue)&&(errorMessage='Invalid integer value: "'+utilities.toString(value)+'".');else if("float"===format.type)formattedValue=utilities.parseFloatingPointNumber(value),utilities.isInvalidNumber(formattedValue)&&(errorMessage='Invalid float value: "'+utilities.toString(value)+'".');else if("string"===format.type){if(formattedValue="string"==typeof value?value:utilities.toString(value),format.trim&&(formattedValue=formattedValue.trim()),void 0!==format.case&&(formattedValue=stringCaseFunctions[format.case](formattedValue)),format.nonEmpty&&0===formattedValue.length){var message="String value cannot be empty.";if(options.throwErrors)throw new Error(message);return options.verbose&&console.error(message),null}}else if("array"===format.type){if("string"==typeof value)try{formattedValue=JSON.parse(value),Array.isArray(formattedValue)||(errorMessage='Invalid stringified array value type - expected array: "'+utilities.toString(value)+'".')}catch(error){errorMessage='Invalid stringified array value: "'+utilities.toString(value)+'".'}else Array.isArray(value)?formattedValue=utilities.clone(value):errorMessage='Invalid value type - expected array or stringified array: "'+utilities.toString(value)+'".';if(utilities.isInvalid(errorMessage)&&Array.isArray(formattedValue)){var formattedArray=[];if(utilities.isObjectStrict(format.format)){var formattedElement=null;for(i=0;i<formattedValue.length;i++)if(void 0!==formattedValue[i]){if(options.throwErrors)formattedElement=utilities.formatValue(formattedValue[i],format.format,options);else{(subOptions=utilities.clone(options)).throwErrors=!0;try{formattedElement=utilities.formatValue(formattedValue[i],format.format,subOptions)}catch(error){return options.verbose&&console.error(error.message),null}}formattedArray.push(formattedElement)}}else for(i=0;i<formattedValue.length;i++)void 0!==formattedValue[i]&&formattedArray.push(utilities.clone(formattedValue[i]));if(formattedValue=formattedArray,format.nonEmpty&&0===formattedValue.length){message="Array value cannot be empty.";if(options.throwErrors)throw new Error(message);return options.verbose&&console.error(message),null}}}else if("date"===format.type)formattedValue=utilities.parseDate(value),utilities.isDate(formattedValue)||(errorMessage='Invalid date value: "'+utilities.toString(value)+'".');else if("regex"===format.type){if(utilities.isRegularExpression(value))formattedValue=utilities.clone(value);else if(options.throwErrors)formattedValue=utilities.parseRegularExpression(value,!0);else try{formattedValue=utilities.parseRegularExpression(value,!0)}catch(error){errorMessage=error.message}utilities.isInvalid(formattedValue)&&(errorMessage='Invalid regular expression value: "'+utilities.toString(value)+'".')}else"function"===format.type&&(formattedValue=value,utilities.isFunction(formattedValue)||(errorMessage='Invalid function value: "'+utilities.toString(value)+'".'));if(utilities.isInvalid(errorMessage)&&"object"===format.type){if(utilities.isValid(value))if("string"==typeof value)try{formattedValue=JSON.parse(value),utilities.isObject(formattedValue,format.strict)||(errorMessage="Invalid stringified object value type - expected "+(format.strict?"strict ":"")+'object: "'+utilities.toString(value)+'".')}catch(error){errorMessage='Invalid stringified object value: "'+utilities.toString(value)+'".'}else utilities.isObject(value,format.strict)?formattedValue=utilities.clone(value):errorMessage="Invalid value type - expected "+(format.strict?"strict ":"")+'object or stringified object: "'+utilities.toString(value)+'".';if(utilities.isInvalid(errorMessage)){if(utilities.isInvalid(formattedValue)&&format.autopopulate&&!utilities.isObject(formattedValue,format.strict)&&(formattedValue={}),utilities.isObjectStrict(format.format)&&utilities.isObject(formattedValue)){var attribute=null,attributes=Object.keys(format.format);for(i=0;i<attributes.length;i++)if(attribute=attributes[i],options.throwErrors){void 0!==(formattedAttribute=utilities.formatValue(formattedValue[attribute],format.format[attribute],options))&&(formattedValue[attribute]=formattedAttribute)}else{var subOptions;(subOptions=utilities.clone(options)).throwErrors=!0;try{var formattedAttribute;void 0!==(formattedAttribute=utilities.formatValue(formattedValue[attribute],format.format[attribute],subOptions))&&(formattedValue[attribute]=formattedAttribute)}catch(error){return options.verbose&&console.error(error.message),null}}if(format.removeExtra){var newValue={};attributes=Object.keys(formattedValue);for(i=0;i<attributes.length;i++)attribute=attributes[i],utilities.isValid(format.format[attribute])&&(newValue[attribute]=formattedValue[attribute]);formattedValue=newValue}if(format.order){var orderedObject={};attributes=Object.keys(format.format);for(i=0;i<attributes.length;i++)void 0!==formattedValue[attribute=attributes[i]]&&(orderedObject[attribute]=formattedValue[attribute],delete formattedValue[attribute]);var extraAttributes=Object.keys(formattedValue);for(i=0;i<extraAttributes.length;i++)orderedObject[attribute=extraAttributes[i]]=formattedValue[attribute];formattedValue=orderedObject}}if(format.nonEmpty&&utilities.isEmptyObject(formattedValue)){message="Object value must contain at least one attribute.";if(options.throwErrors)throw new Error(message);return options.verbose&&console.error(message),null}}}if(utilities.isValid(errorMessage)){if(options.throwErrors)throw new Error(errorMessage);return options.verbose&&console.error(errorMessage),null}if(void 0===formattedValue){if(format.required){message="Missing required value!";if(options.throwErrors)throw new Error(message);return options.verbose&&console.error(message),null}void 0!==format.default&&(formattedValue=utilities.clone(format.default))}if(utilities.isFunction(format.validator))if(options.throwErrors){if(!format.validator(formattedValue,format,options))throw new Error("Validation check failed!")}else try{if(!format.validator(formattedValue,format,options)){message="Validation check failed!";return options.verbose&&console.error(message),null}}catch(error){return options.verbose&&console.error(error.message),null}if(utilities.isFunction(format.formatter))if(options.throwErrors)formattedValue=format.formatter(formattedValue,format,options);else try{formattedValue=format.formatter(formattedValue,format,options)}catch(error){return options.verbose&&console.error(error.message),null}return formattedValue},utilities.formatObject=function(object,format,removeExtra,throwErrors){if(!(utilities.isObjectStrict(object)||utilities.isObjectStrict(removeExtra)&&utilities.isFunction(removeExtra.parser)))return{};var options=null;options=utilities.isObjectStrict(removeExtra)?{throwErrors:utilities.parseBoolean(removeExtra.throwErrors,utilities.parseBoolean(throwErrors,!1)),verbose:utilities.parseBoolean(removeExtra.verbose,!1)}:{throwErrors:utilities.parseBoolean(throwErrors,!1),verbose:!1};var subFormat={type:"object",strict:!1,autopopulate:!!utilities.isObjectStrict(removeExtra)&&utilities.parseBoolean(removeExtra.autopopulate,!1),order:!!utilities.isObjectStrict(removeExtra)&&utilities.parseBoolean(removeExtra.order,!1),removeExtra:utilities.isObjectStrict(removeExtra)?utilities.parseBoolean(removeExtra.removeExtra,utilities.parseBoolean(removeExtra,!1)):utilities.parseBoolean(removeExtra,!1),nullable:!0,required:!1,format:format};utilities.isObjectStrict(removeExtra)&&(utilities.isFunction(removeExtra.parser)&&(subFormat.parser=removeExtra.parser),utilities.isFunction(removeExtra.validator)&&(subFormat.validator=removeExtra.validator),utilities.isFunction(removeExtra.formatter)&&(subFormat.formatter=removeExtra.formatter));var formattedObject=null;if(options.throwErrors)formattedObject=utilities.formatValue(object,subFormat,options);else{var subOptions=utilities.clone(options);subOptions.throwErrors=!0;try{formattedObject=utilities.formatValue(object,subFormat,subOptions)}catch(error){return options.verbose&&console.error(error.message),null}}return utilities.isObjectStrict(formattedObject)||utilities.isObjectStrict(removeExtra)&&utilities.isFunction(removeExtra.parser)?formattedObject:{}},utilities.formatStringList=function(value,stringify){var data=null;if(utilities.isNonEmptyString(value))data=utilities.parseStringList(value);else{if(!utilities.isNonEmptyArray(value))return"string"==typeof value||Array.isArray(value)?"":null;data=value}var formattedList="",formattedValue=null;stringify=utilities.parseBoolean(stringify);for(var i=0;i<data.length;i++){if("string"!=typeof data[i]){if(null===stringify)continue;formattedValue=stringify?utilities.toString(data[i]):data[i]}else formattedValue=data[i].trim();0!==formattedValue.length&&(0<formattedList.length&&(formattedList+=", "),formattedList+=formattedValue)}return formattedList},utilities.trimString=function(value,defaultValue){return"string"==typeof value?value.trim():void 0===defaultValue?null:defaultValue},utilities.trimNullTerminatedString=function(value,defaultValue){if("string"!=typeof value)return void 0===defaultValue?null:defaultValue;var nullTerminatorIndex=value.indexOf("\0");return 0<=nullTerminatorIndex?value.substr(0,nullTerminatorIndex):value},utilities.trimWhitespace=function(value,trimNewlines){if("string"!=typeof value)return null;var trimmedString=value.replace(/^[ \t]+|[ \t]+$/gm,"");return utilities.parseBoolean(trimNewlines,!1)&&(trimmedString=trimmedString.replace(/\r\n?|\n/g,"")),trimmedString},utilities.trimTrailingNewlines=function(value){return"string"!=typeof value?null:utilities.isEmptyString(value)?value:value.replace(/[ \t\r\n]+$/,"")},utilities.replaceNonBreakingSpaces=function(value){return"string"==typeof value?value.replace(/ /gi," "):null},utilities.indentText=function(value,amount,indentation,clearEmptyLines){if("string"!=typeof value)return null;clearEmptyLines=utilities.parseBoolean(clearEmptyLines,!0),(amount=utilities.parseInteger(amount,1))<0&&(amount=0),indentation="string"==typeof indentation?indentation:"\t";for(var totalIndentation="",i=0;i<amount;i++)totalIndentation+=indentation;var line=null,lines=value.split(/\r\n?|\n/g),indentedParagraph="";for(i=0;i<lines.length;i++)line=lines[i],indentedParagraph+=(utilities.isEmptyString(line)&&clearEmptyLines?"":totalIndentation+line)+(i<lines.length-1?"\n":"");return indentedParagraph},utilities.trimLeadingZeroes=function(value){if("string"!=typeof value)return null;if(0===value.length)return value;var formattedValue=value.trim();return 0===formattedValue.length?formattedValue:formattedValue.match(/^[0]+$/)?"0":formattedValue.replace(/^0+/,"")},utilities.addLeadingZeroes=function(value,expectedLength){if(utilities.isInvalid(value))return null;if(value=value.toString(),expectedLength=utilities.parseInteger(expectedLength),utilities.isInvalidNumber(expectedLength)||expectedLength<0)return value;for(var numberOfZeroes=expectedLength-value.length,i=0;i<numberOfZeroes;i++)value="0"+value;return value},utilities.toString=function(value){if(void 0===value)return"undefined";if(null===value)return"null";if("string"==typeof value)return value;if(value===1/0)return"Infinity";if(value===-1/0)return"-Infinity";if("number"==typeof value&&isNaN(value))return"NaN";if(utilities.isDate(value))return value.toString();if(utilities.isRegularExpression(value)){var flags="";for(var flag in regExpFlags)value[flag]&&(flags+=regExpFlags[flag]);return"/"+value.source+"/"+flags}if(utilities.isFunction(value))return value.toString();if(utilities.isError(value)){var error={message:value.message};for(var attribute in value)error[attribute]=value[attribute];return JSON.stringify(error)}return JSON.stringify(value)},utilities.compareDates=function(a,b){return a=utilities.parseDate(a),b=utilities.parseDate(b),null===a&&null===b?0:null===a?-1:null===b?1:a.getTime()-b.getTime()},utilities.compareCasePercentage=function(value){if(utilities.isEmptyString(value))return 0;for(var c=null,upper=0,lower=0,lowerA="a".charCodeAt(),lowerZ="z".charCodeAt(),upperA="A".charCodeAt(),upperZ="Z".charCodeAt(),i=0;i<value.length;i++)lowerA<=(c=value.charCodeAt(i))&&c<=lowerZ?lower++:upperA<=c&&c<=upperZ&&upper++;return upper-lower},utilities.reverseString=function(value){if("string"!=typeof value)return null;for(var reverse="",i=0;i<value.length;i++)reverse+=value[value.length-i-1];return reverse},utilities.createError=function(message,status){var error=new Error(message);return error.status=utilities.parseInteger(status,500),error},utilities.clone=function(value){if(!utilities.isObject(value))return value;if(value instanceof Boolean)return new Boolean(value.valueOf());if(value instanceof Date)return(copy=new Date).setTime(value.getTime()),copy;if(value instanceof Array){for(var copy=[],i=0,length=value.length;i<length;i++)copy[i]=utilities.clone(value[i]);return copy}if(value instanceof Set)return new Set(value);if(value instanceof Map)return new Map(value);if(value instanceof RegExp){var flags="";for(var flag in regExpFlags)value[flag]&&(flags+=regExpFlags[flag]);return new RegExp(value.source,flags)}if("undefined"!=typeof Buffer&&value instanceof Buffer)return Buffer.from instanceof Function?Buffer.from(value):new Buffer(value);if(value instanceof Object){copy=null;if(value instanceof Error){(copy=new Error(value.message)).stack=utilities.clone(value.stack);var properties=Object.keys(value);for(i=0;i<properties.length;i++)copy[properties[i]]=utilities.clone(value[properties[i]])}else copy={};for(var attribute in value)Object.prototype.hasOwnProperty.call(value,attribute)&&(copy[attribute]=utilities.clone(value[attribute]));return copy}return value},utilities.merge=function(a,b,copy,deepMerge){if(!utilities.isObject(a)||Array.isArray(a))return null;var newObject=null;if(newObject=(copy=utilities.parseBoolean(copy,!0))?utilities.clone(a):a,!utilities.isObject(a)||Array.isArray(a)||!utilities.isObject(b)||Array.isArray(b))return newObject;var attribute=null,value=null,newValue=null,attributes=Object.keys(b);deepMerge=utilities.parseBoolean(deepMerge,!0);for(var i=0;i<attributes.length;i++)value=newObject[attribute=attributes[i]],newValue=copy?utilities.clone(b[attribute]):b[attribute],deepMerge&&utilities.isObject(value)&&!Array.isArray(value)&&utilities.isObject(newValue)&&!Array.isArray(newValue)?newObject[attribute]=utilities.merge(value,newValue):newObject[attribute]=newValue;return newObject},utilities.calculateAge=function(value){var currentDate=new Date,formattedDate=utilities.parseDate(value);return null===formattedDate||currentDate<formattedDate?-1:Math.floor((currentDate-formattedDate)/1e3/86400/365.25)},utilities.prependSlash=function(value){if("string"!=typeof value)return null;var formattedValue=value.trim();return 0===formattedValue.length||"/"!==formattedValue[0]&&"\\"!==formattedValue[0]&&(formattedValue="/"+formattedValue),formattedValue},utilities.appendSlash=function(value){if("string"!=typeof value)return null;var formattedValue=value.trim();return 0===formattedValue.length||"/"!==formattedValue[formattedValue.length-1]&&"\\"!==formattedValue[formattedValue.length-1]&&(formattedValue+="/"),formattedValue},utilities.joinPaths=function(base,path){var formattedBase="string"==typeof base?base.trim().replace(/[\/\\]+$/,""):null,formattedPath="string"==typeof path?path.trim().replace(/^[\/\\]+/,""):null,newPath="";return utilities.isNonEmptyString(formattedBase)&&(newPath+=formattedBase,utilities.isNonEmptyString(formattedPath)&&(newPath+="/")),utilities.isNonEmptyString(formattedPath)&&(newPath+=formattedPath),newPath},utilities.createQueryString=function(value,includeQuestionMark){if(!utilities.isObjectStrict(value))return"";var parameters=Object.keys(value).map(function(key){return encodeURIComponent(key)+"="+encodeURIComponent(utilities.toString(value[key]))}).join("&");return 0===parameters.length?"":(utilities.parseBoolean(includeQuestionMark,!1)?"?":"")+parameters},utilities.createRange=function(start,end){var formattedStart=utilities.parseInteger(start),formattedEnd=utilities.parseInteger(end);if(1===arguments.length&&(formattedEnd=formattedStart,formattedStart=0),utilities.isInvalidNumber(formattedStart)||utilities.isInvalidNumber(formattedEnd)||formattedEnd<formattedStart)return[];for(var range=[],i=formattedStart;i<=formattedEnd;i++)range.push(i);return range},utilities.futureMonths=function(date,prependZero){if(null===(date=utilities.parseDate(date)))return null;var currentDate=new Date,month=0;date.getFullYear()===currentDate.getFullYear()&&(month=currentDate.getMonth());var months=[];prependZero=utilities.parseBoolean(prependZero,!1);for(var i=0;i<12;i++)month<=i&&(prependZero?months.push((i<=8?"0":"")+(i+1)):months.push(i+1));return months},utilities.visibleElements=function(elements){if(!Array.isArray(elements))return[];for(var visibleElements=[],i=0;i<elements.length;i++)utilities.isVisible(elements[i])&&visibleElements.push(elements[i]);return visibleElements},utilities.hiddenElements=function(elements){if(!Array.isArray(elements))return[];for(var hiddenElements=[],i=0;i<elements.length;i++)utilities.isHidden(elements[i])&&hiddenElements.push(elements[i]);return hiddenElements},utilities.enabledElements=function(elements){if(!Array.isArray(elements))return[];for(var enabledElements=[],i=0;i<elements.length;i++)utilities.isEnabled(elements[i])&&enabledElements.push(elements[i]);return enabledElements},utilities.disabledElements=function(elements){if(!Array.isArray(elements))return[];for(var disabledElements=[],i=0;i<elements.length;i++)utilities.isDisabled(elements[i])&&disabledElements.push(elements[i]);return disabledElements},utilities.elementsWithAttribute=function(elements,attribute,hasAttribute){if(!Array.isArray(elements)||utilities.isEmptyString(attribute))return[];var element=null,filteredElements=[];attribute=attribute.trim(),hasAttribute=utilities.parseBoolean(hasAttribute,!0);for(var i=0;i<elements.length;i++)element=elements[i],utilities.isObject(element)&&(utilities.isInvalid(elements[i][attribute])?hasAttribute||filteredElements.push(element):hasAttribute&&filteredElements.push(elements[i]));return filteredElements},utilities.elementsWithoutAttribute=function(elements,attribute){return utilities.elementsWithAttribute(elements,attribute,!1)},utilities.matchAttribute=function(element,attribute,value){return!!utilities.isObject(element)&&(!!utilities.isEmptyString(attribute)||element[attribute.trim()]===value)},utilities.generateVersions=function(version,prefix,suffix){if(null===(version=utilities.parseVersion(version)))return null;prefix=utilities.trimString(prefix),suffix=utilities.trimString(suffix);for(var versions=[],value=null,i=0;i<version.length;i++){value="",utilities.isNonEmptyString(prefix)&&(value+=prefix);for(var j=0;j<=i;j++)0<j&&(value+="_"),value+=version[j];utilities.isNonEmptyString(suffix)&&(value+=suffix),versions.push(value)}return versions},utilities.parseVersion=function(value,trimTrailingZeroes){var formattedValue=utilities.isValidNumber(value)?value.toString():value;if("string"!=typeof formattedValue)return null;var version=[],versionData=formattedValue.match(/[^. \t]+/g);if(null===versionData||0===versionData.length)return null;for(var part=null,i=0;i<versionData.length;i++)if(validator.isInt(versionData[i])){if(part=utilities.parseInteger(versionData[i]),utilities.isInvalidNumber(part)||part<0)continue;version.push(part.toString())}else version.push(versionData[i]);if(utilities.parseBoolean(trimTrailingZeroes,!1))for(;!(version.length<=1)&&"0"===version[version.length-1];)version.pop();return 0===version.length?null:version},utilities.compareVersions=function(v1,v2,caseSensitive,throwErrors){if(caseSensitive=utilities.parseBoolean(caseSensitive,!1),throwErrors=utilities.parseBoolean(throwErrors,!1),null===(v1=utilities.parseVersion(v1))){if(throwErrors)throw new Error("Cannot compare invalid or empty first version.");return null}if(null===(v2=utilities.parseVersion(v2))){if(throwErrors)throw new Error("Cannot compare invalid or empty second version.");return null}for(var index=0;;){if(index>=v1.length){if(v1.length===v2.length)return 0;for(var i=index;i<v2.length;i++)if("0"!==v2[i])return-1;return 0}if(index>=v2.length){for(i=index;i<v1.length;i++)if("0"!==v1[i])return 1;return 0}var formattedA=utilities.parseInteger(v1[index]),formattedB=utilities.parseInteger(v2[index]);if(utilities.isInvalidNumber(formattedA)&&(formattedA=caseSensitive?v1[index]:v1[index].toUpperCase()),utilities.isInvalidNumber(formattedB)&&(formattedB=caseSensitive?v2[index]:v2[index].toUpperCase()),Number.isInteger(formattedA)){if(!Number.isInteger(formattedB))return-1}else if(Number.isInteger(formattedB))return 1;if(formattedB<formattedA)return 1;if(formattedA<formattedB)return-1;index++}},utilities.hasPostalCodeValidator=function(country){if("string"!=typeof country)return!1;var formattedCountry=country.trim().toUpperCase();return 0!==formattedCountry.length&&utilities.isRegularExpression(postalCodeValidators[formattedCountry])},utilities.validatePostalCode=function(value,country){if("string"!=typeof country)return!1;var formattedCountry=country.trim().toUpperCase();return!(0===formattedCountry.length||!utilities.isRegularExpression(postalCodeValidators[formattedCountry]))&&postalCodeValidators[formattedCountry].test(value)},utilities}); | ||
!function(global,factory){"object"==typeof exports&&"undefined"!=typeof module?module.exports=factory():"function"==typeof define&&define.amd?define(factory):global.utilities=factory()}(this,function(){"use strict";"undefined"!=typeof require&&("undefined"==typeof validator&&(global.validator=require("validator")),"undefined"==typeof changeCase&&(global.changeCase=require("change-case-bundled")));var utilities={},postalCodeValidators={UK:/^([A-Z]){1}([0-9][0-9]|[0-9]|[A-Z][0-9][A-Z]|[A-Z][0-9][0-9]|[A-Z][0-9]|[0-9][A-Z]){1}([ ])?([0-9][A-z][A-z]){1}$/i,JE:/^JE\d[\dA-Z]?[ ]?\d[ABD-HJLN-UW-Z]{2}$/,GG:/^GY\d[\dA-Z]?[ ]?\d[ABD-HJLN-UW-Z]{2}$/,IM:/^IM\d[\dA-Z]?[ ]?\d[ABD-HJLN-UW-Z]{2}$/,US:/^([0-9]{5})(?:[-\s]*([0-9]{4}))?$/,CA:/^([A-Z][0-9][A-Z])\s*([0-9][A-Z][0-9])$/i,DE:/^\d{5}$/,JP:/^\d{3}-\d{4}$/,FR:/^\d{2}[ ]?\d{3}$/,AU:/^\d{4}$/,IT:/^\d{5}$/,CH:/^\d{4}$/,AT:/^\d{4}$/,ES:/^\d{5}$/,NL:/^\d{4}[ ]?[A-Z]{2}$/,BE:/^\d{4}$/,DK:/^\d{4}$/,SE:/^\d{3}[ ]?\d{2}$/,NO:/^\d{4}$/,BR:/^\d{5}[\-]?\d{3}$/,PT:/^\d{4}([\-]\d{3})?$/,FI:/^\d{5}$/,AX:/^22\d{3}$/,KR:/^\d{3}[\-]\d{3}$/,CN:/^\d{6}$/,TW:/^\d{3}(\d{2})?$/,SG:/^\d{6}$/,DZ:/^\d{5}$/,AD:/^AD\d{3}$/,AR:/^([A-HJ-NP-Z])?\d{4}([A-Z]{3})?$/,AM:/^(37)?\d{4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A-Z0-9]+([- ]?[A-Z0-9]+)*)?$/i},regExpFlags={global:"g",multiline:"m",ignoreCase:"i",sticky:"y",unicode:"u"},formatTypes=["boolean","bool","integer","int","float","number","string","object","array","date","regex","regexp","regularexpression","function","func"],stringCaseFunctions={camel:changeCase.camelCase,constant:changeCase.constantCase,dot:changeCase.dotCase,header:changeCase.headerCase,lower:changeCase.lowerCase,lowerFirst:changeCase.lowerCaseFirst,no:changeCase.noCase,param:changeCase.paramCase,pascal:changeCase.pascalCase,path:changeCase.pathCase,sentence:changeCase.sentenceCase,snake:changeCase.snakeCase,swap:changeCase.swapCase,title:changeCase.titleCase,upper:changeCase.upperCase,upperFirst:changeCase.upperCaseFirst};return utilities.isValid=function(value){return null!=value},utilities.isInvalid=function(value){return null==value},utilities.isBoolean=function(value,allowObjects){return!0===value||!1===value||!!allowObjects&&value instanceof Boolean},utilities.isValidNumber=function(value){return"number"==typeof value&&!isNaN(value)&&value!==-1/0&&value!==1/0},utilities.isInvalidNumber=function(value){return"number"!=typeof value||isNaN(value)||value===-1/0||value===1/0},utilities.isEmptyString=function(value,trim){return"string"!=typeof value||(utilities.parseBoolean(trim,!0)?0===value.trim().length:0===value.length)},utilities.isNonEmptyString=function(value,trim){return"string"==typeof value&&(utilities.parseBoolean(trim,!0)?0!==value.trim().length:0!==value.length)},utilities.isObject=function(value,strict){return void 0!==value&&(strict?null!==value&&value.constructor===Object:value instanceof Object&&!(value instanceof Function))},utilities.isObjectStrict=function(value){return null!=value&&value.constructor===Object},utilities.isEmptyObject=function(value){return null!=value&&value.constructor===Object&&0===Object.keys(value).length},utilities.isNonEmptyObject=function(value){return null!=value&&value.constructor===Object&&0!==Object.keys(value).length},utilities.isEmptyArray=function(value){return!Array.isArray(value)||0===value.length},utilities.isNonEmptyArray=function(value){return Array.isArray(value)&&0!==value.length},utilities.isDate=function(value){return value instanceof Date},utilities.isError=function(value){return value instanceof Error},utilities.isRegularExpression=function(value){return value instanceof RegExp},utilities.isFunction=function(value){return value instanceof Function},utilities.isComment=function(value,comment){if(utilities.isEmptyString(value))return!1;utilities.isEmptyString(comment)&&(comment="//");for(var commentStartIndex=-1,i=0;i<value.length;i++)if(" "!==value[i]&&"\t"!=value[i]){if(value[i]==comment[0]){commentStartIndex=i;break}return!1}if(commentStartIndex<0||value.length-commentStartIndex<comment.length)return!1;for(i=commentStartIndex;i<value.length&&!(i-commentStartIndex>=comment.length);i++)if(value[i]!=comment[i-commentStartIndex])return!1;return!0},utilities.isVisible=function(element){return!!utilities.isObject(element)&&(utilities.isBoolean(element.visible)?element.visible:utilities.isFunction(element.visible)?element.visible():utilities.isBoolean(element.hidden)?!element.hidden:!utilities.isFunction(element.hidden)||!element.hidden())},utilities.isHidden=function(element){return!utilities.isObject(element)||(utilities.isBoolean(element.visible)?!element.visible:utilities.isFunction(element.visible)?!element.visible():utilities.isBoolean(element.hidden)?element.hidden:!!utilities.isFunction(element.hidden)&&element.hidden())},utilities.isEnabled=function(element){return!!utilities.isObject(element)&&(utilities.isBoolean(element.enabled)?element.enabled:utilities.isFunction(element.enabled)?element.enabled():utilities.isBoolean(element.disabled)?!element.disabled:!utilities.isFunction(element.disabled)||!element.disabled())},utilities.isDisabled=function(element){return!utilities.isObject(element)||(utilities.isBoolean(element.enabled)?!element.enabled:utilities.isFunction(element.enabled)?!element.enabled():utilities.isBoolean(element.disabled)?element.disabled:!!utilities.isFunction(element.disabled)&&element.disabled())},utilities.parseBoolean=function(value,defaultValue){if(utilities.isBoolean(value))return value;if(utilities.isBoolean(value,!0))return value.valueOf();if(utilities.isBoolean(defaultValue)||(defaultValue=null),utilities.isInvalid(value))return defaultValue;if(0===value)return!1;if(1===value)return!0;if("string"!=typeof value)return defaultValue;var formattedValue=value.trim().toLowerCase();if(0===formattedValue.length)return defaultValue;if(1===formattedValue.length){var character=formattedValue.charAt(0);return"t"===character||"y"===character||"f"!==character&&"n"!==character&&("0"!==character&&("1"===character||defaultValue))}return"true"===formattedValue||"yes"===formattedValue||"on"===formattedValue||"false"!==formattedValue&&"no"!==formattedValue&&"off"!==formattedValue&&defaultValue},utilities.parseInteger=function(value,defaultValue){var newValue=NaN;return"number"==typeof value?newValue=parseInt(value):"string"==typeof value&&validator.isFloat(value)&&(newValue=parseInt(value)),utilities.isInvalidNumber(newValue)?(defaultValue=parseInt(defaultValue),utilities.isValidNumber(defaultValue)?defaultValue:NaN):newValue},utilities.parseFloatingPointNumber=function(value,defaultValue){var newValue=NaN;return"number"==typeof value?newValue=value:"string"==typeof value&&validator.isFloat(value)&&(newValue=parseFloat(value)),utilities.isInvalidNumber(newValue)?utilities.isValidNumber(defaultValue)?defaultValue:NaN:newValue},utilities.parseDate=function(value,defaultValue){if(utilities.isDate(defaultValue)||(defaultValue=null),"number"==typeof value)return utilities.isInvalidNumber(value)||!Number.isInteger(value)?defaultValue:new Date(parseInt(value));if("string"==typeof value){var formattedValue=value.trim();if(0===formattedValue.length)return defaultValue;var timestamp=null;return timestamp=validator.isInt(formattedValue)?parseInt(formattedValue):Date.parse(formattedValue),utilities.isInvalidNumber(timestamp)?defaultValue:new Date(timestamp)}return value instanceof Date?value:defaultValue},utilities.parseTime=function(value,throwErrors){if(throwErrors=utilities.parseBoolean(throwErrors),utilities.isEmptyString(value)){if(throwErrors)throw new Error("Invalid or empty time value.");return null}var formattedValue=value.trim(),hour=null,minutes=null,regularTime=formattedValue.match(/^[ \t]*(([2-9]|[1][0-2]?)(:([0-5][0-9]))?[ \t]*([ap]m))[ \t]*$/i);if(regularTime){var regularHour=utilities.parseInteger(utilities.trimLeadingZeroes(regularTime[2]));if(utilities.isInvalidNumber(regularHour)){if(throwErrors)throw new Error('Invalid regular hour: "'+regularTime[2]+'".');return null}minutes=utilities.parseInteger(utilities.trimLeadingZeroes(regularTime[4])),utilities.isInvalidNumber(minutes)&&(minutes=0);var morning="AM"===(period=regularTime[5].toUpperCase())||"PM"!==period&&null;if(null===morning){if(throwErrors)throw new Error('Invalid period: "'+regularTime[5]+'".');return null}24===(hour=morning?regularHour+(12===regularHour?12:0):regularHour+(12===regularHour?0:12))&&(hour=0)}else{var militaryTime=formattedValue.match(/^[ \t]*(((([0-1][0-9])|(2[0-3])):?([0-5][0-9]))|((24):?(00)))[ \t]*$/i);if(!militaryTime){if(throwErrors)throw new Error('Invalid time: "'+formattedValue+'".');return null}var militaryHour=militaryTime[3],militaryMinutes=militaryTime[6];if((utilities.isInvalid(militaryHour)||utilities.isInvalid(militaryMinutes))&&(militaryHour=militaryTime[8],utilities.isNonEmptyString(militaryTime[9])&&(militaryMinutes=militaryTime[9])),utilities.isInvalid(militaryHour)||utilities.isInvalid(militaryMinutes)){if(throwErrors)throw new Error('Invalid military time: "'+formattedValue+'".');return null}if(hour=utilities.parseInteger(utilities.trimLeadingZeroes(militaryHour)),utilities.isInvalidNumber(hour)){if(throwErrors)throw new Error('Invalid military time hour: "'+militaryHour+'".');return null}if(minutes=utilities.parseInteger(utilities.trimLeadingZeroes(militaryMinutes)),utilities.isInvalidNumber(minutes)){if(throwErrors)throw new Error('Invalid military time minutes: "'+militaryMinutes+'".');return null}24===hour&&0===minutes&&(hour=0)}if(hour<0||23<hour){if(throwErrors)throw new Error('Time hour is out of range (0 - 23): "'+hour+'".');return null}if(minutes<0||59<minutes){if(throwErrors)throw new Error('Time minutes is out of range (0 - 59): "'+minutes+'".');return null}var period=hour<12?"AM":"PM";return{regular:{raw:(regularHour=0===hour?12:hour<=12?hour:hour-12)+":"+utilities.addLeadingZeroes(minutes,2)+" "+period,hour:regularHour,minutes:minutes,period:period,morning:hour<12},military:{raw:utilities.addLeadingZeroes(hour,2)+utilities.addLeadingZeroes(minutes,2),hour:hour,minutes:minutes}}},utilities.parsePostalCode=function(value){if(utilities.isEmptyString(value))return null;var postalCodeData=value.match(/[ \t]*([A-Z][0-9][A-Z])[_\- \t]?([0-9][A-Z][0-9])[ \t]*/i);return postalCodeData?(postalCodeData[1]+postalCodeData[2]).toUpperCase():null},utilities.parseEmail=function(value){if(utilities.isEmptyString(value))return null;var emailData=value.trim().toLowerCase().match(/([^+@]+)(\+.*)?(@.+\..+)/);return utilities.isInvalid(emailData)||emailData.length<4?null:emailData[1]+emailData[3]},utilities.parseEmailDomain=function(value){if(utilities.isEmptyString(value))return null;var emailDomainData=value.trim().toLowerCase().match(/([^+@]+)(\+.*)?@(.+\..+)/);return utilities.isInvalid(emailDomainData)||emailDomainData.length<4?null:emailDomainData[3]},utilities.parseStringList=function(value){if("string"!=typeof value)return null;if(0===value.length)return[];for(var data=value.split(/[;,]+/),formattedList=[],formattedValue=null,i=0;i<data.length;i++)0!==(formattedValue=data[i].trim()).length&&formattedList.push(formattedValue);return formattedList},utilities.parseRegularExpression=function(value,throwErrors){if(throwErrors=utilities.parseBoolean(throwErrors,!1),utilities.isRegularExpression(value))return value;if(utilities.isEmptyString(value)){if(throwErrors)throw new Error("Regular expression cannot be empty.");return null}var regExpData=value.match(/\s*\/(.*)\/(.*)\s*/);if(!regExpData){if(throwErrors)throw new Error("Invalid regular expression value.");return null}if(throwErrors)return new RegExp(regExpData[1],regExpData[2]);try{return new RegExp(regExpData[1],regExpData[2])}catch(error){return null}},utilities.parseYouTubeLink=function(value){if(utilities.isEmptyString(value))return null;var formattedValue=value.trim(),linkData=formattedValue.match(/.*(?:youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=)([^#\&\?]*).*/i);return linkData&&11<=linkData[1].length?linkData[1]:formattedValue.match(/[A-Z0-9_-]{11,}/i)?formattedValue:null},utilities.formatValue=function(value,format,options){if(utilities.isObjectStrict(options)?(options={throwErrors:utilities.parseBoolean(options.throwErrors,!1)}).verbose=utilities.parseBoolean(options.verbose,!options.throwErrors):options={throwErrors:!1,verbose:!0},!utilities.isObjectStrict(format))return utilities.clone(value);format=utilities.clone(format);var errorMessage=null;if(utilities.isEmptyString(format.type))errorMessage="Invalid required type format value - expected non-empty string.";else{var originalType=format.type;format.type=format.type.trim().toLowerCase();for(var validFormatType=!1,i=0;i<formatTypes.length;i++)if(format.type===formatTypes[i]){validFormatType=!0;break}if(validFormatType){if("bool"===format.type)format.type="boolean";else if("int"===format.type)format.type="integer";else if("number"===format.type)format.type="float";else if("string"===format.type){if(void 0!==format.trim){var originalTrim=format.trim;format.trim=utilities.parseBoolean(format.trim),null===format.trim&&(errorMessage='Invalid optional trim string format value - expected boolean, received "'+utilities.toString(originalTrim)+'".')}if(utilities.isInvalid(errorMessage)&&void 0!==format.case)if(utilities.isEmptyString(format.case))errorMessage="Invalid optional string case format value - expected non-empty string.";else{var originalCase=format.case;format.case=changeCase.camel(format.case.trim()),utilities.isFunction(stringCaseFunctions[format.case])||(errorMessage="Invalid optional case format value - expected one of "+Object.keys(stringCaseFunctions).join(", ")+', received "'+utilities.toString(originalCase)+'".')}}else if("object"===format.type){if(void 0!==format.strict){var originalStrict=format.strict;format.strict=utilities.parseBoolean(format.strict),null===format.strict&&(errorMessage='Invalid optional strict object format value - expected boolean, received "'+utilities.toString(originalStrict)+'".')}if(utilities.isInvalid(errorMessage)&&void 0!==format.autopopulate){var originalAutopopulate=format.autopopulate;format.autopopulate=utilities.parseBoolean(format.autopopulate),null===format.autopopulate&&(errorMessage='Invalid optional autopopulate object format value - expected boolean, received "'+utilities.toString(originalAutopopulate)+'".')}if(utilities.isInvalid(errorMessage)&&void 0!==format.order){var originalOrder=format.order;format.order=utilities.parseBoolean(format.order),null===format.order&&(errorMessage='Invalid optional order object format value - expected boolean, received "'+utilities.toString(originalOrder)+'".')}if(utilities.isInvalid(errorMessage)&&void 0!==format.removeExtra){var originalRemoveExtra=format.removeExtra;format.removeExtra=utilities.parseBoolean(format.removeExtra),null===format.removeExtra&&(errorMessage='Invalid optional removeExtra object format value - expected boolean, received "'+utilities.toString(originalRemoveExtra)+'".')}}else"regexp"===format.type||"regularexpression"===format.type?format.type="regex":"func"===format.type&&(format.type="function");if(utilities.isInvalid(errorMessage)&&("object"!==format.type&&"array"!==format.type||void 0===format.format||utilities.isObjectStrict(format.format)||(errorMessage='Invalid optional format format value - expected object, received "'+utilities.toString(format.format)+'".')),utilities.isInvalid(errorMessage)&&("string"===format.type||"object"===format.type||"array"===format.type)&&void 0!==format.nonEmpty){var originalNonEmpty=format.nonEmpty;format.nonEmpty=utilities.parseBoolean(format.nonEmpty),null===format.nonEmpty&&(errorMessage='Invalid optional nonEmpty format value - expected boolean, received "'+utilities.toString(originalNonEmpty)+'".')}}else errorMessage="Invalid required type format value - expected one of "+formatTypes.join(", ")+', received "'+utilities.toString(originalType)+'".'}if(utilities.isInvalid(errorMessage)&&void 0!==format.nullable){var originalNullable=format.nullable;format.nullable=utilities.parseBoolean(format.nullable),null===format.nullable&&(errorMessage='Invalid optional nullable format value - expected boolean, received "'+utilities.toString(originalNullable)+'".')}if(utilities.isInvalid(errorMessage)&&void 0!==format.required){var originalRequired=format.required;format.required=utilities.parseBoolean(format.required),null===format.required&&(errorMessage='Invalid optional required format value - expected boolean, received "'+utilities.toString(originalRequired)+'".')}if(utilities.isInvalid(errorMessage)&&void 0!==format.parser&&(utilities.isFunction(format.parser)||(errorMessage='Invalid optional parser format value - expected function, received "'+utilities.toString(format.parser)+'".')),utilities.isInvalid(errorMessage)&&void 0!==format.validator&&(utilities.isFunction(format.validator)||(errorMessage='Invalid optional validator format value - expected function, received "'+utilities.toString(format.validator)+'".')),utilities.isInvalid(errorMessage)&&void 0!==format.formatter&&(utilities.isFunction(format.formatter)||(errorMessage='Invalid optional formatter format value - expected function, received "'+utilities.toString(format.formatter)+'".')),utilities.isValid(errorMessage)){if(options.throwErrors)throw new Error(errorMessage);return options.verbose&&console.error(errorMessage),null}if(utilities.isFunction(format.parser))if(options.throwErrors)value=format.parser(value,format,options);else try{value=format.parser(value,format,options)}catch(error){return options.verbose&&console.error(error.message),null}var formattedValue=void 0;if(void 0===value)formattedValue=utilities.clone(format.default);else if(null===value)format.nullable?formattedValue=null:void 0!==format.default?formattedValue=utilities.clone(format.default):errorMessage="Value cannot be null!";else if("boolean"===format.type)null===(formattedValue=utilities.parseBoolean(value))&&(errorMessage='Invalid boolean value: "'+utilities.toString(value)+'".');else if("integer"===format.type)formattedValue=utilities.parseInteger(value),utilities.isInvalidNumber(formattedValue)&&(errorMessage='Invalid integer value: "'+utilities.toString(value)+'".');else if("float"===format.type)formattedValue=utilities.parseFloatingPointNumber(value),utilities.isInvalidNumber(formattedValue)&&(errorMessage='Invalid float value: "'+utilities.toString(value)+'".');else if("string"===format.type){if(formattedValue="string"==typeof value?value:utilities.toString(value),format.trim&&(formattedValue=formattedValue.trim()),void 0!==format.case&&(formattedValue=stringCaseFunctions[format.case](formattedValue)),format.nonEmpty&&0===formattedValue.length){var message="String value cannot be empty.";if(options.throwErrors)throw new Error(message);return options.verbose&&console.error(message),null}}else if("array"===format.type){if("string"==typeof value)try{formattedValue=JSON.parse(value),Array.isArray(formattedValue)||(errorMessage='Invalid stringified array value type - expected array: "'+utilities.toString(value)+'".')}catch(error){errorMessage='Invalid stringified array value: "'+utilities.toString(value)+'".'}else Array.isArray(value)?formattedValue=utilities.clone(value):errorMessage='Invalid value type - expected array or stringified array: "'+utilities.toString(value)+'".';if(utilities.isInvalid(errorMessage)&&Array.isArray(formattedValue)){var formattedArray=[];if(utilities.isObjectStrict(format.format)){var formattedElement=null;for(i=0;i<formattedValue.length;i++)if(void 0!==formattedValue[i]){if(options.throwErrors)formattedElement=utilities.formatValue(formattedValue[i],format.format,options);else{(subOptions=utilities.clone(options)).throwErrors=!0;try{formattedElement=utilities.formatValue(formattedValue[i],format.format,subOptions)}catch(error){return options.verbose&&console.error(error.message),null}}formattedArray.push(formattedElement)}}else for(i=0;i<formattedValue.length;i++)void 0!==formattedValue[i]&&formattedArray.push(utilities.clone(formattedValue[i]));if(formattedValue=formattedArray,format.nonEmpty&&0===formattedValue.length){message="Array value cannot be empty.";if(options.throwErrors)throw new Error(message);return options.verbose&&console.error(message),null}}}else if("date"===format.type)formattedValue=utilities.parseDate(value),utilities.isDate(formattedValue)||(errorMessage='Invalid date value: "'+utilities.toString(value)+'".');else if("regex"===format.type){if(utilities.isRegularExpression(value))formattedValue=utilities.clone(value);else if(options.throwErrors)formattedValue=utilities.parseRegularExpression(value,!0);else try{formattedValue=utilities.parseRegularExpression(value,!0)}catch(error){errorMessage=error.message}utilities.isInvalid(formattedValue)&&(errorMessage='Invalid regular expression value: "'+utilities.toString(value)+'".')}else"function"===format.type&&(formattedValue=value,utilities.isFunction(formattedValue)||(errorMessage='Invalid function value: "'+utilities.toString(value)+'".'));if(utilities.isInvalid(errorMessage)&&"object"===format.type){if(utilities.isValid(value))if("string"==typeof value)try{formattedValue=JSON.parse(value),utilities.isObject(formattedValue,format.strict)||(errorMessage="Invalid stringified object value type - expected "+(format.strict?"strict ":"")+'object: "'+utilities.toString(value)+'".')}catch(error){errorMessage='Invalid stringified object value: "'+utilities.toString(value)+'".'}else utilities.isObject(value,format.strict)?formattedValue=utilities.clone(value):errorMessage="Invalid value type - expected "+(format.strict?"strict ":"")+'object or stringified object: "'+utilities.toString(value)+'".';if(utilities.isInvalid(errorMessage)){if(utilities.isInvalid(formattedValue)&&format.autopopulate&&!utilities.isObject(formattedValue,format.strict)&&(formattedValue={}),utilities.isObjectStrict(format.format)&&utilities.isObject(formattedValue)){var attribute=null,attributes=Object.keys(format.format);for(i=0;i<attributes.length;i++)if(attribute=attributes[i],options.throwErrors){void 0!==(formattedAttribute=utilities.formatValue(formattedValue[attribute],format.format[attribute],options))&&(formattedValue[attribute]=formattedAttribute)}else{var subOptions;(subOptions=utilities.clone(options)).throwErrors=!0;try{var formattedAttribute;void 0!==(formattedAttribute=utilities.formatValue(formattedValue[attribute],format.format[attribute],subOptions))&&(formattedValue[attribute]=formattedAttribute)}catch(error){return options.verbose&&console.error(error.message),null}}if(format.removeExtra){var newValue={};attributes=Object.keys(formattedValue);for(i=0;i<attributes.length;i++)attribute=attributes[i],utilities.isValid(format.format[attribute])&&(newValue[attribute]=formattedValue[attribute]);formattedValue=newValue}if(format.order){var orderedObject={};attributes=Object.keys(format.format);for(i=0;i<attributes.length;i++)void 0!==formattedValue[attribute=attributes[i]]&&(orderedObject[attribute]=formattedValue[attribute],delete formattedValue[attribute]);var extraAttributes=Object.keys(formattedValue);for(i=0;i<extraAttributes.length;i++)orderedObject[attribute=extraAttributes[i]]=formattedValue[attribute];formattedValue=orderedObject}}if(format.nonEmpty&&utilities.isEmptyObject(formattedValue)){message="Object value must contain at least one attribute.";if(options.throwErrors)throw new Error(message);return options.verbose&&console.error(message),null}}}if(utilities.isValid(errorMessage)){if(options.throwErrors)throw new Error(errorMessage);return options.verbose&&console.error(errorMessage),null}if(void 0===formattedValue){if(format.required){message="Missing required value!";if(options.throwErrors)throw new Error(message);return options.verbose&&console.error(message),null}void 0!==format.default&&(formattedValue=utilities.clone(format.default))}if(utilities.isFunction(format.validator))if(options.throwErrors){if(!format.validator(formattedValue,format,options))throw new Error("Validation check failed!")}else try{if(!format.validator(formattedValue,format,options)){message="Validation check failed!";return options.verbose&&console.error(message),null}}catch(error){return options.verbose&&console.error(error.message),null}if(utilities.isFunction(format.formatter))if(options.throwErrors)formattedValue=format.formatter(formattedValue,format,options);else try{formattedValue=format.formatter(formattedValue,format,options)}catch(error){return options.verbose&&console.error(error.message),null}return formattedValue},utilities.formatObject=function(object,format,removeExtra,throwErrors){if(!(utilities.isObjectStrict(object)||utilities.isObjectStrict(removeExtra)&&utilities.isFunction(removeExtra.parser)))return{};var options=null;options=utilities.isObjectStrict(removeExtra)?{throwErrors:utilities.parseBoolean(removeExtra.throwErrors,utilities.parseBoolean(throwErrors,!1)),verbose:utilities.parseBoolean(removeExtra.verbose,!1)}:{throwErrors:utilities.parseBoolean(throwErrors,!1),verbose:!1};var subFormat={type:"object",strict:!1,autopopulate:!!utilities.isObjectStrict(removeExtra)&&utilities.parseBoolean(removeExtra.autopopulate,!1),order:!!utilities.isObjectStrict(removeExtra)&&utilities.parseBoolean(removeExtra.order,!1),removeExtra:utilities.isObjectStrict(removeExtra)?utilities.parseBoolean(removeExtra.removeExtra,utilities.parseBoolean(removeExtra,!1)):utilities.parseBoolean(removeExtra,!1),nullable:!0,required:!1,format:format};utilities.isObjectStrict(removeExtra)&&(utilities.isFunction(removeExtra.parser)&&(subFormat.parser=removeExtra.parser),utilities.isFunction(removeExtra.validator)&&(subFormat.validator=removeExtra.validator),utilities.isFunction(removeExtra.formatter)&&(subFormat.formatter=removeExtra.formatter));var formattedObject=null;if(options.throwErrors)formattedObject=utilities.formatValue(object,subFormat,options);else{var subOptions=utilities.clone(options);subOptions.throwErrors=!0;try{formattedObject=utilities.formatValue(object,subFormat,subOptions)}catch(error){return options.verbose&&console.error(error.message),null}}return utilities.isObjectStrict(formattedObject)||utilities.isObjectStrict(removeExtra)&&utilities.isFunction(removeExtra.parser)?formattedObject:{}},utilities.formatStringList=function(value,stringify){var data=null;if(utilities.isNonEmptyString(value))data=utilities.parseStringList(value);else{if(!utilities.isNonEmptyArray(value))return"string"==typeof value||Array.isArray(value)?"":null;data=value}var formattedList="",formattedValue=null;stringify=utilities.parseBoolean(stringify);for(var i=0;i<data.length;i++){if("string"!=typeof data[i]){if(null===stringify)continue;formattedValue=stringify?utilities.toString(data[i]):data[i]}else formattedValue=data[i].trim();0!==formattedValue.length&&(0<formattedList.length&&(formattedList+=", "),formattedList+=formattedValue)}return formattedList},utilities.trimString=function(value,defaultValue){return"string"==typeof value?value.trim():void 0===defaultValue?null:defaultValue},utilities.trimNullTerminatedString=function(value,defaultValue){if("string"!=typeof value)return void 0===defaultValue?null:defaultValue;var nullTerminatorIndex=value.indexOf("\0");return 0<=nullTerminatorIndex?value.substr(0,nullTerminatorIndex):value},utilities.trimWhitespace=function(value,trimNewlines){if("string"!=typeof value)return null;var trimmedString=value.replace(/^[ \t]+|[ \t]+$/gm,"");return utilities.parseBoolean(trimNewlines,!1)&&(trimmedString=trimmedString.replace(/\r\n?|\n/g,"")),trimmedString},utilities.trimTrailingNewlines=function(value){return"string"!=typeof value?null:utilities.isEmptyString(value)?value:value.replace(/[ \t\r\n]+$/,"")},utilities.replaceNonBreakingSpaces=function(value){return"string"==typeof value?value.replace(/ /gi," "):null},utilities.indentText=function(value,amount,indentation,clearEmptyLines){if("string"!=typeof value)return null;clearEmptyLines=utilities.parseBoolean(clearEmptyLines,!0),(amount=utilities.parseInteger(amount,1))<0&&(amount=0),indentation="string"==typeof indentation?indentation:"\t";for(var totalIndentation="",i=0;i<amount;i++)totalIndentation+=indentation;var line=null,lines=value.split(/\r\n?|\n/g),indentedParagraph="";for(i=0;i<lines.length;i++)line=lines[i],indentedParagraph+=(utilities.isEmptyString(line)&&clearEmptyLines?"":totalIndentation+line)+(i<lines.length-1?"\n":"");return indentedParagraph},utilities.trimLeadingZeroes=function(value){if("string"!=typeof value)return null;if(0===value.length)return value;var formattedValue=value.trim();return 0===formattedValue.length?formattedValue:formattedValue.match(/^[0]+$/)?"0":formattedValue.replace(/^0+/,"")},utilities.addLeadingZeroes=function(value,expectedLength){if(utilities.isInvalid(value))return null;if(value=value.toString(),expectedLength=utilities.parseInteger(expectedLength),utilities.isInvalidNumber(expectedLength)||expectedLength<0)return value;for(var numberOfZeroes=expectedLength-value.length,i=0;i<numberOfZeroes;i++)value="0"+value;return value},utilities.toString=function(value){if(void 0===value)return"undefined";if(null===value)return"null";if("string"==typeof value)return value;if(value===1/0)return"Infinity";if(value===-1/0)return"-Infinity";if("number"==typeof value&&isNaN(value))return"NaN";if(utilities.isDate(value))return value.toString();if(utilities.isRegularExpression(value)){var flags="";for(var flag in regExpFlags)value[flag]&&(flags+=regExpFlags[flag]);return"/"+value.source+"/"+flags}if(utilities.isFunction(value))return value.toString();if(utilities.isError(value)){var error={message:value.message};for(var attribute in value)error[attribute]=value[attribute];return JSON.stringify(error)}return JSON.stringify(value)},utilities.compareDates=function(a,b){return a=utilities.parseDate(a),b=utilities.parseDate(b),null===a&&null===b?0:null===a?-1:null===b?1:a.getTime()-b.getTime()},utilities.compareCasePercentage=function(value){if(utilities.isEmptyString(value))return 0;for(var c=null,upper=0,lower=0,lowerA="a".charCodeAt(),lowerZ="z".charCodeAt(),upperA="A".charCodeAt(),upperZ="Z".charCodeAt(),i=0;i<value.length;i++)lowerA<=(c=value.charCodeAt(i))&&c<=lowerZ?lower++:upperA<=c&&c<=upperZ&&upper++;return upper-lower},utilities.reverseString=function(value){if("string"!=typeof value)return null;for(var reverse="",i=0;i<value.length;i++)reverse+=value[value.length-i-1];return reverse},utilities.createError=function(message,status){var error=new Error(message);return error.status=utilities.parseInteger(status,500),error},utilities.clone=function(value){if(!utilities.isObject(value))return value;if(value instanceof Boolean)return new Boolean(value.valueOf());if(value instanceof Date)return(copy=new Date).setTime(value.getTime()),copy;if(value instanceof Array){for(var copy=[],i=0,length=value.length;i<length;i++)copy[i]=utilities.clone(value[i]);return copy}if(value instanceof Set)return new Set(value);if(value instanceof Map)return new Map(value);if(value instanceof RegExp){var flags="";for(var flag in regExpFlags)value[flag]&&(flags+=regExpFlags[flag]);return new RegExp(value.source,flags)}if("undefined"!=typeof Buffer&&value instanceof Buffer)return Buffer.from instanceof Function?Buffer.from(value):new Buffer(value);if(value instanceof Object){copy=null;if(value instanceof Error){(copy=new Error(value.message)).stack=utilities.clone(value.stack);var properties=Object.keys(value);for(i=0;i<properties.length;i++)copy[properties[i]]=utilities.clone(value[properties[i]])}else copy={};for(var attribute in value)Object.prototype.hasOwnProperty.call(value,attribute)&&(copy[attribute]=utilities.clone(value[attribute]));return copy}return value},utilities.merge=function(a,b,copy,deepMerge){if(!utilities.isObject(a)||Array.isArray(a))return null;var newObject=null;if(newObject=(copy=utilities.parseBoolean(copy,!0))?utilities.clone(a):a,!utilities.isObject(a)||Array.isArray(a)||!utilities.isObject(b)||Array.isArray(b))return newObject;var attribute=null,value=null,newValue=null,attributes=Object.keys(b);deepMerge=utilities.parseBoolean(deepMerge,!0);for(var i=0;i<attributes.length;i++)value=newObject[attribute=attributes[i]],newValue=copy?utilities.clone(b[attribute]):b[attribute],deepMerge&&utilities.isObject(value)&&!Array.isArray(value)&&utilities.isObject(newValue)&&!Array.isArray(newValue)?newObject[attribute]=utilities.merge(value,newValue):newObject[attribute]=newValue;return newObject},utilities.calculateAge=function(value){var currentDate=new Date,formattedDate=utilities.parseDate(value);return null===formattedDate||currentDate<formattedDate?-1:Math.floor((currentDate-formattedDate)/1e3/86400/365.25)},utilities.prependSlash=function(value){if("string"!=typeof value)return null;var formattedValue=value.trim();return 0===formattedValue.length||"/"!==formattedValue[0]&&"\\"!==formattedValue[0]&&(formattedValue="/"+formattedValue),formattedValue},utilities.appendSlash=function(value){if("string"!=typeof value)return null;var formattedValue=value.trim();return 0===formattedValue.length||"/"!==formattedValue[formattedValue.length-1]&&"\\"!==formattedValue[formattedValue.length-1]&&(formattedValue+="/"),formattedValue},utilities.joinPaths=function(base,path){var formattedBase="string"==typeof base?base.trim().replace(/[\/\\]+$/,""):null,formattedPath="string"==typeof path?path.trim().replace(/^[\/\\]+/,""):null,newPath="";return utilities.isNonEmptyString(formattedBase)&&(newPath+=formattedBase,utilities.isNonEmptyString(formattedPath)&&(newPath+="/")),utilities.isNonEmptyString(formattedPath)&&(newPath+=formattedPath),newPath},utilities.createQueryString=function(value,includeQuestionMark){if(!utilities.isObjectStrict(value))return"";var parameters=Object.keys(value).map(function(key){return encodeURIComponent(key)+"="+encodeURIComponent(utilities.toString(value[key]))}).join("&");return 0===parameters.length?"":(utilities.parseBoolean(includeQuestionMark,!1)?"?":"")+parameters},utilities.createRange=function(start,end){var formattedStart=utilities.parseInteger(start),formattedEnd=utilities.parseInteger(end);if(1===arguments.length&&(formattedEnd=formattedStart,formattedStart=0),utilities.isInvalidNumber(formattedStart)||utilities.isInvalidNumber(formattedEnd)||formattedEnd<formattedStart)return[];for(var range=[],i=formattedStart;i<=formattedEnd;i++)range.push(i);return range},utilities.futureMonths=function(date,prependZero){if(null===(date=utilities.parseDate(date)))return null;var currentDate=new Date,month=0;date.getFullYear()===currentDate.getFullYear()&&(month=currentDate.getMonth());var months=[];prependZero=utilities.parseBoolean(prependZero,!1);for(var i=0;i<12;i++)month<=i&&(prependZero?months.push((i<=8?"0":"")+(i+1)):months.push(i+1));return months},utilities.visibleElements=function(elements){if(!Array.isArray(elements))return[];for(var visibleElements=[],i=0;i<elements.length;i++)utilities.isVisible(elements[i])&&visibleElements.push(elements[i]);return visibleElements},utilities.hiddenElements=function(elements){if(!Array.isArray(elements))return[];for(var hiddenElements=[],i=0;i<elements.length;i++)utilities.isHidden(elements[i])&&hiddenElements.push(elements[i]);return hiddenElements},utilities.enabledElements=function(elements){if(!Array.isArray(elements))return[];for(var enabledElements=[],i=0;i<elements.length;i++)utilities.isEnabled(elements[i])&&enabledElements.push(elements[i]);return enabledElements},utilities.disabledElements=function(elements){if(!Array.isArray(elements))return[];for(var disabledElements=[],i=0;i<elements.length;i++)utilities.isDisabled(elements[i])&&disabledElements.push(elements[i]);return disabledElements},utilities.elementsWithAttribute=function(elements,attribute,hasAttribute){if(!Array.isArray(elements)||utilities.isEmptyString(attribute))return[];var element=null,filteredElements=[];attribute=attribute.trim(),hasAttribute=utilities.parseBoolean(hasAttribute,!0);for(var i=0;i<elements.length;i++)element=elements[i],utilities.isObject(element)&&(utilities.isInvalid(elements[i][attribute])?hasAttribute||filteredElements.push(element):hasAttribute&&filteredElements.push(elements[i]));return filteredElements},utilities.elementsWithoutAttribute=function(elements,attribute){return utilities.elementsWithAttribute(elements,attribute,!1)},utilities.matchAttribute=function(element,attribute,value){return!!utilities.isObject(element)&&(!!utilities.isEmptyString(attribute)||element[attribute.trim()]===value)},utilities.generateVersions=function(version,prefix,suffix){if(null===(version=utilities.parseVersion(version)))return null;prefix=utilities.trimString(prefix),suffix=utilities.trimString(suffix);for(var versions=[],value=null,i=0;i<version.length;i++){value="",utilities.isNonEmptyString(prefix)&&(value+=prefix);for(var j=0;j<=i;j++)0<j&&(value+="_"),value+=version[j];utilities.isNonEmptyString(suffix)&&(value+=suffix),versions.push(value)}return versions},utilities.parseVersion=function(value,trimTrailingZeroes){var formattedValue=utilities.isValidNumber(value)?value.toString():value;if("string"!=typeof formattedValue)return null;var version=[],versionData=formattedValue.match(/[^. \t]+/g);if(null===versionData||0===versionData.length)return null;for(var part=null,i=0;i<versionData.length;i++)if(validator.isInt(versionData[i])){if(part=utilities.parseInteger(versionData[i]),utilities.isInvalidNumber(part)||part<0)continue;version.push(part.toString())}else version.push(versionData[i]);if(utilities.parseBoolean(trimTrailingZeroes,!1))for(;!(version.length<=1)&&"0"===version[version.length-1];)version.pop();return 0===version.length?null:version},utilities.compareVersions=function(v1,v2,caseSensitive,throwErrors){if(caseSensitive=utilities.parseBoolean(caseSensitive,!1),throwErrors=utilities.parseBoolean(throwErrors,!1),null===(v1=utilities.parseVersion(v1))){if(throwErrors)throw new Error("Cannot compare invalid or empty first version.");return null}if(null===(v2=utilities.parseVersion(v2))){if(throwErrors)throw new Error("Cannot compare invalid or empty second version.");return null}for(var index=0;;){if(index>=v1.length){if(v1.length===v2.length)return 0;for(var i=index;i<v2.length;i++)if("0"!==v2[i])return-1;return 0}if(index>=v2.length){for(i=index;i<v1.length;i++)if("0"!==v1[i])return 1;return 0}var formattedA=utilities.parseInteger(v1[index]),formattedB=utilities.parseInteger(v2[index]);if(utilities.isInvalidNumber(formattedA)&&(formattedA=caseSensitive?v1[index]:v1[index].toUpperCase()),utilities.isInvalidNumber(formattedB)&&(formattedB=caseSensitive?v2[index]:v2[index].toUpperCase()),Number.isInteger(formattedA)){if(!Number.isInteger(formattedB))return-1}else if(Number.isInteger(formattedB))return 1;if(formattedB<formattedA)return 1;if(formattedA<formattedB)return-1;index++}},utilities.hasPostalCodeValidator=function(country){if("string"!=typeof country)return!1;var formattedCountry=country.trim().toUpperCase();return 0!==formattedCountry.length&&utilities.isRegularExpression(postalCodeValidators[formattedCountry])},utilities.validatePostalCode=function(value,country){if("string"!=typeof country)return!1;var formattedCountry=country.trim().toUpperCase();return!(0===formattedCountry.length||!utilities.isRegularExpression(postalCodeValidators[formattedCountry]))&&postalCodeValidators[formattedCountry].test(value)},utilities}); |
{ | ||
"name": "extra-utilities", | ||
"version": "1.2.16", | ||
"version": "1.2.17", | ||
"description": "A collection of useful helper functions.", | ||
@@ -5,0 +5,0 @@ "main": "dist/extra-utilities.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
99672
1905