11/**
2- * Bunch of useful filters for angularJS
3- * @version v0.4.7 - 2014-10-03 * @link https://github.com/a8m/angular-filter
2+ * Bunch of useful filters for angularJS(with no external dependencies!)
3+ * @version v0.4.8 - 2014-10-13 * @link https://github.com/a8m/angular-filter
44 * @author Ariel Mashraki <ariel@mashraki.co.il>
55 * @license MIT License, http://www.opensource.org/licenses/MIT
66 */
@@ -22,6 +22,7 @@ var isDefined = angular.isDefined,
2222
2323
2424/**
25+ * @description
2526 * get an object and return array of values
2627 * @param object
2728 * @returns {Array }
@@ -34,7 +35,6 @@ function toArray(object) {
3435}
3536
3637/**
37- *
3838 * @param value
3939 * @returns {boolean }
4040 */
@@ -43,6 +43,7 @@ function isNull(value) {
4343}
4444
4545/**
46+ * @description
4647 * return if object contains partial object
4748 * @param partial{object}
4849 * @param object{object}
@@ -58,6 +59,7 @@ function objectContains(partial, object) {
5859}
5960
6061/**
62+ * @description
6163 * search for approximate pattern in string
6264 * @param word
6365 * @param pattern
@@ -76,6 +78,7 @@ function hasApproxPattern(word, pattern) {
7678}
7779
7880/**
81+ * @description
7982 * return the first n element of an array,
8083 * if expression provided, is returns as long the expression return truthy
8184 * @param array
@@ -103,7 +106,6 @@ if (!String.prototype.contains) {
103106}
104107
105108/**
106- *
107109 * @param num {Number}
108110 * @param decimal {Number}
109111 * @param $math
@@ -112,6 +114,35 @@ if (!String.prototype.contains) {
112114function convertToDecimal ( num , decimal , $math ) {
113115 return $math . round ( num * $math . pow ( 10 , decimal ) ) / ( $math . pow ( 10 , decimal ) ) ;
114116}
117+
118+ /**
119+ * @description
120+ * Get an object, and return an array composed of it's properties names(nested too).
121+ * @param obj {Object}
122+ * @param stack {Array}
123+ * @param parent {String}
124+ * @returns {Array }
125+ * @example
126+ * parseKeys({ a:1, b: { c:2, d: { e: 3 } } }) ==> ["a", "b.c", "b.d.e"]
127+ */
128+ function deepKeys ( obj , stack , parent ) {
129+ stack = stack || [ ] ;
130+ var keys = Object . keys ( obj ) ;
131+
132+ keys . forEach ( function ( el ) {
133+ //if it's a nested object
134+ if ( isObject ( obj [ el ] ) && ! isArray ( obj [ el ] ) ) {
135+ //concatenate the new parent if exist
136+ var p = parent ? parent + '.' + el : parent ;
137+ deepKeys ( obj [ el ] , stack , p || el ) ;
138+ } else {
139+ //create and save the key
140+ var key = parent ? parent + '.' + el : el ;
141+ stack . push ( key )
142+ }
143+ } ) ;
144+ return stack
145+ }
115146/**
116147 * @ngdoc filter
117148 * @name a8m.angular
@@ -485,6 +516,43 @@ angular.module('a8m.count-by', [])
485516 }
486517 } ] ) ;
487518
519+ /**
520+ * @ngdoc filter
521+ * @name defaults
522+ * @kind function
523+ *
524+ * @description
525+ * defaultsFilter allows to specify a default fallback value for properties that resolve to undefined.
526+ */
527+
528+ angular . module ( 'a8m.defaults' , [ ] )
529+ . filter ( 'defaults' , [ '$parse' , function ( $parse ) {
530+ return function ( collection , defaults ) {
531+
532+ collection = ( isObject ( collection ) ) ? toArray ( collection ) : collection ;
533+
534+ if ( ! isArray ( collection ) || ! isObject ( defaults ) ) {
535+ return collection ;
536+ }
537+
538+ var keys = deepKeys ( defaults ) ;
539+
540+ collection . forEach ( function ( elm ) {
541+ //loop through all the keys
542+ keys . forEach ( function ( key ) {
543+ var getter = $parse ( key ) ;
544+ var setter = getter . assign ;
545+ //if it's not exist
546+ if ( isUndefined ( getter ( elm ) ) ) {
547+ //get from defaults, and set to the returned object
548+ setter ( elm , getter ( defaults ) )
549+ }
550+ } ) ;
551+ } ) ;
552+
553+ return collection ;
554+ }
555+ } ] ) ;
488556/**
489557 * @ngdoc filter
490558 * @name every
@@ -1953,6 +2021,7 @@ angular.module('angular.filter', [
19532021 'a8m.after-where' ,
19542022 'a8m.before' ,
19552023 'a8m.before-where' ,
2024+ 'a8m.defaults' ,
19562025 'a8m.where' ,
19572026 'a8m.reverse' ,
19582027 'a8m.remove' ,
0 commit comments