-
Notifications
You must be signed in to change notification settings - Fork 57
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
Comments
angular 6 is using typescript. You should be able to just import it, are you not? |
Well, the complain is quite valid. If I import it 'as is' in a default Angular CLI 7 project I have an error:
|
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');
}
}
} |
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.
The text was updated successfully, but these errors were encountered: