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

Commit

Permalink
added Enumerable.Range (#18)
Browse files Browse the repository at this point in the history
* updated select to use projection function

* added Enumerable.Range

* added negative numbers test for Enumerable.Range

* updated select method and added tests

* updated select documentation
  • Loading branch information
Gerald Burkholder authored Mar 28, 2018
1 parent 7ebd019 commit ecb1b60
Show file tree
Hide file tree
Showing 7 changed files with 9,812 additions and 45 deletions.
118 changes: 87 additions & 31 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,33 @@
## Getting Started

### yarn

```bash
## install with yarn
$ yarn add asenumerable
```

### bower

```bash
## install with bower
$ bower install enumerablejs
```

### npm

```bash
## install with npm
$ npm install asenumerable
```

## How to use

On any native javascript array, simply call asEnumerable() followed by subsequent calls to any
of the methods outlined in this documentation.

## Enumerable
The main type for enumerablejs. Exposes linq style methods for use on javascript arrays.
The main type for enumerablejs is `Enumerable`. It exposes linq style methods for use on javascript arrays.

```javascript
var arr = [{ name:"def", value: 1, description: "test object 2" }];

Expand All @@ -43,7 +47,29 @@ var b = new Enumerable(arr);
var c = b.take(1).count(); //c = 1
```

## toArray
## Static Methods

### Range

Generates a sequence of integral numbers within a specified range.

```javascript
var result = Enumerable.Range(4, 9);
//[4,5,6,7,8,9,10,11,12]

var result = Enumerable.Range(1, 10)
.select(function(i, item, col)
{
return item * item;
});

///[1,4,9,16,25,36,49,64,81,100]
```

## Methods

### toArray

converts the enumerable object back to a plain javascript array

```javascript
Expand All @@ -56,15 +82,15 @@ var b = new Enumerable(arr);
var c = b.toArray();
```

## where
### where

Filters a sequence of values based on a predicate.

|Parameter | Description |
| --- | --- |
| fn | A function to test each element for a condition. |

``` javascript

//consider the following array.
var arr = [
{ name:"abc", value: 0, description: "test object 1" },
Expand All @@ -83,38 +109,55 @@ var result = arr.asEnumerable().where(function (i, item, collection)
[{ name:"def", value: 1, description: "test object 2" }]
```

## select
### select

Projects each element of a sequence into a new form.

|Parameter | Description |
| --- | --- |
| properties | A string array of property names to select. |
| obj | A function to create a projection of an item. Alternativley, this may also be a string array of property names to select. |

#### select via a projection function

``` javascript
var arr = [1,2,3,4]

arr.asEnumerable()
.select(function(i, item, col)
{
return item * item;
});

//[1,4,9,16]
```

#### select via string array of properties

As of v1.1.1, if a single property is specified in the array `["property_name"]` then the result will not be an object, but an array of the values of that property from each item.

However if multiple properties are specified in the array `["property_name_a", "property_name_b", "property_name_c"]` the result will be an array of object each containing the specified properties. This is also how single property selection versions prior to v1.1.1.

``` javascript
//consider the following array.
var arr = [
{ name:"abc", value: 0, description: "test object 1" },
{ name:"def", value: 1, description: "test object 2" }];


//select a single property.
var result = arr.asEnumerable().select(["name"]);


//the result will be an enumerable containing the following array.
[{ name: "abc" }, { name: "def" }]


//select multiple properties.
var result = arr.asEnumerable().select(["name", "description"]);


//the result will be an enumerable containing the following array.
[{ name: "abc", description: "test object 1" }, { name: "def", description: "test object 2" }]
```

## orderBy
### orderBy

Sorts the elements of a sequence in ascending order according to a key.

|Parameter | Description |
Expand All @@ -123,7 +166,6 @@ Sorts the elements of a sequence in ascending order according to a key.
| fn | a function applied to each element before performing the order operation. fn can be null or undefined. |

``` javascript

//consider the following array
var arr = [
{ name:"def", value: 0, description: "test object 1" },
Expand All @@ -143,7 +185,8 @@ var result = arr.asEnumerable().orderBy("name");
var result = arr.asEnumerable().orderBy("name", function(item) { return item.value + 1; });
```

## orderByDescending
### orderByDescending

Sorts an array of objects in a sequence in descending order according to a key.

|Parameter | Description |
Expand All @@ -152,7 +195,6 @@ Sorts an array of objects in a sequence in descending order according to a key.
| fn | a function applied to each element before performing the order operation. fn can be null or undefined. |

```javascript

//consider the following array
var arr = [
{ name:"def", value: 0, description: "test object 1" },
Expand All @@ -171,7 +213,8 @@ var result = arr.asEnumerable().orderByDescending("value");
var result = arr.asEnumerable().orderByDescending("value", function(item) { return item.value + 1; });
```

## any
### any

Determines whether any element of a sequence satisfies a condition.

``` javascript
Expand All @@ -180,11 +223,10 @@ var arr = [];

//result equals false
var result = arr.asEnumerable().any();


```

## count
### count

Returns the number of elements in a sequence.

|Parameter | Description |
Expand All @@ -206,54 +248,62 @@ var result = testArray.count(function(i, item, col)
});
```

## first
### first

Returns the first element of a sequence.

``` javascript
var arr = [];
var result = arr.asEnumerable().first();
```
## firstOrDefault

### firstOrDefault

Returns the first element of a sequence, or a default value if the sequence contains no elements.

``` javascript
var arr = [];
var result = arr.asEnumerable().firstOrDefault();
```

## last
### last

Returns the last element of a sequence.

``` javascript
var arr = [];
var result = arr.asEnumerable().last();
```

## lastOrDefault
### lastOrDefault

Returns the last element of a sequence, or a default value if the sequence contains no elements.

``` javascript
var arr = [];
var result = arr.asEnumerable().lastOrDefault();
```

## single
### single

Returns the only element of a sequence, and throws an exception if there is not exactly one element in the sequence.

``` javascript
var arr = [];
var result = arr.asEnumerable().single();
```

## singleOrDefault
### singleOrDefault

Returns a single, specific element of a sequence, or a default value if that element is not found.

``` javascript
var arr = [];
var result = arr.asEnumerable().singleOrDefault();
```

## take
### take

Returns a specified number of contiguous elements from the start of a sequence.

``` javascript
Expand All @@ -270,7 +320,8 @@ var result = arr.asEnumerable().takeWhile(function (i, item, collection)
return (item.name === "test");
});
```
## skipWhile
### skipWhile

Bypasses elements in a sequence as long as a specified condition is true and then returns the remaining elements.

``` javascript
Expand All @@ -280,25 +331,30 @@ var result = arr.asEnumerable().skipWhile(function (i, item, collection)
return (item.name === "test");
});
```
## toArray
### toArray

Converts an enumerable object to a native javascript array.

``` javascript
var enumObj = new Enumerable();

var arr = enumObj.toArray();
```

## minimum
### minimum

Returns the minimum value in a sequence.

``` javascript
var arr = [];
var result = arr.asEnumerable().minmum();
```
## maximum

### maximum

Returns the maximum value in a sequence.

``` javascript
var arr = [];
var result = arr.asEnumerable().maximum();
```
```
Loading

0 comments on commit ecb1b60

Please sign in to comment.