Skip to content

Commit

Permalink
[non-]strict mode examples added.
Browse files Browse the repository at this point in the history
  • Loading branch information
bfontaine committed Feb 6, 2013
1 parent 58cf190 commit 35313c7
Showing 1 changed file with 64 additions and 6 deletions.
70 changes: 64 additions & 6 deletions README.md
@@ -1,19 +1,16 @@
ArrayDB
=======
# ArrayDB

[![Build Status](https://travis-ci.org/bfontaine/ArrayDB.png)](https://travis-ci.org/bfontaine/ArrayDB)

Use your arrays as DB tables, and make queries on them.

Install
-------
## Install

```
[sudo] npm install [-g] arraydb
```

Usage
-----
## Usage

ArrayDB objects expose one method, `.query`, which use pattern-matching:

Expand All @@ -40,3 +37,64 @@ ArrayDB.monkeyPatch();

typeof [].query === 'function'; // true
```

### Strict mode

`.query` works in *strict* mode by default. In this mode, objects have to be
strictly equal to be matched. You can specify the mode using an object:

```js
var a = new ArrayDB( NaN, 'foo' );

a.query({ query: NaN }); // [ NaN ]
a.query({ query: NaN, strict: true }); // [ NaN ]
a.query({ query: NaN, strict: false }); // [ NaN, 'foo' ]
```

### Non-Strict mode

Non-strict mode provide some helpers to match elements:

* Regexps can be used to test strings
* Functions can be used to test anything
* Booleans match truthy (and falsy) elements
* `NaN` match any non-number (where `isNaN(element)` is true)

```js
var a = new ArrayDB(
{ age: 2, name: "Bar" },
{ age: 46, name: "Foo" },
{ name: "NoAge" }
);

// returns only objects with an `age` property
a.query({ query: { age: true }, strict: false });

// returns only names that start with "F"
a.query({ query: { name: /^F/ }, strict: false });
```

Also, `ArrayDB` provides some helpers:

* `.lt( e )`: match elements lesser than `e`
* `.gt( e )`: match elements greater than `e`
* `.le( e )`: match elements lesser than, or equal to `e`
* `.ge( e )`: match elements greater than, or equal to `e`
* `.eq( e )`: match elements equal to `e`
* `.ne( e )`: match elements that are not equal to `e`
* `.any()`: match anything

```js
var a = new ArrayDB(
{ name: "John Doe", age: 23 },
{ name: "Foo Bar", age: 12 },
{ name: "Bar Foo", age: 35 },
{ name: "Bar Moo", age: 42 }
);

// match only objects with a .age property greater than 18
a.query({ query: { age: ArrayDB.gt( 18 ) }, strict: false });

// match only objects with an .age property equal to 42
a.query({ query: { age: ArrayDB.eq( 42 ) }, strict: false });
```

0 comments on commit 35313c7

Please sign in to comment.