GithubNPM
Options
All
  • Public
  • Public/Protected
  • All
Menu

Package containing various operators for generating Observable with random values in RxJS

Random Module

Variables

RND_STR_DEFAULTS: FromRandomStringOpts = ...

The default options for fromRandomStr Contains 4 boolean properties that can be set for the string generator

  • caps (default: true) - Provide all Latin upper-case characters
  • lower (default: true) - Provide all Latin lower-case characters
  • number (default: true) - Provide all Latin number characters
  • special (default: false) - Provide additional ASCII non-letter or number characters

Random Numbers Functions

  • fromRandom(min?: number, max?: number, emitDelay?: number): Observable<number>
  • Returns an Observable that emits random numbers generated using Math.random. The values are generated between the passed min and max range.

    example

    Return random number between 0 and 10

    fromRandom(0, 10).subscribe();
    

    Output: 8.332405921423248, 7.9304238877750315, 4.603376889484552...

    Parameters

    • min: number = 0

      The minimum value to create a random number between. Default is 0.

    • max: number = 1

      The maximum value to create a random number between. Default is 1.

    • emitDelay: number = 0

      Optionally emit each value at the set delay, otherwise emit immediately

    Returns Observable < number >

    Observable that emits a random number between the min and max

  • Returns an Observable that emits a number generated using Crypto.getRandomValues, and using Math.random selects one random number value from the TypedArray.

    By default this Observable will generate 4-bit signed values

    see

    Typed Array Views

    example

    Emits a stream of random 4-byte numbers

    fromRandomCrypto().subscribe();
    

    Output: 1228475997, 258463200, 850749141, 3060206219...

    example

    Emits a stream of random 1-byte every second

    fromRandomCrypto(1000, { bytes: 1 }).subscribe();
    

    Output: 15, 8, 234, 12...

    Parameters

    • emitDelay: number = 0

      Optionally emit each value at the set delay, otherwise emit immediately

    • opts: FromRandomCryptoOpts = ...

      Optional options for the Observable to generate different byte lengths and signed numbers

    Returns Observable < number >

    Observable with a stream of random numbers

  • fromRandomInt(min?: number, max?: number, emitDelay?: number): Observable<number>
  • Returns an Observable that emits random integer numbers generated using Math.random. The values are generated between the passed min and max range.

    example

    Return random number between 0 and 100

    fromRandomInt().subscribe();
    

    Output: 7, 94, 14, 12, 86...

    Parameters

    • min: number = 0

      The minimum value to create a random number between. Default is 0.

    • max: number = 100

      The maximum value to create a random number between. Default is 100.

    • emitDelay: number = 0

      Optionally emit each value at the set delay, otherwise emit immediately

    Returns Observable < number >

    Observable that emits a random integer number between the min and max

Random Strings Functions

  • fromRandomStr(length?: number, emitDelay?: number, opts?: FromRandomStringOpts): Observable<string>
  • Returns an Observable that emits a string generated at random.

    By default the string will be made up of upper and lower case characters, and numbers. For special characters pass a FromRandomStringOpts options object

    example

    Emit a random string of length 8

    fromRandomStr(8).subscribe();
    

    Output: 'A3gtYb76', '0br2YgT6', 'TL184Avf'...

    example

    Emit a random string of length 8 with special characters every second

    const options = { caps: true, lower: true, number: true, special: true };
    fromRandomStr(8, 1000, options).subscribe();
    

    Output: 'A3g!^b76', '0br2£gT6', 'TL1!4Av$'...

    Parameters

    • length: number = 10

      The length of the string to produce

    • emitDelay: number = 0

      Optionally emit each value at the set delay, otherwise emit immediately

    • opts: FromRandomStringOpts = ...

      Additional options to include other characters and numbers

    Returns Observable < string >

    Observable that emits a string

  • fromUUIDv4(emitTime?: number): Observable<string>
  • Returns an Observable that emits a UUID v4, optionally with emitTime otherwise it will generate them per tick.

    remarks

    This operator requires Crypto to work

    example

    Get a random UUID

    fromUUIDv4().pipe(take(1)).subscribe()
    

    Output: 2a6d71bf-6ccd-4810-bc60-c9ffdedf8864

    Parameters

    • emitTime: number = 0

      Optional time in ms to emit the UUID

    Returns Observable < string >

    Observable that emits a UUIDv4

Generated using TypeDoc, the 08/02/2021 at 13:47:36