Skip to content

Commit e0ae283

Browse files
committed
feat: export isValidPassword, align API/typings/tests, bump dependency and update README badge
- Import validation helpers from @dwtechs/checkard and implement/export isValidPassword in dist - Standardize option names (lcase/ucase/num/sym/minLen/maxLen) across tests and type definitions - Rename randomPwd env vars/defaults to PWD_RAND_* and fix internal defOpts naming - Update tests to use unified keys and import containsNumber from @dwtechs/checkard - Bump package version and @dwtechs/checkard to 3.6.0 in package-lock - Update README Jest coverage badge to 97%
1 parent 371087b commit e0ae283

File tree

6 files changed

+259
-190
lines changed

6 files changed

+259
-190
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
[![License: MIT](https://img.shields.io/npm/l/@dwtechs/passken.svg?color=brightgreen)](https://opensource.org/licenses/MIT)
33
[![npm version](https://badge.fury.io/js/%40dwtechs%2Fpassken.svg)](https://www.npmjs.com/package/@dwtechs/passken)
44
[![last version release date](https://img.shields.io/github/release-date/DWTechs/Passken.js)](https://www.npmjs.com/package/@dwtechs/passken)
5-
![Jest:coverage](https://img.shields.io/badge/Jest:coverage-95%25-brightgreen.svg)
5+
![Jest:coverage](https://img.shields.io/badge/Jest:coverage-97%25-brightgreen.svg)
66

77
- [Synopsis](#synopsis)
88
- [Support](#support)

dist/passken.js

Lines changed: 64 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ SOFTWARE.
2424
https://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

2929
const 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 };

package-lock.json

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/passken.d.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
1-
export type Options = {
2-
len: number;
3-
num: boolean;
4-
ucase: boolean;
5-
lcase: boolean;
6-
sym: boolean;
7-
strict: boolean;
8-
similarChars: boolean;
1+
export type RandomOptions = {
2+
len: number;
3+
num: boolean;
4+
ucase: boolean;
5+
lcase: boolean;
6+
sym: boolean;
7+
strict: boolean;
8+
similarChars: boolean;
9+
};
10+
export type ValidationOptions = {
11+
lcase: boolean;
12+
ucase: boolean;
13+
num: boolean;
14+
sym: boolean;
15+
maxLen: number;
16+
minLen: number;
917
};
1018

11-
declare function randomPwd(opts?: Partial<Options>): string;
19+
declare function randomPwd(opts?: Partial<RandomOptions>): string;
20+
declare function isValidPassword(s: string, opts?: Partial<ValidationOptions>, throwErr?: boolean): boolean;
1221

1322
export {
1423
randomPwd,
24+
isValidPassword,
1525
};

0 commit comments

Comments
 (0)