Skip to content

Commit

Permalink
Move utility functions that don't need to be class members into a
Browse files Browse the repository at this point in the history
private namespace.
  • Loading branch information
ian-r-rose committed May 4, 2019
1 parent b899ab0 commit 94d9a3e
Showing 1 changed file with 55 additions and 52 deletions.
107 changes: 55 additions & 52 deletions packages/outputarea/src/model.ts
Expand Up @@ -237,7 +237,7 @@ export class OutputAreaModel implements IOutputAreaModel {
*/
set(index: number, value: nbformat.IOutput): void {
// Normalize stream data.
this._normalize(value);
Private.normalize(value);
let item = this._createItem({ value, trusted: this._trusted });
this.list.set(index, item);
}
Expand Down Expand Up @@ -303,7 +303,7 @@ export class OutputAreaModel implements IOutputAreaModel {
let trusted = this._trusted;

// Normalize the value.
this._normalize(value);
Private.normalize(value);

// Consolidate outputs if they are stream outputs of the same kind.
if (
Expand All @@ -315,9 +315,8 @@ export class OutputAreaModel implements IOutputAreaModel {
// text to the current item and replace the previous item.
// This also replaces the metadata of the last item.
this._lastStream += value.text as string;
this._lastStream = Private.removeOverwrittenChars(this._lastStream);
value.text = this._lastStream;
this._removeOverwrittenChars(value);
this._lastStream = value.text;
let item = this._createItem({ value, trusted });
let index = this.length - 1;
let prev = this.list.get(index);
Expand All @@ -327,7 +326,7 @@ export class OutputAreaModel implements IOutputAreaModel {
}

if (nbformat.isStream(value)) {
this._removeOverwrittenChars(value);
value.text = Private.removeOverwrittenChars(value.text as string);
}

// Create the new item.
Expand All @@ -345,53 +344,6 @@ export class OutputAreaModel implements IOutputAreaModel {
return this.list.push(item);
}

/**
* Normalize an output.
*/
private _normalize(value: nbformat.IOutput): void {
if (nbformat.isStream(value)) {
if (Array.isArray(value.text)) {
value.text = (value.text as string[]).join('\n');
}
}
}

/**
* Remove characters that are overridden by backspace characters.
*/
private _fixBackspace(txt: string): string {
let tmp = txt;
do {
txt = tmp;
// Cancel out anything-but-newline followed by backspace
tmp = txt.replace(/[^\n]\x08/gm, '');
} while (tmp.length < txt.length);
return txt;
}

/**
* Remove chunks that should be overridden by the effect of
* carriage return characters.
*/
private _fixCarriageReturn(txt: string): string {
txt = txt.replace(/\r+\n/gm, '\n'); // \r followed by \n --> newline
while (txt.search(/\r[^$]/g) > -1) {
const base = txt.match(/^(.*)\r+/m)[1];
let insert = txt.match(/\r+(.*)$/m)[1];
insert = insert + base.slice(insert.length, base.length);
txt = txt.replace(/\r+.*$/m, '\r').replace(/^.*\r/m, insert);
}
return txt;
}

/*
* Remove characters overridden by backspaces and carriage returns
*/
private _removeOverwrittenChars(value: nbformat.IOutput): void {
let tmp = value.text as string;
value.text = this._fixCarriageReturn(this._fixBackspace(tmp));
}

/**
* A flag that is set when we want to clear the output area
* *after* the next addition to it.
Expand Down Expand Up @@ -460,3 +412,54 @@ export namespace OutputAreaModel {
*/
export const defaultContentFactory = new ContentFactory();
}

/**
* A namespace for module-private functionality.
*/
namespace Private {
/**
* Normalize an output.
*/
export function normalize(value: nbformat.IOutput): void {
if (nbformat.isStream(value)) {
if (Array.isArray(value.text)) {
value.text = (value.text as string[]).join('\n');
}
}
}

/**
* Remove characters that are overridden by backspace characters.
*/
function fixBackspace(txt: string): string {
let tmp = txt;
do {
txt = tmp;
// Cancel out anything-but-newline followed by backspace
tmp = txt.replace(/[^\n]\x08/gm, '');
} while (tmp.length < txt.length);
return txt;
}

/**
* Remove chunks that should be overridden by the effect of
* carriage return characters.
*/
function fixCarriageReturn(txt: string): string {
txt = txt.replace(/\r+\n/gm, '\n'); // \r followed by \n --> newline
while (txt.search(/\r[^$]/g) > -1) {
const base = txt.match(/^(.*)\r+/m)[1];
let insert = txt.match(/\r+(.*)$/m)[1];
insert = insert + base.slice(insert.length, base.length);
txt = txt.replace(/\r+.*$/m, '\r').replace(/^.*\r/m, insert);
}
return txt;
}

/*
* Remove characters overridden by backspaces and carriage returns
*/
export function removeOverwrittenChars(text: string): string {
return fixCarriageReturn(fixBackspace(text));
}
}

0 comments on commit 94d9a3e

Please sign in to comment.