Skip to content
This repository has been archived by the owner on Aug 11, 2022. It is now read-only.

Commit

Permalink
Added prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
luvies committed Feb 28, 2019
1 parent f080409 commit b0e30b5
Show file tree
Hide file tree
Showing 8 changed files with 1,252 additions and 326 deletions.
5 changes: 5 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"singleQuote": true,
"trailingComma": "all",
"endOfLine": "lf"
}
14 changes: 8 additions & 6 deletions lib/aggregates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export function average<TSource>(
if (ccount === 0) {
throw new Error(Errors.Empty);
}
return total / ccount as any;
return (total / ccount) as any;
}

/**
Expand Down Expand Up @@ -187,7 +187,7 @@ export function count<TElement>(
function getElementAt<TElement>(
iterable: Iterable<TElement>,
index: number,
): { found: false } | { found: true, element: TElement } {
): { found: false } | { found: true; element: TElement } {
let cindex = 0;
for (const element of iterable) {
if (cindex === index) {
Expand Down Expand Up @@ -241,7 +241,7 @@ export function elementAtOrDefault<TElement>(
function getFirst<TElement>(
iterable: Iterable<TElement>,
predicate: BoolPredicate<TElement> = () => true,
): { items: false } | { items: true, element: TElement } {
): { items: false } | { items: true; element: TElement } {
for (const element of iterable) {
if (predicate(element)) {
return { items: true, element };
Expand Down Expand Up @@ -331,7 +331,7 @@ export function iterableEquals<TElement>(
function getLast<TElement>(
iterable: Iterable<TElement>,
predicate: BoolPredicate<TElement> = () => true,
): { items: false } | { items: true, element: TElement } {
): { items: false } | { items: true; element: TElement } {
let items = false;
let latest: TElement;
for (const element of iterable) {
Expand Down Expand Up @@ -449,7 +449,9 @@ export function min<TSource>(
*/
export function resolveAll<TElement>(
iterable: Iterable<TElement>,
): Promise<TElement extends PromiseLike<infer TResult> ? TResult[] : TElement[]> {
): Promise<
TElement extends PromiseLike<infer TResult> ? TResult[] : TElement[]
> {
return Promise.all(iterable) as any;
}

Expand All @@ -459,7 +461,7 @@ export function resolveAll<TElement>(
function getSingle<TElement>(
iterable: Iterable<TElement>,
predicate: BoolPredicate<TElement>,
): { found: false } | { found: true, element: TElement } {
): { found: false } | { found: true; element: TElement } {
for (const element of iterable) {
if (predicate(element)) {
return { found: true, element };
Expand Down
141 changes: 98 additions & 43 deletions lib/iterators.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import * as aggregates from './aggregates.ts';
import { AggFn, BoolPredicate, CallbackFn, ComparerFn, MapFn, StrFn } from './aggregates.ts';
import {
AggFn,
BoolPredicate,
CallbackFn,
ComparerFn,
MapFn,
StrFn,
} from './aggregates.ts';

// Helpers types.

Expand All @@ -11,7 +18,10 @@ type IndexMapFn<TSource, TResult> = (source: TSource, index: number) => TResult;
/**
* A function that combines 2 types into another.
*/
type CombineFn<TFirst, TSecond, TResult> = (first: TFirst, second: TSecond) => TResult;
type CombineFn<TFirst, TSecond, TResult> = (
first: TFirst,
second: TSecond,
) => TResult;
/**
* A function that takes in 2 values and returns the sorting number.
*/
Expand All @@ -24,8 +34,10 @@ type IndexPredicate<TSource> = (element: TSource, index: number) => boolean;
* A function that takes in a value and an index and returns whether the
* value is of a given type.
*/
type IndexIsPredicate<TSource, TResult extends TSource> =
(element: TSource, index: number) => element is TResult;
type IndexIsPredicate<TSource, TResult extends TSource> = (
element: TSource,
index: number,
) => element is TResult;

/**
* A grouping of elements based on the key.
Expand Down Expand Up @@ -91,7 +103,10 @@ export abstract class Lazy<TElement> implements Iterable<TElement> {
* account that some lazy iterators *require* the interable to be finite to work.
* Check the remarks on the function you want to use to see which ones will work.
*/
public static repeat<TElement>(element: TElement, count?: number): Lazy<TElement> {
public static repeat<TElement>(
element: TElement,
count?: number,
): Lazy<TElement> {
return new LazyRepeat(element, count);
}

Expand Down Expand Up @@ -278,8 +293,14 @@ export abstract class Lazy<TElement> implements Iterable<TElement> {
* @remarks This will iterate until the condition is satisfied, or until the
* iterable ends.
*/
public firstOrDefault(defaultValue: TElement, predicate: BoolPredicate<TElement>): TElement;
public firstOrDefault(defaultValue: TElement, predicate?: BoolPredicate<TElement>) {
public firstOrDefault(
defaultValue: TElement,
predicate: BoolPredicate<TElement>,
): TElement;
public firstOrDefault(
defaultValue: TElement,
predicate?: BoolPredicate<TElement>,
) {
return aggregates.firstOrDefault(this, defaultValue, predicate);
}

Expand All @@ -303,7 +324,10 @@ export abstract class Lazy<TElement> implements Iterable<TElement> {
* @remarks This will check for both order and value, and will iterate
* both iterables completely.
*/
public iterableEquals(second: Iterable<TElement>, comparer?: ComparerFn<TElement>) {
public iterableEquals(
second: Iterable<TElement>,
comparer?: ComparerFn<TElement>,
) {
return aggregates.iterableEquals(this, second, comparer);
}

Expand Down Expand Up @@ -343,8 +367,14 @@ export abstract class Lazy<TElement> implements Iterable<TElement> {
* satisfied the condition.
* @remarks This will cause a complete iteration of the iterable object.
*/
public lastOrDefault(defaultValue: TElement, predicate: BoolPredicate<TElement>): TElement;
public lastOrDefault(defaultValue: TElement, predicate?: BoolPredicate<TElement>) {
public lastOrDefault(
defaultValue: TElement,
predicate: BoolPredicate<TElement>,
): TElement;
public lastOrDefault(
defaultValue: TElement,
predicate?: BoolPredicate<TElement>,
) {
return aggregates.lastOrDefault(this, defaultValue, predicate);
}

Expand Down Expand Up @@ -394,8 +424,12 @@ export abstract class Lazy<TElement> implements Iterable<TElement> {
* @returns A promise that will resolve to a lazy iterable object.
* @remarks This will cause a complete iteration of the iterable object.
*/
public resolveAll(): Promise<TElement extends PromiseLike<infer TResult> ? Lazy<TResult> : Lazy<TElement>> {
return aggregates.resolveAll(this).then(iterable => Lazy.from(iterable)) as any;
public resolveAll(): Promise<
TElement extends PromiseLike<infer TResult> ? Lazy<TResult> : Lazy<TElement>
> {
return aggregates
.resolveAll(this)
.then(iterable => Lazy.from(iterable)) as any;
}

/**
Expand All @@ -421,7 +455,10 @@ export abstract class Lazy<TElement> implements Iterable<TElement> {
* @remarks This will iterate until the condition is met or until the iterable
* ends.
*/
public singleOrDefault(predicate: BoolPredicate<TElement>, defaultValue: TElement) {
public singleOrDefault(
predicate: BoolPredicate<TElement>,
defaultValue: TElement,
) {
return aggregates.singleOrDefault(this, predicate, defaultValue);
}

Expand Down Expand Up @@ -482,7 +519,10 @@ export abstract class Lazy<TElement> implements Iterable<TElement> {
* @returns A `Map<TKey, TResult>` derived from the iterable.
* @remarks This will cause a complete iteration of the iterable object.
*/
public toMap<TKey, TResult = TElement>(keyFn: MapFn<TElement, TKey>, valueFn?: MapFn<TElement, TResult>) {
public toMap<TKey, TResult = TElement>(
keyFn: MapFn<TElement, TKey>,
valueFn?: MapFn<TElement, TResult>,
) {
return aggregates.toMap(this, keyFn, valueFn);
}

Expand Down Expand Up @@ -565,7 +605,9 @@ export abstract class Lazy<TElement> implements Iterable<TElement> {
* @remarks When this is iterated (not before), the underlying iterator is walked through
* completely.
*/
public groupBy<TKey>(keyFn: MapFn<TElement, TKey>): Lazy<IGrouping<TKey, TElement>>;
public groupBy<TKey>(
keyFn: MapFn<TElement, TKey>,
): Lazy<IGrouping<TKey, TElement>>;
/**
* Groups the elements by key and projects each element using the given function.
* @param keyFn The function to extract the key from each element.
Expand All @@ -591,11 +633,7 @@ export abstract class Lazy<TElement> implements Iterable<TElement> {
elementSelector: MapFn<TElement, TItem>,
resultSelector: CombineFn<TKey, Iterable<TItem>, TResult>,
): Lazy<TResult>;
public groupBy<
TKey,
TItem = TElement,
TResult = IGrouping<TKey, TElement>
>(
public groupBy<TKey, TItem = TElement, TResult = IGrouping<TKey, TElement>>(
keyFn: MapFn<TElement, TKey>,
elementSelector?: MapFn<TElement, TItem>,
resultSelector?: CombineFn<TKey, Iterable<TItem>, TResult>,
Expand Down Expand Up @@ -719,7 +757,9 @@ export abstract class Lazy<TElement> implements Iterable<TElement> {
* @param selector The transformation function to use for each element.
* @remarks Does not cause additional unexpected iteration.
*/
public select<TResult>(selector: IndexMapFn<TElement, TResult>): Lazy<TResult> {
public select<TResult>(
selector: IndexMapFn<TElement, TResult>,
): Lazy<TResult> {
return new LazySelect(this, selector);
}

Expand All @@ -730,7 +770,9 @@ export abstract class Lazy<TElement> implements Iterable<TElement> {
* is the index that the element was at in the source iterable, *not* the resulting one.
* @remarks Does not cause additional unexpected iteration.
*/
public selectMany<TResult>(selector: IndexMapFn<TElement, Iterable<TResult>>): Lazy<TResult> {
public selectMany<TResult>(
selector: IndexMapFn<TElement, Iterable<TResult>>,
): Lazy<TResult> {
return new LazySelectMany(this, selector);
}

Expand Down Expand Up @@ -902,9 +944,7 @@ class LazyEmpty<TElement> extends Lazy<TElement> {
* @hidden
*/
class LazyIterator<TElement> extends Lazy<TElement> {
public constructor(
private readonly _iterable: Iterable<TElement>,
) {
public constructor(private readonly _iterable: Iterable<TElement>) {
super();
}

Expand Down Expand Up @@ -960,7 +1000,10 @@ class LazyIterator<TElement> extends Lazy<TElement> {
return super.first();
}

public firstOrDefault(defaultValue: TElement, predicate?: BoolPredicate<TElement>) {
public firstOrDefault(
defaultValue: TElement,
predicate?: BoolPredicate<TElement>,
) {
if (predicate) {
return super.firstOrDefault(defaultValue, predicate);
}
Expand Down Expand Up @@ -992,7 +1035,10 @@ class LazyIterator<TElement> extends Lazy<TElement> {
return super.last();
}

public lastOrDefault(defaultValue: TElement, predicate?: BoolPredicate<TElement>) {
public lastOrDefault(
defaultValue: TElement,
predicate?: BoolPredicate<TElement>,
) {
if (predicate) {
return super.lastOrDefault(defaultValue, predicate);
}
Expand Down Expand Up @@ -1092,9 +1138,7 @@ class LazyAppendPrepend<TElement> extends Lazy<TElement> {
class LazyConcat<TElement> extends Lazy<TElement> {
private readonly _iterables: Array<Iterable<TElement>>;

public constructor(
..._iterables: Array<Iterable<TElement>>
) {
public constructor(..._iterables: Array<Iterable<TElement>>) {
super();
this._iterables = _iterables;
}
Expand Down Expand Up @@ -1135,7 +1179,8 @@ class LazyDefaultIfEmpty<TElement> extends Lazy<TElement> {
class LazyDistinct<TElement, TKey = TElement> extends Lazy<TElement> {
public constructor(
private readonly _iterable: Iterable<TElement>,
private readonly _compareOn: MapFn<TElement, TKey> = ((element: TElement) => element) as any,
private readonly _compareOn: MapFn<TElement, TKey> = ((element: TElement) =>
element) as any,
) {
super();
}
Expand All @@ -1159,7 +1204,8 @@ class LazyExcept<TElement, TKey = TElement> extends Lazy<TElement> {
public constructor(
private readonly _firstIterable: Iterable<TElement>,
private readonly _secondIterable: Iterable<TElement>,
private readonly _compareOn: MapFn<TElement, TKey> = ((element: TElement) => element) as any,
private readonly _compareOn: MapFn<TElement, TKey> = ((element: TElement) =>
element) as any,
) {
super();
}
Expand Down Expand Up @@ -1189,13 +1235,17 @@ class LazyGroupBy<
TKey,
TElement = TSource,
TResult = IGrouping<TKey, TElement>
> extends Lazy<TResult> {
> extends Lazy<TResult> {
public constructor(
private readonly _iterable: Iterable<TSource>,
private readonly _keyFn: MapFn<TSource, TKey>,
private readonly _elementSelector: MapFn<TSource, TElement> = source => source as any,
private readonly _resultSelector: CombineFn<TKey, Iterable<TElement>, TResult> =
(key, elements) => ({ key, elements }) as any,
private readonly _elementSelector: MapFn<TSource, TElement> = source =>
source as any,
private readonly _resultSelector: CombineFn<
TKey,
Iterable<TElement>,
TResult
> = (key, elements) => ({ key, elements } as any),
) {
super();
}
Expand Down Expand Up @@ -1261,7 +1311,8 @@ class LazyIntersect<TElement, TKey = TElement> extends Lazy<TElement> {
public constructor(
private readonly _firstIterable: Iterable<TElement>,
private readonly _secondIterable: Iterable<TElement>,
private readonly _compareOn: MapFn<TElement, TKey> = ((element: TElement) => element) as any,
private readonly _compareOn: MapFn<TElement, TKey> = ((element: TElement) =>
element) as any,
) {
super();
}
Expand Down Expand Up @@ -1359,9 +1410,7 @@ class LazyOrderBy<TElement, TKey> extends Lazy<TElement> {
* @hidden
*/
class LazyReverse<TElement> extends Lazy<TElement> {
public constructor(
private readonly _iterable: Iterable<TElement>,
) {
public constructor(private readonly _iterable: Iterable<TElement>) {
super();
}

Expand Down Expand Up @@ -1581,7 +1630,8 @@ class LazyUnion<TElement, TKey = TElement> extends Lazy<TElement> {
public constructor(
private readonly _firstIterable: Iterable<TElement>,
private readonly _secondIterable: Iterable<TElement>,
private readonly _compareOn: MapFn<TElement, TKey> = ((element: TElement) => element) as any,
private readonly _compareOn: MapFn<TElement, TKey> = ((element: TElement) =>
element) as any,
) {
super();
}
Expand Down Expand Up @@ -1625,11 +1675,16 @@ class LazyWhere<TElement> extends Lazy<TElement> {
/**
* @hidden
*/
class LazyZip<TFirst, TSecond, TResult = [TFirst, TSecond]> extends Lazy<TResult> {
class LazyZip<TFirst, TSecond, TResult = [TFirst, TSecond]> extends Lazy<
TResult
> {
public constructor(
private readonly _firstIterable: Iterable<TFirst>,
private readonly _secondIterable: Iterable<TSecond>,
private readonly _selector: CombineFn<TFirst, TSecond, TResult> = (first, second) => [first, second] as any,
private readonly _selector: CombineFn<TFirst, TSecond, TResult> = (
first,
second,
) => [first, second] as any,
) {
super();
}
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@
"prepare": "rm -rf dist/* && deno --allow-read --allow-write https://gist.githubusercontent.com/luvies/d85fc46c5b4e255b845743082bdbc3c0/raw/fcad5914596c75f72868500c87493f5cc569629e/node_prebuild.ts --in lib --out prebuild && tsc --project ./tsconfig.build.json",
"test": "deno ./test/run_tests.ts",
"init-types": "deno --prefetch ./test/run_tests.ts",
"lint": "tslint --project .",
"lint": "tslint --project . && prettier --check 'lib/**/*.ts' 'test/**/*.ts'",
"fix": "tslint --project . --fix && prettier --check --write 'lib/**/*.ts' 'test/**/*.ts'",
"docs": "yarn prepare && typedoc --out docs ./prebuild --mode file"
},
"devDependencies": {
"deno_ls_plugin": "^0.1.0",
"prettier": "^1.16.4",
"tslint": "^5.13.0",
"tslint-config-prettier": "^1.18.0",
"tslint-eslint-rules": "^5.4.0",
Expand Down
Loading

0 comments on commit b0e30b5

Please sign in to comment.