Skip to content

Commit

Permalink
fixed linting errors and CSV parser
Browse files Browse the repository at this point in the history
  • Loading branch information
ppaska committed Apr 23, 2020
1 parent 763e150 commit 5eb7be8
Show file tree
Hide file tree
Showing 10 changed files with 240 additions and 229 deletions.
7 changes: 5 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended"
]
}
],
"rules": {
"@typescript-eslint/no-explicit-any": "off"
}
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "datapipe-js",
"version": "0.2.8",
"version": "0.2.9",
"description": "dataPipe is a JavaScript library for data manipulations, data transformations and data wrangling library inspired by LINQ (C#) and Pandas (Python)",
"main": "dist/data-pipe.min.js",
"module": "dist/data-pipe.esm.js",
Expand All @@ -21,7 +21,7 @@
"docs:md": "npx typedoc src --out md-docs --plugin typedoc-plugin-markdown",
"deploy": "npm run docs && npx gh-pages -d docs",
"dev": "npx rollup --config rollup.config.dev.js --watch",
"lint": "eslint . --ext .ts",
"lint": "npx eslint . --ext .ts",
"lint-fix": "eslint . --ext .ts --fix"
},
"repository": {
Expand Down
4 changes: 2 additions & 2 deletions src/_internals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ export function fieldSelector(input: string | string[] | Selector<any, string>):
if (typeof input === "function") {
return input;
} else if (typeof input === "string") {
return (item) => item[input];
return (item): any => item[input];
} else if (Array.isArray(input)) {
return (item) => input.map(r => item[r]).join('|');
return (item): any => input.map(r => item[r]).join('|');
} else {
throw Error(`Unknown input. Can't create a fieldSelector`)
}
Expand Down
128 changes: 64 additions & 64 deletions src/array/joins.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,69 @@
import { Selector } from "../types";
import { fieldSelector } from "../_internals";

function verifyJoinArgs(
leftArray: any[],
rightArray: any[],
leftKeySelector: (item: any) => string,
rightKeySelector: (item: any) => string,
resultSelector: (leftItem: any, rightItem: any) => any
): void {
if (!leftArray || !Array.isArray(leftArray)) {
throw Error('leftArray is not provided or not a valid')
}
if (!rightArray || !Array.isArray(rightArray)) {
throw Error('rightArray is not provided or not a valid')
}

if (typeof leftKeySelector !== 'function') {
throw Error('leftKeySelector is not provided or not a valid function')
}

if (typeof rightKeySelector !== 'function') {
throw Error('rightKeySelector is not provided or not a valid function')
}

if (typeof resultSelector !== 'function') {
throw Error('resultSelector is not provided or not a valid function')
}
}

function leftOrInnerJoin(
isInnerJoin: boolean,
leftArray: any[],
rightArray: any[],
leftKey: string | string[] | Selector<any, string>,
rightKey: string | string[] | Selector<any, string>,
resultSelector: (leftItem: any, rightItem: any) => any
): any[] {
const leftKeySelector = fieldSelector(leftKey);
const rightKeySelector = fieldSelector(rightKey);

verifyJoinArgs(leftArray, rightArray, leftKeySelector, rightKeySelector, resultSelector);

// build a lookup map
const rightArrayMap = Object.create(null);
for (const item of rightArray) {
rightArrayMap[rightKeySelector(item)] = item;
}

const result: any[] = [];
for (const leftItem of leftArray) {
const leftKey = leftKeySelector(leftItem);
const rightItem = rightArrayMap[leftKey] || null;

if (isInnerJoin && !rightItem) { continue; }

const resultItem = resultSelector(leftItem, rightItem);

// if result is null then probably a left item was modified
result.push(resultItem || leftItem);
}

return result;
}


/**
* leftJoin returns all elements from the left array (leftArray), and the matched elements from the right array (rightArray).
* The result is NULL from the right side, if there is no match.
Expand Down Expand Up @@ -114,7 +177,7 @@ export function merge(

const targetKeySelector = fieldSelector(targetKey);
const sourceKeySelector = fieldSelector(sourceKey);
verifyJoinArgs(targetArray, sourceArray, targetKeySelector, sourceKeySelector, () => { });
verifyJoinArgs(targetArray, sourceArray, targetKeySelector, sourceKeySelector, () => false);

// build a lookup maps for both arrays.
// so, both of them have to be unique, otherwise it will flattern result
Expand All @@ -140,66 +203,3 @@ export function merge(

return targetArray;
}


function verifyJoinArgs(
leftArray: any[],
rightArray: any[],
leftKeySelector: (item: any) => string,
rightKeySelector: (item: any) => string,
resultSelector: (leftItem: any, rightItem: any) => any
): void {
if (!leftArray || !Array.isArray(leftArray)) {
throw Error('leftArray is not provided or not a valid')
}
if (!rightArray || !Array.isArray(rightArray)) {
throw Error('rightArray is not provided or not a valid')
}

if (typeof leftKeySelector !== 'function') {
throw Error('leftKeySelector is not provided or not a valid function')
}

if (typeof rightKeySelector !== 'function') {
throw Error('rightKeySelector is not provided or not a valid function')
}

if (typeof resultSelector !== 'function') {
throw Error('resultSelector is not provided or not a valid function')
}
}

function leftOrInnerJoin(
isInnerJoin: boolean,
leftArray: any[],
rightArray: any[],
leftKey: string | string[] | Selector<any, string>,
rightKey: string | string[] | Selector<any, string>,
resultSelector: (leftItem: any, rightItem: any) => any
): any[] {
const leftKeySelector = fieldSelector(leftKey);
const rightKeySelector = fieldSelector(rightKey);

verifyJoinArgs(leftArray, rightArray, leftKeySelector, rightKeySelector, resultSelector);

// build a lookup map
const rightArrayMap = Object.create(null);
for (const item of rightArray) {
rightArrayMap[rightKeySelector(item)] = item;
}

const result: any[] = [];
for (const leftItem of leftArray) {
const leftKey = leftKeySelector(leftItem);
const rightItem = rightArrayMap[leftKey] || null;

if (isInnerJoin && !rightItem) { continue; }

const resultItem = resultSelector(leftItem, rightItem);

// if result is null then probably a left item was modified
result.push(resultItem || leftItem);
}

return result;
}
58 changes: 29 additions & 29 deletions src/array/stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,35 @@ import { Selector, Predicate } from "../types";
import { parseNumber } from "../utils";
import { isArrayEmptyOrNull } from "./utils";

function fieldSelector(field?: string | Selector): Selector {
if (!field) {
return (item: any): any => item;
}
return typeof field === 'function' ? field as Selector : (item: any): any => item[String(field)];
}

function fieldComparator(field?: string | Selector): (a: any, b: any) => number {
return (a: any, b: any): number => {
const aVal = parseNumber(a, fieldSelector(field));
const bVal = parseNumber(b, fieldSelector(field));

if (bVal === undefined) {
return 1;
}

if (aVal === undefined) {
return -1;
}

return aVal - bVal >= 0 ? 1 : -1;
}
}

function getNumberValuesArray(array: any[], field?: string | Selector): number[] {
const elementSelector = fieldSelector(field);
return array.map(item => parseNumber(item, elementSelector)).filter(v => v !== undefined) as number[];
}

/**
* Sum of items in array.
* @param array The array to process.
Expand Down Expand Up @@ -246,32 +275,3 @@ export function median(array: any[], field?: Selector | string): number | null {
array.sort(fieldComparator(field));
return quantile(getNumberValuesArray(array, field), 0.5);
}

function fieldComparator(field?: string | Selector): (a: any, b: any) => number {
return (a: any, b: any) => {
const aVal = parseNumber(a, fieldSelector(field));
const bVal = parseNumber(b, fieldSelector(field));

if (bVal === undefined) {
return 1;
}

if (aVal === undefined) {
return -1;
}

return aVal - bVal >= 0 ? 1 : -1;
}
}

function fieldSelector(field?: string | Selector): Selector {
if (!field) {
return (item: any) => item;
}
return typeof field === 'function' ? field as Selector : (item: any) => item[String(field)];
}

function getNumberValuesArray(array: any[], field?: string | Selector): number[] {
const elementSelector = fieldSelector(field);
return array.map(item => parseNumber(item, elementSelector)).filter(v => v !== undefined) as number[];
}
2 changes: 1 addition & 1 deletion src/array/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export function pivot(array: any, rowFields: string | string[],columnField: stri

const groups: { [key: string]: any[] } = Object.create(null);
columnValues = columnValues || [];
aggFunction = aggFunction || ((a: any[]) => sum(a));
aggFunction = aggFunction || ((a: any[]): number | null => sum(a));

const elementSelector = fieldSelector(rowFields);

Expand Down
Loading

0 comments on commit 5eb7be8

Please sign in to comment.