Skip to content

Commit

Permalink
Update sort for array with undefined fields
Browse files Browse the repository at this point in the history
  • Loading branch information
ViktorKukurba committed Mar 25, 2020
1 parent bb59cd2 commit 763e150
Show file tree
Hide file tree
Showing 14 changed files with 129 additions and 91 deletions.
4 changes: 4 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
dist
**/*/*.d.ts
./*js
12 changes: 12 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"root": true,
"parser": "@typescript-eslint/parser",
"plugins": [
"@typescript-eslint"
],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended"
]
}
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,5 +188,5 @@ module.exports = {
// watchPathIgnorePatterns: [],

// Whether to use watchman for file crawling
// watchman: true,
// watchman: true
};
15 changes: 10 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "datapipe-js",
"version": "0.2.7",
"version": "0.2.8",
"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 @@ -20,7 +20,9 @@
"docs": "npx typedoc src --plugin none",
"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"
"dev": "npx rollup --config rollup.config.dev.js --watch",
"lint": "eslint . --ext .ts",
"lint-fix": "eslint . --ext .ts --fix"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -48,6 +50,9 @@
],
"devDependencies": {
"@types/jest": "^24.9.1",
"@typescript-eslint/eslint-plugin": "^2.24.0",
"@typescript-eslint/parser": "^2.24.0",
"eslint": "^6.8.0",
"jest": "^24.9.0",
"jest-fetch-mock": "^2.1.2",
"rollup": "^1.31.1",
Expand All @@ -57,8 +62,8 @@
"rollup-plugin-typescript2": "^0.25.3",
"rollup-plugin-uglify": "^6.0.4",
"ts-jest": "^24.3.0",
"typedoc": "^0.15.8",
"typedoc-plugin-markdown": "^2.2.16",
"typescript": "^3.7.5"
"typedoc": "^0.17.3",
"typedoc-plugin-markdown": "^2.2.17",
"typescript": "^3.8.3"
}
}
18 changes: 9 additions & 9 deletions src/array/joins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,17 @@ export function fullJoin(
// build a lookup maps for both arrays.
// so, both of them have to be unique, otherwise it will flattern result
const leftArrayMap = Object.create(null);
for (let item of rightArray) {
for (const item of rightArray) {
leftArrayMap[leftKeySelector(item)] = item;
}

const rightArrayMap = Object.create(null);
for (let item of rightArray) {
for (const item of rightArray) {
rightArrayMap[rightKeySelector(item)] = item;
}

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

Expand All @@ -87,7 +87,7 @@ export function fullJoin(
}

// add remaining right items
for (let rightItemKey in rightArrayMap) {
for (const rightItemKey in rightArrayMap) {
const rightItem = rightArrayMap[rightItemKey];
const resultItem = resultSelector(null, rightItem);

Expand Down Expand Up @@ -119,16 +119,16 @@ export function merge(
// build a lookup maps for both arrays.
// so, both of them have to be unique, otherwise it will flattern result
const targetArrayMap = Object.create(null);
for (let item of sourceArray) {
for (const item of sourceArray) {
targetArrayMap[targetKeySelector(item)] = item;
}

const sourceArrayMap = Object.create(null);
for (let item of sourceArray) {
for (const item of sourceArray) {
sourceArrayMap[sourceKeySelector(item)] = item;
}

for (let sourceItemKey of Object.keys(sourceArrayMap)) {
for (const sourceItemKey of Object.keys(sourceArrayMap)) {
const sourceItem = sourceArrayMap[sourceItemKey];
if (!targetArrayMap[sourceItemKey]) {
targetArray.push(sourceItem);
Expand Down Expand Up @@ -184,12 +184,12 @@ function leftOrInnerJoin(

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

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

Expand Down
32 changes: 16 additions & 16 deletions src/array/stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import { isArrayEmptyOrNull } from "./utils";
* sum([{ val: 1 }, { val: 5 }], i => i.val); // 6
*/
export function sum(array: any[], field?: Selector | string): number | null {
if (isArrayEmptyOrNull(array)) { return null };
if (isArrayEmptyOrNull(array)) { return null }

const elementSelector = fieldSelector(field);

let sum: number = 0;
let sum = 0;
for (const item of array) {
const numberVal = parseNumber(item, elementSelector);
if (numberVal) {
Expand All @@ -38,7 +38,7 @@ export function sum(array: any[], field?: Selector | string): number | null {
* avg([1, 5, 3]); // 3
*/
export function avg(array: any[], field?: Selector | string): number | null {
if (isArrayEmptyOrNull(array)) { return null };
if (isArrayEmptyOrNull(array)) { return null }

const elementSelector = fieldSelector(field);

Expand Down Expand Up @@ -73,7 +73,7 @@ export function min(array: any[], field?: Selector | string): number | Date | nu
export function max(array: any[], field?: Selector | string): number | Date | null {
const elementSelector = fieldSelector(field);
const numberArray = getNumberValuesArray(array, elementSelector);
if (isArrayEmptyOrNull(numberArray)) { return null };
if (isArrayEmptyOrNull(numberArray)) { return null }
const max = Math.max(...numberArray);
const item = elementSelector ? elementSelector(array[0]) : array[0];
if (item instanceof Date) {
Expand Down Expand Up @@ -103,7 +103,7 @@ export function count(array: any[], predicate?: Predicate): number | null {
* @param predicate Predicate function invoked per iteration.
*/
export function first<T = any>(array: T[], predicate?: Predicate): T | null {
if (isArrayEmptyOrNull(array)) { return null };
if (isArrayEmptyOrNull(array)) { return null }

if (!predicate) {
return array[0];
Expand All @@ -122,7 +122,7 @@ export function first<T = any>(array: T[], predicate?: Predicate): T | null {
* @param predicate Predicate function invoked per iteration.
*/
export function last<T = any>(array: T[], predicate?: Predicate): T | null {
if (isArrayEmptyOrNull(array)) { return null };
if (isArrayEmptyOrNull(array)) { return null }

let lastIndex = array.length - 1;
if (!predicate) {
Expand All @@ -144,7 +144,7 @@ export function last<T = any>(array: T[], predicate?: Predicate): T | null {
* @param elementSelector Function invoked per iteration.
*/
export function countBy(array: any[], elementSelector: Selector): { [key: string]: number } {
if (!array || !Array.isArray(array)) { throw Error('No array provided') };
if (!array || !Array.isArray(array)) { throw Error('No array provided') }

const results: { [key: string]: number } = {};
const length = array.length;
Expand All @@ -165,7 +165,7 @@ export function countBy(array: any[], elementSelector: Selector): { [key: string
* @param field Property name or Selector function invoked per iteration.
*/
export function mean(array: any[], field?: Selector | string): number | null {
if (isArrayEmptyOrNull(array)) { return null };
if (isArrayEmptyOrNull(array)) { return null }

let res = 0;
for (let i = 0, c = 0, len = array.length; i < len; ++i) {
Expand All @@ -185,23 +185,23 @@ export function mean(array: any[], field?: Selector | string): number | null {
* @param p quantile.
*/
export function quantile(array: any[], p: number, field?: Selector | string): number | null {
if (isArrayEmptyOrNull(array)) { return null };
if (isArrayEmptyOrNull(array)) { return null }

const len = (array.length - 1) * p + 1;
const l = Math.floor(len);
const elementSelector = fieldSelector(field);
const val = elementSelector ? elementSelector(array[l - 1]) : array[l - 1];
const e = len - l;
return e ? val + e * (array[l] - val) : val;
};
}

/**
* Get sample variance of an array.
* @param array The array to process.
* @param field Property name or Selector function invoked per iteration.
*/
export function variance(array: any[], field?: Selector | string): number | null {
if (isArrayEmptyOrNull(array)) { return null };
if (isArrayEmptyOrNull(array)) { return null }

const elementSelector = fieldSelector(field);
if (!Array.isArray(array) || array.length < 2) {
Expand All @@ -218,34 +218,34 @@ export function variance(array: any[], field?: Selector | string): number | null
}
M2 = M2 / (c - 1);
return M2;
};
}

/**
* Get the sample standard deviation of an array.
* @param array The array to process.
* @param field Property name or Selector function invoked per iteration.
*/
export function stdev(array: any[], field?: Selector | string): number | null {
if (isArrayEmptyOrNull(array)) { return null };
if (isArrayEmptyOrNull(array)) { return null }

const varr = variance(array, field);

if (varr === null) { return null; }

return Math.sqrt(varr);
};
}

/**
* Get median of an array.
* @param array The array to process.
* @param field Property name or Selector function invoked per iteration.
*/
export function median(array: any[], field?: Selector | string): number | null {
if (isArrayEmptyOrNull(array)) { return null };
if (isArrayEmptyOrNull(array)) { return 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) => {
Expand Down
12 changes: 6 additions & 6 deletions src/array/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { sum } from "./stats";
* @param elementSelector Function invoked per iteration.
*/
export function groupBy(array: any[], groupByFields: string | string[] | Selector): any[] {
if (!Array.isArray(array)) { throw Error('An array is not provided') };
if (!Array.isArray(array)) { throw Error('An array is not provided') }

if (!array.length) { return array; }

Expand All @@ -33,15 +33,15 @@ export function groupBy(array: any[], groupByFields: string | string[] | Selecto
* flatten([1, 4, [2, [5, 5, [9, 7]], 11], 0]); // length 9
*/
export function flatten(array: any[]): any[] {
if (!Array.isArray(array)) { throw Error('An array is not provided') };
if (!Array.isArray(array)) { throw Error('An array is not provided') }

if (!array.length) { return array; }

let res: any = [];
const length = array.length;

for (let i = 0; i < length; i++) {
var value = array[i];
const value = array[i];
if (Array.isArray(value)) {
res = [...res, ...flatten(value)];
} else {
Expand All @@ -63,7 +63,7 @@ export function flatten(array: any[]): any[] {
export function pivot(array: any, rowFields: string | string[],columnField: string, dataField: string,
aggFunction?: (array: any[]) => any | null, columnValues?: string[]): any[] {

if (!Array.isArray(array)) { throw Error('An array is not provided') };
if (!Array.isArray(array)) { throw Error('An array is not provided') }

if (!array.length) { return array; }

Expand All @@ -90,7 +90,7 @@ export function pivot(array: any, rowFields: string | string[],columnField: stri
}
}

var result: any[] = [];
const result: any[] = [];
for (const groupName of Object.keys(groups)) {

const item = Object.create(null);
Expand Down Expand Up @@ -118,7 +118,7 @@ export function pivot(array: any, rowFields: string | string[],columnField: stri
* @param data
*/
export function transpose(data: any[]): any[] {
if (!Array.isArray(data)) { throw Error('An array is not provided') };
if (!Array.isArray(data)) { throw Error('An array is not provided') }

if (!data.length) { return data; }

Expand Down
Loading

0 comments on commit 763e150

Please sign in to comment.