Skip to content

Commit

Permalink
async examples and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
awlawl committed Feb 4, 2013
2 parents ee37118 + 82b48f7 commit f9128af
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,22 @@ arr = new LINQ(files)
```

Asynchronous (ALINQ)

####Javascript:
```javascript
var ALINQ = require("node-linq").ALINQ

var files = ['test.txt', 'choni.txt', 'legacy.zip', 'secrets.txt', 'etc.rar'];

var q = new ALINQ(files);
q.Where(function(file,cb) { cb(file.match('.txt$')=='.txt'); });
q.OrderBy(function(file, cb) { cb(file);});
//ToArray is not needed in this case

q.Execute(function(arr) {
//arr is now [ 'choni.txt', 'secrets.txt', 'text.txt' ]
}
```
####CoffeeScript:
```coffee-script
{ALINQ} = require 'node-linq'
Expand Down Expand Up @@ -161,6 +177,27 @@ arr = new LINQ(users)
Asynchronous (ALINQ)
####Javascript:
```javascript
var ALINQ = require("node-linq").ALINQ
var users = [
{name: 'Bob', joined: new Date('12/27/1993')},
{name: 'Tom', joined: new Date('12/25/1993')},
{name: 'Bill', joined: new Date('11/10/1992')},
];
var q = new ALINQ(users);
q.OrderBy(function(user, cb) { cb(user.joined);});
q.Select(function(user, cb) { cb(user.name); });
//ToArray is not needed here
q.Execute(function(arr) {
//arr is now [ 'Bill','Tom','Bob' ]
}
```
####CoffeeScript:
```coffee-script
{ALINQ} = require 'node-linq'
Expand Down
44 changes: 44 additions & 0 deletions test/examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,47 @@ describe('Synchronous (LINQ)', function(){
});
});

describe('Asynchronous (ALINQ)', function(){
it('Simple Where, OrderBy ', function(done){

var files = ['test.txt', 'choni.txt', 'legacy.zip', 'secrets.txt', 'etc.rar'];

var q = new ALINQ(files);
q.Where(function(file,cb) { cb(file.match('.txt$')=='.txt'); });
q.OrderBy(function(file, cb) { cb(file);});

q.Execute(function(arr) {

assert.equal(3, arr.length, "There should be the correct number of items in the final array.");
assert.equal('choni.txt', arr[0], "The first item must be the alphabetically sorted correctly.");
assert.equal('secrets.txt', arr[1], "The second item must be the alphabetically sorted correctly.");
assert.equal('test.txt', arr[2], "The third item must be the alphabetically sorted correctly.");

done();

});

});
it('Simple OrderBy, Select and ToArray', function(done) {
var users = [
{name: 'Bob', joined: new Date('12/27/1993')},
{name: 'Tom', joined: new Date('12/25/1993')},
{name: 'Bill', joined: new Date('11/10/1992')},
];

var q = new ALINQ(users);
q.OrderBy(function(user, cb) { cb(user.joined);});
q.Select(function(user, cb) { cb(user.name); });

q.Execute(function(arr) {

assert.equal(3, arr.length, "There should be the correct number of items in the final array.");
assert.equal('Bill', arr[0], "The first item must be the alphabetically sorted correctly.");
assert.equal('Tom', arr[1], "The second item must be the alphabetically sorted correctly.");
assert.equal('Bob', arr[2], "The third item must be the alphabetically sorted correctly.");

done();

});
});
});

0 comments on commit f9128af

Please sign in to comment.