Add and use the 'intersperse' helper function.

This commit is contained in:
Daniel 2020-05-11 22:14:45 +00:00
parent 74d6d04d70
commit e9867a7353
2 changed files with 19 additions and 9 deletions

View file

@ -129,6 +129,22 @@ namespace ts {
return map;
}
/**
* Creates a new array with `element` interspersed in between each element of `input`
* if there is more than 1 value in `input`. Otherwise, returns the existing array.
*/
export function intersperse<T>(input: T[], element: T): T[] {
if (input.length <= 1) {
return input;
}
const result: T[] = [];
for (let i = 0, n = input.length; i < n; i++) {
if (i) result.push(element);
result.push(input[i]);
}
return result;
}
/**
* Iterates through `array` by index and performs the callback on each element of array until the callback
* returns a falsey value, then returns false.

View file

@ -89,20 +89,14 @@ namespace ts.JsDoc {
// Eg. const a: Array<string> | Array<number>; a.length
// The property length will have two declarations of property length coming
// from Array<T> - Array<string> and Array<number>
const documentationComment: SymbolDisplayPart[] = [];
const documentationComment: string[] = [];
forEachUnique(declarations, declaration => {
for (const { comment } of getCommentHavingNodes(declaration)) {
if (comment === undefined) continue;
const commentTextPart = textPart(comment);
if (!contains(documentationComment, commentTextPart)) {
if (documentationComment.length) {
documentationComment.push(lineBreakPart());
}
documentationComment.push(commentTextPart);
}
pushIfUnique(documentationComment, comment);
}
});
return documentationComment;
return intersperse(map(documentationComment, textPart), lineBreakPart());
}
function getCommentHavingNodes(declaration: Declaration): readonly (JSDoc | JSDocTag)[] {