Skip to content

Commit

Permalink
make it possible to read line by line
Browse files Browse the repository at this point in the history
  • Loading branch information
CordlessWool committed Feb 10, 2024
1 parent e9125ba commit 3f7263d
Show file tree
Hide file tree
Showing 7 changed files with 298 additions and 49 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# loom-io

This repository should include service to handle and work with files. The goal is to handle git and filesystem operations and offer more functionality the nodejs fs functions
Currently this library helps to read files and directories. There is also planned to offer functions to edit and load sources like git

## How to

Expand Down
115 changes: 113 additions & 2 deletions src/core/editor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ describe('Editor', () => {
});




describe('test loop and background function', () => {

test('searchInChunk', async () => {
Expand Down Expand Up @@ -263,7 +265,7 @@ describe('Editor', () => {
const chunkSize = faker.number.int({min: 100, max: 200});
const p = editor.unwrappedLoopReverseCalcNextChunk(cur, chunkSize, valueLength, min);
expect(p).toBeDefined();
expect(p!.position).toBe(cur - (chunkSize + valueLength/2));
expect(p!.position).toBe(cur - (chunkSize + Math.floor(valueLength/2)));
expect(p!.length).toBe(chunkSize + valueLength);
});

Expand All @@ -277,11 +279,120 @@ describe('Editor', () => {
const p = editor.unwrappedLoopReverseCalcNextChunk(cur, chunkSize, valueLength, min);
expect(p).toBeDefined();
expect(p!.position).toBe(min);
expect(p!.length).toBe(cur - min + valueLength/2);
expect(p!.length).toBe(cur - min + Math.floor(valueLength/2));
});


});

});

describe('line read', () => {

test('read first line as string', async () => {
const testFile = `${TestFilesystemHelper.STATIC_TEST_DIR}/editor.md`;
const reader = await createEditor(testFile);
const result = await reader.getFirstLine();
expect(result).toBeDefined();
const line = await result?.read('utf8');
expect(line).toBeDefined();
expect(line).toBeTypeOf('string');
expect(line).toBe('---');
reader.close();
});

test('read line', async () => {
const testFile = `${TestFilesystemHelper.STATIC_TEST_DIR}/editor.md`;
const reader = await createEditor(testFile);
const result = await reader.getFirstLine();
expect(result).toBeDefined();
const line = await result?.read();
expect(line).toBeDefined();
expect(line).toBeInstanceOf(Buffer);
reader.close();
});

test('get next line', async () => {
const testFile = `${TestFilesystemHelper.STATIC_TEST_DIR}/editor.md`;
const reader = await createEditor(testFile);
const result = await reader.getFirstLine();
expect(result).toBeDefined();
await result.next();
expect(result).toBeDefined();
expect(result.read('utf8')).resolves.toBe('createdAt: 2020-12-17');
reader.close();
});

test('read till last lines', async () => {
const testFile = `${TestFilesystemHelper.STATIC_TEST_DIR}/editor.md`;
const reader = await createEditor(testFile);
const result = await reader.getFirstLine();
expect(result).toBeDefined();
let count = 0;
while(await result.hasNext()) {
const line = await result.read('utf8');
if(line === '---') {
count++;
}
await result.next();
}
expect(result.read('utf8')).resolves.toBe('### EOF');
expect(count).toBe(2);
reader.close();
});

test('read last line', async () => {
const testFile = `${TestFilesystemHelper.STATIC_TEST_DIR}/editor.md`;
const reader = await createEditor(testFile);
const result = await reader.getLastLine();
expect(result).toBeDefined();
const line = await result.read();
expect(line).toBeDefined();
expect(line).toBeInstanceOf(Buffer);
reader.close();
});

test('read last line as string', async () => {
const testFile = `${TestFilesystemHelper.STATIC_TEST_DIR}/editor.md`;
const reader = await createEditor(testFile);
const result = await reader.getLastLine();
expect(result).toBeDefined();
const line = await result.read('utf8');
expect(line).toBeDefined();
expect(line).toBeTypeOf('string');
expect(line).toBe('### EOF');
reader.close();
});

test('get prev line', async () => {
const testFile = `${TestFilesystemHelper.STATIC_TEST_DIR}/editor.md`;
const reader = await createEditor(testFile);
const result = await reader.getLastLine();
expect(result).toBeDefined();
await result.prev();
expect(result).toBeDefined();
expect(result.read('utf8')).resolves.toContain('e some ideas in my mind, but not sure if they');
reader.close();
});

test('read till first lines', async () => {
const testFile = `${TestFilesystemHelper.STATIC_TEST_DIR}/editor.md`;
const reader = await createEditor(testFile);
const result = await reader.getLastLine();
expect(result).toBeDefined();

let count = 0;
while(await result.hasPrev()) {
await result.prev();
const line = await result.read('utf8');
if(line === '---') {
count++;
}
}
expect(result.read('utf8')).resolves.toBe('---');
expect(count).toBe(2);
reader.close();
});

});
});
10 changes: 5 additions & 5 deletions src/core/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,12 @@ export class Editor implements Reader, Writer, ReaderInternal{
* @returns
*/
protected loopReverseCalcNextChunk(current: number, chunkSize: number, valueLength: number, min: number): {position: number, length: number} {
let nextPosition = current - (chunkSize + valueLength/2);
let nextPosition = current - (chunkSize + Math.floor(valueLength/2));
let length: number = chunkSize + valueLength;

if(nextPosition < min) {
nextPosition = min;
length = current - min + valueLength/2;
length = current - min + Math.floor(valueLength/2);
}


Expand All @@ -191,7 +191,8 @@ export class Editor implements Reader, Writer, ReaderInternal{
}

async read(start: number, length: number): Promise<Buffer> {
const data = await this.file.read({position: start, length});
const buffer = Buffer.alloc(length);
const data = await this.file.read({position: start, buffer});
return data.buffer;
}

Expand Down Expand Up @@ -240,9 +241,8 @@ export class Editor implements Reader, Writer, ReaderInternal{

TextItemList.patch(last, {
...last.content,
last: true,
start: last.content.end,
end: await this.getSizeInBytes()
end: fileSize + bSeparator.length
});

return new LineResult(last, bSeparator, this);
Expand Down
68 changes: 66 additions & 2 deletions src/core/helper/result.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { describe, test, expect, beforeAll } from 'vitest';
import { SearchResult } from './result';
import { LineResult, SearchResult } from './result';
import { TextItemList } from './textItemList';
import { ReaderInternal } from '../editor';
import { faker } from '@faker-js/faker';
import exp from 'constants';

Check failure on line 6 in src/core/helper/result.spec.ts

View workflow job for this annotation

GitHub Actions / lint & test

'exp' is defined but never used


class MockReader implements ReaderInternal {
Expand Down Expand Up @@ -51,7 +52,7 @@ class MockReader implements ReaderInternal {
}


describe('Result', () => {
describe('SearchResult', () => {

let reader: MockReader;

Expand Down Expand Up @@ -84,5 +85,68 @@ describe('Result', () => {
const result = await reader.searchFirst('test');
expect(result?.searchValue.toString()).toBe('test');
});
});

describe('LineResult', () => {

let reader: MockReader;

beforeAll(() => {
reader = new MockReader();
});

test('constructor', async () => {
const item = new TextItemList({ start: 0, end: 10});
const result = new LineResult(item, Buffer.from('\r\n'), reader);
expect(result).toBeDefined();
});

test('patchPrevItems', async () => {
const item0 = new TextItemList({ start: 6, end: 7});
const item1 = new TextItemList({ start: 30, end: 31});
const item2 = new TextItemList({ start: 89, end: 90});
const item3 = new TextItemList({ start: 100, end: 200});
item0.add(item1);
item1.add(item2);
item2.add(item3);
new LineResult(item3, Buffer.from('\n'), reader);
expect(item0.start).toBe(7);
expect(item0.end).toBe(31);
expect(item1.start).toBe(31);
expect(item1.end).toBe(90);
expect(item2.start).toBe(90);
expect(item2.end).toBe(100);
expect(item3.start).toBe(100);
expect(item3.end).toBe(200);
});

test('patchNextItems', async () => {
const item1 = new TextItemList({ start: 0, end: 10});
const item2 = new TextItemList({ start: 74, end: 75});
const item3 = new TextItemList({ start: 75, end: 76});
const item4 = new TextItemList({ start: 89, end: 90});
item3.add(item4);
item1.add(item2);
item2.add(item3);
new LineResult(item1, Buffer.from('test'), reader);
expect(item1.start).toBe(0);
expect(item1.end).toBe(10);
expect(item2.start).toBe(10);
expect(item2.end).toBe(75);
expect(item3.start).toBe(75);
expect(item3.end).toBe(76);
expect(item4.start).toBe(76);
expect(item4.end).toBe(90);

});

test('hasNext', async () => {
const item1 = new TextItemList({ start: 0, end: 10});
const item2 = new TextItemList({ start: 10, end: 20});
item1.add(item2);
const lineResult = new SearchResult(item1, Buffer.from('test'), reader);
const hasNext = await lineResult.hasNext();
expect(hasNext).toBe(true);
});

});
Loading

0 comments on commit 3f7263d

Please sign in to comment.