mirror of
https://github.com/Pomax/BezierInfo-2.git
synced 2025-09-30 01:59:00 +02:00
renamed graphics-element dir
This commit is contained in:
43
docs/js/graphics-element/lib/split-code-sections.js
Normal file
43
docs/js/graphics-element/lib/split-code-sections.js
Normal file
@@ -0,0 +1,43 @@
|
||||
/**
|
||||
* Get all code that isn't contained in functions.
|
||||
* We're going to regexp our way to flawed victory here.
|
||||
*/
|
||||
export default function splitCodeSections(code) {
|
||||
// removs comments and superfluous white space.
|
||||
code = code.replace(/\\\*[\w\s\r\n]+?\*\\/, ``);
|
||||
code = code.replace(/\r?\n(\r?\n)+/, `\n`);
|
||||
|
||||
const re = /\b[\w\W][^\s]*?\([^)]*\)[\r\n\s]*{/;
|
||||
const cuts = [];
|
||||
for (let result = code.match(re); result; result = code.match(re)) {
|
||||
result = result[0];
|
||||
|
||||
let start = code.indexOf(result);
|
||||
let end = start + result.length;
|
||||
let depth = 0;
|
||||
let slice = Array.from(code).slice(start + result.length);
|
||||
|
||||
slice.some((c, pos) => {
|
||||
if (c === `{`) {
|
||||
depth++;
|
||||
return false;
|
||||
}
|
||||
if (c === `}`) {
|
||||
if (depth > 0) {
|
||||
depth--;
|
||||
return false;
|
||||
}
|
||||
end += pos + 1;
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
let cut = code.slice(start, end);
|
||||
cuts.push(cut);
|
||||
code = code.replace(cut, ``);
|
||||
}
|
||||
return {
|
||||
quasiGlobal: code,
|
||||
classCode: cuts.join(`\n`),
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user