interface-forge - v2.5.0
    Preparing search index...

    Class SimpleHelpersModule

    Module with various helper methods providing basic (seed-dependent) operations useful for implementing faker methods (without methods requiring localized data).

    Hierarchy (View Summary)

    Index

    Constructors

    Properties

    Methods

    • Returns random element from the given array.

      Type Parameters

      • const T

        The type of the elements to pick from.

      Parameters

      • array: readonly T[]

        The array to pick the value from.

      Returns T

      If the given array is empty.

      faker.helpers.arrayElement(['cat', 'dog', 'mouse']) // 'dog'
      

      6.3.0

    • Returns a subset with random elements of the given array in random order.

      Type Parameters

      • const T

        The type of the elements to pick from.

      Parameters

      • array: readonly T[]

        Array to pick the value from.

      • Optionalcount: number | { max: number; min: number }

        Number or range of elements to pick. When not provided, random number of elements will be picked. When value exceeds array boundaries, it will be limited to stay inside.

        • number
        • { max: number; min: number }
          • max: number

            The maximum number of elements to pick.

          • min: number

            The minimum number of elements to pick.

      Returns T[]

      faker.helpers.arrayElements(['cat', 'dog', 'mouse']) // ['mouse', 'cat']
      faker.helpers.arrayElements([1, 2, 3, 4, 5], 2) // [4, 2]
      faker.helpers.arrayElements([1, 2, 3, 4, 5], { min: 2, max: 4 }) // [3, 5, 1]

      6.3.0

    • Returns a random value from an Enum object.

      This does the same as objectValue except that it ignores (the values assigned to) the numeric keys added for TypeScript enums.

      Type Parameters

      • T extends Record<string | number, string | number>

        Type of generic enums, automatically inferred by TypeScript.

      Parameters

      • enumObject: T

        Enum to pick the value from.

      Returns T[keyof T]

      enum Color { Red, Green, Blue }
      faker.helpers.enumValue(Color) // 1 (Green)

      enum Direction { North = 'North', South = 'South'}
      faker.helpers.enumValue(Direction) // 'South'

      enum HttpStatus { Ok = 200, Created = 201, BadRequest = 400, Unauthorized = 401 }
      faker.helpers.enumValue(HttpStatus) // 200 (Ok)

      8.0.0

    • Generates a string matching the given regex like expressions.

      This function doesn't provide full support of actual RegExp. Features such as grouping, anchors and character classes are not supported. If you are looking for a library that randomly generates strings based on RegExps, see randexp.js

      Supported patterns:

      • x{times} => Repeat the x exactly times times.
      • x{min,max} => Repeat the x min to max times.
      • [x-y] => Randomly get a character between x and y (inclusive).
      • [x-y]{times} => Randomly get a character between x and y (inclusive) and repeat it times times.
      • [x-y]{min,max} => Randomly get a character between x and y (inclusive) and repeat it min to max times.
      • [^...] => Randomly get an ASCII number or letter character that is not in the given range. (e.g. [^0-9] will get a random non-numeric character).
      • [-...] => Include dashes in the range. Must be placed after the negate character ^ and before any character sets if used (e.g. [^-0-9] will not get any numeric characters or dashes).
      • /[x-y]/i => Randomly gets an uppercase or lowercase character between x and y (inclusive).
      • x? => Randomly decide to include or not include x.
      • [x-y]? => Randomly decide to include or not include characters between x and y (inclusive).
      • x* => Repeat x 0 or more times.
      • [x-y]* => Repeat characters between x and y (inclusive) 0 or more times.
      • x+ => Repeat x 1 or more times.
      • [x-y]+ => Repeat characters between x and y (inclusive) 1 or more times.
      • . => returns a wildcard ASCII character that can be any number, character or symbol. Can be combined with quantifiers as well.

      Parameters

      • pattern: string | RegExp

        The template string/RegExp to generate a matching string for.

      Returns string

      If min value is more than max value in quantifier, e.g. #{10,5}.

      If an invalid quantifier symbol is passed in.

      faker.helpers.fromRegExp('#{5}') // '#####'
      faker.helpers.fromRegExp('#{2,9}') // '#######'
      faker.helpers.fromRegExp('[1-7]') // '5'
      faker.helpers.fromRegExp('#{3}test[1-5]') // '###test3'
      faker.helpers.fromRegExp('[0-9a-dmno]') // '5'
      faker.helpers.fromRegExp('[^a-zA-Z0-8]') // '9'
      faker.helpers.fromRegExp('[a-d0-6]{2,8}') // 'a0dc45b0'
      faker.helpers.fromRegExp('[-a-z]{5}') // 'a-zab'
      faker.helpers.fromRegExp(/[A-Z0-9]{4}-[A-Z0-9]{4}/) // 'BS4G-485H'
      faker.helpers.fromRegExp(/[A-Z]{5}/i) // 'pDKfh'
      faker.helpers.fromRegExp(/.{5}/) // '14(#B'
      faker.helpers.fromRegExp(/Joh?n/) // 'Jon'
      faker.helpers.fromRegExp(/ABC*DE/) // 'ABDE'
      faker.helpers.fromRegExp(/bee+p/) // 'beeeeeeeep'

      8.0.0

    • Returns the result of the callback if the probability check was successful, otherwise undefined.

      Type Parameters

      • const TResult

        The type of result of the given callback.

      Parameters

      • callback: () => TResult

        The callback to that will be invoked if the probability check was successful.

      • Optionaloptions: { probability?: number }

        The options to use.

        • Optionalprobability?: number

          The probability ([0.00, 1.00]) of the callback being invoked.

          0.5
          

      Returns undefined | TResult

      faker.helpers.maybe(() => 'Hello World!') // 'Hello World!'
      faker.helpers.maybe(() => 'Hello World!', { probability: 0.1 }) // undefined
      faker.helpers.maybe(() => 'Hello World!', { probability: 0.9 }) // 'Hello World!'

      6.3.0

    • Generates an array containing values returned by the given method.

      Type Parameters

      • const TResult

        The type of elements.

      Parameters

      • method: (v: unknown, index: number) => TResult

        The method used to generate the values. The method will be called with (_, index), to allow using the index in the generated value e.g. as id.

      • Optionaloptions: { count?: number | { max: number; min: number } }

        The optional options object.

        • Optionalcount?: number | { max: number; min: number }

          The number or range of elements to generate.

          3
          

      Returns TResult[]

      faker.helpers.multiple(() => faker.person.firstName()) // [ 'Aniya', 'Norval', 'Dallin' ]
      faker.helpers.multiple(() => faker.person.firstName(), { count: 3 }) // [ 'Santos', 'Lavinia', 'Lavinia' ]
      faker.helpers.multiple((_, i) => `${faker.color.human()}-${i + 1}`) // [ 'orange-1', 'orchid-2', 'sky blue-3' ]

      8.0.0

    • Replaces the {{placeholder}} patterns in the given string mustache style.

      Parameters

      • text: undefined | string

        The template string to parse.

      • data: Record<string, string | Parameters<string["replace"]>[1]>

        The data used to populate the placeholders. This is a record where the key is the template placeholder, whereas the value is either a string or a function suitable for String.replace().

      Returns string

      faker.helpers.mustache('I found {{count}} instances of "{{word}}".', {
      count: () => `${faker.number.int()}`,
      word: "this word",
      }) // 'I found 57591 instances of "this word".'

      2.0.1

    • Returns a random [key, value] pair from the given object.

      Type Parameters

      • const T extends Record<string, unknown>

        The type of the object to select from.

      Parameters

      • object: T

        The object to be used.

      Returns [keyof T, T[keyof T]]

      If the given object is empty.

      faker.helpers.objectEntry({ Cheetah: 120, Falcon: 390, Snail: 0.03 }) // ['Snail', 0.03]
      

      8.0.0

    • Returns a random key from the given object.

      Type Parameters

      • const T extends Record<string, unknown>

        The type of the object to select from.

      Parameters

      • object: T

        The object to be used.

      Returns keyof T

      If the given object is empty.

      faker.helpers.objectKey({ Cheetah: 120, Falcon: 390, Snail: 0.03 }) // 'Falcon'
      

      6.3.0

    • Returns a random value from the given object.

      Type Parameters

      • const T extends Record<string, unknown>

        The type of object to select from.

      Parameters

      • object: T

        The object to be used.

      Returns T[keyof T]

      If the given object is empty.

      faker.helpers.objectValue({ Cheetah: 120, Falcon: 390, Snail: 0.03 }) // 390
      

      6.3.0

    • Helper method that converts the given number or range to a number.

      Parameters

      • numberOrRange: number | { max: number; min: number }

        The number or range to convert.

        • number
        • { max: number; min: number }
          • max: number

            The maximum value for the range.

          • min: number

            The minimum value for the range.

      Returns number

      faker.helpers.rangeToNumber(1) // 1
      faker.helpers.rangeToNumber({ min: 1, max: 10 }) // 5

      8.0.0

    • Replaces the symbols and patterns in a credit card schema including Luhn checksum.

      This method supports both range patterns [4-9] as well as the patterns used by replaceSymbolWithNumber(). L will be replaced with the appropriate Luhn checksum.

      Parameters

      • Optionalstring: string

        The credit card format pattern. Defaults to '6453-####-####-####-###L'.

      • Optionalsymbol: string

        The symbol to replace with a digit. Defaults to '#'.

      Returns string

      faker.helpers.replaceCreditCardSymbols() // '6453-4876-8626-8995-3771'
      faker.helpers.replaceCreditCardSymbols('1234-[4-9]-##!!-L') // '1234-9-5298-2'

      5.0.0

    • Parses the given string symbol by symbols and replaces the placeholder appropriately.

      • # will be replaced with a digit (0 - 9).
      • ? will be replaced with an upper letter ('A' - 'Z')
      • and * will be replaced with either a digit or letter.

      Parameters

      • Optionalstring: string

        The template string to parse. Defaults to ''.

      Returns string

      faker.helpers.replaceSymbols() // ''
      faker.helpers.replaceSymbols('#####') // '98441'
      faker.helpers.replaceSymbols('?????') // 'ZYRQQ'
      faker.helpers.replaceSymbols('*****') // '4Z3P7'
      faker.helpers.replaceSymbols('Your pin is: #?*#?*') // 'Your pin is: 0T85L1'

      3.0.0

    • Takes an array and randomizes it in place then returns it.

      Type Parameters

      • const T

        The type of the elements to shuffle.

      Parameters

      • list: T[]

        The array to shuffle.

      • options: { inplace: true }

        The options to use when shuffling.

        • inplace: true

          Whether to shuffle the array in place or return a new array.

          false
          

      Returns T[]

      faker.helpers.shuffle(['a', 'b', 'c'], { inplace: true }) // [ 'b', 'c', 'a' ]
      

      8.0.0

    • Returns a randomized version of the array.

      Type Parameters

      • const T

        The type of the elements to shuffle.

      Parameters

      • list: readonly T[]

        The array to shuffle.

      • Optionaloptions: { inplace?: false }

        The options to use when shuffling.

        • Optionalinplace?: false

          Whether to shuffle the array in place or return a new array.

          false
          

      Returns T[]

      faker.helpers.shuffle(['a', 'b', 'c']) // [ 'b', 'c', 'a' ]
      faker.helpers.shuffle(['a', 'b', 'c'], { inplace: false }) // [ 'b', 'c', 'a' ]

      2.0.1

    • Returns a randomized version of the array.

      Type Parameters

      • const T

        The type of the elements to shuffle.

      Parameters

      • list: T[]

        The array to shuffle.

      • Optionaloptions: { inplace?: boolean }

        The options to use when shuffling.

        • Optionalinplace?: boolean

          Whether to shuffle the array in place or return a new array.

          false
          

      Returns T[]

      faker.helpers.shuffle(['a', 'b', 'c']) // [ 'b', 'c', 'a' ]
      faker.helpers.shuffle(['a', 'b', 'c'], { inplace: true }) // [ 'b', 'c', 'a' ]
      faker.helpers.shuffle(['a', 'b', 'c'], { inplace: false }) // [ 'b', 'c', 'a' ]

      2.0.1

    • Slugifies the given string. For that all spaces ( ) are replaced by hyphens (-) and most non word characters except for dots and hyphens will be removed.

      Parameters

      • Optionalstring: string

        The input to slugify. Defaults to ''.

      Returns string

      faker.helpers.slugify() // ''
      faker.helpers.slugify("Hello world!") // 'Hello-world'

      2.0.1

    • Takes an array of strings or function that returns a string and outputs a unique array of strings based on that source. This method does not store the unique state between invocations.

      If there are not enough unique values to satisfy the length, if the source is an array, it will only return as many items as are in the array. If the source is a function, it will return after a maximum number of attempts has been reached.

      Type Parameters

      • const T

        The type of the elements.

      Parameters

      • source: readonly T[] | (() => T)

        The strings to choose from or a function that generates a string.

      • length: number

        The number of elements to generate.

      Returns T[]

      faker.helpers.uniqueArray(faker.word.sample, 3) // ['mob', 'junior', 'ripe']
      faker.helpers.uniqueArray(faker.definitions.person.first_name.generic, 6) // ['Silas', 'Montana', 'Lorenzo', 'Alayna', 'Aditya', 'Antone']
      faker.helpers.uniqueArray(["Hello", "World", "Goodbye"], 2) // ['World', 'Goodbye']

      6.0.0

    • Returns a weighted random element from the given array. Each element of the array should be an object with two keys weight and value.

      • Each weight key should be a number representing the probability of selecting the value, relative to the sum of the weights. Weights can be any positive float or integer.
      • Each value key should be the corresponding value.

      For example, if there are two values A and B, with weights 1 and 2 respectively, then the probability of picking A is 1/3 and the probability of picking B is 2/3.

      Type Parameters

      • const T

        The type of the elements to pick from.

      Parameters

      • array: readonly { value: T; weight: number }[]

        Array to pick the value from.

      Returns T

      faker.helpers.weightedArrayElement([{ weight: 5, value: 'sunny' }, { weight: 4, value: 'rainy' }, { weight: 1, value: 'snowy' }]) // 'sunny', 50% of the time, 'rainy' 40% of the time, 'snowy' 10% of the time
      

      8.0.0