all files / workspace/ index.js

100% Statements 26/26
75% Branches 12/16
100% Functions 1/1
100% Lines 18/18
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87                                      10× 10×     10×                 10×             10×         10×         10×         10×         10×     10×     689×     689× 689× 31332×         689×     689×       10×      
'use strict'
 
/**
 * Returns an array of random strings for fuzzing
 * @param {number} num: number of test strings to return
 * @param {number} max: maximum length of test string
 * @param {object} opt: options
 * @return {array}
 */
module.exports = function(num=10, max=20, opt={
  // Set to true to include tests with...
  none: true, // Empty string
  whitespace: true, // Various whitespace chars
  quotes: true, // Combinations of quotes
  backslashing: true, // Combinations of backslashes
  symbols: true, // Various symbols
  foreign: true, // Foreign chars
  alphanumeric: true, // Ordinary letters and numbers
} ){
  
  let chars = []
  let tests = []
  
  // Whitespace characters
  if (opt.whitespace!==false) chars = chars.concat([
    ' ', // Space
    '  ', // Tab
    '\n', // Newline
    '\r', // Return
    '\r\n', // Carrage return
  ])
  
  // Quotation characters
  if (opt.quotes!==false) chars = chars.concat([
    '\'', '\'\'', '\'\'\'', // Single quotes
    '"', '""', '"""', // Double quotes
    '`', '``', '```', // Backticks
  ])
  
  // Backslashes
  Eif (opt.backslashing!==false) chars = chars.concat([
    '\\', '\\\\',
  ])
  
  // Symbols
  Eif (opt.symbols!==false) chars = chars.concat(
    '°~!@#$%€^&*()-_─=+[]{}|;:,./<>?¿¹²³¼½¬€¶←↓→»«¢„“”·…–'.split('')
  )
  
  // Foreign characters
  Eif (opt.foreign!==false) chars = chars.concat(
    'ŧłßöäüñáóíúýéâêîôûŷàèìòùảẻỉỏỷÿïøþłĸŋđðſæµёйцукенгшщзхъэждлорпавыфячсмитьбюЁЙЦУКЕНГШЩЗХЪЭЖДЛОРПАВЫФЯЧСМИТЬБЮ'.split('')
  )
  
  // Ordinary letters and numbers
  Eif (opt.alphanumeric!==false) chars = chars.concat(
    'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'.split('')
  )
  
  // Set minimum string length
  const min = (opt.none!==false)? 0 : 1
  
  // Add tests until we have enough tests
  while (tests.length<num) {
    
    // Pick a random number from min to max
    const len = Math.floor(Math.random() * (max - min + 1)) + min
    
    // Create a string of that length
    let s = ''
    while (s.length<len) {
      s += chars[Math.floor(Math.random()*chars.length)]
    }
    
    // Make sure we didn't go over the max length
    // (some chars have multiple characters)
    while (s.length>len) s = s.substring(1)
    
    // Add that string to the tests if not already
    if (!tests.includes(s)) tests.push(s)
    
  }
  
  return tests
  
}