Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Request] Add support for angular #22

Open
gopal-g opened this issue Aug 26, 2018 · 3 comments
Open

[Request] Add support for angular #22

gopal-g opened this issue Aug 26, 2018 · 3 comments

Comments

@gopal-g
Copy link

gopal-g commented Aug 26, 2018

I am not sure if this is possible , but would like to know if this can be used with Angular 6.

If yes can someone suggest me steps to use it cause i tried by creating a service and directly requiring this library but i ran into issues.

I also tried directly including this password-generator.min.js in angular.json file and accessing the function in my service that didn't work either typescript slaps a function not defined error!

If some one could help I would be really grateful. Thank you.

@tkrugg
Copy link

tkrugg commented Sep 4, 2018

angular 6 is using typescript. You should be able to just import it, are you not?
Here is a working example: https://codesandbox.io/s/jv2rv31prv

@smnbbrv
Copy link

smnbbrv commented Mar 29, 2019

Well, the complain is quite valid. If I import it 'as is' in a default Angular CLI 7 project I have an error:

ERROR in ./node_modules/password-generator/lib/password-generator.js
Module not found: Error: Can't resolve 'crypto' in '/<...>/node_modules/password-generator/lib'

@smnbbrv
Copy link

smnbbrv commented Mar 29, 2019

Luckily, the library is pretty compact, so it is very easy to convert it to a service:

import { Injectable } from '@angular/core';

declare const msCrypto: Crypto;

@Injectable({
  providedIn: 'root',
})
export class PasswordService {

  private vowel = /[aeiou]$/i;
  private consonant = /[bcdfghjklmnpqrstvwxyz]$/i;

  generate(length = 10, memorable = true, pattern = /\w/, prefix = '') {
    const validChars: string[] = [];

    if (!memorable) {
      for (let i = 33; i < 126; i++) {
        const char = String.fromCharCode(i);

        if (char.match(pattern)) {
          validChars.push(char);
        }
      }

      if (!validChars.length) {
        throw new Error('Invalid pattern');
      }
    }

    while (prefix.length < length) {
      let char: string;

      if (memorable) {
        pattern = prefix.match(this.consonant) ? this.vowel : this.consonant;
        char = String.fromCharCode(this.random(33, 126)).toLowerCase();
      } else {
        char = validChars[this.random(0, validChars.length)];
      }

      if (char.match(pattern)) {
        prefix += char;
      }
    }

    return prefix;
  }

  private random(min: number, max: number) {
    const arr = new Uint8Array(max);

    this.getRandomValues(arr);

    for (const key in arr) {
      if (arr.hasOwnProperty(key)) {
        const value = arr[key];

        if (value > min && value < max) {
          return value;
        }
      }
    }

    return this.random(min, max);
  }

  private getRandomValues(buf: Uint8Array) {
    if (crypto && crypto.getRandomValues) {
      crypto.getRandomValues(buf);
    } else if (msCrypto && msCrypto.getRandomValues) {
      msCrypto.getRandomValues(buf);
    } else {
      throw new Error('Crypto unavailable');
    }
  }

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants