Skip to content

Commit

Permalink
Merge pull request #45 from deanilvincent/releases/v2.0.3
Browse files Browse the repository at this point in the history
Releases/v2.0.3
  • Loading branch information
deanilvincent authored Apr 25, 2021
2 parents 0dac380 + be98dab commit c3aa7f0
Show file tree
Hide file tree
Showing 8 changed files with 1,239 additions and 27 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ A simple way to check that password strength of a certain passphrase. A password

## Setup & Basic Usage
```
const passwordStrength = require('check-password-strength')
const { passwordStrength } = require('check-password-strength')
// OR
import { passwordStrength } from 'check-password-strength'
console.log(passwordStrength('asdfasdf').value)
// Too weak (It will return Too weak if the value doesn't match the RegEx conditions)
Expand Down Expand Up @@ -125,7 +127,6 @@ The minDiversity and minLength parameters of the first element cannot be overrid
```
passwordStrength('myPassword', yourCustomOptions)
```

### RegEx

**Strong**
Expand All @@ -151,6 +152,9 @@ passwordStrength('myPassword', yourCustomOptions)
| (?=.{8,}) | The string must be eight characters or longer for Medium strength |
| (?=.{6,}) | Mininum of 6 characters for Weak strength |

## TypeScript type declarations ☑
Available starting version `v2.0.3` and above. (Thanks to [@Mesoptier!](https://github.com/Mesoptier))

## Other resources

##### For .NET Project
Expand Down
22 changes: 22 additions & 0 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Node.js
# Build a general Node.js project with npm.
# Add steps that analyze code, save build artifacts, deploy, and more:
# https://docs.microsoft.com/azure/devops/pipelines/languages/javascript

trigger:
- master
- releases/*

pool:
vmImage: 'ubuntu-latest'

steps:
- task: NodeTool@0
inputs:
versionSpec: '10.x'
displayName: 'Install Node.js'

- script: |
npm install
npm test
displayName: 'npm install and test'
30 changes: 30 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
export interface Option<V> {
id: number;
value: V;
minDiversity: number;
minLength: number;
}

export interface FirstOption<V> extends Option<V> {
minDiversity: 0;
minLength: 0;
}

export type Options<V> = [FirstOption<V>, ...Option<V>[]];

export const defaultOptions: Options<string>;

export type DiversityType = "lowercase" | "uppercase" | "symbol" | "number";

export interface Result<V> {
id: number;
value: V;
contains: DiversityType[];
length: number;
}

export function passwordStrength<V = string>(
password: string,
options?: Options<V>,
allowedSymbols?: string,
): Result<V>;
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ const defaultOptions = [
}
]

const escapeRegExp = (string) => string.replace(/[-.*+?^${}()|[\]\\]/g, '\\$&')

const passwordStrength = (password, options = defaultOptions, allowedSymbols="!@#$%^&*") => {

let passwordCopy = password || ''
Expand All @@ -49,7 +51,7 @@ const passwordStrength = (password, options = defaultOptions, allowedSymbols="!@

if (allowedSymbols) {
rules.push({
regex: `[${allowedSymbols}]`,
regex: `[${escapeRegExp(allowedSymbols)}]`,
message: 'symbol'
})
}
Expand Down
68 changes: 68 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { expectType } from "tsd";
import { DiversityType, passwordStrength } from "./index";

// Test result types
expectType<string>(passwordStrength("asdfasdf").value);
expectType<number>(passwordStrength("asdfasdf").id);
expectType<number>(passwordStrength("asdfasdf").length);
expectType<DiversityType[]>(passwordStrength("asdfasdf").contains);

// Test options with custom value (string)
expectType<string>(
passwordStrength("asdfasdf", [
{
id: 0,
value: "Too weak",
minDiversity: 0,
minLength: 0,
},
{
id: 1,
value: "Weak",
minDiversity: 2,
minLength: 6,
},
{
id: 2,
value: "Medium",
minDiversity: 4,
minLength: 8,
},
{
id: 3,
value: "Strong",
minDiversity: 4,
minLength: 10,
},
]).value
);

// Test options with custom value (object)
expectType<{ message: string, color: string }>(
passwordStrength("asdfasdf", [
{
id: 0,
value: {message: "Too weak", color: "red"},
minDiversity: 0,
minLength: 0,
},
{
id: 1,
value: {message: "Weak", color: "orange"},
minDiversity: 2,
minLength: 6,
},
{
id: 2,
value: {message: "Medium", color: "yellow"},
minDiversity: 4,
minLength: 8,
},
{
id: 3,
value: {message: "Strong", color: "green"},
minDiversity: 4,
minLength: 10,
},
]).value
);
20 changes: 20 additions & 0 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,23 @@ it("[overridden allowedSymbols] Should contains symbols if the password have one
expect(contains).toEqual(expect.not.arrayContaining(['number']));
expect(contains).toEqual(expect.arrayContaining(['symbol']));
});

it("[overridden allowedSymbols] Should contains symbols if allowedSymbol is a special regex char", () => {
expect(app("[", undefined, '[]').contains).toEqual(expect.arrayContaining(['symbol']));
expect(app("]", undefined, '[]').contains).toEqual(expect.arrayContaining(['symbol']));
expect(app("(", undefined, '()').contains).toEqual(expect.arrayContaining(['symbol']));
expect(app(")", undefined, '()').contains).toEqual(expect.arrayContaining(['symbol']));
expect(app("{", undefined, '{}').contains).toEqual(expect.arrayContaining(['symbol']));
expect(app("}", undefined, '{}').contains).toEqual(expect.arrayContaining(['symbol']));
expect(app("*", undefined, '*').contains).toEqual(expect.arrayContaining(['symbol']));
expect(app("^", undefined, '^').contains).toEqual(expect.arrayContaining(['symbol']));
expect(app("/", undefined, '/').contains).toEqual(expect.arrayContaining(['symbol']));
expect(app("\\", undefined, '\\').contains).toEqual(expect.arrayContaining(['symbol']));
expect(app("-", undefined, '-').contains).toEqual(expect.arrayContaining(['symbol']));
expect(app("=", undefined, '=').contains).toEqual(expect.arrayContaining(['symbol']));
expect(app("?", undefined, '?').contains).toEqual(expect.arrayContaining(['symbol']));
expect(app("+", undefined, '+').contains).toEqual(expect.arrayContaining(['symbol']));
expect(app(".", undefined, '.').contains).toEqual(expect.arrayContaining(['symbol']));
expect(app("$", undefined, '$').contains).toEqual(expect.arrayContaining(['symbol']));
expect(app("[](){}*^/\\-=?+.$", undefined, '[](){}*^/\\-=?+.$').contains).toEqual(expect.arrayContaining(['symbol']));
});
Loading

0 comments on commit c3aa7f0

Please sign in to comment.