Skip to content

Commit

Permalink
feat!: change name of conflicting functions
Browse files Browse the repository at this point in the history
  • Loading branch information
rob893 committed Feb 16, 2022
1 parent dc5636f commit eeeabac
Show file tree
Hide file tree
Showing 16 changed files with 146 additions and 173 deletions.
152 changes: 39 additions & 113 deletions src/__benchmarks__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,120 +8,46 @@ import { sumBenchmark } from './sum';
import { listBenchmark } from './list';
import { queueBenchmark } from './queue';
import { xorBenchmark } from './xor';
import { IEnumerable } from '../types';
import { ArrayEnumerable } from '../enumerables/ArrayEnumerable';
import { BasicEnumerable } from '../enumerables/BasicEnumerable';
import { from } from '../functions/from';

declare global {
// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface Array<T> extends Omit<IEnumerable<T>, 'forEach' | 'toString' | 'any'> {}
// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface Map<K, V> extends Omit<IEnumerable<[K, V]>, 'forEach' | 'toString' | 'any'> {}
// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface Set<T> extends Omit<IEnumerable<T>, 'forEach' | 'toString' | 'any'> {}
// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface String extends Omit<IEnumerable<string>, 'forEach' | 'toString' | 'any'> {}
}

const arrEnumProps = ArrayEnumerable.prototype;
const enumProps = BasicEnumerable.prototype;
const arrProps = Array.prototype;
const nativeProps = new Set(Object.getOwnPropertyNames(arrProps));
const protos = [
[Array.prototype, Array.name],
[Map.prototype, Map.name],
[Set.prototype, Set.name],
[String.prototype, String.name]
];
const nativeProtos = new Set(protos.map(([p]) => Object.getOwnPropertyNames(p)).flat());

const enumPropNames = Object.getOwnPropertyNames(enumProps);
const arrEnumPropNames = Object.getOwnPropertyNames(arrEnumProps);
const args = process.argv.slice(2);

for (const [proto, protoName] of protos) {
for (const prop of enumPropNames) {
if (!nativeProtos.has(prop)) {
(proto as any)[prop as any] = function (...params: any[]) {
return (from(this) as any)[prop as any](...params);
};
} else {
console.log(`${prop} is a collision on ${protoName}!!!`);
}
}
if (args.length === 0) {
throw new Error('Benchmark name must be passed.');
}

// declare global {
// interface Array<T> {
// where(predicate: (item: T) => boolean): IEnumerable<T>;
// // take(amount: number): Array<T>;
// }
// }

// const arrProto = Array.prototype;

// arrProto.where = function <T>(predicate: (item: T) => boolean): IEnumerable<T> {
// return from(this as unknown as T[]).where(predicate);
// };

//arrProto.slice;

console.log(
[1, 2, 3]
.pipe(x => console.log(x))
.where(e => e % 2 === 1)
.take(1)
.toArray()
);

console.log(
new Map([
[1, 'a'],
[2, 'b']
])
.where(([k, v]) => k === 2)
.select(([_, v]) => v)
.toArray()
);
// const args = process.argv.slice(2);

// if (args.length === 0) {
// throw new Error('Benchmark name must be passed.');
// }

// const benchmark = args[0];

// switch (benchmark.toLowerCase()) {
// case 'any':
// anyBenchmark();
// break;
// case 'all':
// allBenchmark();
// break;
// case 'where':
// whereBenchmark();
// break;
// case 'complex':
// complexBenchmark();
// break;
// case 'count':
// countBenchmark();
// break;
// case 'select':
// selectBenchmark();
// break;
// case 'sum':
// sumBenchmark();
// break;
// case 'list':
// listBenchmark();
// break;
// case 'queue':
// queueBenchmark();
// break;
// case 'xor':
// xorBenchmark();
// break;
// default:
// throw new Error(`${benchmark} is not a valid benchmark name.`);
// }
const benchmark = args[0];

switch (benchmark.toLowerCase()) {
case 'any':
anyBenchmark();
break;
case 'all':
allBenchmark();
break;
case 'where':
whereBenchmark();
break;
case 'complex':
complexBenchmark();
break;
case 'count':
countBenchmark();
break;
case 'select':
selectBenchmark();
break;
case 'sum':
sumBenchmark();
break;
case 'list':
listBenchmark();
break;
case 'queue':
queueBenchmark();
break;
case 'xor':
xorBenchmark();
break;
default:
throw new Error(`${benchmark} is not a valid benchmark name.`);
}
6 changes: 3 additions & 3 deletions src/__tests__/concat.ts → src/__tests__/concatenate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { getEnumerables } from '../__test-utilities__/utilities';

describe.each([...getEnumerables()])('concat', (src, enumerable) => {
it('should return an Enumerable', () => {
const result = enumerable(src([1, 2, 3])).concat([4, 5, 6]);
const result = enumerable(src([1, 2, 3])).concatenate([4, 5, 6]);

expect(result).toBeInstanceOf(BasicEnumerable);
});
Expand All @@ -20,15 +20,15 @@ describe.each([...getEnumerables()])('concat', (src, enumerable) => {
[[], [1, 1], [1, 1]],
[[1, 1], [], [1, 1]]
])('should add elements to end of sequence', (first, second, expected) => {
const result = enumerable(src<unknown>(first)).concat(src<unknown>(second)).toArray();
const result = enumerable(src<unknown>(first)).concatenate(src<unknown>(second)).toArray();

expect(result).toEqual(expected);
});

it('should concat several collections', () => {
const items = [1, 2, 3];

const result = from(items).concat([4, 5], [], [6], [7, 8, 9]).toArray();
const result = from(items).concatenate([4, 5], [], [6], [7, 8, 9]).toArray();

expect(result).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9]);
});
Expand Down
10 changes: 6 additions & 4 deletions src/__tests__/join.ts → src/__tests__/innerJoin.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { join } from '../functions/join';
import { innerJoin } from '../functions/innerJoin';
import { BasicEnumerable } from '../enumerables/BasicEnumerable';
import { getEnumerables } from '../__test-utilities__/utilities';

describe.each([...getEnumerables()])('join', (src, enumerable) => {
it.each([[1, 2, 3], new Set([1, 2, 3]), '123', new Map()])('should return an Enumerable', collection => {
const result = join<unknown, unknown, unknown, unknown>(
const result = innerJoin<unknown, unknown, unknown, unknown>(
collection,
[],
x => x,
Expand Down Expand Up @@ -34,7 +34,7 @@ describe.each([...getEnumerables()])('join', (src, enumerable) => {
const pets = enumerable(src([barley, boots, whiskers, daisy, scratchy]));

const result = people
.join(
.innerJoin(
pets,
person => person,
pet => pet.owner,
Expand Down Expand Up @@ -69,7 +69,7 @@ describe.each([...getEnumerables()])('join', (src, enumerable) => {
const pets = enumerable(src([barley, boots, whiskers, daisy, scratchy]));

const result = people
.join(
.innerJoin(
pets,
person => person,
pet => pet.owner,
Expand All @@ -86,3 +86,5 @@ describe.each([...getEnumerables()])('join', (src, enumerable) => {
]);
});
});

new Map().forEach;
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,31 @@ import { getEnumerables } from '../__test-utilities__/utilities';

describe.each([...getEnumerables()])('reverse', (src, enumerable, addSrc) => {
it('should return an Enumberable', () => {
const result = enumerable(src([1, 2, 3])).reverse();
const result = enumerable(src([1, 2, 3])).reverseImmutable();

expect(result).toBeInstanceOf(BasicEnumerable);
});

it('should reverse the elements', () => {
const items = src([1, 2, 3]);

const result = enumerable(items).reverse().toArray();
const result = enumerable(items).reverseImmutable().toArray();

expect(result).toEqual([3, 2, 1]);
});

it('should not mutate src', () => {
const items = src([1, 2, 3]);

const _ = enumerable(items).reverse().toArray();
const _ = enumerable(items).reverseImmutable().toArray();

expect([...items]).toEqual([1, 2, 3]);
});

it('should have deferred execution', () => {
const items = src([1, 2, 3]);

const result = enumerable(items).reverse();
const result = enumerable(items).reverseImmutable();

addSrc(items, 4, 5);

Expand Down
42 changes: 25 additions & 17 deletions src/enumerables/BasicEnumerable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { any } from '../functions/any';
import { applyAppend } from '../functions/applicators/applyAppend';
import { applyAsEnumerable } from '../functions/applicators/applyAsEnumerable';
import { applyChunk } from '../functions/applicators/applyChunk';
import { applyConcat } from '../functions/applicators/applyConcat';
import { applyConcatenate } from '../functions/applicators/applyConcatenate';
import { applyDefaultIfEmpty } from '../functions/applicators/applyDefaultIfEmpty';
import { applyDistinct } from '../functions/applicators/applyDistinct';
import { applyExcept } from '../functions/applicators/applyExcept';
Expand All @@ -13,7 +13,7 @@ import { applyFullJoinHeterogeneous, applyFullJoinHomogeneous } from '../functio
import { applyGroupBy } from '../functions/applicators/applyGroupBy';
import { applyGroupJoin } from '../functions/applicators/applyGroupJoin';
import { applyIntersect } from '../functions/applicators/applyIntersect';
import { applyJoin } from '../functions/applicators/applyJoin';
import { applyInnerJoin } from '../functions/applicators/applyInnerJoin';
import { applyLeftJoinHeterogeneous, applyLeftJoinHomogeneous } from '../functions/applicators/applyLeftJoin';
import { applyMax } from '../functions/applicators/applyMax';
import { applyMin } from '../functions/applicators/applyMin';
Expand All @@ -22,7 +22,7 @@ import { applyOrderBy } from '../functions/applicators/applyOrderBy';
import { applyPipe } from '../functions/applicators/applyPipe';
import { applyPrepend } from '../functions/applicators/applyPrepend';
import { applyQuantile } from '../functions/applicators/applyQuantile';
import { applyReverse } from '../functions/applicators/applyReverse';
import { applyReverseImmutable } from '../functions/applicators/applyReverseImmutable';
import { applyRightJoinHeterogeneous, applyRightJoinHomogeneous } from '../functions/applicators/applyRightJoin';
import { applySelect, applySelectMany } from '../functions/applicators/applySelect';
import { applyShuffle } from '../functions/applicators/applyShuffle';
Expand Down Expand Up @@ -332,27 +332,27 @@ export class BasicEnumerable<TSource> implements IEnumerable<TSource> {
* @example
* ```typescript
* const numbers = [1, 2];
* const moreNumbers = from(numbers).concat([3, 4, 5]); // [1, 2, 3, 4, 5]
* const moreNumbers = from(numbers).concatenate([3, 4, 5]); // [1, 2, 3, 4, 5]
* ```
* @param collection The sequence to concatenate to the first sequence.
* @returns An IEnumerable<TSource> that contains the concatenated elements of the two input sequences.
*/
public concat(collection: Iterable<TSource>): IEnumerable<TSource>;
public concatenate(collection: Iterable<TSource>): IEnumerable<TSource>;

/**
* Concatenates two or more sequences.
* @example
* ```typescript
* const numbers = [1, 2];
* const evenMoreNumbers = from(numbers).concat([3, 4], [5, 6]); // [1, 2, 3, 4, 5, 6]
* const evenMoreNumbers = from(numbers).concatenate([3, 4], [5, 6]); // [1, 2, 3, 4, 5, 6]
* ```
* @param collections The sequences to concatenate to the first sequence.
* @returns An IEnumerable<TSource> that contains the concatenated elements of the two or more input sequences.
*/
public concat(...collections: Iterable<TSource>[]): IEnumerable<TSource>;
public concatenate(...collections: Iterable<TSource>[]): IEnumerable<TSource>;

public concat(...collections: Iterable<TSource>[]): IEnumerable<TSource> {
return applyConcat(this.factory, this, collections);
public concatenate(...collections: Iterable<TSource>[]): IEnumerable<TSource> {
return applyConcatenate(this.factory, this, collections);
}

/**
Expand Down Expand Up @@ -1239,7 +1239,7 @@ export class BasicEnumerable<TSource> implements IEnumerable<TSource> {
* const people = from([magnus, terry, adam, john]);
* const pets = from([barley, boots, whiskers, daisy, scratchy]);
*
* const result = people.join(
* const result = people.innerJoin(
* pets,
* person => person,
* pet => pet.owner,
Expand All @@ -1263,7 +1263,7 @@ export class BasicEnumerable<TSource> implements IEnumerable<TSource> {
* @param resultSelector A function to create a result element from two matching elements.
* @returns An IEnumerable<TResult> that has elements of type TResult that are obtained by performing an inner join on two sequences.
*/
public join<TInner, TKey, TResult>(
public innerJoin<TInner, TKey, TResult>(
inner: Iterable<TInner>,
outerKeySelector: (item: TSource) => TKey,
innerKeySelector: (item: TInner) => TKey,
Expand All @@ -1288,7 +1288,7 @@ export class BasicEnumerable<TSource> implements IEnumerable<TSource> {
* const people = from([magnus, terry, adam, john]);
* const pets = from([barley, boots, whiskers, daisy, scratchy]);
*
* const result = people.join(
* const result = people.innerJoin(
* pets,
* person => person,
* pet => pet.owner,
Expand All @@ -1314,22 +1314,30 @@ export class BasicEnumerable<TSource> implements IEnumerable<TSource> {
* @param equalityComparer A function to compare keys.
* @returns An IEnumerable<TResult> that has elements of type TResult that are obtained by performing an inner join on two sequences.
*/
public join<TInner, TKey, TResult>(
public innerJoin<TInner, TKey, TResult>(
inner: Iterable<TInner>,
outerKeySelector: (item: TSource) => TKey,
innerKeySelector: (item: TInner) => TKey,
resultSelector: (item: TSource, inner: TInner) => TResult,
equalityComparer: EqualityComparer<TKey>
): IEnumerable<TResult>;

public join<TInner, TKey, TResult>(
public innerJoin<TInner, TKey, TResult>(
inner: Iterable<TInner>,
outerKeySelector: (item: TSource) => TKey,
innerKeySelector: (item: TInner) => TKey,
resultSelector: (item: TSource, inner: TInner) => TResult,
equalityComparer?: EqualityComparer<TKey>
): IEnumerable<TResult> {
return applyJoin(this.factory, this, inner, outerKeySelector, innerKeySelector, resultSelector, equalityComparer);
return applyInnerJoin(
this.factory,
this,
inner,
outerKeySelector,
innerKeySelector,
resultSelector,
equalityComparer
);
}

/**
Expand Down Expand Up @@ -1618,8 +1626,8 @@ export class BasicEnumerable<TSource> implements IEnumerable<TSource> {
* Inverts the order of the elements in a sequence.
* @returns A sequence whose elements correspond to those of the input sequence in reverse order.
*/
public reverse(): IEnumerable<TSource> {
return applyReverse(this.factory, this);
public reverseImmutable(): IEnumerable<TSource> {
return applyReverseImmutable(this.factory, this);
}

/**
Expand Down
Loading

0 comments on commit eeeabac

Please sign in to comment.