Skip to content

Commit

Permalink
feat: Refactored project structure
Browse files Browse the repository at this point in the history
  • Loading branch information
iFaxity committed Jun 22, 2022
1 parent a49f9e4 commit f54ef53
Show file tree
Hide file tree
Showing 70 changed files with 12,874 additions and 30,450 deletions.
41,091 changes: 11,673 additions & 29,418 deletions package-lock.json

Large diffs are not rendered by default.

28 changes: 7 additions & 21 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,12 @@
"dist"
],
"scripts": {
"prebuild": "npm run clean",
"build": "node ./bin/build",
"clean": "rm -rf dist types",
"build": "vite build",
"build:types": "tsc --emitDeclarationOnly",
"lint": "tsc --noEmit",
"docs": "typedoc src",
"pretest": "rm -rf .nyc_output",
"test": "cypress run",
"posttest": "rm -rf .nyc_output"
"test": "vitest",
"coverage": "vitest run --coverage"
},
"nyc": {
"reporter": "lcov",
Expand All @@ -46,25 +45,12 @@
]
},
"devDependencies": {
"@babel/core": "^7.12.3",
"@babel/plugin-proposal-class-properties": "^7.12.1",
"@babel/preset-env": "^7.12.1",
"@babel/preset-typescript": "^7.12.1",
"@cypress/code-coverage": "^3.8.3",
"@cypress/webpack-preprocessor": "^5.4.10",
"@semantic-release/changelog": "^5.0.1",
"@semantic-release/git": "^9.0.0",
"@types/chai": "^4.2.14",
"@types/mocha": "^8.0.3",
"@types/node": "^14.14.6",
"babel-loader": "^8.1.0",
"babel-plugin-istanbul": "^6.0.0",
"chai": "^4.2.0",
"cypress": "^5.5.0",
"esbuild": "^0.8.2",
"tslib": "^1.14.1",
"typedoc": "^0.19.2",
"typescript": "^4.0.5",
"webpack": "^4.44.2"
"vite": "^2.9.12",
"vitest": "^0.15.2"
}
}
39 changes: 39 additions & 0 deletions src/aggregate/aggregate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Accumulator, Collector, enumerate } from '../core';

/**
* Applies an accumulator function over an array.
* @param accumulator - An accumulator function to be invoked on each element.
* @param seed - The initial accumulator value.
* @returns The transformed final accumulator value.
*/
export function aggregate<T, TAcc, TRes = any>(accumulator: Accumulator<T, TAcc, TRes>): Collector<T, TRes>;
export function aggregate<T, TAcc, TRes>(seed: TAcc, accumulator: Accumulator<T, TAcc, TRes>): Collector<T, TRes>;
export function aggregate<T, TAcc, TRes>(...args: any[]): Collector<T, TRes> {
return function(source) {
let seed: T|TAcc|TRes;
let accumulator: Accumulator<T, TAcc, TRes>;
let idx = 0;

const e = enumerate(source);

if (args.length >= 2) {
[ seed, accumulator ] = args;
} else {
accumulator = args[0];

if (e.moveNext()) {
seed = e.current;

// TODO: This is right?
idx += 1;
}
}

while(e.moveNext()) {
seed = accumulator(seed as TAcc, e.current, idx++);
}

return seed as TRes;
};
}

24 changes: 24 additions & 0 deletions src/aggregate/average.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Selector, Collector } from '../core';

/**
* Computes the average of an array of numbers that are obtained by invoking
* a transform function on each element of the input array.
* @param selector - A transform function to apply to each element.
* @returns The average of the array.
*/
export function average<T>(selector?: Selector<T, number>): Collector<T, number> {
if (typeof selector != 'function') {
selector = Number;
}

return function(source) {
let len = 0;
let acc = 0;

for (let item of source) {
acc += selector(item, len++);
}

return acc / len;
};
}
24 changes: 24 additions & 0 deletions src/aggregate/count.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Collector, Predicate, enumerate } from '../core';
import { where } from '../projection';

/**
* Returns a number that represents how many elements in the specified array satisfy a condition.
* @param predicate - A function to test each element for a condition.
* @returns A number that represents how many elements in the array satisfy the condition in the predicate function.
*/
export function count<T>(predicate?: Predicate<T>): Collector<T, number> {
return function(source) {
if (typeof predicate == 'function') {
source = where(predicate)(source);
}

const e = enumerate(source);
let acc = 0;

while (e.moveNext()) {
acc += 1;
}

return acc;
};
}
6 changes: 6 additions & 0 deletions src/aggregate/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export * from './aggregate';
export * from './average';
export * from './count';
export * from './max';
export * from './min';
export * from './sum';
28 changes: 28 additions & 0 deletions src/aggregate/max.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Selector, Collector, enumerate } from '../core';
import { select } from '../projection';

/**
* Returns the maximum value in an array of numbers. Optionally with a element transform function.
* @param selector - A transform function to apply to each element.
* @returns A number that corresponds to the maximum value in the array.
*/
export function max<T>(selector?: Selector<T, number>): Collector<T, number> {
if (typeof selector == 'function') {
selector = Number;
}

return function(source) {
const e = enumerate<number>(select(selector)(source));

e.moveNext();
let maximum = e.current;

while (e.moveNext()) {
if (e.current > maximum) {
maximum = e.current;
}
}

return maximum;
};
}
28 changes: 28 additions & 0 deletions src/aggregate/min.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Selector, Collector, enumerate } from '../core';
import { select } from '../projection';

/**
* Returns the minimum value in an array of numbers. Optionally with a element transform function.
* @param selector - A transform function to apply to each element.
* @returns A number that corresponds to the minimum value in the array.
*/
export function min<T>(selector?: Selector<T, number>): Collector<T, number> {
if (typeof selector == 'function') {
selector = Number;
}

return function(source) {
const e = enumerate<number>(select(selector)(source));

e.moveNext();
let minimum = e.current;

while (e.moveNext()) {
if (e.current < minimum) {
minimum = e.current;
}
}

return minimum;
};
}
24 changes: 24 additions & 0 deletions src/aggregate/sum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Selector, Collector } from '../core';
import { select, pipe } from '../projection';

/**
* Computes the sum of the array of number values that are obtained by invoking
* a transform function on each element of the input array.
* @param transform - A transform function to apply to each element.
* @returns The sum of the projected values.
*/
export function sum<T>(transform?: Selector<T, number>): Collector<T, number> {
if (typeof transform == 'function') {
return pipe(select(transform), sum());
}

return function(source) {
let acc = 0;

for (let item of source) {
acc += Number(item);
}

return acc;
};
}
Loading

0 comments on commit f54ef53

Please sign in to comment.