Permalink
Browse files

Merge branch 'newFilters' of git://github.com/ProLoser/angular-ui int…

…o pr156
  • Loading branch information...
2 parents a38ed49 + 8f68c6c commit 9e721d256d8ef41328a6fabfb534127d25ba8ced @pkozlowski-opensource pkozlowski-opensource committed Aug 19, 2012
View
38 modules/filters/extract/extract.js
@@ -0,0 +1,38 @@
+
+/**
+ * Extracts properties of an array of objects and returns an array of strings
+ *
+ * @TODO Make paths go deeper than 1 level
+ * @TODO Allow an array for separator param?
+ * @param dataset {array[object]} array of objects to extract from
+ * @param paths {mixed} the property to retrieve from the objects
+ * string: property name
+ * array[string]: array of property names to be joined by separator in the order passed
+ * @param [separator] {string} Optional the string to join multiple properties
+ */
+angular.module('ui.filters').filter('extract', function(){
+ // paths can optionally be an array
+ // separator is how to merge those different paths
+ return function(value, paths, separator) {
+ var result = [],
+ newItem;
+ if (angular.isArray(value) || angular.isObject(value)) {
+ angular.forEach(value, function(item) {
+ // if paths is an array
+ if (angular.isArray(paths)) {
+ newItem = [];
+ angular.forEach(paths, function(path){
+ newItem.push(item[path]);
+ });
+ result.push(newItem.join(separator));
+ // if paths is a string
+ } else {
+ result.push(item[paths]);
+ }
+ });
+ } else {
+ result = value;
+ }
+ return result;
+ };
+});
View
49 modules/filters/extract/test/extractSpec.js
@@ -0,0 +1,49 @@
+describe('extract', function() {
+ var extractFilter, testData = [
+ { firstName: 'Igor', lastName: 'Minar', age: '10' },
+ { firstName: 'Dean', lastName: 'Sofer', age: '20' },
+ { firstName: 'Jason', lastName: 'Stathom', age: '30' }
+ ];
+
+ beforeEach(module('ui.filters'));
+ beforeEach(inject(function($filter) {
+ extractFilter = $filter('extract');
+ }));
+
+ it('should return an array of properties', function() {
+ expect(extractFilter(testData, 'extract')).toEqual('Prefix <span class="ui-match">extract</span> Suffix');
+ });
+ it('should extract nothing if no match found', function() {
+ expect(extractFilter(testData, 'no match')).toEqual(testData);
+ });
+ it('should extract nothing for the undefined filter', function() {
+ expect(extractFilter(testData, undefined)).toEqual(testData);
+ });
+ it('should work correctly for number filters', function() {
+ expect(extractFilter('3210123', 0)).toEqual('321<span class="ui-match">0</span>123');
+ });
+ it('should work correctly for number text', function() {
+ expect(extractFilter(3210123, '0')).toEqual('321<span class="ui-match">0</span>123');
+ });
+ it('should extract a matching phrase', function() {
+ expect(extractFilter(testData, 'extract', true)).toEqual('Prefix <span class="ui-match">extract</span> Suffix');
+ });
+ it('should extract nothing if no match found', function() {
+ expect(extractFilter(testData, 'no match', true)).toEqual(testData);
+ });
+ it('should extract nothing for the undefined filter', function() {
+ expect(extractFilter(testData, undefined, true)).toEqual(testData);
+ });
+ it('should work correctly for number filters', function() {
+ expect(extractFilter('3210123', 0, true)).toEqual('321<span class="ui-match">0</span>123');
+ });
+ it('should work correctly for number text', function() {
+ expect(extractFilter(3210123, '0', true)).toEqual('321<span class="ui-match">0</span>123');
+ });
+ it('should not extract a phrase with different letter-casing', function() {
+ expect(extractFilter(testData, 'extract', true)).toEqual(testData);
+ });
+ it('should extract nothing if empty filter string passed - issue #114', function() {
+ expect(extractFilter(testData, '')).toEqual(testData);
+ });
+});
View
34 modules/filters/format/format.js
@@ -0,0 +1,34 @@
+
+/**
+ * A replacement utility for internationalization very similar to sprintf.
+ *
+ * @param replace {mixed} The tokens to replace depends on type
+ * string: all instances of $0 will be replaced
+ * array: each instance of $0, $1, $2 etc. will be placed with each array item in corresponding order
+ * object: all attributes will be iterated through, with :key being replaced with its corresponding value
+ * @return string
+ *
+ * @example: 'Hello :name, how are you :day'.format({ name:'John', day:'Today' })
+ * @example: 'Records $0 to $1 out of $2 total'.format(['10', '20', '3000'])
+ * @example: '$0 agrees to all mentions $0 makes in the event that $0 hits a tree while $0 is driving drunk'.format('Bob')
+ */
+angular.module('ui.filters').filter('format', function(){
+ return function(value, replace) {
+ if (!value) {
+ return value;
+ }
+ var target = value.toString(), token;
+ if (replace === undefined) {
+ return target;
+ }
+ if (!angular.isArray(replace) && !angular.isObject(replace)) {
+ return target.split('$0').join(replace);
+ }
+ token = angular.isArray(replace) && '$' || ':';
+
+ angular.forEach(replace, function(value, key){
+ target = target.split(token+key).join(value);
+ });
+ return target;
+ };
+});
View
21 modules/filters/format/test/formatSpec.js
@@ -0,0 +1,21 @@
+describe('format', function() {
+ var formatFilter;
+
+ beforeEach(module('ui.filters'));
+ beforeEach(inject(function($filter) {
+ formatFilter = $filter('format');
+ }));
+
+ it('should replace all instances of $0 if string token is passed', function() {
+ expect(formatFilter('First $0, then $0, finally $0', 'bob')).toEqual('First bob, then bob, finally bob');
+ });
+ it('should replace all instances of $n based on order of token array', function() {
+ expect(formatFilter('First $0, then $1, finally $2', ['bob','frank','dianne'])).toEqual('First is bob, then frank, finally dianne');
+ });
+ it('should replace all instances :tokens based on keys of token object', function() {
+ expect(formatFilter('First is :first, next is :second, finally there is :third', {first:'bob',second:'frank',third:'dianne'})).toEqual('First is bob, then frank, finally dianne');
+ });
+ it('should do nothing if tokens are undefined', function() {
+ expect(formatFilter('Hello There')).toEqual('Hello There');
+ });
+});

0 comments on commit 9e721d2

Please sign in to comment.