Skip to content

Commit

Permalink
feat: indexer overload for subDirectories
Browse files Browse the repository at this point in the history
  • Loading branch information
Johan committed Aug 27, 2019
1 parent 71c62da commit fe7428c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 11 deletions.
25 changes: 19 additions & 6 deletions src/index.ts
Expand Up @@ -7,13 +7,18 @@ export enum PathType {
File = 2
}

interface Indexable<T> {
(name: string): T;
(): T[];
}

export interface FlexiPath extends ParsedPath {
path: string;
isRoot(): boolean;
exists(): boolean;
type(): PathType;
parent(): FlexiPath | null;
subDirectories(): FlexiPath[];
subDirectories: Indexable<FlexiPath>;
files(): FlexiPath[];
}

Expand Down Expand Up @@ -42,17 +47,23 @@ export const FlexiPath = (path: string): FlexiPath => {
const parentPath = (): string => join(path, up);
const parent = (): FlexiPath | null =>
(!isRoot() && FlexiPath(parentPath())) || null;
const subDirectories = (): FlexiPath[] =>
readdir()
.filter(x => x.isDirectory())
.map(x => FlexiPath(join(path, x.name)));

const subDirectories: Indexable<FlexiPath> = (directoryName?: any): any => {
if (directoryName === undefined) {
return readdir()
.filter(x => x.isDirectory())
.map(x => FlexiPath(join(path, x.name)));
}

return FlexiPath(join(path, directoryName));
};

const files = (): FlexiPath[] =>
readdir()
.filter(x => x.isFile())
.map(x => FlexiPath(join(path, x.name)));

return {
const api: FlexiPath = {
root,
dir,
ext,
Expand All @@ -66,6 +77,8 @@ export const FlexiPath = (path: string): FlexiPath => {
subDirectories,
files
};

return api;
};

export const Root = () => FlexiPath("/");
Expand Down
11 changes: 10 additions & 1 deletion test/index.subDirectories.test.ts
Expand Up @@ -2,7 +2,7 @@ import FlexiPath from "../src";
import testData from "./jest/createTestData";

describe("subDirectories", () => {
it("should contain sub directory", () => {
it("should contain subdirectory", () => {
const subDirectory = testData.createDirectory("subDirectory");

expect(
Expand All @@ -23,4 +23,13 @@ describe("subDirectories", () => {
it("should be empty when path is invalid", () => {
expect(FlexiPath("invalid").subDirectories()).toBeEmpty();
});

describe("when passing name", () => {
it("should have subdirectory with parent", () => {
const parent = FlexiPath("root/");
const sub = parent.subDirectories("sub");

expect(sub.parent()).toHaveMatchingMembersOf(parent);
});
});
});
16 changes: 12 additions & 4 deletions test/jest/createTestData.ts
Expand Up @@ -6,13 +6,21 @@ export const testDir = join(__dirname, "flex.test-data");
export const testFile = join(testDir, "testfile.js");

beforeEach(() => {
shell.rm("-rf", testDir);
shell.mkdir(testDir);
shell.touch(testFile);
try {
shell.rm("-rf", testDir);
shell.mkdir(testDir);
shell.touch(testFile);
} catch (e) {
console.error(e);
}
});

afterEach(() => {
shell.rm("-rf", testDir);
try {
shell.rm("-rf", testDir);
} catch (e) {
console.error(e);
}
});

export const createFile = (relativePath: string, fileName: string): string => {
Expand Down

0 comments on commit fe7428c

Please sign in to comment.