Skip to content

Commit

Permalink
Merge pull request #1 from xeinebiu/rename_functions
Browse files Browse the repository at this point in the history
rename functions to be consistent with Javascript
  • Loading branch information
xeinebiu authored Oct 15, 2022
2 parents fef01a1 + 0672c76 commit 33cc064
Show file tree
Hide file tree
Showing 19 changed files with 134 additions and 128 deletions.
76 changes: 41 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@ we use Iterables to stream over collections.

Iterables are useful when you want to chain several operations on a collection such as

- where
- filter
- filterNotNull
- group
- sort
- map
- mapNotNull
- take
- skip
- every
- none
- some
- etc ...

For example, lets consider a case. We need to work with a collection to filter the numbers greater than ``20``, map to
Expand All @@ -32,8 +38,8 @@ const mappedValue = value.toString();

````typescript
const data = [1, 10, 20, 30, 40, 50, ...];
const mappedValue = toExtendedIterable(data)
.where(x => x > 20)
const mappedValue = asIterable(data)
.filter(x => x > 20)
.skip(2)
.map(x => x.toString())
.first()
Expand Down Expand Up @@ -79,31 +85,31 @@ npm i @xeinebiu/ts-iterable

````typescript
const data = [1, 2, 3, 4, 5];
const iterable = toExtendedIterable(data);
const iterable = asIterable(data);
````

### Where
### Filter

````typescript
// without the Iterable
const filtered = data.filter(x => x < 4);

// with iterable
const filtered = toExtendedIterable(data)
.where(x => x < 4)
const filtered = asIterable(data)
.filter(x => x < 4)
.toList();

// result [1, 2, 3]
````

### Where Not Null
### Filter Not Null

Filter `undefined|null` values out

````typescript
const data = [1, 2, null, 3, undefiend, 4];
const filtered = toExtendedIterable(data)
.whereNotNull();
const filtered = asIterable(data)
.filterNotNull();

// result [1, 2, 3, 4]
````
Expand All @@ -117,31 +123,31 @@ Take specific amount of elements
const taken = data.slice(0, 3);

// with iterable
const taken = toExtendedIterable(data)
const taken = asIterable(data)
.take(3)
.toList();

// result [1, 2, 3]
````

### All
### Every

Return `true` if all elements match the predicate.

````typescript
const result = toExtendedIterable(data)
.all(x => x.toString() !== "hello world");
const result = asIterable(data)
.every(x => x.toString() !== "hello world");

// result true
````

### Any
### Some

Return `true` if any of the elements match the predicate

````typescript
const result = toExtendedIterable(data)
.any(x => x.toString() !== "1");
const result = asIterable(data)
.some(x => x.toString() !== "1");

// result true
````
Expand All @@ -151,7 +157,7 @@ const result = toExtendedIterable(data)
Return `true` if all the elements do not match the predicate

````typescript
const result = toExtendedIterable(data)
const result = asIterable(data)
.none(x => x <= -1);

// result true
Expand All @@ -162,8 +168,8 @@ const result = toExtendedIterable(data)
Return the first element if available, otherwise throw ``NoElementError``

````typescript
const result = toExtendedIterable(data)
.where(x => x > 4)
const result = asIterable(data)
.filter(x => x > 4)
.first();

// result 5
Expand All @@ -174,8 +180,8 @@ const result = toExtendedIterable(data)
Return the first element if available, otherwise null.

````typescript
const result = toExtendedIterable(data)
.where(x => x > 100)
const result = asIterable(data)
.filter(x => x > 100)
.firstOrNull();

// result null
Expand All @@ -186,8 +192,8 @@ const result = toExtendedIterable(data)
Map the elements using a mapper

````typescript
const result = toExtendedIterable(data)
.where(x => x < 3)
const result = asIterable(data)
.filter(x => x < 3)
.map(x => x.toString())
.toList();

Expand All @@ -201,8 +207,8 @@ Map the elements using the mapper and avoid inserting `null|undefined` values in
````typescript
const data = [1, null, 2, undefined, 3];

const result = toExtendedIterable(data)
.where(x => x < 3)
const result = asIterable(data)
.filter(x => x < 3)
.mapNotNull(x => x?.toString())
.toList();

Expand All @@ -214,7 +220,7 @@ const result = toExtendedIterable(data)
Offset the elements cursor starting from index 0

````typescript
const result = toExtendedIterable(data)
const result = asIterable(data)
.skip(1)
.toList();

Expand All @@ -226,34 +232,34 @@ const result = toExtendedIterable(data)
Take specific amount of elements

````typescript
const result = toExtendedIterable(data)
const result = asIterable(data)
.take(2)
.toList();

// result ["1", "2"]
````

### Sorted
### Sort

Sort all elements and return new [ExtendedIterable]

````typescript
const sorted = toExtendedIterable(data)
.sorted((a, b) => b - a)
const sorted = asIterable(data)
.sort((a, b) => b - a)
.toList();

// result [5, 4, 3, 2, 1]
````

### Grouped
### Group

Group all elements and return new [ExtendedIterable]

````typescript
const data = [-9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

const groupedData = toExtendedIterable(data)
.grouped(x => {
const groupedData = asIterable(data)
.group(x => {
if (x < 0) return "negative";
return "positive";
})
Expand All @@ -271,7 +277,7 @@ const groupedData = toExtendedIterable(data)
Convert the Iterable to a collection.

````typescript
const list = toExtendedIterable(data)
const list = asIterable(data)
.toList();

// result [1, 2, 3, 4, 5]
Expand Down
8 changes: 6 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@xeinebiu/ts-iterable",
"version": "1.0.0",
"version": "1.1.0",
"description": "Enumerable arrays for Typescript",
"main": "index.js",
"files": [
Expand Down Expand Up @@ -38,5 +38,8 @@
"ts-jest": "29.0.3",
"typescript": "^4.8.4"
},
"dependencies": {}
"dependencies": {
"@types/lodash": "^4.14.186",
"lodash": "^4.17.21"
}
}
23 changes: 13 additions & 10 deletions src/default/default.extended-iterable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
DefaultTakeIterator,
DefaultWhereIterator,
} from './iterator';
import { iterableToList } from './util';

export class DefaultExtendedIterable<T> implements ExtendedIterable<T> {
constructor(private readonly iterator: () => DefaultIterator<T>) {}
Expand All @@ -17,14 +16,14 @@ export class DefaultExtendedIterable<T> implements ExtendedIterable<T> {
return this.iterator();
}

public all(predicate: (element: T) => boolean): boolean {
public every(predicate: (element: T) => boolean): boolean {
for (const element of this) {
if (!predicate(element)) return false;
}
return true;
}

public any(predicate: (element: T) => boolean): boolean {
public some(predicate: (element: T) => boolean): boolean {
for (const element of this) {
if (predicate(element)) return true;
}
Expand Down Expand Up @@ -75,13 +74,13 @@ export class DefaultExtendedIterable<T> implements ExtendedIterable<T> {
return new DefaultExtendedIterable(iterator);
}

public where(predicate: (element: T) => boolean): ExtendedIterable<T> {
public filter(predicate: (element: T) => boolean): ExtendedIterable<T> {
const iterator = () =>
new DefaultWhereIterator(this.iterator(), predicate);
return new DefaultExtendedIterable(iterator);
}

public whereNotNull(): ExtendedIterable<NonNullable<T>> {
public filterNotNull(): ExtendedIterable<NonNullable<T>> {
const iterator = () =>
new DefaultMapNotNullIterator(this.iterator(), x => {
if (x) return x;
Expand All @@ -91,17 +90,17 @@ export class DefaultExtendedIterable<T> implements ExtendedIterable<T> {
return new DefaultExtendedIterable(iterator);
}

public sorted(sorter: (a: T, b: T) => number): ExtendedIterable<T> {
const list = iterableToList(this);
public sort(sorter: (a: T, b: T) => number): ExtendedIterable<T> {
const list = this.toList();
list.sort(sorter);

return new DefaultExtendedIterable(
() => new DefaultIterator<T>(list[Symbol.iterator]()),
);
}

public grouped<K>(group: (a: T) => K): ExtendedIterable<[K, T[]]> {
const list = iterableToList(this);
public group<K>(group: (a: T) => K): ExtendedIterable<[K, T[]]> {
const list = this.toList();

const map = new Map<K, T[]>();
list.forEach(item => {
Expand All @@ -120,6 +119,10 @@ export class DefaultExtendedIterable<T> implements ExtendedIterable<T> {
}

public toList(): T[] {
return iterableToList(this);
const result: T[] = [];
for (const element of this) {
result.push(element);
}
return result;
}
}
1 change: 0 additions & 1 deletion src/default/util/index.ts

This file was deleted.

7 changes: 0 additions & 7 deletions src/default/util/iterable.util.ts

This file was deleted.

12 changes: 6 additions & 6 deletions src/extended.iterable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ export interface ExtendedIterable<T> extends Iterable<T> {
/**
* Return `true` if all elements match the [predicate]
*/
all(predicate: (element: T) => boolean): boolean;
every(predicate: (element: T) => boolean): boolean;

/**
* Return `true` if any of the elements match the [predicate]
*/
any(predicate: (element: T) => boolean): boolean;
some(predicate: (element: T) => boolean): boolean;

/**
* Return the first element if available, otherwise throw [NoElementError]
Expand Down Expand Up @@ -49,22 +49,22 @@ export interface ExtendedIterable<T> extends Iterable<T> {
/**
* Filter the elements which match the [predicate]
*/
where(predicate: (element: T) => boolean): ExtendedIterable<T>;
filter(predicate: (element: T) => boolean): ExtendedIterable<T>;

/**
* Filter `undefined|null` values out
*/
whereNotNull(): ExtendedIterable<NonNullable<T>>;
filterNotNull(): ExtendedIterable<NonNullable<T>>;

/**
* Sort all elements and return new [ExtendedIterable]
*/
sorted(sorter: (a: T, b: T) => number): ExtendedIterable<T>;
sort(sorter: (a: T, b: T) => number): ExtendedIterable<T>;

/**
* Group all elements and return new [ExtendedIterable]
*/
grouped<K>(group: (a: T) => K): ExtendedIterable<[K, T[]]>;
group<K>(group: (a: T) => K): ExtendedIterable<[K, T[]]>;

/**
* Convert the [IEnumerable] to [Array]
Expand Down
Loading

0 comments on commit 33cc064

Please sign in to comment.