Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: rob893/typescript-extended-linq
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v1.2.0
Choose a base ref
...
head repository: rob893/typescript-extended-linq
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v1.2.1
Choose a head ref
  • 3 commits
  • 16 files changed
  • 1 contributor

Commits on Sep 5, 2021

  1. 1.1.0

    rob893 committed Sep 5, 2021
    Copy the full SHA
    d929b3f View commit details

Commits on Sep 6, 2021

  1. chore: fix merge conflict

    rob893 committed Sep 6, 2021
    Copy the full SHA
    a7a2718 View commit details
  2. fix: fix bugs

    rob893 committed Sep 6, 2021
    Copy the full SHA
    02f2a5a View commit details
11 changes: 11 additions & 0 deletions src/__tests__/append.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { from } from '..';

describe('append', () => {
it.each([[], new Set([1, 2]), [1, 2]])('should append a new element', src => {
const last = 3;

const res = from(src).append(last).toArray();

expect(res[res.length - 1]).toBe(last);
});
});
43 changes: 43 additions & 0 deletions src/__tests__/contains.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { from } from '..';

describe('contains', () => {
it('should return true for containing the item', () => {
const target = 3;

const result = from([1, 2, 3, 4]).contains(target);

expect(result).toBe(true);
});

it('should return false for not containing the item', () => {
const target = 6;

const result = from([1, 2, 3, 4]).contains(target);

expect(result).toBe(false);
});

it('should return true for containing the item using equality comparer', () => {
const target = { id: 2, foo: 'foo' };

const result = from([
{ id: 3, foo: 'foo' },
{ id: 2, foo: 'fooz' },
{ id: 1, foo: 'foo' }
]).contains(target, (a, b) => a.id === b.id);

expect(result).toBe(true);
});

it('should return false for not containing the item using equality comparer', () => {
const target = { id: 5, foo: 'foo' };

const result = from([
{ id: 3, foo: 'foo' },
{ id: 2, foo: 'fooz' },
{ id: 1, foo: 'foo' }
]).contains(target, (a, b) => a.id === b.id);

expect(result).toBe(false);
});
});
30 changes: 30 additions & 0 deletions src/__tests__/count.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { from } from '..';

describe('count', () => {
it('should return the count', () => {
const items = [1, 2, 3];

const result = from(items).count();

expect(result).toBe(items.length);
});

it('should return 0 for empty', () => {
const result = from([]).count();

expect(result).toBe(0);
});

it('should return count that matches condition', () => {
const items = [
{ id: 1, foo: 'asdf' },
{ id: 2, foo: 'asdf' },
{ id: 3, foo: 'asdf' },
{ id: 4, foo: 'asdf' }
];

const result = from(items).count(x => x.id % 2 === 0);

expect(result).toBe(2);
});
});
102 changes: 102 additions & 0 deletions src/__tests__/distinct.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import { Enumerable, from } from '..';

describe('distinct', () => {
it('should return an Enumerable', () => {
const items = [1, 2, 3];

const result = from(items).distinct();

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

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

const result = from(items).distinct().toArray();

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

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

const result = from(items).distinct();

items.push(4, 4);

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

it('should return the union of two collections using passed comparer', () => {
const items = [
{ id: 1, foo: 'asdf' },
{ id: 2, foo: 'asdf' },
{ id: 3, foo: 'asdf' },
{ id: 2, foo: 'asdf' },
{ id: 3, foo: 'asdf' },
{ id: 4, foo: 'asdf' }
];

const result = from(items)
.distinct((a, b) => a.id === b.id)
.toArray();

expect(result).toEqual([
{ id: 1, foo: 'asdf' },
{ id: 2, foo: 'asdf' },
{ id: 3, foo: 'asdf' },
{ id: 4, foo: 'asdf' }
]);
});
});

describe('distinctBy', () => {
it('should return an Enumerable', () => {
const items = [1, 2, 3];

const result = from(items).distinctBy(x => x);

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

it('should return distinct items by key', () => {
const items = [
{ id: 1, foo: 'asdf' },
{ id: 2, foo: 'asdf' },
{ id: 3, foo: 'asdf' },
{ id: 2, foo: 'asdf' },
{ id: 3, foo: 'asdf' },
{ id: 4, foo: 'asdf' }
];

const result = from(items)
.distinctBy(x => x.id)
.toArray();

expect(result).toEqual([
{ id: 1, foo: 'asdf' },
{ id: 2, foo: 'asdf' },
{ id: 3, foo: 'asdf' },
{ id: 4, foo: 'asdf' }
]);
});

it('should deferred execution', () => {
const items = [
{ id: 1, foo: 'asdf' },
{ id: 2, foo: 'asdf' },
{ id: 3, foo: 'asdf' }
];

const result = from(items).distinctBy(x => x.id);

items.push({ id: 4, foo: 'asdf' });

expect(result.toArray()).toEqual([
{ id: 1, foo: 'asdf' },
{ id: 2, foo: 'asdf' },
{ id: 3, foo: 'asdf' },
{ id: 4, foo: 'asdf' }
]);
});
});
133 changes: 133 additions & 0 deletions src/__tests__/except.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import { Enumerable, from } from '..';

describe('except', () => {
it('should return an Enumerable', () => {
const items = [1, 2, 3];

const result = from(items).except([1, 2, 3]);

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

it('should return the exception of two collections', () => {
const items = [0, 1, 2, 3, 6];

const otherItems = [2, 3, 4, 5];

const result = from(items).except(otherItems).toArray();

expect(result).toEqual([0, 1, 6]);
});

it('should return the exception of two collections when one is empty', () => {
const items = [1, 2, 3];

const result = from(items).except([]).toArray();

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

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

const otherItems = [2, 2, 3, 2, 2, 3, 4, 4, 4, 4, 5, 5, 5];

const result = from(items).except(otherItems).toArray();

expect(result).toEqual([1]);
});

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

const otherItems = [2, 3, 4, 5];

const result = from(items).except(otherItems);

items.push(5, 6);

expect(result.toArray()).toEqual([1, 6]);
});

it('should return the exception of two collections using passed comparer', () => {
const items = [
{ id: 1, foo: 'asdf' },
{ id: 2, foo: 'asdf' },
{ id: 3, foo: 'asdf' }
];

const otherItems = [
{ id: 2, foo: 'asdf' },
{ id: 3, foo: 'asdf' },
{ id: 4, foo: 'asdf' }
];

const result = from(items)
.except(otherItems, (a, b) => a.id === b.id)
.toArray();

expect(result).toEqual([{ id: 1, foo: 'asdf' }]);
});
});

describe('exceptBy', () => {
it('should return an Enumerable', () => {
const items = [1, 2, 3];

const result = from(items).exceptBy([1, 2, 3], x => x);

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

it('should return the exception of two collections by id', () => {
const items = [
{ id: 1, foo: 'asdf' },
{ id: 2, foo: 'asdf' },
{ id: 3, foo: 'asdf' },
{ id: 5, foo: 'asdf' }
];

const result = from(items)
.exceptBy([2, 3, 4], x => x.id)
.toArray();

expect(result).toEqual([
{ id: 1, foo: 'asdf' },
{ id: 5, foo: 'asdf' }
]);
});

it('should not have duplicates by key', () => {
const items = [
{ id: 1, foo: 'asdf' },
{ id: 1, foo: 'asdf' },
{ id: 2, foo: 'asdf' },
{ id: 1, foo: 'asdf' },
{ id: 3, foo: 'asdf' },
{ id: 3, foo: 'asdf' }
];

const result = from(items)
.exceptBy([2, 3, 3, 4, 4], x => x.id)
.toArray();

expect(result).toEqual([{ id: 1, foo: 'asdf' }]);
});

it('should deferred execution', () => {
const items = [
{ id: 1, foo: 'asdf' },
{ id: 2, foo: 'asdf' },
{ id: 3, foo: 'asdf' }
];

const result = from(items).exceptBy([2, 3, 4, 5], x => x.id);

items.push({ id: 6, foo: 'asdf' });

expect(result.toArray()).toEqual([
{ id: 1, foo: 'asdf' },
{ id: 6, foo: 'asdf' }
]);
});
});
Loading