|
1 | 1 | # PowerArray
|
2 |
| -Working with Arrays in Javascript requires manual iteration, is error-prone, difficult to read, repetitive and time-consuming. |
| 2 | +Working with Arrays in Javascript requires manual iteration, is error-prone, difficult to read, repetitive and time-consuming. PowerArray simplifies almost everything that has to do with that bads. |
3 | 3 |
|
4 |
| -**What can PowerArray do for you?** |
5 |
| - |
6 |
| -To see it by yourself, please check how many time you need to find out what is doing this code: |
| 4 | +## Simple example |
| 5 | +The following example filters an array, sort the results and iterates the first 10 matches. It uses the **Where**, **Sort**, **Take** and **RunEach** PowerArray functions. |
7 | 6 |
|
| 7 | +>Given an array of objects (representing persons), called 'peopleArray' |
8 | 8 | ```javascript
|
9 |
| -// Given an Array of objects representing persons, called 'peopleArray' |
| 9 | +// With PowerArray: |
10 | 10 | peopleArray
|
11 |
| - .Where({ age: Between(18,70), gender: 'M'}) |
12 |
| - .Sort({ lastName: Sort.AscendingIgnoringCase}) |
13 |
| - .RunEach(function(item, i) { |
14 |
| - console.debug(i + ' ' + item.name); //do something |
15 |
| - }); |
16 |
| -``` |
17 |
| - |
18 |
| -And how long for this one? |
| 11 | + .Where({ age: Between(18,70), gender: 'M' }) // filter |
| 12 | + .Sort{ lastName: Sort.AscendingIgnoringCase }) // sort |
| 13 | + .Take(10) |
| 14 | + .RunEach(function(item, i) { console.debug(i + ' ' + item.name); } ); // do stuff |
19 | 15 |
|
20 |
| -```javascript |
21 |
| -// Given an Array of objects representing persons, called 'peopleArray' |
| 16 | +// In plain Javascript |
22 | 17 | var result = [], i = 0, l, item;
|
23 | 18 | for(l = peopleArray.length; i < l; i++) {
|
24 | 19 | item = peopleArray[i];
|
25 |
| - if(item.age > 18 && item.age < 70 && item.gender === 'M') { |
| 20 | + if(item.age > 18 && item.age < 70 && item.gender === 'M') { // filtering |
26 | 21 | result.push(item);
|
27 | 22 | }
|
28 | 23 | }
|
| 24 | + |
29 | 25 | result.sort(function (a, b) {
|
30 |
| - return a.lastName.toLowerCase().localeCompare(b.lastName.toLowerCase()); |
| 26 | + return a.lastName.toLowerCase().localeCompare(b.lastName.toLowerCase()); // sorting |
31 | 27 | });
|
32 |
| -for(i = 0, l = result.length; i < l; i++) { |
33 |
| - console.debug(i + ' ' + result[i].name); //do something |
| 28 | + |
| 29 | +for(i = 0, l = result.length; i < l && i < 10; i++) { |
| 30 | + console.debug(i + ' ' + result[i].name); // do stuff |
34 | 31 | }
|
35 | 32 | ```
|
36 | 33 |
|
37 |
| -**Both codes do exactly the same**, but the first is (at least for me), much easier to understand. It uses the **Where**, **Sort** and **RunEach** PowerArray methods. |
| 34 | +**Both codes do exactly the same**, but the first is (at least for me), much easier to understand. Power array adds also global auxiliar functions, that are ready to be embedded on [Conditions-Object](#ConditionsObjectDescription). |
38 | 35 |
|
39 | 36 | This library helps to simplify code, to make it intuitive and readable.
|
40 | 37 |
|
@@ -91,6 +88,7 @@ This signature accepts an array of Conditions-Objects. If an item fulfill all co
|
91 | 88 | Condition-Object, it will be included in the results (and other Condition-Objects will not be evaluated for that item).
|
92 | 89 | This Signature is ideal to build (a kind of) `OR` statements that cannot be expressed in a single Conditions-Object.
|
93 | 90 | Examples:
|
| 91 | + |
94 | 92 | ```Javascript
|
95 | 93 | // Given an Array of objects representing persons, called 'peopleArray'
|
96 | 94 | // Task: find all single men between 20 and 30 years old and all married woman between 25 and 35 years old
|
|
0 commit comments