All files PasswordStrengthMeter.js

86.67% Statements 26/30
94.44% Branches 17/18
85.71% Functions 6/7
86.67% Lines 26/30
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77                                  3x 3x 3x 3x 3x       3x       3x     3x 3x 2x 10x 5x       3x 3x       3x                     6x 6x 6x 6x 6x 6x 2x   6x 6x 1x   6x                
import React from 'react';
import {shallowCopyExcept} from './utils';
 
export default class PasswordStrengthMeter extends React.Component {
    static propTypes = {
        minLength : React.PropTypes.number,          // min password length
        strengthThreshold : React.PropTypes.number.isRequired,  // lowest security level possible
        password : React.PropTypes.string.isRequired,  // password
        rules : React.PropTypes.array
    };
 
    static defaultProps = {
        minLength : 0,
        rules : [new RegExp('[a-z]'), new RegExp('[A-Z]'), new RegExp('[0-9]'), new RegExp('[\\W]'), new RegExp('.{9,}')]
    };
 
    constructor(props) {
        super();
        this.state = { level : 0, length : 0 };
        this.props = props;
        this._calculate = this._calculate.bind(this);
        this._prepare(props);
    }
 
    _prepare(props) {
        this.divProps = shallowCopyExcept({}, props, ['rules','password','strengthThreshold','minLength']);
    }
 
    _calculate(password) {
        Iif (password === undefined || password === null) {
            password = '';
        }
        let level = 0;
        if (password.length > 0 && password.length >= this.props.minLength) {
            this.props.rules.forEach((r) => {
                if (r.test(password)) {
                    level++;
                }
            });
        }
        this.setState({level : level, length : password.length });
        return level;
    }
 
    componentDidMount() {
        this._calculate(this.props.password);
    }
 
    componentWillReceiveProps(nextProps) {
        this.props = nextProps;
        this._prepare(nextProps);
        this._calculate(nextProps.password);
    }
 
 
    render() {
        const {length,level} = this.state;
        const max = this.props.rules.length + 1;
        const step = 100 / max;
        const prc = ( step * (level + (length > 0 ? 1 : 0)) )  + "%";
        let cl = 'progress-bar-danger';
        if (level >= this.props.strengthThreshold) {
            cl = 'progress-bar-warning';
        }
        let i = Math.floor((max -1 - this.props.strengthThreshold) / 2) + this.props.strengthThreshold;
        if (level == max -1 || level >= i) {
            cl = 'progress-bar-success'
        }
        return (
            <div {... this.divProps} className="password-strength-meter progress">
                <div className={`progress-bar ${cl} password-strength-meter-bar`} style={{width: prc}}>{this.props.children}</div>
            </div>
        );
    }
 
}