Skip to content

Commit

Permalink
chore: Remove lodash
Browse files Browse the repository at this point in the history
Have been wanting to do this for a while... finally got around to it.
  • Loading branch information
Gerrit0 committed Jul 5, 2021
1 parent 3c5ae51 commit b04b3b0
Show file tree
Hide file tree
Showing 19 changed files with 163 additions and 159 deletions.
20 changes: 4 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions package.json
Expand Up @@ -22,7 +22,6 @@
"dependencies": {
"glob": "^7.1.7",
"handlebars": "^4.7.7",
"lodash": "^4.17.21",
"lunr": "^2.3.9",
"marked": "^2.1.1",
"minimatch": "^3.0.0",
Expand All @@ -35,7 +34,6 @@
},
"devDependencies": {
"@types/glob": "^7.1.3",
"@types/lodash": "^4.14.170",
"@types/lunr": "^2.3.3",
"@types/marked": "^2.0.3",
"@types/minimatch": "3.0.4",
Expand Down
1 change: 0 additions & 1 deletion src/lib/converter/converter.ts
@@ -1,5 +1,4 @@
import * as ts from "typescript";
import * as _ from "lodash";

import { Application } from "../application";
import { Type, ProjectReflection, ReflectionKind } from "../models/index";
Expand Down
7 changes: 3 additions & 4 deletions src/lib/converter/factories/comment.ts
@@ -1,5 +1,4 @@
import * as ts from "typescript";
import { toArray } from "lodash";

import { Comment, CommentTag } from "../../models/comments/index";
import { Logger } from "../../utils";
Expand Down Expand Up @@ -61,10 +60,10 @@ function getJSDocCommentRanges(node: ts.Node, text: string): ts.CommentRange[] {
ts.SyntaxKind.ParenthesizedExpression,
].includes(node.kind);

let commentRanges = toArray(ts.getLeadingCommentRanges(text, node.pos));
let commentRanges = ts.getLeadingCommentRanges(text, node.pos) ?? [];
if (hasTrailingCommentRanges) {
commentRanges = toArray(
ts.getTrailingCommentRanges(text, node.pos)
commentRanges = (
ts.getTrailingCommentRanges(text, node.pos) ?? []
).concat(commentRanges);
}

Expand Down
11 changes: 8 additions & 3 deletions src/lib/converter/plugins/CommentPlugin.ts
Expand Up @@ -17,9 +17,14 @@ import {
} from "../factories/comment";
import { Converter } from "../converter";
import { Context } from "../context";
import { partition, uniq } from "lodash";
import { ReflectionType, SourceReference } from "../../models";
import { BindOption, filterMap, removeIfPresent } from "../../utils";
import {
BindOption,
filterMap,
removeIfPresent,
unique,
partition,
} from "../../utils";

/**
* These tags are not useful to display in the generated documentation.
Expand Down Expand Up @@ -244,7 +249,7 @@ export class CommentPlugin extends ConverterComponent {
project.removeReflection(reflection)
);
someRemoved.forEach((reflection) => {
reflection.sources = uniq(
reflection.sources = unique(
reflection.signatures!.reduce<SourceReference[]>(
(c, s) => c.concat(s.sources || []),
[]
Expand Down
1 change: 1 addition & 0 deletions src/lib/converter/plugins/InheritDocPlugin.ts
Expand Up @@ -81,6 +81,7 @@ export class InheritDocPlugin extends ConverterComponent {
if (referencedReflection instanceof Reflection) {
copyComment(item, referencedReflection);
}
return true;
};
reflection.traverse(descendantsCallback);
}
Expand Down
7 changes: 6 additions & 1 deletion src/lib/models/reflections/abstract.ts
Expand Up @@ -5,6 +5,7 @@ import { Comment } from "../comments/comment";
import { TypeParameterReflection } from "./type-parameter";
import { splitUnquotedString } from "./utils";
import { ProjectReflection } from "./project";
import { NeverIfInternal } from "../../utils";

/**
* Holds all data models used by TypeDoc.
Expand Down Expand Up @@ -298,7 +299,9 @@ export interface TraverseCallback {
* May return false to bail out of any further iteration. To preserve backwards compatibility, if
* a function returns undefined, iteration must continue.
*/
(reflection: Reflection, property: TraverseProperty): boolean | void;
(reflection: Reflection, property: TraverseProperty):
| boolean
| NeverIfInternal<void>;
}

/**
Expand Down Expand Up @@ -550,6 +553,7 @@ export abstract class Reflection {
}
return false;
}
return true;
});

return result;
Expand Down Expand Up @@ -610,6 +614,7 @@ export abstract class Reflection {
indent += " ";
this.traverse((child) => {
lines.push(child.toStringHierarchy(indent));
return true;
});

return lines.join("\n");
Expand Down
3 changes: 1 addition & 2 deletions src/lib/models/reflections/container.ts
Expand Up @@ -7,7 +7,6 @@ import {
import { ReflectionCategory } from "../ReflectionCategory";
import { ReflectionGroup } from "../ReflectionGroup";
import { DeclarationReflection } from "./declaration";
import { toArray } from "lodash";

export class ContainerReflection extends Reflection {
/**
Expand Down Expand Up @@ -44,7 +43,7 @@ export class ContainerReflection extends Reflection {
* @param callback The callback function that should be applied for each child reflection.
*/
traverse(callback: TraverseCallback) {
for (const child of toArray(this.children)) {
for (const child of this.children ?? []) {
if (callback(child, TraverseProperty.Children) === false) {
return;
}
Expand Down
5 changes: 2 additions & 3 deletions src/lib/models/reflections/declaration.ts
@@ -1,4 +1,3 @@
import { toArray } from "lodash";
import type * as ts from "typescript";
import { ReferenceType, ReflectionType, Type } from "../types/index";
import {
Expand Down Expand Up @@ -179,7 +178,7 @@ export class DeclarationReflection
* @param callback The callback function that should be applied for each child reflection.
*/
traverse(callback: TraverseCallback) {
for (const parameter of toArray(this.typeParameters)) {
for (const parameter of this.typeParameters ?? []) {
if (callback(parameter, TraverseProperty.TypeParameter) === false) {
return;
}
Expand All @@ -196,7 +195,7 @@ export class DeclarationReflection
}
}

for (const signature of toArray(this.signatures)) {
for (const signature of this.signatures ?? []) {
if (callback(signature, TraverseProperty.Signatures) === false) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/models/reflections/project.ts
Expand Up @@ -173,7 +173,7 @@ export class ProjectReflection extends ContainerReflection {
}
this.getReferenceGraph().delete(reflection.id);

reflection.traverse((child) => this.removeReflection(child));
reflection.traverse((child) => (this.removeReflection(child), true));

const parent = reflection.parent as DeclarationReflection;
parent?.traverse((child, property) => {
Expand Down
5 changes: 2 additions & 3 deletions src/lib/models/reflections/signature.ts
Expand Up @@ -9,7 +9,6 @@ import {
} from "./abstract";
import { ParameterReflection } from "./parameter";
import { TypeParameterReflection } from "./type-parameter";
import { toArray } from "lodash";
import type { DeclarationReflection } from "./declaration";

export class SignatureReflection
Expand Down Expand Up @@ -98,13 +97,13 @@ export class SignatureReflection
}
}

for (const parameter of toArray(this.typeParameters)) {
for (const parameter of this.typeParameters ?? []) {
if (callback(parameter, TraverseProperty.TypeParameter) === false) {
return;
}
}

for (const parameter of toArray(this.parameters)) {
for (const parameter of this.parameters ?? []) {
if (callback(parameter, TraverseProperty.Parameters) === false) {
return;
}
Expand Down
1 change: 1 addition & 0 deletions src/lib/output/themes/DefaultTheme.ts
Expand Up @@ -297,6 +297,7 @@ export class DefaultTheme extends Theme {
if (child instanceof DeclarationReflection) {
DefaultTheme.applyAnchorUrl(child, container);
}
return true;
});
}

Expand Down
18 changes: 18 additions & 0 deletions src/lib/utils/array.ts
Expand Up @@ -81,6 +81,24 @@ export function unique<T>(arr: Iterable<T> | undefined): T[] {
return Array.from(new Set(arr));
}

export function partition<T>(
iter: Iterable<T>,
predicate: (item: T) => boolean
): [T[], T[]] {
const left: T[] = [];
const right: T[] = [];

for (const item of iter) {
if (predicate(item)) {
left.push(item);
} else {
right.push(item);
}
}

return [left, right];
}

/**
* Filters out duplicate values from the given array with a custom equals check.
* @param arr
Expand Down
21 changes: 3 additions & 18 deletions src/lib/utils/component.ts
@@ -1,8 +1,5 @@
import * as _ from "lodash";

import { Application } from "../application";
import { EventDispatcher, Event, EventMap } from "./events";
import { DeclarationOption } from "./options/declaration";

/**
* Exposes a reference to the root Application component.
Expand Down Expand Up @@ -128,11 +125,6 @@ export abstract class AbstractComponent<O extends ComponentHost>
*/
public componentName!: string;

/**
* A list of options defined by this component.
*/
private _componentOptions?: DeclarationOption[];

/**
* Create new Component instance.
*/
Expand Down Expand Up @@ -162,13 +154,6 @@ export abstract class AbstractComponent<O extends ComponentHost>
return this;
}

/**
* Return all option declarations emitted by this component.
*/
getOptionDeclarations(): DeclarationOption[] {
return (this._componentOptions || []).slice();
}

/**
* Return the application / root component instance.
*/
Expand Down Expand Up @@ -219,7 +204,7 @@ export abstract class ChildableComponent<
constructor(owner: O | typeof DUMMY_APPLICATION_OWNER) {
super(owner);

_.entries(this._defaultComponents || {}).forEach(
Object.entries(this._defaultComponents || {}).forEach(
([name, component]) => {
this.addComponent(name, component);
}
Expand All @@ -236,7 +221,7 @@ export abstract class ChildableComponent<
}

getComponents(): C[] {
return _.values(this._componentChildren);
return Object.values(this._componentChildren || {});
}

hasComponent(name: string): boolean {
Expand Down Expand Up @@ -287,7 +272,7 @@ export abstract class ChildableComponent<
}

removeAllComponents() {
for (const component of _.values(this._componentChildren)) {
for (const component of Object.values(this._componentChildren || {})) {
component.stopListening();
}

Expand Down

0 comments on commit b04b3b0

Please sign in to comment.