-
-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
291 additions
and
92 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
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,89 @@ | ||
'use strict' | ||
|
||
const returnFnRE = /^\s+return ([.a-zA-Z0-9]+)\(\w+\)$/ | ||
const fnRE = /^\s*function\s+/ | ||
const fnNameRE = /^\s+function ([a-zA-Z0-9_]+) \(input\) {$/ | ||
const jsonConcatRE = /^\s*json\s*\+=/ | ||
const letJsonRE = /^\s*let json =/ | ||
const returnJsonRE = /^\s*return json\s*$/ | ||
const returnEmptyStringRE = /^\s*return '' \+/ | ||
const closingCurlyBracketRE = /^\s*}\s*$/ | ||
/** | ||
* @param {Array<string>} code | ||
* @returns {Array<string>} | ||
*/ | ||
function optimize (raw) { | ||
const code = raw.split('\n') | ||
/** | ||
* @type {Array<string>} | ||
*/ | ||
const dedupedLevel1 = [] | ||
|
||
for (let i = 0; i < code.length; i++) { | ||
if (i > 0 && jsonConcatRE.test(code[i]) && jsonConcatRE.test(code[i - 1])) { | ||
const mergedEntry = code[i - 1] + ' +' + code[i].substring(code[i].indexOf('json +=') + 7) | ||
dedupedLevel1.pop() // Remove the previous entry | ||
dedupedLevel1.push(mergedEntry) | ||
} else { | ||
dedupedLevel1.push(code[i]) | ||
} | ||
} | ||
|
||
/** | ||
* @type {Array<string>} | ||
*/ | ||
const dedupedLevel2 = [] | ||
for (let i = 0; i < dedupedLevel1.length; i++) { | ||
if (i > 0 && jsonConcatRE.test(dedupedLevel1[i]) && letJsonRE.test(dedupedLevel1[i - 1])) { | ||
const mergedEntry = dedupedLevel1[i - 1] + ' +' + dedupedLevel1[i].substring(dedupedLevel1[i].indexOf('json +=') + 7) | ||
dedupedLevel2.pop() // Remove the previous entry | ||
dedupedLevel2.push(mergedEntry) | ||
} else { | ||
dedupedLevel2.push(dedupedLevel1[i]) | ||
} | ||
} | ||
|
||
/** | ||
* @type {Array<string>} | ||
*/ | ||
const dedupedLevel3 = [] | ||
for (let i = 0; i < dedupedLevel2.length; i++) { | ||
if (i > 0 && returnJsonRE.test(dedupedLevel2[i]) && letJsonRE.test(dedupedLevel2[i - 1])) { | ||
const mergedEntry = dedupedLevel2[i].slice(0, dedupedLevel2[i].indexOf('return') + 6) + dedupedLevel2[i - 1].substring(dedupedLevel2[i - 1].indexOf('let json =') + 10) | ||
dedupedLevel3.pop() // Remove the previous entry | ||
dedupedLevel3.push(mergedEntry) | ||
} else { | ||
dedupedLevel3.push(dedupedLevel2[i]) | ||
} | ||
} | ||
|
||
/** | ||
* @type {Array<string>} | ||
*/ | ||
for (let i = 0; i < dedupedLevel3.length; i++) { | ||
if (returnEmptyStringRE.test(dedupedLevel3[i])) { | ||
dedupedLevel3[i] = dedupedLevel3[i].replace('return \'\' +', 'return') | ||
} | ||
} | ||
|
||
const dedupedLevel4 = [] | ||
for (let i = 0; i < dedupedLevel3.length; i++) { | ||
if ( | ||
fnRE.test(dedupedLevel3[i]) && | ||
returnFnRE.test(dedupedLevel3[i + 1]) && | ||
closingCurlyBracketRE.test(dedupedLevel3[i + 2]) | ||
) { | ||
const serializerFnName = dedupedLevel3[i + 1].match(returnFnRE)[1] | ||
const fnName = dedupedLevel3[i].match(fnNameRE)[1] | ||
const whitespace = dedupedLevel3[i].slice(0, dedupedLevel3[i].indexOf('f')) | ||
dedupedLevel4[i] = `${whitespace}const ${fnName} = ${serializerFnName}` | ||
i += 2 | ||
} else { | ||
dedupedLevel4.push(dedupedLevel3[i]) | ||
} | ||
} | ||
|
||
return dedupedLevel4.join('\n') | ||
} | ||
|
||
module.exports = optimize |
Oops, something went wrong.