├── .gitignore ├── .travis.yml ├── .jshintrc ├── .editorconfig ├── test ├── karma.conf.js └── unit │ └── currency-filter.spec.js ├── bower.json ├── CHANGELOG.md ├── Gruntfile.js ├── LICENSE ├── dist ├── currency-filter.min.js └── currency-filter.min.map ├── package.json ├── src └── currency-filter.js └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | bower_components 2 | node_modules 3 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "0.12" 4 | before_script: 5 | - npm install grunt-cli -g 6 | - npm install -g bower 7 | - bower install 8 | -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "node": true, 3 | "browser": true, 4 | "es5": true, 5 | "esnext": true, 6 | "bitwise": true, 7 | "camelcase": true, 8 | "curly": true, 9 | "eqeqeq": true, 10 | "immed": true, 11 | "indent": 2, 12 | "latedef": true, 13 | "newcap": true, 14 | "noarg": true, 15 | "quotmark": "single", 16 | "regexp": true, 17 | "undef": true, 18 | "unused": true, 19 | "strict": true, 20 | "trailing": true, 21 | "smarttabs": true, 22 | "globals": { 23 | "angular": false 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # EditorConfig helps developers define and maintain consistent 2 | # coding styles between different editors and IDEs 3 | # editorconfig.org 4 | 5 | root = true 6 | 7 | 8 | [*] 9 | 10 | # Change these settings to your own preference 11 | indent_style = space 12 | indent_size = 2 13 | 14 | # We recommend you to keep these unchanged 15 | end_of_line = lf 16 | charset = utf-8 17 | trim_trailing_whitespace = true 18 | insert_final_newline = true 19 | 20 | [*.md] 21 | trim_trailing_whitespace = false 22 | indent_size = 4 23 | -------------------------------------------------------------------------------- /test/karma.conf.js: -------------------------------------------------------------------------------- 1 | /*global module */ 2 | module.exports = function ( config ) { 3 | 'use strict'; 4 | 5 | var files = [ 6 | '../bower_components/angular/angular.js', 7 | '../bower_components/angular-mocks/angular-mocks.js', 8 | '../src/currency-filter.js', 9 | '../test/unit/currency-filter.spec.js' 10 | ]; 11 | 12 | config.set({ 13 | files : files, 14 | basePath: '', 15 | frameworks: ['jasmine'], 16 | reporters: ['progress'], 17 | browsers: ['PhantomJS'], 18 | autoWatch: true, 19 | singleRun: false, 20 | colors: true 21 | }); 22 | }; 23 | -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "angular-currency-filter", 3 | "version": "2.2.0", 4 | "authors": [ 5 | "Oliver Kovacs " 6 | ], 7 | "description": "Extend angular's built in currency filter.", 8 | "main": "./src/currency-filter.js", 9 | "keywords": [ 10 | "angularjs", 11 | "currency", 12 | "filter" 13 | ], 14 | "license": "MIT", 15 | "ignore": [ 16 | "**/.*", 17 | "node_modules", 18 | "bower_components", 19 | "test", 20 | "tests" 21 | ], 22 | "devDependencies": { 23 | "angular": "~1.2.1", 24 | "angular-mocks": "~1.2.1" 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # 2.2.0 2 | - Allowing the specification of an optional suffix string. Thanks @brophdawg11 [Issue #9](https://github.com/Zmetser/angular-currency-filter/issues/9) 3 | 4 | # 2.1.0 5 | - Adds the `customFormat` parameter to set the group and decimals separators. [Issue #4](https://github.com/Zmetser/angular-currency-filter/issues/4) 6 | 7 | # 2.0.0 8 | - **Breaking** If the amount is not a number, the formatter defaults to 0 instead of empty string. [Issue #5](https://github.com/Zmetser/angular-currency-filter/issues/5) 9 | 10 | # 1.0.2 11 | 12 | - This release is a build of v1.0.1 13 | - **Breaking** rename /dist/currency-filter.min.js to /dist/currency-filter.min.map 14 | 15 | # 1.0.1 16 | 17 | ## Bug Fixes 18 | 19 | - **IE8 Compatibility** fix ie8 toString.call ([f4fba9f7](https://github.com/Zmetser/angular-currency-filter/commit/f4fba9f78f3b6701e63bf0559614a96f46d79fdd)) 20 | - **npm** package update ([e2c7eff9](https://github.com/Zmetser/angular-currency-filter/commit/e2c7eff9ccfd8ad32d92d1299c269f723bac2bc6)) 21 | -------------------------------------------------------------------------------- /Gruntfile.js: -------------------------------------------------------------------------------- 1 | module.exports = function(grunt) { 2 | grunt.loadNpmTasks('grunt-contrib-uglify'); 3 | grunt.loadNpmTasks('grunt-karma'); 4 | 5 | grunt.initConfig({ 6 | 7 | pkg: grunt.file.readJSON('package.json'), 8 | 9 | uglify: { 10 | my_target: { 11 | options: { 12 | sourceMap: 'dist/currency-filter.map.js', 13 | banner: '/*! <%= pkg.name %> - v<%= pkg.version %> - ' + 14 | '<%= grunt.template.today("yyyy-mm-dd") %> - ' + 15 | '<%= pkg.repository.url %> */' 16 | }, 17 | files: { 18 | 'dist/currency-filter.min.js': ['src/currency-filter.js'] 19 | } 20 | } 21 | }, 22 | 23 | karma: { 24 | unit: { 25 | configFile: 'test/karma.conf.js', 26 | autoWatch: false, 27 | singleRun: true 28 | } 29 | } 30 | 31 | }); 32 | 33 | grunt.registerTask('test', ['karma:unit']); 34 | grunt.registerTask('build', ['uglify']); 35 | 36 | grunt.registerTask('default', ['karma:unit', 'uglify']); 37 | 38 | }; 39 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2013-2016 Oliver Kovacs 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /dist/currency-filter.min.js: -------------------------------------------------------------------------------- 1 | /*! angular-currency-filter - v2.2.0 - 2016-09-13 - https://github.com/Zmetser/angular-currency-filter.git */ 2 | !function(a){"use strict";var b=function(a){return a===!0||a===!1||"[object Boolean]"===Object.prototype.toString.call(a)};a.module("currencyFilter",[]).filter("currency",["$injector","$locale",function(c,d){var e=c.get("$filter"),f=e("number"),g=d.NUMBER_FORMATS,h=g.PATTERNS[1];return g.DEFAULT_PRECISION=a.isUndefined(g.DEFAULT_PRECISION)?2:g.DEFAULT_PRECISION,function(c,d,e,i,j){a.isNumber(c)||(c=0),a.isObject(d)&&(j=d),(a.isUndefined(d)||a.isObject(d))&&(d=g.CURRENCY_SYM);var k=c<0,l=[];(b(e)||a.isString(e))&&(i=e,e=g.DEFAULT_PRECISION),e=a.isUndefined(e)?g.DEFAULT_PRECISION:e,c=Math.abs(c);for(var m=j&&a.isString(j.GROUP_SEP)?j.GROUP_SEP:g.GROUP_SEP,n=j&&a.isString(j.DECIMAL_SEP)?j.DECIMAL_SEP:g.DECIMAL_SEP,o=f(c,e),p=[],q=0;q", 34 | "contributors": [ 35 | "Matt Brophy (https://github.com/brophdawg11)" 36 | ], 37 | "license": "MIT", 38 | "bugs": { 39 | "url": "https://github.com/Zmetser/angular-currency-filter/issues" 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /dist/currency-filter.min.map: -------------------------------------------------------------------------------- 1 | {"version":3,"sources":["../src/currency-filter.js"],"names":["angular","isBoolean","obj","Object","prototype","toString","call","module","filter","$injector","$locale","$filter","get","numberFilter","formats","NUMBER_FORMATS","pattern","PATTERNS","DEFAULT_PRECISION","isUndefined","amount","currencySymbol","fractionSize","suffixSymbol","customFormat","isNumber","isObject","CURRENCY_SYM","isNegative","parts","isString","Math","abs","groupSep","GROUP_SEP","decimalSep","DECIMAL_SEP","number","formattedNumber","i","length","push","join","negPre","posPre","negSuf","posSuf","replace"],"mappings":";CACC,SAAUA,GACT,YAEA,IAAIC,GAAY,SAAWC,GACzB,MAAOA,MAAQ,GAAQA,KAAQ,GAAiD,qBAAxCC,OAAOC,UAAUC,SAASC,KAAKJ,GAGzEF,GAAQO,OAAO,qBACbC,OAAO,YAAa,YAAa,UAAW,SAAWC,EAAWC,GAChE,GAAIC,GAAUF,EAAUG,IAAI,WACxBC,EAAeF,EAAQ,UACvBG,EAAUJ,EAAQK,eAClBC,EAAUF,EAAQG,SAAS,EAG/B,OADAH,GAAQI,kBAAoBlB,EAAQmB,YAAYL,EAAQI,mBAAqB,EAAIJ,EAAQI,kBAClF,SAAWE,EAAQC,EAAgBC,EAAcC,EAAcC,GAC9DxB,EAAQyB,SAASL,KAAYA,EAAS,GACvCpB,EAAQ0B,SAASL,KAAoBG,EAAeH,IACpDrB,EAAQmB,YAAYE,IAAmBrB,EAAQ0B,SAASL,MAAoBA,EAAiBP,EAAQa,aAC1G,IAAIC,GAAaR,EAAS,EACtBS,MAEA5B,EAAUqB,IAAiBtB,EAAQ8B,SAASR,MAC5CC,EAAeD,EACfA,EAAeR,EAAQI,mBAG3BI,EAAetB,EAAQmB,YAAYG,GAAgBR,EAAQI,kBAAoBI,EAE/EF,EAASW,KAAKC,IAAIZ,EAOlB,KAAK,GALDa,GAAWT,GAAgBxB,EAAQ8B,SAASN,EAAaU,WAAaV,EAAaU,UAAYpB,EAAQoB,UACvGC,EAAaX,GAAgBxB,EAAQ8B,SAASN,EAAaY,aAAeZ,EAAaY,YAActB,EAAQsB,YAC7GC,EAASxB,EAAcO,EAAQE,GAE/BgB,KACKC,EAAI,EAAGA,EAAIF,EAAOG,OAAQD,IAC7BF,EAAOE,KAAOzB,EAAQoB,UACxBI,EAAgBG,KAAKR,GACdI,EAAOE,KAAOzB,EAAQsB,YAC7BE,EAAgBG,KAAKN,GAErBG,EAAgBG,KAAKJ,EAAOE,GAiBhC,OAfAD,GAAkBA,EAAgBI,KAAK,IAEvCb,EAAMY,KAAKb,EAAaZ,EAAQ2B,OAAS3B,EAAQ4B,QAE7C5C,EAAQ8B,SAASP,IACjBM,EAAMY,KAAKpB,GACXQ,EAAMY,KAAKH,GACXT,EAAMY,KAAKlB,KAEXM,EAAMY,KAAMlB,EAAgCe,EAAjBjB,GAC3BQ,EAAMY,KAAKlB,EAAeF,EAAiBiB,IAG/CT,EAAMY,KAAKb,EAAaZ,EAAQ6B,OAAS7B,EAAQ8B,QAE1CjB,EAAMa,KAAK,IAAIK,QAAQ,UAAW,SAI9C/C","file":"currency-filter.min.js"} -------------------------------------------------------------------------------- /src/currency-filter.js: -------------------------------------------------------------------------------- 1 | /*global toString */ 2 | (function( angular ) { 3 | 'use strict'; 4 | 5 | var isBoolean = function ( obj ) { 6 | return obj === true || obj === false || Object.prototype.toString.call(obj) === '[object Boolean]'; 7 | }; 8 | 9 | angular.module('currencyFilter', []). 10 | filter('currency', ['$injector', '$locale', function ( $injector, $locale ) { 11 | var $filter = $injector.get('$filter'); 12 | var numberFilter = $filter('number'); 13 | var formats = $locale.NUMBER_FORMATS; 14 | var pattern = formats.PATTERNS[1]; 15 | // https://github.com/angular/angular.js/pull/3642 16 | formats.DEFAULT_PRECISION = angular.isUndefined(formats.DEFAULT_PRECISION) ? 2 : formats.DEFAULT_PRECISION; 17 | return function ( amount, currencySymbol, fractionSize, suffixSymbol, customFormat ) { 18 | if ( !angular.isNumber(amount) ) { amount = 0; } 19 | if ( angular.isObject(currencySymbol) ) { customFormat = currencySymbol; } 20 | if ( angular.isUndefined(currencySymbol) || angular.isObject(currencySymbol) ) { currencySymbol = formats.CURRENCY_SYM; } 21 | var isNegative = amount < 0; 22 | var parts = []; 23 | 24 | if (isBoolean(fractionSize) || angular.isString(fractionSize)) { 25 | suffixSymbol = fractionSize; 26 | fractionSize = formats.DEFAULT_PRECISION; 27 | } 28 | 29 | fractionSize = angular.isUndefined(fractionSize) ? formats.DEFAULT_PRECISION : fractionSize; 30 | 31 | amount = Math.abs(amount); 32 | 33 | var groupSep = customFormat && angular.isString(customFormat.GROUP_SEP) ? customFormat.GROUP_SEP : formats.GROUP_SEP; 34 | var decimalSep = customFormat && angular.isString(customFormat.DECIMAL_SEP) ? customFormat.DECIMAL_SEP : formats.DECIMAL_SEP; 35 | var number = numberFilter( amount, fractionSize ); 36 | 37 | var formattedNumber = []; 38 | for (var i = 0; i < number.length; i++) { 39 | if (number[i] === formats.GROUP_SEP) 40 | formattedNumber.push(groupSep); 41 | else if (number[i] === formats.DECIMAL_SEP) 42 | formattedNumber.push(decimalSep); 43 | else 44 | formattedNumber.push(number[i]); 45 | } 46 | formattedNumber = formattedNumber.join(''); 47 | 48 | parts.push(isNegative ? pattern.negPre : pattern.posPre); 49 | 50 | if (angular.isString(suffixSymbol)) { 51 | parts.push(currencySymbol); 52 | parts.push(formattedNumber); 53 | parts.push(suffixSymbol); 54 | } else { 55 | parts.push(!suffixSymbol ? currencySymbol : formattedNumber); 56 | parts.push(suffixSymbol ? currencySymbol : formattedNumber); 57 | } 58 | 59 | parts.push(isNegative ? pattern.negSuf : pattern.posSuf); 60 | 61 | return parts.join('').replace(/\u00A4/g, ''); 62 | }; 63 | }]); 64 | 65 | }( angular )); 66 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Angular Currency Filter 2 | 3 | [![Build Status](https://api.travis-ci.org/Zmetser/angular-currency-filter.png?branch=master)](https://travis-ci.org/Zmetser/angular-currency-filter) 4 | 5 | Extend [angular's built in currency filter](http://docs.angularjs.org/api/ng.filter:currency). 6 | 7 | ## Description 8 | Formats a number as a currency (ie *$1,234.56* or *914.3534€*). 9 | When no currency symbol is provided, default symbol for current locale is used. 10 | 11 | ## Usage 12 | 13 | Overwrites angular's default currency filter if module: `currencyFilter` is injected. *(complete example in the Example section)* 14 | 15 | ### In HTML Template Binding 16 | {{ currency_expression | currency:symbol[:fractionSize[:suffixSymbol[:customFormat]]] }} 17 | 18 | ### In JavaScript 19 | $filter('currency')(amount, symbol[, fractionSize[, suffixSymbol[, customFormat]]]) 20 | 21 | #### Paramaters 22 | 23 | Param | Type | Details 24 | :----------- | :------ | :------ 25 | amount | number | Input to filter. 26 | symbol | string | Currency symbol or identifier to be displayed. Falls back to [ng.$locale](https://code.angularjs.org/1.2.1/docs/api/ng.$locale). 27 | fractionSize | number | Number of decimal places to round the number to. Falls back to [ng.$locale](https://code.angularjs.org/1.2.1/docs/api/ng.$locale) 28 | suffixSymbol | boolean or string | If set to true the currency symbol will be placed after the amount. If passed as a string, will apply currencySymbol as a prefix and suffixSymbol as a suffix 29 | customFormat | object | Customize group and decimal separators (`GROUP_SEP`, `DECIMAL_SEP`) Both falls back to [ng.$locale](https://code.angularjs.org/1.2.1/docs/api/ng.$locale). 30 | 31 | #### Returns 32 | 33 | String: Formatted number. 34 | 35 | ### Use cases 36 | 37 | var formats = { 38 | GROUP_SEP: ' ', 39 | DECIMAL_SEP: ',' 40 | }; 41 | 42 | // With all parameters 43 | expect(currency(1234.4239, '€', 1, true, formats)).toEqual('1 234,4€'); 44 | 45 | // With all parameters, using string suffix 46 | expect(currency(1234.4239, '€', 1, 'EUR', formats)).toEqual('€1 234,4EUR'); 47 | 48 | // With missing fraction size 49 | expect(currency(1234.4239, '€', true)).toEqual('1,234.42€'); 50 | 51 | // With missing fraction size, using string suffix 52 | expect(currency(1234.4239, '€', 'EUR')).toEqual('€1,234.42EUR'); 53 | 54 | // With fraction size only 55 | expect(currency(1234.4239, '$', 3)).toEqual('$1,234.424'); 56 | 57 | // Only with symbol 58 | expect(currency(1234.4239, '$')).toEqual('$1,234.42'); 59 | 60 | // Only with custom group and decimal separators 61 | expect(currency(1234.4239, formats)).toEqual('$1 234,42'); 62 | 63 | ### Example 64 | 65 | #### HTML Template Binding 66 | 67 | 68 | 69 | 70 | #### JavaScript 71 | 72 | angular.module('app', ['currencyFilter']). 73 | controller('Ctrl', function ( $scope, $filter ) { 74 | var currency = $filter('currency'); 75 | $scope.price = currency(1234.4239, '€', 0, true); // 1234€ 76 | $scope.price = currency(1234.4239, '$', 0, ' USD'); // $1234 USD 77 | }); 78 | 79 | 80 | ## Install 81 | 82 | ### Via bower 83 | 84 | bower install --save angular-currency-filter 85 | 86 | Include `src/currency-filter.js` or `dist/currency-filter.min.js` to your project. 87 | 88 | 89 | 90 | Don't forget to add `currencyFilter` module to your app's dependecies. 91 | 92 | ## Test && Build 93 | 94 | $ npm install 95 | $ bower install 96 | 97 | ### Test 98 | 99 | $ grunt test 100 | 101 | ### Build 102 | 103 | $ grunt build 104 | 105 | ## Compatibility 106 | 107 | Functionality verified with unit test with angular versions from `v1.2.1` to `v1.4.9`. 108 | -------------------------------------------------------------------------------- /test/unit/currency-filter.spec.js: -------------------------------------------------------------------------------- 1 | /*global beforeEach, describe, it, inject, expect, module */ 2 | 'use strict'; 3 | 4 | describe('currency', function() { 5 | 6 | var currency, 7 | defaultAmount = '$0.00'; 8 | 9 | beforeEach(module('currencyFilter')); 10 | 11 | beforeEach(inject(function($filter){ 12 | currency = $filter('currency'); 13 | })); 14 | 15 | describe('default functionality', function() { 16 | 17 | it('should do basic currency filtering', function() { 18 | expect(currency(0)).toEqual(defaultAmount); 19 | expect(currency(-999)).toEqual('($999.00)'); 20 | expect(currency(1234.5678, 'USD$')).toEqual('USD$1,234.57'); 21 | }); 22 | 23 | it('should fallback to 0 for non-numeric amount primitives', function() { 24 | expect(currency()).toBe(defaultAmount); 25 | expect(currency('123')).toBe(defaultAmount); 26 | expect(currency('abc')).toBe(defaultAmount); 27 | expect(currency(null)).toBe(defaultAmount); 28 | expect(currency(false)).toBe(defaultAmount); 29 | }); 30 | 31 | it('should handle zero and nearly-zero values properly', function() { 32 | // This expression is known to yield 4.440892098500626e-16 instead of 0.0. 33 | expect(currency(1.07 + 1 - 2.07)).toBe('$0.00'); 34 | expect(currency(0.008)).toBe('$0.01'); 35 | expect(currency(0.003)).toBe('$0.00'); 36 | }); 37 | 38 | }); 39 | 40 | describe('advanced functionality', function() { 41 | 42 | it('should handle custom precision', function() { 43 | // https://github.com/angular/angular.js/pull/3642/files#diff-3c33c1e5c05e3b1acc555656428b48d5R98 44 | expect(currency(1234.5678, 'rub', 3)).toEqual('rub1,234.568'); 45 | expect(currency(1234.5678, 'rub', 0)).toEqual('rub1,235'); 46 | }); 47 | 48 | it('should handle custom currency position', function() { 49 | expect(currency(1234.42, '€', 0, true)).toEqual('1,234€'); 50 | expect(currency(1234.42, 'USD$', 2, false)).toEqual('USD$1,234.42'); 51 | expect(currency(1234.42, 'USD$')).toEqual('USD$1,234.42'); 52 | expect(currency(1234.42)).toEqual('$1,234.42'); 53 | }); 54 | 55 | it('should allow suffixSymbol to be a string', function() { 56 | expect(currency(1234.42, '€', 0, 'EUR')).toEqual('€1,234EUR'); 57 | expect(currency(1234.42, '$', 2, 'USD')).toEqual('$1,234.42USD'); 58 | }); 59 | }); 60 | 61 | describe('test API consistency', function() { 62 | var groupSep = {GROUP_SEP: ' '}; 63 | 64 | it('with common use cases', function () { 65 | expect(currency(1234.42, '€', true)).toEqual('1,234.42€'); 66 | expect(currency(1234, '$', 0)).toEqual('$1,234'); 67 | }); 68 | 69 | it('with no amount', function () { 70 | expect(currency()).toEqual('$0.00'); 71 | expect(currency(null, 'USD$')).toEqual('USD$0.00'); 72 | expect(currency(null, 'USD$', 1)).toEqual('USD$0.0'); 73 | expect(currency(null, '€', 1, true)).toEqual('0.0€'); 74 | expect(currency(null, '€', true)).toEqual('0.00€'); 75 | }); 76 | 77 | describe('group separator as', function () { 78 | it('second param', function () { 79 | expect(currency(1000, groupSep)).toEqual('$1 000.00'); 80 | }); 81 | 82 | it('last param', function () { 83 | expect(currency(1000, '€', 1, true, groupSep)).toEqual('1 000.0€'); 84 | }); 85 | }); 86 | }); 87 | 88 | describe('group separator', function() { 89 | 90 | it('should fall back to default angluar locale', function () { 91 | expect(currency(1234.42)).toEqual('$1,234.42'); 92 | }); 93 | 94 | it('should change the group separator to dash', function () { 95 | var groupSep = {GROUP_SEP: ' '}; 96 | 97 | expect(currency(123.42, groupSep)).toEqual('$123.42'); 98 | expect(currency(1234.42, groupSep)).toEqual('$1 234.42'); 99 | expect(currency(123456789.42, groupSep)).toEqual('$123 456 789.42'); 100 | }); 101 | 102 | it('should remove the group separator', function () { 103 | var groupSep = {GROUP_SEP: ''}; 104 | 105 | expect(currency(123.42, groupSep)).toEqual('$123.42'); 106 | expect(currency(1234.42, groupSep)).toEqual('$1234.42'); 107 | expect(currency(123456789.42, groupSep)).toEqual('$123456789.42'); 108 | }); 109 | 110 | }); 111 | 112 | describe('decimal separator', function() { 113 | 114 | it('should fall back to default angluar locale', function () { 115 | expect(currency(1234.42)).toEqual('$1,234.42'); 116 | }); 117 | 118 | it('should change the group separator to dash', function () { 119 | var decimalSep = {DECIMAL_SEP: ','}; 120 | 121 | expect(currency(1.42, decimalSep)).toEqual('$1,42'); 122 | }); 123 | }); 124 | 125 | describe('demo', function() { 126 | 127 | it('should verify demo use cases', function() { 128 | var formats = { 129 | GROUP_SEP: ' ', 130 | DECIMAL_SEP: ',' 131 | }; 132 | 133 | // With all parameters 134 | expect(currency(1234.4239, '€', 1, true, formats)).toEqual('1 234,4€'); 135 | 136 | // With all parameters, using string suffix 137 | expect(currency(1234.4239, '€', 1, 'EUR', formats)).toEqual('€1 234,4EUR'); 138 | 139 | // With missing fraction size 140 | expect(currency(1234.4239, '€', true)).toEqual('1,234.42€'); 141 | 142 | // With missing fraction size, using string suffix 143 | expect(currency(1234.4239, '€', 'EUR')).toEqual('€1,234.42EUR'); 144 | 145 | // With fraction size only 146 | expect(currency(1234.4239, '$', 3)).toEqual('$1,234.424'); 147 | 148 | // Only with symbol 149 | expect(currency(1234.4239, '$')).toEqual('$1,234.42'); 150 | 151 | // Only with custom group and decimal separators 152 | expect(currency(1234.4239, formats)).toEqual('$1 234,42'); 153 | }); 154 | 155 | }); 156 | 157 | }); 158 | --------------------------------------------------------------------------------