Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 53 additions & 35 deletions JavaScript/d-decompose.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,55 @@ const introspect = (
}
return inventory;
};
// Parsing by Ovsky Sasha
const lineCompose = (lines, signature) => {
signature.title = (lines.shift() || '').replace('//', '').trim();
lines = lines.map(
d => d.trim().replace(/^(.*) \/\//, '$1:').replace(',:', ':')
);
for (let line of lines) {
if (line.startsWith('//')) {
line = line.replace(/^\/\/ /, '').trim();
if (NAMED_LINES.find(s => line.startsWith(s))) {
const [name, comment] = section(line, ': ');
signature.comments.push({ name, comment });
} else if (signature.parameters.length === 0) {
if (signature.description.length > 0) {
signature.description += '\n';
}
signature.description += line;
} else {
const par = last(signature.parameters);
par.comment += '\n' + line;
}
} else {
const [name, text] = section(line, ': ');
let [type, comment] = section(text, ', ');
if (!ALL_TYPES.find(s => type.startsWith(s))) {
comment = type;
type = '';
}
signature.parameters.push({ name, type, comment });
}
}
};
// End of Parsing

//Find position
const findPos = str => {
const pos = FUNC_TERMS.map(indexing(str))
.filter(k => k !== -1)
.reduce((prev, cur) => (prev < cur ? prev : cur), str.length);
return pos;
};
//------------------------

// Convert to String fn
const fnToString = (method, iface) => {
const fn = iface[method];
return fn.toString();
};
// End Convertation
const badIntrospect = (
// Introspect interface
namespace // hash of interfaces
Expand All @@ -133,50 +181,19 @@ const badIntrospect = (
const methods = {};
inventory[name] = methods;
for (const method in iface) {
const fn = iface[method];
const signature = {
title: '', description: '',
parameters: [], comments: []
};
let s = fn.toString();
let pos = FUNC_TERMS.map(indexing(s))
.filter(k => k !== -1)
.reduce((prev, cur) => (prev < cur ? prev : cur), s.length);
let s = fnToString(method, iface);
let pos = findPos(s);
if (pos !== -1) {
s = s.substring(0, pos);
pos = s.indexOf('\n');
s = s.substring(pos + 1);
let lines = s.split('\n');
const lines = s.split('\n');
lines.pop();
signature.title = (lines.shift() || '').replace('//', '').trim();
lines = lines.map(
d => d.trim().replace(/^(.*) \/\//, '$1:').replace(',:', ':')
);
for (let line of lines) {
if (line.startsWith('//')) {
line = line.replace(/^\/\/ /, '').trim();
if (NAMED_LINES.find(s => line.startsWith(s))) {
const [name, comment] = section(line, ': ');
signature.comments.push({ name, comment });
} else if (signature.parameters.length === 0) {
if (signature.description.length > 0) {
signature.description += '\n';
}
signature.description += line;
} else {
const par = last(signature.parameters);
par.comment += '\n' + line;
}
} else {
const [name, text] = section(line, ': ');
let [type, comment] = section(text, ', ');
if (!ALL_TYPES.find(s => type.startsWith(s))) {
comment = type;
type = '';
}
signature.parameters.push({ name, type, comment });
}
}
lineCompose(lines, signature);
}
methods[method] = Object.assign({
method: name + '.' + method
Expand All @@ -186,6 +203,7 @@ const badIntrospect = (
return inventory;
};


const iface = { common: { merge, section } };
console.dir(introspect(iface));
console.dir(badIntrospect(iface), { depth: 5 });
Loading