Skip to content

Commit

Permalink
extends readme and add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
CordlessWool committed Jan 17, 2024
1 parent 9ab557f commit 9f0c4c2
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 35 deletions.
22 changes: 17 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ const str = file.text();
const json = file.json();
const buffer = file.plain();

// If you want to have the directory just call
file.parent // it is the same as for a directory


// Register a plugin, look below to get more information on plugins
Loom.register(/*some plugin*/)
Expand All @@ -27,15 +30,16 @@ By default the system can read json and yml and convert it to json. To Support m
Reading a dir Works similar.

```ts
import Loom, {type File} from '@loom-io/fs'
import Loom, {type File, type Directory} from '@loom-io/fs'

const root = Loom.root(); // Returns a directory object of the project root, to call the system root call Loom.dir('/)
const dir = Loom.dir('some/dir');

// here you have functions to list the content of the dir and get files
const files = dir.files(true); // return all files in the directory and it's subdirectories as type File.

for(let file of files) {
const json = (file as File).json();
const json = file.json();
// do something with the content
}

Expand All @@ -44,11 +48,19 @@ const list = dir.list() // returns a iterable to get Files and Directories
for(let el of list) {
if(el instanceOf List) { // check if it is a File
console.log(el.text()); // do some Stuff with the file content
} else {
} else (el instanceOf Directory) {
console.log(el.list().length) // in the other case it is an directory and you can go on working with it
}
}

// to get the parent of a directory
if(dir.parent !== undefined) {
dir.parent.list()
}

// to select a specific sub directory call
dir.subDir('sub/path'); // this is not validated and only throws an arrow an calling list or files

```

## Plugins
Expand All @@ -59,7 +71,7 @@ To handle and covert more file types to json loom-io/fs allow to register plugin
// Plugin type to convert file content to json
export type LoomFSFileConverter = {
type: PLUGIN_TYPE.FILE_CONVERTER,
extentions: string[],
extensions: string[],
parse<T = unknown>(content: string): T
stringify<T = unknown>(content: T): string
}
Expand All @@ -74,7 +86,7 @@ import { PLUGIN_TYPE, type LoomFSFileConverter } from 'loom-io/fs';
//example for the plugin to convert json strings.
export default {
type: PLUGIN_TYPE.FILE_CONVERTER,
extentions: ['json'],
extensions: ['json'],
parse: JSON.parse,
stringify: JSON.stringify
} satisfies LoomFSFileConverter;
Expand Down
7 changes: 7 additions & 0 deletions src/core/file.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ describe('Test File Service', () => {
const plugins = FileTest.getConvertPlugins();
expect(plugins).toHaveLength(2);
});

test('get parent or dir', () => {
const file = File.from('./test/data/test.json');
expect(file.dir).instanceOf(Directory);
expect(file.dir.path).toBe(`${process.cwd()}/test/data`);
expect(file.dir).toBe(file.parent);
});


describe('Test with generated file', () => {
Expand Down
16 changes: 0 additions & 16 deletions src/core/list.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,22 +256,6 @@ describe('Test List', () => {
expect(newList).toBeInstanceOf(List);
});

test('Add Directory and paths', async () => {
const dir = new Directory('./test/data');
const list = await dir.list();
const revealedList = new RevealedList(list);
const paths = revealedList.getWraps().map((wrap) => wrap.dirent);
const newList = new RevealedList();
newList.revealedAdd(dir, paths);
expect(newList).toBeInstanceOf(List);
expect(newList).not.toBe(list);

const newListArray = newList.asArray();
const listArray = list.asArray();
expect(newListArray).toHaveLength(listArray.length);
expect(newListArray).toStrictEqual(listArray);
});

test('Add List', async () => {
const dir = new Directory('./test/data');
const list = await dir.list();
Expand Down
22 changes: 8 additions & 14 deletions src/core/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,35 +31,29 @@ export class List<T extends ListTypes = ListTypes> {
dir?: Directory,
_paths?: Dirent[])
{
this.dirWrap = [];
if(dir && _paths) {
this.add(dir, _paths);
this.dirWrap = _paths.map((p) => new DirentWrapper(dir, p));
} else {
this.dirWrap = [];
}
}

protected add(paths: DirentWrapper[]): void
protected add(dir: Directory, paths: Dirent[]): void
protected add(list: List<T>): void
protected add(listOrWrap: List<T> | DirentWrapper[]): void
protected add(dirOrListOrDirentWrapper: Directory | DirentWrapper[] | List<T>, paths?: Dirent[]) {
protected add(listOrDirentWrapper: DirentWrapper[] | List<T>) {

if(dirOrListOrDirentWrapper instanceof Directory) {
if(paths === undefined) {
throw new Error('List constructor requires paths argument if the first argument is a Directory');
}
const wrapped = paths.map((path) => new DirentWrapper(dirOrListOrDirentWrapper, path));
this.dirWrap.push(...wrapped);
} else if(dirOrListOrDirentWrapper instanceof List) {
this.dirWrap.push(...dirOrListOrDirentWrapper.dirWrap);
if(listOrDirentWrapper instanceof List) {
this.dirWrap.push(...listOrDirentWrapper.dirWrap);
} else {
this.dirWrap.push(...dirOrListOrDirentWrapper);
this.dirWrap.push(...listOrDirentWrapper);
}
}

concat<U extends ListTypes>(...lists: Array<List<U>>): List<T | U> {
const newList = List.from<T | U>(this);
for(const list of lists) {
newList.add(list.dirWrap);
newList.add(list);
}

return newList;
Expand Down

0 comments on commit 9f0c4c2

Please sign in to comment.