Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also .

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also .
...
  • 13 commits
  • 3 files changed
  • 0 commit comments
  • 1 contributor
Showing with 100 additions and 32 deletions.
  1. +32 −1 README.md
  2. +26 −0 spec/gtl_spec.coffee
  3. +42 −31 src/gtl.coffee
View
33 README.md
@@ -158,6 +158,18 @@ gtl.filter(['but break', 'my heart', 'for I must', 'hold my tongue'], { grep: /m
// => ['my heart', 'for I must', 'hold my tongue']
```
+#### fuzzy
+
+You can find elements using fuzzy search:
+
+``` js
+gtl.filter(
+ ['but break', 'my heart', 'for I must', 'hold my tongue'],
+ fuzzy: 'ut'
+);
+// => ['but break', 'for I must']
+```
+
### Iterator rules
#### or (alias: in)
@@ -220,6 +232,25 @@ gtl.filter([1, 2, 3, 4], odd: false)
You can't use `or`, `in` and `and` rule name because it's reserved to build-ins iterator rules.
+## gtl.curry
+
+You can make copy of gtl.filter with predefined options:
+
+``` js
+var findWilly = gtl.curry({ fuzzy: 'willy' });
+
+findWilly(['storm we are ill', 'is we ill yo', 'trololo will']);
+// => ['is we ill yo']
+```
+
+## gtl.clone
+
+You can create clone of gtl with custom set of rules by calling gtl.clone:
+
+``` js
+clonedGtl = gtl.clone();
+```
+
# Changelog
This project uses [Semantic Versioning](http://semver.org/) for release numbering.
@@ -244,4 +275,4 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
26 spec/gtl_spec.coffee
@@ -74,6 +74,14 @@ describe 'Greater than less', ->
grep: /m./
).should.eql ['my heart', 'for I must', 'hold my tongue']
+ describe 'fuzzy', ->
+
+ it 'should filter array by fuzzy search in strings', ->
+ gtl.filter(
+ ['but break', 'my heart', 'for I must', 'hold my tongue']
+ fuzzy: 'ut'
+ ).should.eql ['but break', 'for I must']
+
describe 'multiply', ->
it 'should filter by multiply rules', ->
@@ -126,3 +134,21 @@ describe 'Greater than less', ->
or: ['one', 'two']
and: 'three'
).should.eql [{ one: 1, two: 5, three: 4 }, { one: 4, two: 4, three: 9 }]
+
+ describe 'gtl.curry', ->
+
+ it 'should curry filter rules', ->
+ findWilly = gtl.curry(fuzzy: 'willy')
+ findWilly(
+ ['storm we are ill', 'is we ill yo', 'trololo will']
+ ).should.eql ['is we ill yo']
+
+ it 'should merge curried rules with passed by user'
+
+ it 'should curry iterator'
+
+ describe 'gtl.clone', ->
+
+ it 'should clone gtl object', ->
+ cloned = gtl.clone()
+ cloned.rules.should.not.eq gtl.rules
View
73 src/gtl.coffee
@@ -20,10 +20,7 @@ gtl = {}
###
Internal: clone array
###
-clone = (array) ->
- result = []
- result.push(elm) for elm in array
- result
+clone = (array) -> array.slice()
###
Internal: get element by path
@@ -41,10 +38,11 @@ getByPath = (obj, path) ->
Internal: is satisfied to iterator rule
###
isSatisfiedToIteratorRule = (rule, results) ->
- if rule == 'or'
- results.indexOf(true) != -1
- else if rule == 'and'
- results.indexOf(false) == -1
+ switch rule
+ when 'or'
+ results.indexOf(true) != -1
+ when 'and'
+ results.indexOf(false) == -1
###
Internal: filter array by rule and return copy
@@ -70,10 +68,11 @@ filter = (array, comparator, rule, iterator) ->
results.push \
comparator(getByPath(elm, iterator), rule)
- if iteratorRule.iterator.constructor == String
- compare(iteratorRule.iterator)
- else if iteratorRule.iterator.constructor == Array
- compare(i) for i in iteratorRule.iterator
+ switch iteratorRule.iterator.constructor
+ when String
+ compare(iteratorRule.iterator)
+ when Array
+ compare(i) for i in iteratorRule.iterator
unless isSatisfiedToIteratorRule(iteratorRule.rule, results)
satisfied = false
@@ -88,16 +87,14 @@ filter = (array, comparator, rule, iterator) ->
gtl.filter = (array, rules, iterator) ->
result = clone(array)
- unless iterator?
+ unless iterator
iterator = []
- if rules.or? or rules.in?
- iterator.push(
- rule: 'or'
- iterator: rules.or || rules.in
- )
+ if rules.or or rules.in
+ iterator.push(rule: 'or', iterator: rules.or || rules.in)
- iterator.push(rule: 'and', iterator: rules.and) if rules.and?
+ if rules.and
+ iterator.push(rule: 'and', iterator: rules.and)
if iterator.length == 0
iterator = (elm) -> elm
@@ -114,28 +111,24 @@ gtl.rules = {}
###
Public: greater than comparator
###
-gtl.rules.greaterThan = (a, b) -> a > b
-gtl.rules.gt = gtl.rules.greaterThan
+gtl.rules.gt = gtl.rules.greaterThan = (a, b) -> a > b
###
Public: greater than or equal to comparator
###
-gtl.rules.greaterThanOrEqualTo = (a, b) -> a >= b
-gtl.rules.gte = gtl.rules.greaterThanOrEqualTo
-gtl.rules.gteq = gtl.rules.greaterThanOrEqualTo
+gtl.rules.gte = gtl.rules.gteq = gtl.rules.greaterThanOrEqualTo =
+ (a, b) -> a >= b
###
Public: less than comparator
###
-gtl.rules.lessThan = (a, b) -> a < b
-gtl.rules.lt = gtl.rules.lessThan
+gtl.rules.lt = gtl.rules.lessThan = (a, b) -> a < b
###
Public: less than or equal to comparator
###
-gtl.rules.lessThanOrEqualTo = (a, b) -> a <= b
-gtl.rules.lte = gtl.rules.lessThanOrEqualTo
-gtl.rules.lteq = gtl.rules.lessThanOrEqualTo
+gtl.rules.lte = gtl.rules.lteq = gtl.rules.lessThanOrEqualTo =
+ (a, b) -> a <= b
###
Public: only comparator
@@ -149,16 +142,34 @@ gtl.rules.only = (a, bs) ->
###
Public: except comparator
###
-gtl.rules.except = (a, bs) ->
+gtl.rules.not = gtl.rules.except = (a, bs) ->
not gtl.rules.only(a, bs)
-gtl.rules.not = gtl.rules.except
###
Public: grep comparator
###
gtl.rules.grep = (str, substr) ->
str.search(substr) != -1
+###
+ Public: fuzzy comparator
+###
+gtl.rules.fuzzy = (str, searchStr) ->
+ subStr = str
+ for char in searchStr
+ if -1 != i = subStr.search(char)
+ subStr = subStr.slice(i + 1)
+ else
+ return false
+ true
+
+###
+ Public: curry function
+###
+gtl.curry = (curriedRules) ->
+ (array, rules) ->
+ gtl.filter(array, curriedRules)
+
# Export gtl to global scope
if window?
window.gtl = gtl

No commit comments for this range

Something went wrong with that request. Please try again.