Skip to content

Commit

Permalink
Merge pull request #5 from Benoit-Welsch/Fix/Scanner-Flat
Browse files Browse the repository at this point in the history
Fix : Flat on folder should return arr of files
  • Loading branch information
Benoit-Welsch committed Dec 14, 2023
2 parents 50589d7 + 34bda59 commit ab3e020
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ scanner.files;
scanner.folders;
scanner.path;

// return a array of folders
// return a array of files
scanner.flat();
// return json and remove circular references
scanner.toJson();
Expand Down
3 changes: 1 addition & 2 deletions genDoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ const getToc = (names: (string | undefined)[]) => {

const allDoc = root
.flat()
.map(({ files }) => files.filter((file) => file.name.endsWith('.md')))
.filter((file) => typeof file !== 'undefined')
.filter((file) => file.name.endsWith('.md'))
.flat();

const base = allDoc.find((file) => file.name === 'Base.md');
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "1.1.3",
"version": "1.1.4",
"name": "@lv00/toolkit",
"module": "./dist/index.js",
"type": "module",
Expand Down
2 changes: 1 addition & 1 deletion src/Scanner/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ scanner.files;
scanner.folders;
scanner.path;

// return a array of folders
// return a array of files
scanner.flat();
// return json and remove circular references
scanner.toJson();
Expand Down
14 changes: 14 additions & 0 deletions src/Scanner/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { it, expect } from 'bun:test';
import { readFolder } from '.';
import { file } from 'bun';

it('should scan current folder', () => {
const libFolder = readFolder('./src/Scanner');
Expand Down Expand Up @@ -43,3 +44,16 @@ it('should scan parent folder', () => {
//expect to find Scanner folder
expect(libFolder.folders.find((f) => f.path === 'src/Scanner')).toBeDefined();
});

it('should scan parent folder and get only files', () => {
const libFolder = readFolder('./src/');
const files = libFolder.flat();

const nbOfIndexFiles = libFolder.folders.length + 1;

//expect nbOfIndexFiles of index.ts in files
expect(files.filter((f) => f.name === 'index.ts')).toBeArrayOfSize(
nbOfIndexFiles,
);

});
17 changes: 6 additions & 11 deletions src/Scanner/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ export class File {
return value;
});
}

fqn() {
return path.join(this.path, this.name);
}
}

export class Folder {
Expand Down Expand Up @@ -70,18 +74,9 @@ export class Folder {
}

flat() {
let data: Folder[] = [];
let data: File[] = [];
const mergerFolder = (folder: Folder) => {
data = data.concat(
folder.files.map((f) => {
return new Folder({
path: f.path,
files: [f],
folders: [],
parent: folder,
});
}),
);
data = data.concat(folder.files);
folder.folders.forEach((f) => {
mergerFolder(f);
});
Expand Down

0 comments on commit ab3e020

Please sign in to comment.