Skip to content

Commit

Permalink
[Chore]: Technical: Translate table utils (#1742)
Browse files Browse the repository at this point in the history
* Reanamed js files

Signed-off-by: Maksim Suslov <maksim.suslov@actionengine.com>

* Moved types from d.ts

Signed-off-by: Maksim Suslov <maksim.suslov@actionengine.com>

* Fixed ts errors

Signed-off-by: Maksim Suslov <maksim.suslov@actionengine.com>

* Fixed tests

Signed-off-by: Maksim Suslov <maksim.suslov@actionengine.com>

* Applied suggested changes

Signed-off-by: Maksim Suslov <maksim.suslov@actionengine.com>
  • Loading branch information
HeimEndyd committed Mar 21, 2022
1 parent 7ee74eb commit 7a11260
Show file tree
Hide file tree
Showing 15 changed files with 367 additions and 408 deletions.
43 changes: 0 additions & 43 deletions src/utils/table-utils/data-container-utils.d.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,32 @@
import {RowDataContainer} from './row-data-container';
import {IndexedDataContainer} from './indexed-data-container';

/** @type {import('./data-container-utils').DataForm} */
import {DataContainerInterface} from './data-container-interface';
import {Field} from './kepler-table';

export type DataContainerOptions = {
inputDataFormat?: string; // one of DataForm
fields?: Field[];
};

export const DataForm = {
ROWS_ARRAY: 'ROWS_ARRAY'
};

/** @type {import('./data-container-utils').DataContainerOptions} */
const defaultOptions = {
const defaultOptions: DataContainerOptions = {
inputDataFormat: DataForm.ROWS_ARRAY
};

/** @type {typeof import('./data-container-utils').createDataContainer} */
export function createDataContainer(data, options = defaultOptions) {
/**
* Creates a data container wrapper for the data.
* @param data Data.
* @param options Options.
* @returns A data container object which is based on data and options.
*/
export function createDataContainer(
data: any[],
options: DataContainerOptions = defaultOptions
): DataContainerInterface {
options = {...defaultOptions, ...options};

if (options.inputDataFormat === DataForm.ROWS_ARRAY) {
Expand All @@ -42,17 +56,32 @@ export function createDataContainer(data, options = defaultOptions) {
throw Error('Failed to create a data container: not implemented format');
}

/** @type {typeof import('./data-container-utils').createIndexedDataContainer} */
export function createIndexedDataContainer(dataContainer, indices) {
/**
* Creates a data container wrapper around another data container.
* @param dataContainer Parent data container.
* @param indices An array of row indices in the parent data container.
*/
export function createIndexedDataContainer(
dataContainer: DataContainerInterface,
indices: number[]
): DataContainerInterface {
return new IndexedDataContainer(dataContainer, indices);
}

/** @type {typeof import('./data-container-utils').getSampleData} */
export function getSampleData(dataContainer, sampleSize = 500) {
/**
* Get a sample of rows from a data container.
* @param dataContainer Data container to get samples from.
* @param sampleSize Max number of samples.
* @returns A data container which contains samples from the original data container.
*/
export function getSampleData(
dataContainer: DataContainerInterface,
sampleSize = 500
): DataContainerInterface {
const numberOfRows = dataContainer.numRows();
const sampleStep = Math.max(Math.floor(numberOfRows / sampleSize), 1);

const indices = [];
const indices: number[] = [];
for (let i = 0; i < numberOfRows; i += sampleStep) {
indices.push(i);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// TODO: Move JDoc to correct place
// @ts-nocheck
import {DataContainerInterface} from './data-container-interface';

/**
Expand Down Expand Up @@ -25,12 +27,12 @@ export class DataRow {
constructor(dataContainer: DataContainerInterface | null, rowIndex: number);

/**
* Conditionally creates a DataRow object.
* @param sharedRowDesc Accepts forllowing options:
* - true indicates that new DataRow should be created.
* - falsy value or a DataRow object is passed through without any change.
* @returns A new DataRow object or unchanged input argument.
*/
* Conditionally creates a DataRow object.
* @param sharedRowDesc Accepts forllowing options:
* - true indicates that new DataRow should be created.
* - falsy value or a DataRow object is passed through without any change.
* @returns A new DataRow object or unchanged input argument.
*/
static createSharedRow(sharedRowDesc: SharedRowOptions): SharedRowOptionsResult;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,44 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

import {DataContainerInterface} from './data-container-interface';

export type SharedRowOptions = DataRow | boolean | undefined;
export type SharedRowOptionsResult = DataRow | false | undefined;

export class DataRow {
constructor(dataContainer, rowIndex) {
this.setSource(dataContainer, rowIndex);
_dataContainer: DataContainerInterface | null;

_rowIndex: number;

constructor(dataContainer: DataContainerInterface | null, rowIndex: number) {
this._dataContainer = dataContainer;
this._rowIndex = rowIndex;
}

static createSharedRow(sharedRowDesc) {
static createSharedRow(sharedRowDesc: SharedRowOptions): SharedRowOptionsResult {
if (sharedRowDesc === true) {
return new DataRow(null, 0);
}
return sharedRowDesc;
}

valueAt(columnIndex) {
return this._dataContainer.valueAt(this._rowIndex, columnIndex);
valueAt(columnIndex: number): any {
return this._dataContainer?.valueAt(this._rowIndex, columnIndex);
}

values() {
return this._dataContainer.rowAsArray(this._rowIndex);
values(): any[] {
return this._dataContainer ? this._dataContainer.rowAsArray(this._rowIndex) : [];
}

setSource(dataContainer, rowIndex) {
setSource(dataContainer: DataContainerInterface, rowIndex: number): void {
this._dataContainer = dataContainer;
this._rowIndex = rowIndex;
}

map(handler) {
const numColumns = this._dataContainer.numColumns();
const out = [];
map(handler: (elem: any, index: number) => any): any[] {
const numColumns = this._dataContainer?.numColumns() || 0;
const out: any[] = [];
for (let column = 0; column < numColumns; ++column) {
out[column] = handler(this.valueAt(column), column);
}
Expand Down
1 change: 0 additions & 1 deletion src/utils/table-utils/index.d.ts

This file was deleted.

File renamed without changes.
26 changes: 0 additions & 26 deletions src/utils/table-utils/indexed-data-container.d.ts

This file was deleted.

0 comments on commit 7a11260

Please sign in to comment.