Skip to content

Latest commit

 

History

History
114 lines (89 loc) · 3.78 KB

RandomStringService.md

File metadata and controls

114 lines (89 loc) · 3.78 KB

RandomStringService

After imported CryptoModule in your module, you can access to RandomStringService anywhere with dependency injection.

import { CryptoModule, RandomStringService } from '@akanass/nestjsx-crypto';
import { Injectable, Module } from '@nestjs/common';

@Injectable()
class NestJSServiceWithCrypto {
    constructor(private readonly _randomStringService: RandomStringService) {}
}

@Module({
    imports: [
        CryptoModule // if you want to change openssl path you have to call `.setConfig({ pem: { pathOpenSSL: '/path/to/openssl' } })` when importing
    ],
    providers: [
        NestJSServiceWithCrypto
    ]
})
export class NestJSModuleNeedsCryptoModule {}

Table of contents

API in Detail

.generate([options])

Creates random string for given options

Parameters:

  • {GenerateOptions | number} options (optional): object or number to configure data of generation.

Response:

{RxJS.Observable} The successfully generated string.

Example:

this._randomStringService.generate()
    .subscribe(
        (s: string) => console.log(s), // Show `XwPp9xazJ0ku5CZnlmgAx2Dld8SHkAeT` in the console
        e => console.error(e.message) // Show error message in the console
    );


this._randomStringService.generate(7)
    .subscribe(
        (s: string) => console.log(s), // Show `xqm5wXX` in the console
        e => console.error(e.message) // Show error message in the console
    );


this._randomStringService.generate(
    {
        length: 12,
        charset: 'alphabetic'
    }
).subscribe(
    (s: string) => console.log(s), // Show `AqoTIzKurxJi` in the console
    e => console.error(e.message) // Show error message in the console
);


this._randomStringService.generate(
    {
        charset: 'abc'
    }
).subscribe(
    (s: string) => console.log(s), // Show `accbaabbbbcccbccccaacacbbcbbcbbc` in the console
    e => console.error(e.message) // Show error message in the console
);

Back to top

Parameters types in detail

GenerateOptions:

  • {number} length (optional): the length of the random string. (default: 32)
  • {boolean} readable (optional): exclude poorly readable chars: 0OIl. (default: false)
  • {string} charset (optional): define the character set for the string. (default: alphanumeric) (alphanumeric - [0-9 a-z A-Z], alphabetic - [a-z A-Z], numeric - [0-9], hex - [0-9 a-f], custom - any given characters)
  • {string} capitalization (optional): define whether the output should be lowercase / uppercase only. (default: null) (lowercase, uppercase)

Back to top

Change History

Back to top