password-meter
Version:
This password meter library is inspired by pointing system in http://www.passwordmeter.com/, in which the main purpose is to help the end users to have more stronger passwords.
3 lines (2 loc) • 10.3 kB
JavaScript
System.register([],(function(e){"use strict";return{execute:function(){e("PasswordMeter",function(){function e(e,t){this.requirements=e,this.scoreRange=t,this.uppercaseLetters="ABCDEFGHIJKLMNOPQRSTUVWXYZ",this.lowercaseLetters="abcdefghijklmnopqrstuvwxyz",this.numbers="1234567890"}return e.prototype.startsWith=function(e,t){return 0===e.lastIndexOf(t,0)},e.prototype.endsWith=function(e,t){return-1!==e.indexOf(t,e.length-t.length)},e.prototype.chunkString=function(e,t){for(var r=Math.ceil(e.length/t),s=new Array(r),n=0,i=0;i<r;i++)n=i*t,s[i]=e.substring(n,n+t);return s},e.prototype.getLength=function(e){return e?e.length:0},e.prototype.doesNotContains=function(e,t){return!e||(!t||t.every((function(t){return-1==e.indexOf(t)})))},e.prototype.contains=function(e,t){return!!e&&(!!t&&t.every((function(t){return e.indexOf(t)>=0})))},e.prototype.containsOne=function(e,t){return!!e&&(!!t&&t.some((function(t){return e.indexOf(t)>=0})))},e.prototype.isInBlackList=function(e,t){if(e){if(t){for(var r=0;r<t.length;r++)if(e===t[r])return!0;return!1}return!1}return!1},e.prototype.between=function(e,t,r){return e>=t&&e<r},e.prototype.isIMessage=function(e){return void 0!==e.message},e.prototype.isNumber=function(e){if(e){return/^\d+$/.test(e)}return!1},e.prototype.isLetter=function(e){if(e){return/^[a-zA-Z]+$/.test(e)}return!1},e.prototype.isUppercaseLetter=function(e){if(e){return/^[A-Z]+$/.test(e)}return!1},e.prototype.isLowercaseLetter=function(e){if(e){return/^[a-z]+$/.test(e)}return!1},e.prototype.isSymbol=function(e){return!!e&&(!this.isNumber(e)&&!this.isLetter(e))},e.prototype.getSymbols=function(e){var t="";if(e)for(var r=0;r<e.length;r++)this.isSymbol(e[r])&&(t+=e[r]);if(0!==t.length)return t},e.prototype.getLengthScore=function(e){if(e){return 9*this.getLength(e)}return 0},e.prototype.getUppercaseLettersScore=function(e){var t=this;if(e){var r=0;return e.split("").forEach((function(e){t.isUppercaseLetter(e)&&r++})),0==r?0:2*(this.getLength(e)-r)}return 0},e.prototype.getLowercaseLettersScore=function(e){var t=this;if(e){var r=0;return e.split("").forEach((function(e){t.isLowercaseLetter(e)&&r++})),0==r?0:2*(this.getLength(e)-r)}return 0},e.prototype.getNumbersScore=function(e){var t=this;if(e){var r=0;return e.split("").forEach((function(e){t.isNumber(e)&&r++})),0==r?0:4*(this.getLength(e)-r)}return 0},e.prototype.getSymbolsScore=function(e){var t=this;if(e){var r=0;return e.split("").forEach((function(e){t.isSymbol(e)&&r++})),0==r?0:6*(this.getLength(e)-r)}return 0},e.prototype.getLettersOnlyScore=function(e){if(e){if(this.isLetter(e))return-1*this.getLength(e)}return 0},e.prototype.getNumbersOnlyScore=function(e){if(e){if(this.isNumber(e))return-1*this.getLength(e)}return 0},e.prototype.getConsecutiveUppercaseLettersScore=function(e){var t=this;if(e){var r=e.match(/[A-Z]+/g);if(!r)return 0;var s=0;return r.forEach((function(e){t.getLength(e)>1&&(s+=-2*(t.getLength(e)-1))})),s}return 0},e.prototype.getConsecutiveLowercaseLettersScore=function(e){var t=this;if(e){var r=e.match(/[a-z]+/g);if(!r)return 0;var s=0;return r.forEach((function(e){t.getLength(e)>1&&(s+=-2*(t.getLength(e)-1))})),s}return 0},e.prototype.getConsecutiveNumbersScore=function(e){var t=this;if(e){var r=e.match(/[0-9]+/g);if(!r)return 0;var s=0;return r.forEach((function(e){t.getLength(e)>1&&(s+=-2*(t.getLength(e)-1))})),s}return 0},e.prototype.reverseString=function(e){return e.split("").reverse().join("")},e.prototype.sequentialBuilder=function(e,t){if(e){for(var r=[],s=e.split("").length-t,n=0;n<s;n++)for(var i=0;i<s;i++)for(var o=e.substring(i,e.length),u=this.chunkString(o,n+t),a=0;a<u.length;a++)r.push(u[a]),r.push(this.reverseString(u[a]));return this.distinctArray(this.sortByLength(r,t))}return[]},e.prototype.distinctArray=function(e){for(var t=[],r=0,s=e.length;r<s;r++)-1===t.indexOf(e[r])&&""!==e[r]&&t.push(e[r]);return t},e.prototype.sortByLength=function(e,t){e.sort((function(e,t){return t.length-e.length}));for(var r=[],s=0;s<e.length;s++)t?e[s].length>=t&&r.push(e[s]):r.push(e[s]);return r},e.prototype.getSequentialLettersScore=function(e){if(e){var t=this.sequentialBuilder(this.uppercaseLetters,3),r=this.sequentialBuilder(this.lowercaseLetters,3),s=0,n=e,i=e;t.forEach((function(e){-1!=n.indexOf(e)&&(s+=e.length-2,n=n.replace(e,""))})),r.forEach((function(e){-1!=i.indexOf(e)&&(s+=e.length-2,i=i.replace(e,""))}));return-3*s}return 0},e.prototype.getSequentialNumbersScore=function(e){if(e){var t=this.sequentialBuilder(this.numbers,3),r=0,s=e;t.forEach((function(e){-1!=s.indexOf(e)&&(r+=e.length-2,s=s.replace(e,""))}));return-3*r}return 0},e.prototype.getSequentialSymbolsScore=function(e){var t=this.getSymbols(e);if(e&&t){var r=this.sequentialBuilder(t,3),s=0,n=e;r.forEach((function(e){-1!=n.indexOf(e)&&(s+=e.length-2,n=n.replace(e,""))}));return-3*s}return 0},e.prototype.getRepeatCharactersScore=function(e){if(e){var t=e.match(/(.+)(?=.*?\1)/g);if(!t)return 0;var r=this.sortByLength(t)[0].length,s=0;return r>=1&&r<=5&&(s=-8),r>=6&&r<=10&&(s=-5),r>=11&&(s=-2),s*r+(e.length-2*r)}return 0},e.prototype.getRequirementsScore=function(e,t){var r=this.requirements,s=[];if(r){var n="The minimum password length is "+r.minLength+".",i="The maximum password length is "+r.maxLength+".",o="You must use at least "+r.uppercaseLettersMinLength+" uppercase letter(s).",u="You must use at least "+r.lowercaseLettersMinLength+" lowercase letter(s).",a="You must use at least "+r.numbersMinLength+" number(s).",h="You must use at least "+r.symbolsMinLength+" symbol(s).",c="The password must start with "+r.startsWith+".",g="The password must end with "+r.endsWith+".",l="The Password must include at least one item specified ["+r.includeOne+"] .",p="You must use at least "+r.uniqueLettersMinLength+" unique letter(s).",f=(e.match(/[A-Z]/g)||[]).length,L=(e.match(/[a-z]/g)||[]).length,m=(e.match(/[0-9]/g)||[]).length,v=e.length-(f+L+m);if(r.minLength){var d=void 0,y=n;this.isIMessage(r.minLength)?(d=r.minLength.value,y=r.minLength.message):d=r.minLength,r.minLength&&e.length<d&&s.push(y)}if(r.maxLength){d=void 0,y=i;this.isIMessage(r.maxLength)?(d=r.maxLength.value,y=r.maxLength.message):d=r.maxLength,r.maxLength&&e.length>d&&s.push(y)}if(r.startsWith){d=void 0,y=c;this.isIMessage(r.startsWith)?(d=r.startsWith.value,y=r.startsWith.message):d=r.startsWith,this.startsWith(e,d)||s.push(y)}if(r.endsWith){d=void 0,y=g;this.isIMessage(r.endsWith)?(d=r.endsWith.value,y=r.endsWith.message):d=r.endsWith,this.endsWith(e,d)||s.push(y)}if(r.uppercaseLettersMinLength){d=void 0,y=o;this.isIMessage(r.uppercaseLettersMinLength)?(d=r.uppercaseLettersMinLength.value,y=r.uppercaseLettersMinLength.message):d=r.uppercaseLettersMinLength,d>f&&s.push(y)}if(r.lowercaseLettersMinLength){d=void 0,y=u;this.isIMessage(r.lowercaseLettersMinLength)?(d=r.lowercaseLettersMinLength.value,y=r.lowercaseLettersMinLength.message):d=r.lowercaseLettersMinLength,d>L&&s.push(y)}if(r.numbersMinLength){d=void 0,y=a;this.isIMessage(r.numbersMinLength)?(d=r.numbersMinLength.value,y=r.numbersMinLength.message):d=r.numbersMinLength,d>m&&s.push(y)}if(r.symbolsMinLength){d=void 0,y=h;this.isIMessage(r.symbolsMinLength)?(d=r.symbolsMinLength.value,y=r.symbolsMinLength.message):d=r.symbolsMinLength,d>v&&s.push(y)}if(r.uniqueLettersMinLength){d=void 0,y=p;this.isIMessage(r.uniqueLettersMinLength)?(d=r.uniqueLettersMinLength.value,y=r.uniqueLettersMinLength.message):d=r.uniqueLettersMinLength;var b=Array.from(new Set(e.split(""))).length>=d;r.uniqueLettersMinLength&&!b&&s.push(y)}if(r.include){d=void 0,y="The Password must include all the items specified.";this.isIMessage(r.include)?(d=r.include.value,y=r.include.message):d=r.include,this.contains(e,d)||s.push(y)}if(r.exclude){var S=e;d=void 0,y="The Password must exclude all the items specified.";this.isIMessage(r.exclude)?(d=r.exclude.value,y=r.exclude.message):d=r.exclude,t&&(S=e.toLowerCase(),d=d.map((function(e){return e.toLowerCase()}))),this.doesNotContains(S,d)||s.push(y)}if(r.blackList){S=e,d=void 0,y="Your password is in the blacklist.";this.isIMessage(r.blackList)?(d=r.blackList.value,y=r.blackList.message):d=r.blackList,t&&(S=e.toLowerCase(),d=d.map((function(e){return e.toLowerCase()}))),this.isInBlackList(S,d)&&s.push(y)}if(r.includeOne){S=e,d=void 0,y=l;this.isIMessage(r.includeOne)?(d=r.includeOne.value,y=r.includeOne.message):d=r.includeOne,t&&(S=e.toLowerCase(),d=d.map((function(e){return e.toLowerCase()}))),this.containsOne(S,d)||s.push(y)}return s}return[]},e.prototype.getResults=function(e,t,r){void 0===t&&(t=!1),void 0===r&&(r=!1);var s=[];if(e&&e.length>0){for(var n=0;n<e.length;n++)s.push(this.getResult(e[n],t,r));return s}return[]},e.prototype.getResult=function(e,t,r){if(void 0===t&&(t=!1),void 0===r&&(r=!1),e){var s=this.getRequirementsScore(e,t);if(!r&&s.length)return{score:-1,status:"needs requirement(s)",errors:s,percent:0};var n=this.getLengthScore(e)+this.getUppercaseLettersScore(e)+this.getLowercaseLettersScore(e)+this.getNumbersScore(e)+this.getSymbolsScore(e)+this.getLettersOnlyScore(e)+this.getNumbersOnlyScore(e)+this.getRepeatCharactersScore(e)+this.getConsecutiveUppercaseLettersScore(e)+this.getConsecutiveLowercaseLettersScore(e)+this.getConsecutiveNumbersScore(e)+this.getSequentialLettersScore(e)+this.getSequentialNumbersScore(e)+this.getSequentialSymbolsScore(e),i="";this.scoreRange||(this.scoreRange={40:"veryWeak",80:"weak",120:"medium",180:"strong",200:"veryStrong",_:"perfect"});var o=Object.keys(this.scoreRange).sort((function(e,t){return isNaN(e)||isNaN(t)?e>t?1:-1:e-t}));if(o.length<2)return{score:-2,status:"error",errors:'"scoreRange" must have at least two members.',percent:0};for(var u=0;u<o.length;u++){if(null!=o[u]){if(0==u&&this.between(n,1,parseFloat(o[u]))){i=this.scoreRange[o[0]];break}if(u===o.length-1){if("_"!=o[u])return{score:-2,status:"error",errors:'The last member of the "scoreRange" must be "_".',percent:0};if(this.between(n,parseFloat(o[u-1]),1e18)){i=this.scoreRange[o[o.length-1]];break}}if(this.between(n,parseFloat(o[u-1]),parseFloat(o[u]))){i=this.scoreRange[o[u]];break}}}var a=100*n/parseFloat(o[o.length-2]),h={score:n,status:i,percent:a>=100?100:a};return r&&(h=Object.assign(h,{errors:s})),h}return{score:0,status:"Empty",percent:0}},e}())}}}));
//# sourceMappingURL=index.system.min.js.map