Skip to content

Commit

Permalink
refactor: reduce complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
Johan committed Sep 26, 2021
1 parent 9a6f64d commit 195b9ed
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 45 deletions.
1 change: 1 addition & 0 deletions .eslintrc
Expand Up @@ -38,6 +38,7 @@
"import/extensions": ["error", { "ts": "never" }],
"import/prefer-default-export": ["off"],
"newline-per-chained-call": "off",
"no-nested-ternary": "warn",
"no-restricted-syntax": ["off", "ForOfStatement"],
"no-undefined": "error",
"no-undef": "error",
Expand Down
51 changes: 32 additions & 19 deletions src/lib/walker/back.ts
Expand Up @@ -2,6 +2,32 @@ import { flexi } from "..";
import { FlexiPath, Path, WalkedPath, WalkOptions } from "../../types";
import reporter from "./reporter";

/**
* @ignore
*/
const walkUntil = (path: FlexiPath, diff: FlexiPath, options?: WalkOptions): WalkedPath<FlexiPath> | null => {
if (options?.until && options.until(path)) {
const result = path.isRoot() ? flexi.root() : path;
return { diff, result };
}
return null;
};

/**
* @ignore
*/
const emptyOrRoot = (path: FlexiPath, diff: FlexiPath): WalkedPath<FlexiPath> | null => {
if (path.isRoot()) {
return { diff, result: flexi.root() };
}

if (path.isEmpty() || path.parent().isEmpty()) {
return { diff, result: flexi.empty() };
}

return null;
};

/**
* Walks a `path` backwards
* @param path The path to walk
Expand All @@ -10,29 +36,16 @@ import reporter from "./reporter";
*/
const back = (path: Path, options?: WalkOptions, acc?: FlexiPath): WalkedPath<FlexiPath> => {
const parsedPath = flexi.path(path);
const parent = parsedPath.parent();
const diff = acc || flexi.empty();
const diffWithPrependedBase = diff.prepend(parsedPath.base);

reporter(options).report(parsedPath);

if (options && options.until && options.until(parsedPath)) {
const result = parsedPath.isRoot() ? flexi.root() : parsedPath;
return { diff, result };
}

let emptyOrRoot: FlexiPath | undefined;
if (parsedPath.isRoot()) {
emptyOrRoot = flexi.root();
} else if (parsedPath.isEmpty() || parent.isEmpty()) {
emptyOrRoot = flexi.empty();
}
const nextDiff = diff.prepend(parsedPath.base);

if (typeof emptyOrRoot !== "undefined") {
return { diff: nextDiff, result: emptyOrRoot };
}

return back(parent, options, nextDiff);
return (
walkUntil(parsedPath, diff, options) ||
emptyOrRoot(parsedPath, diffWithPrependedBase) ||
back(parsedPath.parent(), options, diffWithPrependedBase)
);
};

export default back;
73 changes: 47 additions & 26 deletions src/lib/walker/forward.ts
@@ -1,46 +1,47 @@
import { create, empty } from "../../models/walked-path";
import { flexi } from "..";
import { FlexiPath, Path, PathType, WalkedPath, WalkOptions } from "../../types";
import reporter from "./reporter";

const empty = 0;

/**
* Walks a `path`
* @param path The path to walk
* @param until Stops walking when condition is met
* @category walker
* @ignore
*/
const forward = (path: Path, options?: WalkOptions): WalkedPath<FlexiPath[]> => {
const parsedPath = flexi.path(path);
const emptyPath = (path: FlexiPath): WalkedPath<FlexiPath[]> | null =>
path.isEmpty() || !path.exists() ? empty : null;

reporter(options).report(parsedPath);
/**
* @ignore
*/
const untilPath = (paths: FlexiPath[], options?: WalkOptions): WalkedPath<FlexiPath[]> | null => {
const untilFunc = options?.until;

if (parsedPath.isEmpty() || !parsedPath.exists()) {
return { result: [], diff: [] };
if (typeof untilFunc === "undefined" || paths.length === 0) {
return null;
}

const content = parsedPath.children();

const untilFunc = options && options.until;

if (typeof untilFunc !== "undefined") {
const result = content.filter((x) => untilFunc(x));
const result = paths.filter((x) => untilFunc(x));

if (result.length > empty) {
return { result, diff: [] };
}
if (result.length > 0) {
return create({ result });
}

let walked = content.reduce<FlexiPath[]>((prev: FlexiPath[], curr: FlexiPath) => {
return null;
};

/**
* @ignore
*/
const walkPath = (path: FlexiPath, options?: WalkOptions | undefined) => {
let walked = path.children().reduce<FlexiPath[]>((prev: FlexiPath[], curr: FlexiPath) => {
const result: FlexiPath[] = [];

if (curr.type() === PathType.Directory) {
const next = parsedPath.append(curr.name);

const next = path.append(curr.name);
// eslint-disable-next-line no-use-before-define
result.push(...forward(next, options).result);
}

if (result.length === empty) {
if (result.length === 0) {
result.push(curr);
}

Expand All @@ -49,11 +50,31 @@ const forward = (path: Path, options?: WalkOptions): WalkedPath<FlexiPath[]> =>
return prev;
}, []);

if (walked.length > empty && typeof untilFunc !== "undefined") {
const untilFunc = options?.until;

if (walked.length > 0 && typeof untilFunc !== "undefined") {
walked = walked.filter((x) => untilFunc(x));
}

return { result: walked, diff: [] };
return walked;
};

/**
* Walks a `path`
* @param path The path to walk
* @param until Stops walking when condition is met
* @category walker
*/
const forward = (path: Path, options?: WalkOptions): WalkedPath<FlexiPath[]> => {
const parsedPath = flexi.path(path);

reporter(options).report(parsedPath);

return (
emptyPath(parsedPath) ||
untilPath(parsedPath.children(), options) ||
create({ result: walkPath(parsedPath, options) })
);
};

export default forward;
13 changes: 13 additions & 0 deletions src/models/walked-path.ts
@@ -0,0 +1,13 @@
import { FlexiPath, WalkedPath } from "../types";

export const empty: WalkedPath<FlexiPath[]> = { diff: [], result: [] };

export const create = (path: Partial<WalkedPath<FlexiPath[]>>): WalkedPath<FlexiPath[]> => ({
...empty,
...path
});

export const walkedPath = {
create,
empty
};

0 comments on commit 195b9ed

Please sign in to comment.