11/**
22 * Bunch of useful filters for angularJS(with no external dependencies!)
3- * @version v0.5.12 - 2016-12-03 * @link https://github.com/a8m/angular-filter
3+ * @version v0.5.14 - 2016-12-06 * @link https://github.com/a8m/angular-filter
44 * @author Ariel Mashraki <[email protected] > 55 * @license MIT License, http://www.opensource.org/licenses/MIT
66 */
@@ -117,11 +117,10 @@ if (!String.prototype.contains) {
117117/**
118118 * @param num {Number}
119119 * @param decimal {Number}
120- * @param $math
121120 * @returns {Number }
122121 */
123- function convertToDecimal ( num , decimal , $math ) {
124- return $math . round ( num * $math . pow ( 10 , decimal ) ) / ( $math . pow ( 10 , decimal ) ) ;
122+ function convertToDecimal ( num , decimal ) {
123+ return Math . round ( num * Math . pow ( 10 , decimal ) ) / ( Math . pow ( 10 , decimal ) ) ;
125124}
126125
127126/**
@@ -1356,36 +1355,24 @@ angular.module('a8m.math.abs', [])
13561355 * Convert bytes into appropriate display
13571356 * 1024 bytes => 1 KB
13581357 */
1359- angular . module ( 'a8m.math.byteFmt' , [ 'a8m.math' ] )
1360- . filter ( 'byteFmt' , [ '$math' , function ( $math ) {
1358+ angular . module ( 'a8m.math.byteFmt' , [ ] )
1359+ . filter ( 'byteFmt' , function ( ) {
1360+ var compared = [ { str : 'B' , val : 1024 } ] ;
1361+ [ 'KB' , 'MB' , 'GB' , 'TB' , 'PB' , 'EB' , 'ZB' , 'YB' ] . forEach ( function ( el , i ) {
1362+ compared . push ( { str : el , val : compared [ i ] . val * 1024 } ) ;
1363+ } ) ;
13611364 return function ( bytes , decimal ) {
1362-
1363- if ( isNumber ( decimal ) && isFinite ( decimal ) && decimal % 1 === 0 && decimal >= 0 &&
1365+ if ( isNumber ( decimal ) && isFinite ( decimal ) && decimal % 1 === 0 && decimal >= 0 &&
13641366 isNumber ( bytes ) && isFinite ( bytes ) ) {
1365- if ( bytes < 1024 ) { // within 1 KB so B
1366- return convertToDecimal ( bytes , decimal , $math ) + ' B' ;
1367- } else if ( bytes < 1048576 ) { // within 1 MB so KB
1368- return convertToDecimal ( ( bytes / 1024 ) , decimal , $math ) + ' KB' ;
1369- } else if ( bytes < 1073741824 ) { // within 1 GB so MB
1370- return convertToDecimal ( ( bytes / 1048576 ) , decimal , $math ) + ' MB' ;
1371- } else if ( bytes < 1099511627776 ) { // 1 TB so GB
1372- return convertToDecimal ( ( bytes / 1073741824 ) , decimal , $math ) + ' GB' ;
1373- } else if ( bytes < 1125899906842624 ) { // 1 PB so TB
1374- return convertToDecimal ( ( bytes / 1099511627776 ) , decimal , $math ) + ' TB' ;
1375- } else if ( bytes < 1152921504606846976 ) { // 1 EB so ZB
1376- return convertToDecimal ( ( bytes / 1125899906842624 ) , decimal , $math ) + ' PB' ;
1377- } else if ( bytes < 1180591620717411303424 ) { // 1 ZB so EB
1378- return convertToDecimal ( ( bytes / 1152921504606846976 ) , decimal , $math ) + ' EB' ;
1379- } else if ( bytes < 1208925819614629174706176 ) { // 1 YB so ZB
1380- return convertToDecimal ( ( bytes / 1180591620717411303424 ) , decimal , $math ) + ' ZB' ;
1381- } else { // 1 YB or more
1382- return convertToDecimal ( ( bytes / 1208925819614629174706176 ) , decimal , $math ) + ' YB' ;
1383- }
1384-
1367+ var i = 0 ;
1368+ while ( i < compared . length - 1 && bytes >= compared [ i ] . val ) i ++ ;
1369+ bytes /= i > 0 ? compared [ i - 1 ] . val : 1 ;
1370+ return convertToDecimal ( bytes , decimal ) + ' ' + compared [ i ] . str ;
13851371 }
1386- return " NaN" ;
1372+ return ' NaN' ;
13871373 }
1388- } ] ) ;
1374+ } ) ;
1375+
13891376/**
13901377 * @ngdoc filter
13911378 * @name degrees
@@ -1394,20 +1381,20 @@ angular.module('a8m.math.byteFmt', ['a8m.math'])
13941381 * @description
13951382 * Convert angle from radians to degrees
13961383 */
1397- angular . module ( 'a8m.math.degrees' , [ 'a8m.math' ] )
1398- . filter ( 'degrees' , [ '$math' , function ( $math ) {
1384+ angular . module ( 'a8m.math.degrees' , [ ] )
1385+ . filter ( 'degrees' , function ( ) {
13991386 return function ( radians , decimal ) {
14001387 // if decimal is not an integer greater than -1, we cannot do. quit with error "NaN"
14011388 // if degrees is not a real number, we cannot do also. quit with error "NaN"
14021389 if ( isNumber ( decimal ) && isFinite ( decimal ) && decimal % 1 === 0 && decimal >= 0 &&
14031390 isNumber ( radians ) && isFinite ( radians ) ) {
1404- var degrees = ( radians * 180 ) / $math . PI ;
1405- return $math . round ( degrees * $math . pow ( 10 , decimal ) ) / ( $math . pow ( 10 , decimal ) ) ;
1391+ var degrees = ( radians * 180 ) / Math . PI ;
1392+ return Math . round ( degrees * Math . pow ( 10 , decimal ) ) / ( Math . pow ( 10 , decimal ) ) ;
14061393 } else {
1407- return " NaN" ;
1394+ return ' NaN' ;
14081395 }
14091396 }
1410- } ] ) ;
1397+ } ) ;
14111398
14121399
14131400
@@ -1420,44 +1407,23 @@ angular.module('a8m.math.degrees', ['a8m.math'])
14201407 * Convert bytes into appropriate display
14211408 * 1024 kilobytes => 1 MB
14221409 */
1423- angular . module ( 'a8m.math.kbFmt' , [ 'a8m.math' ] )
1424- . filter ( 'kbFmt' , [ '$math' , function ( $math ) {
1410+ angular . module ( 'a8m.math.kbFmt' , [ ] )
1411+ . filter ( 'kbFmt' , function ( ) {
1412+ var compared = [ { str : 'KB' , val : 1024 } ] ;
1413+ [ 'MB' , 'GB' , 'TB' , 'PB' , 'EB' , 'ZB' , 'YB' ] . forEach ( function ( el , i ) {
1414+ compared . push ( { str : el , val : compared [ i ] . val * 1024 } ) ;
1415+ } ) ;
14251416 return function ( bytes , decimal ) {
1426-
14271417 if ( isNumber ( decimal ) && isFinite ( decimal ) && decimal % 1 === 0 && decimal >= 0 &&
14281418 isNumber ( bytes ) && isFinite ( bytes ) ) {
1429- if ( bytes < 1024 ) { // within 1 MB so KB
1430- return convertToDecimal ( bytes , decimal , $math ) + ' KB' ;
1431- } else if ( bytes < 1048576 ) { // within 1 GB so MB
1432- return convertToDecimal ( ( bytes / 1024 ) , decimal , $math ) + ' MB' ;
1433- } else if ( bytes < 1073741824 ) { // within 1 TB so GB
1434- return convertToDecimal ( ( bytes / 1048576 ) , decimal , $math ) + ' GB' ;
1435- } else if ( bytes < 1099511627776 ) { // 1 PB so TB
1436- return convertToDecimal ( ( bytes / 1073741824 ) , decimal , $math ) + ' TB' ;
1437- } else if ( bytes < 1125899906842624 ) { // 1 EB so ZB
1438- return convertToDecimal ( ( bytes / 1099511627776 ) , decimal , $math ) + ' PB' ;
1439- } else if ( bytes < 1152921504606846976 ) { // 1 ZB so EB
1440- return convertToDecimal ( ( bytes / 1125899906842624 ) , decimal , $math ) + ' EB' ;
1441- } else if ( bytes < 1180591620717411303424 ) { // 1 YB so ZB
1442- return convertToDecimal ( ( bytes / 1152921504606846976 ) , decimal , $math ) + ' ZB' ;
1443- } else { // 1 YB or more
1444- return convertToDecimal ( ( bytes / 1180591620717411303424 ) , decimal , $math ) + ' YB' ;
1445- }
1419+ var i = 0 ;
1420+ while ( i < compared . length - 1 && bytes >= compared [ i ] . val ) i ++ ;
1421+ bytes /= i > 0 ? compared [ i - 1 ] . val : 1 ;
1422+ return convertToDecimal ( bytes , decimal ) + ' ' + compared [ i ] . str ;
14461423 }
1447- return " NaN" ;
1424+ return ' NaN' ;
14481425 }
1449- } ] ) ;
1450- /**
1451- * @ngdoc module
1452- * @name math
1453- * @description
1454- * reference to global Math object
1455- */
1456- angular . module ( 'a8m.math' , [ ] )
1457- . factory ( '$math' , [ '$window' , function ( $window ) {
1458- return $window . Math ;
1459- } ] ) ;
1460-
1426+ } ) ;
14611427/**
14621428 * @ngdoc filter
14631429 * @name max
@@ -1467,15 +1433,15 @@ angular.module('a8m.math', [])
14671433 * Math.max will get an array and return the max value. if an expression
14681434 * is provided, will return max value by expression.
14691435 */
1470- angular . module ( 'a8m.math.max' , [ 'a8m.math' ] )
1471- . filter ( 'max' , [ '$math' , '$ parse', function ( $math , $parse ) {
1436+ angular . module ( 'a8m.math.max' , [ ] )
1437+ . filter ( 'max' , [ '$parse' , function ( $parse ) {
14721438 return function ( input , expression ) {
14731439
14741440 if ( ! isArray ( input ) ) {
14751441 return input ;
14761442 }
14771443 return isUndefined ( expression )
1478- ? $math . max . apply ( $math , input )
1444+ ? Math . max . apply ( Math , input )
14791445 : input [ indexByMax ( input , expression ) ] ;
14801446 } ;
14811447
@@ -1489,7 +1455,7 @@ angular.module('a8m.math.max', ['a8m.math'])
14891455 var mappedArray = array . map ( function ( elm ) {
14901456 return $parse ( exp ) ( elm ) ;
14911457 } ) ;
1492- return mappedArray . indexOf ( $math . max . apply ( $math , mappedArray ) ) ;
1458+ return mappedArray . indexOf ( Math . max . apply ( Math , mappedArray ) ) ;
14931459 }
14941460 } ] ) ;
14951461/**
@@ -1501,15 +1467,15 @@ angular.module('a8m.math.max', ['a8m.math'])
15011467 * Math.min will get an array and return the min value. if an expression
15021468 * is provided, will return min value by expression.
15031469 */
1504- angular . module ( 'a8m.math.min' , [ 'a8m.math' ] )
1505- . filter ( 'min' , [ '$math' , '$ parse', function ( $math , $parse ) {
1470+ angular . module ( 'a8m.math.min' , [ ] )
1471+ . filter ( 'min' , [ '$parse' , function ( $parse ) {
15061472 return function ( input , expression ) {
15071473
15081474 if ( ! isArray ( input ) ) {
15091475 return input ;
15101476 }
15111477 return isUndefined ( expression )
1512- ? $math . min . apply ( $math , input )
1478+ ? Math . min . apply ( Math , input )
15131479 : input [ indexByMin ( input , expression ) ] ;
15141480 } ;
15151481
@@ -1523,7 +1489,7 @@ angular.module('a8m.math.min', ['a8m.math'])
15231489 var mappedArray = array . map ( function ( elm ) {
15241490 return $parse ( exp ) ( elm ) ;
15251491 } ) ;
1526- return mappedArray . indexOf ( $math . min . apply ( $math , mappedArray ) ) ;
1492+ return mappedArray . indexOf ( Math . min . apply ( Math , mappedArray ) ) ;
15271493 }
15281494 } ] ) ;
15291495/**
@@ -1534,21 +1500,21 @@ angular.module('a8m.math.min', ['a8m.math'])
15341500 * @description
15351501 * percentage between two numbers
15361502 */
1537- angular . module ( 'a8m.math.percent' , [ 'a8m.math' ] )
1538- . filter ( 'percent' , [ '$math' , '$window' , function ( $math , $window ) {
1503+ angular . module ( 'a8m.math.percent' , [ ] )
1504+ . filter ( 'percent' , function ( ) {
15391505 return function ( input , divided , round ) {
15401506
1541- var divider = isString ( input ) ? $window . Number ( input ) : input ;
1507+ var divider = isString ( input ) ? Number ( input ) : input ;
15421508 divided = divided || 100 ;
15431509 round = round || false ;
15441510
1545- if ( ! isNumber ( divider ) || $window . isNaN ( divider ) ) return input ;
1511+ if ( ! isNumber ( divider ) || isNaN ( divider ) ) return input ;
15461512
15471513 return round
1548- ? $math . round ( ( divider / divided ) * 100 )
1514+ ? Math . round ( ( divider / divided ) * 100 )
15491515 : ( divider / divided ) * 100 ;
15501516 }
1551- } ] ) ;
1517+ } ) ;
15521518
15531519/**
15541520 * @ngdoc filter
@@ -1558,19 +1524,19 @@ angular.module('a8m.math.percent', ['a8m.math'])
15581524 * @description
15591525 * Convert angle from degrees to radians
15601526 */
1561- angular . module ( 'a8m.math.radians' , [ 'a8m.math' ] )
1562- . filter ( 'radians' , [ '$math' , function ( $math ) {
1527+ angular . module ( 'a8m.math.radians' , [ ] )
1528+ . filter ( 'radians' , function ( ) {
15631529 return function ( degrees , decimal ) {
15641530 // if decimal is not an integer greater than -1, we cannot do. quit with error "NaN"
15651531 // if degrees is not a real number, we cannot do also. quit with error "NaN"
15661532 if ( isNumber ( decimal ) && isFinite ( decimal ) && decimal % 1 === 0 && decimal >= 0 &&
15671533 isNumber ( degrees ) && isFinite ( degrees ) ) {
15681534 var radians = ( degrees * 3.14159265359 ) / 180 ;
1569- return $math . round ( radians * $math . pow ( 10 , decimal ) ) / ( $math . pow ( 10 , decimal ) ) ;
1535+ return Math . round ( radians * Math . pow ( 10 , decimal ) ) / ( Math . pow ( 10 , decimal ) ) ;
15701536 }
1571- return " NaN" ;
1537+ return ' NaN' ;
15721538 }
1573- } ] ) ;
1539+ } ) ;
15741540
15751541
15761542
@@ -1605,25 +1571,25 @@ angular.module('a8m.math.radix', [])
16051571 * i.e: K for one thousand, M for Million, B for billion
16061572 * e.g: number of users:235,221, decimal:1 => 235.2 K
16071573 */
1608- angular . module ( 'a8m.math.shortFmt' , [ 'a8m.math' ] )
1609- . filter ( 'shortFmt' , [ '$math' , function ( $math ) {
1574+ angular . module ( 'a8m.math.shortFmt' , [ ] )
1575+ . filter ( 'shortFmt' , function ( ) {
16101576 return function ( number , decimal ) {
16111577 if ( isNumber ( decimal ) && isFinite ( decimal ) && decimal % 1 === 0 && decimal >= 0 &&
16121578 isNumber ( number ) && isFinite ( number ) ) {
16131579 if ( number < 1e3 ) {
1614- return number ;
1580+ return '' + number ; // Coerce to string
16151581 } else if ( number < 1e6 ) {
1616- return convertToDecimal ( ( number / 1e3 ) , decimal , $math ) + ' K' ;
1582+ return convertToDecimal ( ( number / 1e3 ) , decimal ) + ' K' ;
16171583 } else if ( number < 1e9 ) {
1618- return convertToDecimal ( ( number / 1e6 ) , decimal , $math ) + ' M' ;
1584+ return convertToDecimal ( ( number / 1e6 ) , decimal ) + ' M' ;
16191585 } else {
1620- return convertToDecimal ( ( number / 1e9 ) , decimal , $math ) + ' B' ;
1586+ return convertToDecimal ( ( number / 1e9 ) , decimal ) + ' B' ;
16211587 }
16221588
16231589 }
1624- return " NaN" ;
1590+ return ' NaN' ;
16251591 }
1626- } ] ) ;
1592+ } ) ;
16271593/**
16281594 * @ngdoc filter
16291595 * @name sum
@@ -2315,7 +2281,6 @@ angular.module('angular.filter', [
23152281 'a8m.join' ,
23162282 'a8m.range' ,
23172283
2318- 'a8m.math' ,
23192284 'a8m.math.max' ,
23202285 'a8m.math.min' ,
23212286 'a8m.math.abs' ,
0 commit comments