Home Reference Source

lib/make/arrays.js

/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

const make = require('../make')
const random = require('../random')

class arrays extends make {
  /**
   * Returns an array containing random values generated by the supplied function
   * @param {Function} fn - Function used to generate values
   * @param {number} limit - Length of the array
   * @returns {Array}
   */
  static filledArray (fn, limit = make.number.tiny()) {
    const array = []

    for (let i = 0; i < limit; i++) {
      const value = random.pick(fn)
      if (value !== null) {
        array.push(value)
      }
    }

    return array
  }
}

module.exports = arrays