# gtl

Greater than less

``npm install gtl``

## Usage

``````TODO: Few examples: simple and advanced
``````

# API

## gtl.filter()

### Filter array of numbers with gt rule (alias to greaterThan)

Full list of rules is avalible in "Avaliable rules".

``````gtl.filter([1, 2, 3, 4, 5], { gt: 3 });
// => [4, 5]
``````

### Filter array of strings

``````gtl.filter(['a', 'b', 'c', 'd'], { lte: 'c' });
// => ['a', 'b', 'c']
``````

### Filter array of objects through iterator

``````gtl.filter(
[{ num : 1 }, { num : 2 }, { num : 3 }, { num : 4 }, { num : 5 }],
{ gte: 4 },
function (obj) {
return obj.num;
}
);
// => [{ num : 4 }, { num : 5 }]
``````

### Combine rules

You can use multiply rules to filter array:

``````gtl.filter([1, 2, 3, 4, 5], { gt: 2, lte: 4 });
// => [3, 4]
``````

You also can use multiply iterator rules:

``````gtl.filter(
[{ one: 1, two: 5, three: 4 }, { one: 4, two: 4, three: 9 }, { one: 4, two: -2, three: 3 }, { one: 5, two: 7, three: 1 }],
{ gte: 4, or: ['one', 'two'], and: 'three' }
);
// => [{ one: 1, two: 5, three: 4 }, { one: 4, two: 4, three: 9 }]
``````

### Avaliable rules

#### greaterThan (alias: gt)

``````gtl.filter([1, 2, 3, 4, 5], { gt: 3 });
// => [4, 5]
``````

#### greaterThanOrEqualTo (aliases: gte, gteq)

``````gtl.filter([1, 2, 3, 4, 5], { gte: 3 });
// => [3, 4, 5]
``````

#### lessThan (alias: lt)

``````gtl.filter([1, 2, 3, 4, 5], { lt: 3 });
// => [1, 2]
``````

#### lessThanOrEqualTo (aliases: lte, lteq)

``````gtl.filter([1, 2, 3, 4, 5], { lte: 3 });
// => [1, 2, 3]
``````

#### only

``````gtl.filter(
[{ num : 1 }, { num : 2 }, { num : 3 }, { num : 4 }, { num : 5 }],
{ only: [1, 2] },
function (obj) {
return obj.num;
}
);
// => [{ num : 1 }, { num : 2 }]
``````

#### except (alias: not)

``````gtl.filter(
[{ num : 1 }, { num : 2 }, { num : 3 }, { num : 4 }, { num : 5 }],
{ except: [1, 3] },
function (obj) {
return obj.num;
}
);
// => [{ num : 2 }, { num : 4 }, { num : 5 }]
``````

#### grep

Grep array by string:

``````gtl.filter(['but break', 'my heart', 'for I must', 'hold my tongue'], { grep: 'my' })
// => ['my heart', 'hold my tongue']
``````

You also can use RegExp:

``````gtl.filter(['but break', 'my heart', 'for I must', 'hold my tongue'], { grep: /m./ })
// => ['my heart', 'for I must', 'hold my tongue']
``````

### Iterator rules

#### or (alias: in)

You can use `in` rule to set fields to search for:

``````gtl.filter(
[{ body: { text: 'but break' } }, { body: { text: 'my heart' } }, { body: { text: 'for I must' } }, { body: { text: 'hold my tongue' } }],
{ grep: 'my', or: 'body.text' }
);
// => [{ body: { text: 'my heart' } }, { body: { text: 'hold my tongue' } }]
``````

You also can specify multiply fields:

``````gtl.filter(
[{ one: 1, two: 5 }, { one: 4, two: 4 }, { one: 4, two: -2 }, { one: 5, two: 7 }],
{ gt: 4, or: ['one', 'two'] }
);
// => [{ one: 1, two: 5 }, { one: 5, two: 7 }]
``````

#### and

``````gtl.filter(
[{ one: 1, two: 5 }, { one: 4, two: 4 }, { one: 4, two: -2 }, { one: 5, two: 7 }],
{ gte: 4, and: ['one', 'two'] }
);
// => [{ one: 4, two: 4 }, { one: 5, two: 7 }]
``````

## gtl.rules

gtl.rules contain all comparsion rules (lt, gt etc).

``````gtl.rules.odd = function (num, rule) {
var isOdd = num % 2 === 1
if rule {
return isOdd;
} else {
return !isOdd;
}
};
``````

... then:

``````gtl.filter([1, 2, 3, 4], odd: true)
// => [1, 3, 5]
gtl.filter([1, 2, 3, 4], odd: false)
// => [2, 4]
``````

You can't use `or`, `in` and `and` rule name because it's reserved to build-ins iterator rules.

# Changelog

This project uses Semantic Versioning for release numbering.

Currently this project in active development but no any releases yet.

# Contributors

Idea and code by @kossnocorp.

Check out full list of contributors.