From 600bb045be6e9bdc041e8a7fefd764cfbcfce701 Mon Sep 17 00:00:00 2001 From: pyoor Date: Mon, 21 May 2018 11:50:44 -0400 Subject: [PATCH] Use template literals throughout --- lib/make/colors.js | 26 +++++++++++++------------- lib/make/fonts.js | 10 +++++----- lib/make/network.js | 23 ++++++++++++----------- lib/make/typed.js | 32 ++++++++++++++++---------------- lib/make/units.js | 2 +- lib/make/uri.js | 2 +- lib/make/webgl.js | 2 +- lib/utils/common.js | 6 +++--- lib/utils/objects.js | 4 ++-- lib/utils/platform.js | 4 ++-- lib/utils/script.js | 12 ++++++------ 11 files changed, 62 insertions(+), 61 deletions(-) diff --git a/lib/make/colors.js b/lib/make/colors.js index 929a992..52d8e22 100644 --- a/lib/make/colors.js +++ b/lib/make/colors.js @@ -25,19 +25,19 @@ class colors extends make { values = [random.number(255), random.number(255), random.number(255)] } else { // Percents - values = ['%' + random.number(255), '%' + random.number(255), '%' + random.number(255)] + values = [`%${random.number(255)}`, `%${random.number(255)}`, `%${random.number(255)}`] } - return 'rgba(' + values.join(',') + ')' + return `rgba(${values.join(',')})` case 1: // Rgba functional notation values = [random.number(255), random.number(255), random.number(255), random.float()] - return 'rgba(' + values.join(',') + ')' + return `rgba(${values.join(',')})` case 2: // 4 char hex - return '#' + random.hex(4) + return `#${random.hex(4)}` default: // 8 char hex - return '#' + random.hex(8) + return `#${random.hex(8)}` } } @@ -46,18 +46,18 @@ class colors extends make { switch (random.number(4)) { case 0: - values = [random.number(255), '%' + random.number(255), '%' + random.number(255)] - return 'hsl(' + values.join(',') + ')' + values = [random.number(255), `%${random.number(255)}`, `%${random.number(255)}`] + return `hsl(${values.join(',')})` case 1: - values = [random.number(255), '%' + random.number(255), '%' + random.number(255), '%' + random.number(255)] - return 'hsl(' + values.join(',') + ')' + values = [random.number(255), `%${random.number(255)}`, `%${random.number(255)}`, `%${random.number(255)}`] + return `hsl(${values.join(',')})` case 2: opt = random.pick(['deg', 'rad', 'grad', 'turn']) - values = [random.number(255) + opt, '%' + random.number(255), '%' + random.number(255), '%' + random.number(255)] - return 'hsl(' + values.join(',') + ')' + values = [random.number(255) + opt, `%${random.number(255)}`, `%${random.number(255)}`, `%${random.number(255)}`] + return `hsl(${values.join(',')})` default: - values = [random.number(255), '%' + random.number(255), '%' + random.number(255), random.float()] - return 'hsl(' + values.join(',') + ')' + values = [random.number(255), `%${random.number(255)}`, `%${random.number(255)}`, random.float()] + return `hsl(${values.join(',')})` } } diff --git a/lib/make/fonts.js b/lib/make/fonts.js index 16ec092..84f8570 100644 --- a/lib/make/fonts.js +++ b/lib/make/fonts.js @@ -58,7 +58,7 @@ class font extends make { static family () { let s = random.pick(font.familyName) if (random.chance(8)) { - s += ', ' + random.pick(font.genericFamily) + s += `, ${random.pick(font.genericFamily)}` } return s } @@ -86,16 +86,16 @@ class font extends make { static font () { let s = '' if (random.chance(4)) { - s += random.pick(font.style) + ' ' + s += `${random.pick(font.style)} ` } if (random.chance(4)) { - s += random.pick(font.variant) + ' ' + s += `${random.pick(font.variant)} ` } if (random.chance(4)) { - s += random.pick(font.weight) + ' ' + s += `${random.pick(font.weight)} ` } if (random.chance(4)) { - s += make.number.any() + '/' + s += `${make.number.any()}/` } s += font.size() s += ' ' diff --git a/lib/make/network.js b/lib/make/network.js index a1a16aa..505d6c8 100644 --- a/lib/make/network.js +++ b/lib/make/network.js @@ -52,7 +52,7 @@ class network extends make { random.pick([0, 1, make.number.any]), ' ', random.pick(['UDP', 'TCP', 'SCTP']), - random.pick(['', '/' + random.pick(['DTLS', 'DTLS-SRTP'])]), + random.pick(['', `/${random.pick(['DTLS', 'DTLS-SRTP'])}`]), ' ', random.pick([make.number.any]), ' ', @@ -117,10 +117,11 @@ class network extends make { } static randomIPv4 () { - return random.pick([random.number(255), make.number.any]) + '.' + - random.pick([random.number(255), make.number.any]) + '.' + - random.pick([random.number(255), make.number.any]) + '.' + - random.pick([random.number(255), make.number.any]) + function octet () { + return random.pick([random.number(255), make.number.any]) + } + + return `${octet()}.${octet()}.${octet()}.${octet()}.` } static randomIPv6 () { @@ -188,7 +189,7 @@ class network extends make { '', '#', '#main-content', - () => '#' + make.text.any() + () => `#${make.text.any()}` ]) } @@ -197,19 +198,19 @@ class network extends make { '', '/', '/index.html', - () => '/' + make.text.any() + () => `/${make.text.any()}` ]) } static protocol () { - return random.pick([ + return `${random.pick([ 'http', 'https', 'ftp', 'telnet', 'chrome', 'resource' - ]) + ':' + ])}:` } static search () { @@ -217,7 +218,7 @@ class network extends make { '', '?', '?foo=bar', - () => '?' + make.text.any() + () => `?${make.text.any()}` ]) } @@ -228,7 +229,7 @@ class network extends make { let max = random.range(2, list.length) let mask = random.pick(list) for (let i = 1; i < max; i++) { - mask += '|' + random.pick(list) + mask += `|${random.pick(list)}` } return mask } diff --git a/lib/make/typed.js b/lib/make/typed.js index 5f957a6..53bc4e1 100644 --- a/lib/make/typed.js +++ b/lib/make/typed.js @@ -10,45 +10,45 @@ class typed extends make { // [-128, 127] let value = (limit !== null) ? random.number(limit) : random.number(129) value = random.chance(10) ? -value : value - return 'new Uint8Array([' + value + '])[0]' + return `new Uint8Array([${value}])[0]` } static octet (limit = null) { // [0, 255] let value = (limit !== null) ? random.number(limit) : random.number(256) - return 'new Int8Array([' + value + '])[0]' + return `new Int8Array([${value}])[0]` } static short (limit = null) { // [-32768, 32767] let value = (limit !== null) ? random.number(limit) : random.number(32769) value = random.chance(10) ? -value : value - return 'new Int16Array([' + value + '])[0]' + return `new Int16Array([${value}])[0]` } static unsignedShort (limit = null) { // [0, 65535] let value = (limit !== null) ? random.number(limit) : random.number(65535) - return 'new Uint16Array([' + value + '])[0]' + return `new Uint16Array([${value}])[0]` } static long (limit = null) { // [-2147483648, 2147483647] let value = (limit !== null) ? random.number(limit) : random.number(2147483649) value = random.chance(10) ? -value : value - return 'new Int32Array([' + value + '])[0]' + return `new Int32Array([${value}])[0]` } static unsignedLong (limit = null) { // [0, 4294967295] let value = (limit !== null) ? random.number(limit) : random.number(4294967296) - return 'new Uint32Array([' + value + '])[0]' + return `new Uint32Array([${value}])[0]` } static float (limit = null) { let base = (limit !== null) ? random.number(limit) : random.number() let value = random.chance(10) ? -(base + random.float()) : (base + random.float()) - return 'new Float32Array([' + value + '])[0]' + return `new Float32Array([${value}])[0]` } static unrestrictedFloat (limit = null) { @@ -56,14 +56,14 @@ class typed extends make { return random.pick([NaN, +Infinity, -Infinity]) } else { let base = (limit !== null) ? random.number(limit) : random.number() - return 'new Float32Array([' + (base + random.float()) + '])[0]' + return `new Float32Array([${base + random.float()}])[0]` } } static double (limit = null) { let base = (limit !== null) ? random.number(limit) : random.number() let value = random.chance(10) ? -(base + random.float()) : (base + random.float()) - return 'new Float64Array([' + value + '])[0]' + return `new Float64Array([${value}])[0]` } static unrestrictedDouble (limit = null) { @@ -72,7 +72,7 @@ class typed extends make { } else { let base = (limit !== null) ? random.number(limit) : random.number() let value = random.chance(10) ? -(base + random.float()) : (base + random.float()) - return 'new Float64Array([' + value + '])[0]' + return `new Float64Array([${value}])[0]` } } @@ -89,12 +89,12 @@ class typed extends make { static arrayBuffer (byteLength = null) { let length = (byteLength !== null) ? byteLength : typed.unsignedShort() - return 'new ArrayBuffer(' + length + ')' + return `new ArrayBuffer(${length})` } static dataView (byteLength = null) { let length = (byteLength !== null) ? byteLength : typed.unsignedShort() - return 'new DataView(' + typed.arrayBuffer(length) + ')' + return `new DataView(${typed.arrayBuffer(length)})` } static typedArray (byteLength = null) { @@ -105,14 +105,14 @@ class typed extends make { 'Int32', 'Uint32', 'Float32', 'Float64' ]) - let method = 'new ' + arrType + 'Array' + let method = `new ${arrType}Array` switch (random.number(16)) { case 0: - return method + '()' + return `${method}()` case 1: - return method + '(' + this.typedArray() + ')' + return `${method}(${this.typedArray()})` default: - return method + '(' + length + ')' + return `${method}(${length})` } } diff --git a/lib/make/units.js b/lib/make/units.js index c27151a..f9f8c3b 100644 --- a/lib/make/units.js +++ b/lib/make/units.js @@ -17,7 +17,7 @@ class unit extends make { } static percent () { - return make.number.any() + '%' + return `${make.number.any()}%` } } diff --git a/lib/make/uri.js b/lib/make/uri.js index cfa9fa3..67a1fc5 100644 --- a/lib/make/uri.js +++ b/lib/make/uri.js @@ -25,7 +25,7 @@ class uri extends make { 'data:image/png,', 'data:', 'javascript:5555', - 'javascript:"QQQQ' + String.fromCharCode(0) + 'UUUU"', + `javascript:"QQQQ${String.fromCharCode(0)}UUUU"`, 'http://a.invalid/', 'http://localhost:6/', 'https://localhost:6/', diff --git a/lib/make/webgl.js b/lib/make/webgl.js index 79b11ea..14e2400 100644 --- a/lib/make/webgl.js +++ b/lib/make/webgl.js @@ -340,7 +340,7 @@ class webgl extends make { ] if (isWebGL2) { attachment.extend([ - 'COLOR_ATTACHMENT' + random.range(0, 15) + `COLOR_ATTACHMENT${random.range(0, 15)}` ]) } return random.item(attachment) diff --git a/lib/utils/common.js b/lib/utils/common.js index 3877221..f9b3101 100644 --- a/lib/utils/common.js +++ b/lib/utils/common.js @@ -7,9 +7,9 @@ const utils = require('../utils') class common extends utils { static objToString (obj) { try { - return '' + obj + return `${obj}` } catch (e) { - return '[' + e + ']' + return `[${e}]` } } @@ -58,7 +58,7 @@ class common extends utils { // https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding#The_Unicode_Problem if (process.browser) { return decodeURIComponent(atob(str).split('').map(function (c) { - return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2) + return `%${('00' + c.charCodeAt(0).toString(16)).slice(-2)}` }).join('')) } else { return Buffer.from(str, 'base64').toString('ascii') diff --git a/lib/utils/objects.js b/lib/utils/objects.js index 5460110..ee7ef0e 100644 --- a/lib/utils/objects.js +++ b/lib/utils/objects.js @@ -30,7 +30,7 @@ class Objects { if (!(category in this.container)) { // return {type:null, name:null}; logger.traceback() - throw new Error(category + ' is not available.') + throw new Error(`${category} is not available.`) } if (last) { return this.container[category].slice(-1)[0] @@ -43,7 +43,7 @@ class Objects { return this.get(category, last).name } catch (e) { logger.traceback() - throw logger.JSError('Error: pick(\'' + category + '\') is undefined.') + throw logger.JSError(`Error: pick('${category}') is undefined.`) } } diff --git a/lib/utils/platform.js b/lib/utils/platform.js index 2905ff4..6dbca47 100644 --- a/lib/utils/platform.js +++ b/lib/utils/platform.js @@ -56,10 +56,10 @@ class platform extends utils { function platformName (candidates) { for (let i = 0; i < candidates.length; i++) { if (candidates[i] in window) { - return 'window.' + candidates[i] + return `window.${candidates[i]}` } if (candidates[i] in navigator) { - return 'navigator.' + candidates[i] + return `navigator.${candidates[i]}` } } return undefined diff --git a/lib/utils/script.js b/lib/utils/script.js index bf09ca0..6c730a1 100644 --- a/lib/utils/script.js +++ b/lib/utils/script.js @@ -29,7 +29,7 @@ class script extends utils { if (typeof (methodArgs) === 'function') { // Todo: Hmmmm.. return methodArgs() } - return objectName + '.' + methodName + script.methodHead(methodArgs) + return `${objectName}.${methodName}${script.methodHead(methodArgs)}` } static setAttribute (objectName, attributeHash) { @@ -47,7 +47,7 @@ class script extends utils { attributeValue = "'" + attributeValue + "'"; } */ - return objectName + '.' + attributeName + operator + attributeValue + ';' + return `${objectName}.${attributeName}${operator}${attributeValue};` } static makeConstraint (keys, values) { @@ -77,7 +77,7 @@ class script extends utils { } static makeLoop (s, max) { - return 'for (let i = 0; i < ' + (max || make.number.range()) + '; i++) {' + s + '}' + return `for (let i = 0; i < ${max || make.number.range()}; i++) {${s}}` } static makeArray (type, arrayLength, cb) { @@ -86,8 +86,8 @@ class script extends utils { } switch (random.number(8)) { case 0: - let src = 'function() { let buffer = new ' + type + 'Array(' + arrayLength + ');' - src += script.makeLoop('buffer[i] = ' + cb() + ';', arrayLength) + let src = `function() { let buffer = new ${type}Array(${arrayLength});` + src += script.makeLoop(`buffer[i] = ${cb()};`, arrayLength) src += 'return buffer;}()' return src case 1: @@ -98,7 +98,7 @@ class script extends utils { } static randListIndex (objName) { - return random.number() + ' % ' + o.pick(objName) + '.length' + return `${random.number()} % ${o.pick(objName)}.length` } static addElementToBody (name) {