@@ -24,7 +24,7 @@ SOFTWARE.
2424https://github.com/DWTechs/Passken.js
2525*/
2626
27- import { isValidInteger , isBoolean } from '@dwtechs/checkard' ;
27+ import { isValidInteger , isBoolean , containsLowerCase , containsUpperCase , containsNumber , containsSpecialCharacter } from '@dwtechs/checkard' ;
2828
2929const list = {
3030 lcase : 'abcdefghijklmnopqrstuvwxyz' ,
@@ -35,24 +35,24 @@ const list = {
3535 snum : '23456789' ,
3636 sym : '!@#%*_-+=:?><./()' ,
3737} ;
38- const { PWD_AUTO_LENGTH , PWD_AUTO_NUMBERS , PWD_AUTO_UPPERCASE , PWD_AUTO_LOWERCASE , PWD_AUTO_SYMBOLS , PWD_AUTO_STRICT , PWD_AUTO_SIMILAR_CHARS , } = process === null || process === void 0 ? void 0 : process . env ;
39- const defOpts = {
40- len : PWD_AUTO_LENGTH || 12 ,
41- num : PWD_AUTO_NUMBERS || true ,
42- ucase : PWD_AUTO_UPPERCASE || true ,
43- lcase : PWD_AUTO_LOWERCASE || true ,
44- sym : PWD_AUTO_SYMBOLS || false ,
45- strict : PWD_AUTO_STRICT || true ,
46- similarChars : PWD_AUTO_SIMILAR_CHARS || false ,
38+ const { PWD_RAND_LENGTH , PWD_RAND_NUMBERS , PWD_RAND_UPPERCASE , PWD_RAND_LOWERCASE , PWD_RAND_SYMBOLS , PWD_RAND_STRICT , PWD_RAND_SIMILAR_CHARS , } = process === null || process === void 0 ? void 0 : process . env ;
39+ const defOpts$1 = {
40+ len : PWD_RAND_LENGTH || 12 ,
41+ num : PWD_RAND_NUMBERS || true ,
42+ ucase : PWD_RAND_UPPERCASE || true ,
43+ lcase : PWD_RAND_LOWERCASE || true ,
44+ sym : PWD_RAND_SYMBOLS || false ,
45+ strict : PWD_RAND_STRICT || true ,
46+ similarChars : PWD_RAND_SIMILAR_CHARS || false ,
4747} ;
48- function create ( opts = defOpts ) {
49- const len = opts . len && isValidInteger ( opts . len , 12 , 64 , true ) ? opts . len : defOpts . len ;
50- const num = isBoolean ( opts . num ) ? opts . num : defOpts . num ;
51- const ucase = isBoolean ( opts . ucase ) ? opts . ucase : defOpts . ucase ;
52- const sym = isBoolean ( opts . sym ) ? opts . sym : defOpts . sym ;
53- const strict = isBoolean ( opts . strict ) ? opts . strict : defOpts . strict ;
54- const similarChars = opts . similarChars ? opts . similarChars : defOpts . similarChars ;
55- let lcase = isBoolean ( opts . lcase ) ? opts . lcase : defOpts . lcase ;
48+ function create ( opts = defOpts$1 ) {
49+ const len = opts . len && isValidInteger ( opts . len , 12 , 64 , true ) ? opts . len : defOpts$1 . len ;
50+ const num = isBoolean ( opts . num ) ? opts . num : defOpts$1 . num ;
51+ const ucase = isBoolean ( opts . ucase ) ? opts . ucase : defOpts$1 . ucase ;
52+ const sym = isBoolean ( opts . sym ) ? opts . sym : defOpts$1 . sym ;
53+ const strict = isBoolean ( opts . strict ) ? opts . strict : defOpts$1 . strict ;
54+ const similarChars = opts . similarChars ? opts . similarChars : defOpts$1 . similarChars ;
55+ let lcase = isBoolean ( opts . lcase ) ? opts . lcase : defOpts$1 . lcase ;
5656 if ( ! lcase && ! num && ! ucase && ! sym )
5757 lcase = true ;
5858 const chars = [ ] ;
@@ -95,4 +95,49 @@ function shuffleArray(a) {
9595 return a ;
9696}
9797
98- export { create as randomPwd } ;
98+ function throwError ( expectedType , actualValue , causedBy ) {
99+ const c = '' ;
100+ throw new Error ( `Passken: Expected ${ expectedType } , but received ${ typeof actualValue } : ${ String ( actualValue ) } ${ c } ` ) ;
101+ }
102+
103+ const { PWD_VALID_MINLENGTH , PWD_VALID_MAXLENGTH , PWD_VALID_NUMBERS , PWD_VALID_UPPERCASE , PWD_VALID_LOWERCASE , PWD_VALID_SYMBOLS , } = process === null || process === void 0 ? void 0 : process . env ;
104+ const defOpts = {
105+ lcase : PWD_VALID_LOWERCASE || true ,
106+ ucase : PWD_VALID_UPPERCASE || true ,
107+ num : PWD_VALID_NUMBERS || true ,
108+ sym : PWD_VALID_SYMBOLS || true ,
109+ minLen : PWD_VALID_MINLENGTH || 12 ,
110+ maxLen : PWD_VALID_MAXLENGTH || 64 ,
111+ } ;
112+ function isValidPassword ( s , opts = defOpts , throwErr = false ) {
113+ const o = Object . assign ( Object . assign ( { } , defOpts ) , opts ) ;
114+ const l = s . length ;
115+ if ( ! ( l >= o . minLen && l <= o . maxLen ) ) {
116+ if ( throwErr )
117+ throwError ( `password with length in range [${ o . minLen } , ${ o . maxLen } ] (actual length: ${ l } )` , s ) ;
118+ return false ;
119+ }
120+ if ( o . lcase && ! containsLowerCase ( s ) ) {
121+ if ( throwErr )
122+ throwError ( 'password containing lowercase letters' , s ) ;
123+ return false ;
124+ }
125+ if ( o . ucase && ! containsUpperCase ( s ) ) {
126+ if ( throwErr )
127+ throwError ( 'password containing uppercase letters' , s ) ;
128+ return false ;
129+ }
130+ if ( o . num && ! containsNumber ( s , 1 , null ) ) {
131+ if ( throwErr )
132+ throwError ( 'password containing numbers' , s ) ;
133+ return false ;
134+ }
135+ if ( o . sym && ! containsSpecialCharacter ( s ) ) {
136+ if ( throwErr )
137+ throwError ( 'password containing special characters' , s ) ;
138+ return false ;
139+ }
140+ return true ;
141+ }
142+
143+ export { isValidPassword , create as randomPwd } ;
0 commit comments