Refactor text generator
This commit is contained in:
parent
b2206ba0b2
commit
21a6025481
1 changed files with 240 additions and 149 deletions
189
lib/make/text.js
189
lib/make/text.js
|
@ -7,18 +7,66 @@ const make = require('../make')
|
||||||
const utils = require('../utils')
|
const utils = require('../utils')
|
||||||
|
|
||||||
class text extends make {
|
class text extends make {
|
||||||
static lineEnd () {
|
/**
|
||||||
|
* Generate a random alphabetic character
|
||||||
|
*/
|
||||||
|
static alpha () {
|
||||||
|
return String.fromCharCode(random.range('A'.charCodeAt(0), 'z'.charCodeAt(0)))
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate a random alphanumeric character
|
||||||
|
*/
|
||||||
|
static alphanum () {
|
||||||
|
return String.fromCharCode(random.range('0'.charCodeAt(0), 'z'.charCodeAt(0)))
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate a random assignment operator
|
||||||
|
*/
|
||||||
|
static assignmentOperator () {
|
||||||
return random.pick([
|
return random.pick([
|
||||||
'\n', '\r', '\r\n', '\n\r'
|
'=', '+=', '-=', '*=', '/=', '%=', '**=', '<<=', '>>=', '>>>=', '&=', '^=', '|='
|
||||||
])
|
])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate a random arithmetic operator
|
||||||
|
*/
|
||||||
|
static arithmeticOperator () {
|
||||||
|
return random.pick([
|
||||||
|
'%', '-', '+', '*', '/'
|
||||||
|
])
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate a random control character
|
||||||
|
*/
|
||||||
static controlChar () {
|
static controlChar () {
|
||||||
return random.pick([
|
return random.pick([
|
||||||
'\b', '\t', '\n', '\v', '\f', '\r', '\0', '\c', '\a', '\e' // eslint-disable-line no-useless-escape
|
'\b', '\t', '\n', '\v', '\f', '\r', '\0', '\c', '\a', '\e' // eslint-disable-line no-useless-escape
|
||||||
])
|
])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate a random digit
|
||||||
|
*/
|
||||||
|
static digit () {
|
||||||
|
return String.fromCharCode(random.range('0'.charCodeAt(0), '9'.charCodeAt(0)))
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate a random line ending
|
||||||
|
*/
|
||||||
|
static lineEnd () {
|
||||||
|
return random.pick([
|
||||||
|
'\n', '\r', '\r\n', '\n\r'
|
||||||
|
])
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate a random token
|
||||||
|
*/
|
||||||
static token () {
|
static token () {
|
||||||
return random.pick([
|
return random.pick([
|
||||||
'*', '+', '%', '-', '!', '^', ':', '|', '&', '<', '>', '.', '"',
|
'*', '+', '%', '-', '!', '^', ':', '|', '&', '<', '>', '.', '"',
|
||||||
|
@ -35,9 +83,9 @@ class text extends make {
|
||||||
static language () {
|
static language () {
|
||||||
return random.pick([
|
return random.pick([
|
||||||
// special casing for i, I, dotted/dotless variants
|
// special casing for i, I, dotted/dotless variants
|
||||||
['tr', 'az'],
|
['tr', 'az', 'crh', 'tt', 'ba'],
|
||||||
// special casing rules: https://developer.mozilla.org/en/CSS/text-transform
|
// special casing rules: https://developer.mozilla.org/en/CSS/text-transform
|
||||||
['nl', 'gr'],
|
['nl', 'el', 'ga'],
|
||||||
// special justification rules
|
// special justification rules
|
||||||
['ja', 'zh'],
|
['ja', 'zh'],
|
||||||
// tend to be RTL
|
// tend to be RTL
|
||||||
|
@ -49,8 +97,12 @@ class text extends make {
|
||||||
])
|
])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate a random character that may affect layout
|
||||||
|
*/
|
||||||
static layoutCharCodes () {
|
static layoutCharCodes () {
|
||||||
return random.pick([
|
return String.fromCodePoint(
|
||||||
|
random.pick([
|
||||||
0, // null
|
0, // null
|
||||||
160, // non-breaking space
|
160, // non-breaking space
|
||||||
0x005C, // backslash, but in some countries, represents local currency symbol (e.g. yen)
|
0x005C, // backslash, but in some countries, represents local currency symbol (e.g. yen)
|
||||||
|
@ -116,10 +168,15 @@ class text extends make {
|
||||||
0x2064, // mathematical invisible separator
|
0x2064, // mathematical invisible separator
|
||||||
0x2044 // fraction slash character
|
0x2044 // fraction slash character
|
||||||
])
|
])
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate a random character that affects bidi layout
|
||||||
|
*/
|
||||||
static bidiCharCodes () {
|
static bidiCharCodes () {
|
||||||
return random.pick([
|
return String.fromCodePoint(
|
||||||
|
random.pick([
|
||||||
0x0660, // START_HINDI_DIGITS
|
0x0660, // START_HINDI_DIGITS
|
||||||
0x0669, // END_HINDI_DIGITS
|
0x0669, // END_HINDI_DIGITS
|
||||||
0x066A, // START_ARABIC_SEPARATOR
|
0x066A, // START_ARABIC_SEPARATOR
|
||||||
|
@ -129,11 +186,17 @@ class text extends make {
|
||||||
0x06f0, // START_FARSI_DIGITS
|
0x06f0, // START_FARSI_DIGITS
|
||||||
0x06f9 // END_FARSI_DIGITS
|
0x06f9 // END_FARSI_DIGITS
|
||||||
])
|
])
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// http://www.unicode.org/Public/6.0.0/ucd/UnicodeData.txt
|
/**
|
||||||
static unicodeCombiningCharacters () {
|
* Generate a random unicode combining character
|
||||||
return random.item([
|
* http://www.unicode.org/Public/6.0.0/ucd/UnicodeData.txt
|
||||||
|
*/
|
||||||
|
static unicodeCombiningCharacter () {
|
||||||
|
return String.fromCodePoint(
|
||||||
|
random.range(
|
||||||
|
...random.item([
|
||||||
[0x0300, 0x036F], // Combining Diacritical Marks
|
[0x0300, 0x036F], // Combining Diacritical Marks
|
||||||
[0x0483, 0x0489],
|
[0x0483, 0x0489],
|
||||||
[0x07EB, 0x07F3],
|
[0x07EB, 0x07F3],
|
||||||
|
@ -153,51 +216,57 @@ class text extends make {
|
||||||
[0x1D1AA, 0x1D1AD],
|
[0x1D1AA, 0x1D1AD],
|
||||||
[0x1D242, 0x1D244]
|
[0x1D242, 0x1D244]
|
||||||
])
|
])
|
||||||
|
)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate a random basic multilingual plane character
|
||||||
|
*/
|
||||||
static unicodeBMP () {
|
static unicodeBMP () {
|
||||||
return random.item([
|
return String.fromCodePoint(
|
||||||
// BMP = Basic Multilingual Plane
|
random.range(0x0000, 0xFFFF)
|
||||||
[0x0000, 0xFFFF]
|
)
|
||||||
])
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate a random supplementary multilingual plane character
|
||||||
|
*/
|
||||||
static unicodeSMP () {
|
static unicodeSMP () {
|
||||||
return random.item([
|
return String.fromCodePoint(
|
||||||
// SMP = Supplementary Multilingual Plane
|
random.range(
|
||||||
|
...random.item([
|
||||||
[0x10000, 0x13FFF],
|
[0x10000, 0x13FFF],
|
||||||
[0x16000, 0x16FFF],
|
[0x16000, 0x16FFF],
|
||||||
[0x1B000, 0x1BFFF],
|
[0x1B000, 0x1BFFF],
|
||||||
[0x1D000, 0x1DFFF],
|
[0x1D000, 0x1DFFF],
|
||||||
[0x1F000, 0x1FFFF]
|
[0x1F000, 0x1FFFF]
|
||||||
])
|
])
|
||||||
|
)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate a random supplementary ideographic plane character
|
||||||
|
*/
|
||||||
static unicodeSIP () {
|
static unicodeSIP () {
|
||||||
return random.item([
|
return String.fromCodePoint(
|
||||||
// SIP = Supplementary Ideographic Plane
|
random.range(
|
||||||
|
...random.item([
|
||||||
[0x20000, 0x2BFFF],
|
[0x20000, 0x2BFFF],
|
||||||
[0x2F000, 0x2FFFF]
|
[0x2F000, 0x2FFFF]
|
||||||
])
|
])
|
||||||
|
)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate a random supplementary special-purpose plane character
|
||||||
|
*/
|
||||||
static unicodeSSP () {
|
static unicodeSSP () {
|
||||||
return random.item([
|
return String.fromCodePoint(
|
||||||
// SSP = Supplementary Special-purpose Plane
|
random.range(0xE0000, 0xE0FFF)
|
||||||
[0xE0000, 0xE0FFF]
|
)
|
||||||
])
|
|
||||||
}
|
|
||||||
|
|
||||||
static assignmentOperator () {
|
|
||||||
return random.pick([
|
|
||||||
'=', '+=', '-=', '*=', '/=', '%=', '**=', '<<=', '>>=', '>>>=', '&=', '^=', '|='
|
|
||||||
])
|
|
||||||
}
|
|
||||||
|
|
||||||
static arithmeticOperator () {
|
|
||||||
return random.pick([
|
|
||||||
'%', '-', '+', '*', '/'
|
|
||||||
])
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static currency () {
|
static currency () {
|
||||||
|
@ -222,26 +291,48 @@ class text extends make {
|
||||||
return utils.common.quote(text.any())
|
return utils.common.quote(text.any())
|
||||||
}
|
}
|
||||||
|
|
||||||
static chars () {
|
/**
|
||||||
return random.pick([
|
* Wrapper for all text generators
|
||||||
text.controlChar,
|
* @returns {string}
|
||||||
text.token,
|
*/
|
||||||
text.assignmentOperator,
|
static random () {
|
||||||
text.arithmeticOperator,
|
return random.choose([
|
||||||
String.fromCharCode(text.layoutCharCodes()),
|
[1, text.alpha],
|
||||||
String.fromCharCode(text.bidiCharCodes())
|
[1, text.alphanum],
|
||||||
|
[1, text.arithmeticOperator],
|
||||||
|
[1, text.assignmentOperator],
|
||||||
|
[1, text.controlChar],
|
||||||
|
[1, text.digit],
|
||||||
|
[1, text.lineEnd],
|
||||||
|
[1, text.token],
|
||||||
|
[3, text.layoutCharCodes],
|
||||||
|
[3, text.bidiCharCodes],
|
||||||
|
[3, text.unicodeCombiningCharacter],
|
||||||
|
[3, text.unicodeBMP],
|
||||||
|
[3, text.unicodeSMP],
|
||||||
|
[3, text.unicodeSIP],
|
||||||
|
[3, text.unicodeSSP]
|
||||||
])
|
])
|
||||||
}
|
}
|
||||||
|
|
||||||
static any () {
|
/**
|
||||||
// Generate a string compromised of random individual characters
|
* Generate a single character
|
||||||
// This might be too slow to used for all 'texts' uses
|
*/
|
||||||
let s = ''
|
static character () {
|
||||||
// TODO: Len calculation take from DOMFuzz - maybe we should revise this?
|
return text.random().charAt(0)
|
||||||
let len = random.pick([make.number.tiny, make.number.range])
|
|
||||||
for (let i = 0; i < len; i++) {
|
|
||||||
s += make.text.chars()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate string comprised of random generators
|
||||||
|
*/
|
||||||
|
static any () {
|
||||||
|
let s = ''
|
||||||
|
let len = random.range(1, 126)
|
||||||
|
|
||||||
|
while (len--) {
|
||||||
|
s += make.text.random()
|
||||||
|
}
|
||||||
|
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue