angular-filter
Advanced tools
Comparing version 0.5.7 to 0.5.8
{ | ||
"name": "angular-filter", | ||
"description": "Bunch of useful filters for angularJS(with no external dependencies!)", | ||
"version": "0.5.7", | ||
"version": "0.5.8", | ||
"filename": "angular-filter.min.js", | ||
@@ -6,0 +6,0 @@ "main": "dist/angular-filter.min.js", |
#Angular-filter [![NPM version][npm-image]][npm-url] [![Build status][travis-image]][travis-url] [![Test coverage][coveralls-image]][coveralls-url] [![License][license-image]][license-url] | ||
>Bunch of useful filters for AngularJS (with no external dependencies!), **v0.5.7** | ||
>Bunch of useful filters for AngularJS (with no external dependencies!), **v0.5.8** | ||
@@ -1332,2 +1332,5 @@ **Notice:** if you want to use `angular-filter` out of AngularJS(e.g: Node, etc..), check [Agile.js repo](https://github.com/a8m/agile) | ||
#Changelog | ||
###0.5.7 | ||
* fix issue #119 | ||
###0.5.6 | ||
@@ -1334,0 +1337,0 @@ * fix issue #145 |
@@ -11,3 +11,3 @@ /** | ||
.filter('filterBy', ['$parse', function( $parse ) { | ||
return function(collection, properties, search) { | ||
return function(collection, properties, search, strict) { | ||
var comparator; | ||
@@ -36,12 +36,15 @@ | ||
} else { | ||
var propList = prop.replace(new RegExp('\\s', 'g'), '').split('+'); | ||
comparator = propList.reduce(function(prev, cur, index) { | ||
return (index === 1) ? $parse(prev)(elm) + ' ' + $parse(cur)(elm) : | ||
prev + ' ' + $parse(cur)(elm); | ||
}); | ||
var propList = prop.replace(/\s+/g, '').split('+'); | ||
comparator = propList | ||
.map(function(prop) { return $parse(prop)(elm); }) | ||
.join(' '); | ||
} | ||
return (isString(comparator) || isNumber(comparator)) | ||
? String(comparator).toLowerCase().contains(search) | ||
: false; | ||
if (!isString(comparator) && !isNumber(comparator)) { | ||
return false; | ||
} | ||
comparator = String(comparator).toLowerCase(); | ||
return strict ? comparator === search : comparator.contains(search); | ||
}); | ||
@@ -48,0 +51,0 @@ }); |
'use strict'; | ||
describe('filterByFilter', function() { | ||
var filter; | ||
beforeEach(module('a8m.filter-by')); | ||
beforeEach(inject(function($filter) { | ||
@@ -14,3 +12,2 @@ filter = $filter('filterBy'); | ||
it('should filter by specific properties and avoid the rest', function() { | ||
var users = [ | ||
@@ -30,7 +27,5 @@ { id: 1, user: { first_name: 'foo', last_name: 'bar', mobile: 4444 } }, | ||
expect(filter(users, ['id', 'user.mobile'], '11')).toEqual([users[3]]); | ||
}); | ||
it('should support to get object as collection', function() { | ||
var users = { | ||
@@ -46,7 +41,5 @@ 0: { id: 1, user: { first_name: 'foo', last_name: 'bar', mobile: 4444 } }, | ||
expect(filter(users, ['user.last_name'], 'bar')).toEqual([users[0]]); | ||
}); | ||
it('should parse concatenate properties, and search them by one field', function() { | ||
var users = [ | ||
@@ -68,7 +61,5 @@ { id: 1, user: { first_name: 'foo', last_name: 'bar', mobile: 4444 } }, | ||
expect(filter(users, ['user.first_name + user.last_name'], 'foo')).toEqual(users); | ||
}); | ||
it('should take care on extreme conditions', function() { | ||
var users = [ | ||
@@ -87,7 +78,5 @@ { id: 1, user: { first_name: 'foo', last_name: 'bar', mobile: 4444 } }, | ||
expect(filter(users, [], [])).toEqual(users); | ||
}); | ||
it('should get a !collection and return it as-is', function() { | ||
expect(filter(!1)).toBeFalsy(); | ||
@@ -97,5 +86,32 @@ expect(filter(1)).toEqual(1); | ||
expect(filter(undefined)).toEqual(undefined); | ||
}); | ||
it('should not coerce non-string/number properties', function() { | ||
var users = [ | ||
{ id: [1, 2], user: { first_name: 'foo', last_name: 'bar', mobile: 4444 } } | ||
]; | ||
expect(filter(users, ['id'], 1)).toEqual([]); | ||
}); | ||
describe('strict mode', function() { | ||
var users = [ | ||
{ id: 1, user: { first_name: 'foo', last_name: 'bar', mobile: 4444 } } | ||
]; | ||
it('should only return exact matches', function() { | ||
expect(filter(users, ['user.first_name'], 'fo', true)).toEqual([]); | ||
expect(filter(users, ['user.first_name'], 'foo', true)).toEqual(users); | ||
}); | ||
it('should handle multiple properties', function() { | ||
expect(filter(users, ['user.first_name', 'user.last_name'], 'ba', true)).toEqual([]); | ||
expect(filter(users, ['user.first_name', 'user.last_name'], 'bar', true)).toEqual(users); | ||
}); | ||
it('should handle concatenation', function() { | ||
expect(filter(users, ['user.first_name + user.last_name'], 'foo ba', true)).toEqual([]); | ||
expect(filter(users, ['user.first_name + user.last_name'], 'foo bar', true)).toEqual(users); | ||
}); | ||
}); | ||
}); |
@@ -22,20 +22,20 @@ 'use strict'; | ||
expect(filter(23423234234,2)).toEqual("21.81 GB"); | ||
}); | ||
it('should return NaN if bytes is not a number', function(){ | ||
expect(filter("0",2)).toEqual("NaN"); | ||
expect(filter([0],2)).toEqual("NaN"); | ||
expect(filter({number:0},0)).toEqual("NaN"); | ||
expect(filter("0",2)).toEqual("NaN"); | ||
expect(filter([0],2)).toEqual("NaN"); | ||
expect(filter({number:0},0)).toEqual("NaN"); | ||
}); | ||
it('should return NaN if decimal point is less than zero or not a number', function(){ | ||
expect(filter(0.45,-1)).toEqual("NaN"); | ||
expect(filter(-0.25,-101)).toEqual("NaN"); | ||
expect(filter(0.45,1.3)).toEqual("NaN"); | ||
expect(filter(0.45,"0")).toEqual("NaN"); | ||
expect(filter(0.45,[3])).toEqual("NaN"); | ||
expect(filter(0.45,{num : 4})).toEqual("NaN"); | ||
expect(filter(0.45,-1)).toEqual("NaN"); | ||
expect(filter(-0.25,-101)).toEqual("NaN"); | ||
expect(filter(0.45,1.3)).toEqual("NaN"); | ||
expect(filter(0.45,"0")).toEqual("NaN"); | ||
expect(filter(0.45,[3])).toEqual("NaN"); | ||
expect(filter(0.45,{num : 4})).toEqual("NaN"); | ||
}); | ||
}); |
@@ -5,8 +5,7 @@ 'use strict'; | ||
//helpers | ||
function n(n) { return n; } | ||
var stub = { fn: function(x) { return n(x) } }; | ||
var stub = { fn: function(x) { return x; } }; | ||
beforeEach(module('a8m.filter-watcher')); | ||
it('should have 2 main functions `isMemoized` and `memozie`', inject(function(filterWatcher) { | ||
it('should have 2 main functions `isMemoized` and `memoize`', inject(function(filterWatcher) { | ||
expect(filterWatcher.isMemoized).toEqual(jasmine.any(Function)); | ||
@@ -16,3 +15,3 @@ expect(filterWatcher.memoize).toEqual(jasmine.any(Function)); | ||
it('should called the function if it\'s not cached', | ||
it('should call the function if it\'s not cached', | ||
inject(function(filterWatcher) { | ||
@@ -42,8 +41,6 @@ var spy = spyOn(stub, 'fn'); | ||
it('should get the result from cache if it\'s memoize', | ||
it('should get the result from cache if it\'s memoized', | ||
inject(function(filterWatcher, $rootScope) { | ||
var scope = $rootScope.$new(); | ||
var spy = spyOn(stub, 'fn').andCallFake(function() { | ||
return 1; | ||
}); | ||
var spy = spyOn(stub, 'fn').andCallThrough(); | ||
function memoize(n) { | ||
@@ -63,5 +60,3 @@ return filterWatcher.isMemoized('fName', n) || | ||
var scope; | ||
var spy = spyOn(stub, 'fn').andCallFake(function() { | ||
return 1; | ||
}); | ||
var spy = spyOn(stub, 'fn').andCallThrough(); | ||
function memoize(n) { | ||
@@ -68,0 +63,0 @@ return filterWatcher.isMemoized('fName', n) || |
361822
6670
1400