71 lines
1.7 KiB
JavaScript
71 lines
1.7 KiB
JavaScript
/*
|
|
function getFirstCodeLine(lines) {
|
|
function isCode(line) {
|
|
line = stripComments(stripWhitespaceFromEnds(line));
|
|
if (line.length === 0) {
|
|
return false;
|
|
}
|
|
return true;
|
|
};
|
|
|
|
let isLineCode
|
|
|
|
lines = lines.filter(isCode);
|
|
if (lines.length > 0) {
|
|
return lines[0];
|
|
}
|
|
return false;
|
|
}
|
|
*/
|
|
|
|
/**
|
|
* @param {string} source;
|
|
**/
|
|
// * @returns {{number: number, source: string, type: 'code'|'comment'|'blank'}}
|
|
// TODO: https://stackoverflow.com/questions/32295263/how-to-document-an-array-of-objects-in-jsdoc
|
|
function splitCodeFromComments(source) {
|
|
let lines = source.split(/\n/); // returns an array of lines
|
|
|
|
const isLineBlank = (l) => { return l.length === 0 ? true : false };
|
|
const isLineComment = (l) => { return stripWhitespaceFromEnds(l).startsWith(';') };
|
|
const getLineType = (l) => {
|
|
console.log('get type for ', l);
|
|
if (isLineBlank(l)) return 'blank';
|
|
if (isLineComment(l)) return 'comment';
|
|
return 'code';
|
|
}
|
|
|
|
return lines.map((line, index) => {
|
|
return {
|
|
number: index,
|
|
source: line,
|
|
type: getLineType(line)
|
|
};
|
|
});
|
|
}
|
|
|
|
console.log(splitCodeFromComments('foo \n\n; bar'));
|
|
|
|
|
|
const logArrayElements = (element, index /*, array */) => {
|
|
console.log(`a[${index}] = ${element}`);
|
|
return index;
|
|
};
|
|
|
|
// Notice that index 2 is skipped, since there is no item at
|
|
// that position in the array.
|
|
console.log([2, 5, , 9].map(logArrayElements));
|
|
|
|
const l = ['', '', '; foo', 'bar'];
|
|
|
|
// console.log(getFirstCodeLine(l));
|
|
|
|
function stripComments(line) {
|
|
return line.replace(/;.+/,"");
|
|
}
|
|
|
|
function stripWhitespaceFromEnds(line) {
|
|
line = line.replace(/^\s+/,"");
|
|
line = line.replace(/\s+$/,"");
|
|
return line;
|
|
} |