-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #89 from grafana/feat/support-scenarios
Feat/support scenarios
- Loading branch information
Showing
37 changed files
with
1,291 additions
and
205 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
test/* | ||
typings/* | ||
loadtest.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
function combineExports(results) { | ||
if (results.length <= 1) { | ||
return | ||
} | ||
|
||
const functionNames = [] | ||
let defaultExported = false | ||
let nameIndex = '' | ||
|
||
for (const result of results) { | ||
// Force `false` if default has already been exported | ||
result.defaultExport = !defaultExported ? result.defaultExport : false | ||
// Once true, remains true for the remaining iterations | ||
defaultExported = defaultExported ? defaultExported : result.defaultExport | ||
const { exportAs } = result | ||
while (functionNames.includes(`${exportAs}${nameIndex}`)) { | ||
nameIndex = nameIndex === '' ? 0 : nameIndex + 1 | ||
} | ||
|
||
// Store exported name | ||
functionNames.push(`${exportAs}${nameIndex}`) | ||
|
||
// Use last stored name | ||
result.exportAs = functionNames.slice(-1)[0] | ||
} | ||
} | ||
|
||
module.exports = combineExports |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
function combineImports(results) { | ||
const [main = {}] = results | ||
const imports = { ...(main.imports || {}) } | ||
|
||
if (results.length > 1) { | ||
for (const result of results) { | ||
Object.entries(result.imports || {}).forEach(([key, value]) => { | ||
if (value === true || imports[key] === undefined) { | ||
imports[key] = value | ||
} | ||
}) | ||
} | ||
} | ||
|
||
return imports | ||
} | ||
|
||
module.exports = combineImports |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/** | ||
* Uppercase first character (capitalize) | ||
* @param {string} subject | ||
* @returns {string} | ||
*/ | ||
function capitalize(subject) { | ||
return typeof subject === 'string' && subject.length > 0 | ||
? `${subject.charAt(0).toUpperCase()}${subject.toLowerCase().slice(1)}` | ||
: '' | ||
} | ||
|
||
module.exports = capitalize |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
const capitalize = require('./capitalize') | ||
const { DEFAULT_FUNCTION_NAME } = require('../constants') | ||
|
||
/** @see https://www.w3schools.com/js/js_reserved.asp */ | ||
const javascriptReservedWords = [ | ||
'abstract', | ||
'arguments', | ||
'await', | ||
'boolean', | ||
'break', | ||
'byte', | ||
'case', | ||
'catch', | ||
'char', | ||
'class', | ||
'const', | ||
'continue', | ||
'debugger', | ||
'default', | ||
'delete', | ||
'do', | ||
'double', | ||
'else', | ||
'enum', | ||
'eval', | ||
'export', | ||
'extends', | ||
'false', | ||
'final', | ||
'finally', | ||
'float', | ||
'for', | ||
'function', | ||
'goto', | ||
'if', | ||
'implements', | ||
'import', | ||
'in', | ||
'instanceof', | ||
'int', | ||
'interface', | ||
'let', | ||
'long', | ||
'native', | ||
'new', | ||
'null', | ||
'package', | ||
'private', | ||
'protected', | ||
'public', | ||
'return', | ||
'short', | ||
'static', | ||
'super', | ||
'switch', | ||
'synchronized', | ||
'this', | ||
'throw', | ||
'throws', | ||
'transient', | ||
'true', | ||
'try', | ||
'typeof', | ||
'var', | ||
'void', | ||
'volatile', | ||
'while', | ||
'with', | ||
'yield', | ||
] | ||
|
||
const k6ReservedWords = [ | ||
// general | ||
'options', | ||
'__ITER', | ||
'__VU', | ||
'open', | ||
// k6 | ||
'sleep', | ||
'check', | ||
'group', | ||
// k6/http | ||
'http', | ||
// jslib | ||
'jsonpath', | ||
'MimeBuilder', | ||
'FormData', | ||
'URL', | ||
'URLSearchParams', | ||
] | ||
|
||
const reservedWords = [...javascriptReservedWords, ...k6ReservedWords] | ||
|
||
/** | ||
* Convert string to valid function name | ||
* @param {string} subject | ||
* @param {string} fallback Name to use if subject becomes an empty string (IMPORTANT: no validation is done for this value) | ||
* @returns {string} | ||
*/ | ||
function strToFunctionName(subject = '', fallback = DEFAULT_FUNCTION_NAME) { | ||
const words = String(subject) | ||
.normalize('NFD') | ||
// didnt dare to use unicode property escapes | ||
// mainly because of older Edge versions | ||
.replace(/[\u0300-\u036f]/g, '') | ||
// convert camelCase into "words" so that proper names are not broken | ||
.replace(/([A-Z])/g, ' $1') | ||
.split(/\s/) | ||
.map(word => word.replace(/\W/g, '')) | ||
.filter(word => word) | ||
|
||
// camelCase capitalization | ||
const [firstWord = '', ...rest] = words | ||
const result = | ||
[firstWord.toLowerCase(), ...rest.map(word => capitalize(word))].join('') || | ||
fallback | ||
|
||
return reservedWords.includes(result) ? `_${result}` : result | ||
} | ||
|
||
module.exports = strToFunctionName |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
const strToFunctionName = require('../helpers/strToFunctionName') | ||
|
||
function exportAs(value = '', result) { | ||
const strValue = String(value) | ||
if (strValue !== '') { | ||
result.defaultExport = false | ||
} | ||
|
||
result.exportAs = strToFunctionName(strValue) | ||
} | ||
|
||
module.exports = exportAs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.