Skip to content

feat: support generator #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,11 @@ export const ExtendedHeader = {
} as const;

export const ExtendedHeaderValues = Object.values(ExtendedHeader);

export const NORMAL_CHUNK_RE =
/^@@\s\-(\d+),?(\d+)?\s\+(\d+),?(\d+)?\s@@\s?(.+)?/;

export const COMBINED_CHUNK_RE =
/^@@@\s\-(\d+),?(\d+)?\s\-(\d+),?(\d+)?\s\+(\d+),?(\d+)?\s@@@\s?(.+)?/;

export const BINARY_CHUNK_RE = /^Binary\sfiles\s(.*)\sand\s(.*)\sdiffer$/;
81 changes: 74 additions & 7 deletions src/context.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,94 @@
import type { Interface } from 'node:readline';
import { FilledGitDiffOptions, GitDiffOptions } from './types';
import { isReadlineInterface } from './utils';

export default class Context {
private line: number = 1;
private lines: string[] = [];
private lines: Generator<string, any, unknown>;
public options: FilledGitDiffOptions = {
noPrefix: false,
};
public constructor(diff: string, options?: GitDiffOptions) {
this.lines = diff.split('\n');
private _currentLine: string;
private _isEof = false;

public constructor(
diff: string | Generator<string, any, unknown>,
options?: GitDiffOptions
) {
if (typeof diff === 'string') {
this.lines = this.getGeneratorFromString(diff);
} else {
this.lines = diff;
}

this._currentLine = this.lines.next().value;

this.options.noPrefix = !!options?.noPrefix;
}

private *getGeneratorFromString(text: string) {
for (const line of text.split('\n')) {
yield line;
}
}

public getCurLine(): string {
return this.lines[this.line - 1];
return this._currentLine;
}

public nextLine(): string | undefined {
this.line++;
const next = this.lines.next();
this._isEof = Boolean(next.done);
this._currentLine = next.value;
return this.getCurLine();
}

public isEof(): boolean {
return this._isEof;
}
}

export class AsyncContext {
public options: FilledGitDiffOptions = {
noPrefix: false,
};
private _currentLine: string = '';
private _isEof = false;
private opened = false;
private lines: AsyncGenerator<string, any, unknown>;

public constructor(
diff: AsyncGenerator<string, any, unknown> | Interface,
options?: GitDiffOptions
) {
if (isReadlineInterface(diff)) {
this.lines = this.getGenerator(diff);
} else {
this.lines = diff;
}

this.options.noPrefix = !!options?.noPrefix;
}

async *getGenerator(stream: Interface) {
for await (const line of stream) {
yield line;
}
}

public async getCurLine(): Promise<string> {
if (!this.opened) await this.nextLine();
return this._currentLine;
}

public async nextLine(): Promise<string | undefined> {
this.opened = true;
const next = await this.lines.next();
this._isEof = Boolean(next.done);
this._currentLine = next.value;
return this.getCurLine();
}

public isEof(): boolean {
return this.line > this.lines.length;
return this._isEof;
}
}
Loading