Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit 949d2dc

Browse files
committed
Release 1.2.0: Support for reconfiguring at runtime via $numeraljsConfig
Fixes #15
1 parent 37cf1b1 commit 949d2dc

File tree

14 files changed

+223
-26
lines changed

14 files changed

+223
-26
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
The MIT License
2-
Copyright (c) 2013 Dave Bauman
2+
Copyright (c) 2013-2015 Dave Bauman
33

44
Permission is hereby granted, free of charge, to any person obtaining a copy
55
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,25 @@ app.config(['$numeraljsConfigProvider', function ($numeraljsConfigProvider) {
118118
}]);
119119
```
120120

121+
### Runtime Configuration
122+
123+
It is possible to change all of the configurations at runtime by injecting `$numeraljsConfig`:
124+
125+
app.controller('numeralExample', function ($scope, $numeraljsConfig) {
126+
$numeraljsConfig.setCurrentLanguage($scope.language);
127+
});
128+
129+
This may be useful for websites with a language switcher, saved user preferences, etc.
130+
121131
## Examples
122132

123-
Check out [example/simple](example/js/app.js) and [example/config](config/js/app.js) for reference.
133+
There are several examples in the `example/` folder which can be used for reference:
134+
135+
* _Simple_: using this library in the most basic way possible
136+
* _Config_: using $numeraljsConfigProvider to configure this library
137+
* _ChangingLanguages_: changing languages (or other properties) at runtime (vs initialization)
138+
* _Bower_: adding a dependency through Bower
139+
* _Browserify_: adding a dependency through Browserify
124140

125141
## Bower
126142

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "angular-numeraljs",
33
"author": "Dave Bauman",
4-
"version": "1.1.6",
4+
"version": "1.2.0",
55
"license": "MIT",
66
"main": ["./dist/angular-numeraljs.js"],
77
"repository": {

commonjs/angular-numeraljs.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
window.require.define({"angular-numeraljs": function(exports, require, module) {
22
/**
33
* AngularJS filter for Numeral.js: number formatting as a filter
4-
* @version v1.1.6 - 2014-10-29
4+
* @version v1.2.0 - 2015-11-17
55
* @link https://github.com/baumandm/angular-numeraljs
66
* @author Dave Bauman <baumandm@gmail.com>
77
* @license MIT License, http://www.opensource.org/licenses/MIT
@@ -33,7 +33,15 @@ angular.module('ngNumeraljs', [])
3333
return {
3434
customFormat: function (name) {
3535
return formats[name] || name;
36-
}
36+
},
37+
38+
setCurrentLanguage: this.setCurrentLanguage,
39+
40+
setDefaultFormat: this.setDefaultFormat,
41+
42+
setFormat: this.setFormat,
43+
44+
setLanguage: this.setLanguage,
3745
};
3846
};
3947
})

commonjs/angular-numeraljs.min.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
window.require.define({"angular-numeraljs.min": function(exports, require, module) {
22
/**
33
* AngularJS filter for Numeral.js: number formatting as a filter
4-
* @version v1.1.6 - 2014-10-29
4+
* @version v1.2.0 - 2015-11-17
55
* @link https://github.com/baumandm/angular-numeraljs
66
* @author Dave Bauman <baumandm@gmail.com>
77
* @license MIT License, http://www.opensource.org/licenses/MIT
88
*/
9-
"use strict";angular.module("ngNumeraljs",[]).provider("$numeraljsConfig",function(){var a={};this.setFormat=function(b,c){a[b]=c},this.setDefaultFormat=function(a){numeral.defaultFormat(a)},this.setLanguage=function(a,b){numeral.language(a,b)},this.setCurrentLanguage=function(a){numeral.language(a)},this.$get=function(){return{customFormat:function(b){return a[b]||b}}}}).filter("numeraljs",["$numeraljsConfig",function(a){return function(b,c){return null==b?b:(c=a.customFormat(c),numeral(b).format(c))}}]);}});
9+
"use strict";angular.module("ngNumeraljs",[]).provider("$numeraljsConfig",function(){var a={};this.setFormat=function(b,c){a[b]=c},this.setDefaultFormat=function(a){numeral.defaultFormat(a)},this.setLanguage=function(a,b){numeral.language(a,b)},this.setCurrentLanguage=function(a){numeral.language(a)},this.$get=function(){return{customFormat:function(b){return a[b]||b},setCurrentLanguage:this.setCurrentLanguage,setDefaultFormat:this.setDefaultFormat,setFormat:this.setFormat,setLanguage:this.setLanguage}}}).filter("numeraljs",["$numeraljsConfig",function(a){return function(b,c){return null==b?b:(c=a.customFormat(c),numeral(b).format(c))}}]);}});
1010

dist/angular-numeraljs.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* AngularJS filter for Numeral.js: number formatting as a filter
3-
* @version v1.1.6 - 2014-10-29
3+
* @version v1.2.0 - 2015-11-17
44
* @link https://github.com/baumandm/angular-numeraljs
55
* @author Dave Bauman <baumandm@gmail.com>
66
* @license MIT License, http://www.opensource.org/licenses/MIT
@@ -32,7 +32,15 @@ angular.module('ngNumeraljs', [])
3232
return {
3333
customFormat: function (name) {
3434
return formats[name] || name;
35-
}
35+
},
36+
37+
setCurrentLanguage: this.setCurrentLanguage,
38+
39+
setDefaultFormat: this.setDefaultFormat,
40+
41+
setFormat: this.setFormat,
42+
43+
setLanguage: this.setLanguage,
3644
};
3745
};
3846
})

dist/angular-numeraljs.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/bower/bower.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"name": "angular-numeraljs-bower-example",
33
"author": "Dave Bauman",
4-
"version": "1.1.6",
4+
"version": "1.2.0",
55
"license": "MIT",
6-
"main": ["./dist/angular-numeraljs.js"],
6+
"private": true,
77
"repository": {
88
"type": "git",
99
"url": "git@github.com:baumandm/angular-numeraljs.git"

example/browserify/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "angular-numeraljs-browserify-sample",
33
"description": "Browserify sample for Angular-numeraljs",
4-
"version": "1.1.4",
4+
"version": "1.2.0",
55
"homepage": "https://github.com/baumandm/angular-numeraljs",
66
"author": "Dave Bauman <baumandm@gmail.com>",
77
"repository": {

example/changeLanguages/index.html

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<!DOCTYPE html>
2+
<html ng-app="exampleApp">
3+
<head>
4+
<meta charset="utf-8">
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6+
<title>Angular-numeraljs Example - Changing Languages</title>
7+
<meta name="description" content="Changing languages example for the Angular-numeraljs module">
8+
<meta name="viewport" content="width=device-width, initial-scale=1">
9+
10+
<script src="../../test/lib/numeral.js"></script>
11+
<script src="../../test/lib/angular/angular.js"></script>
12+
<script src="../../src/angular-numeraljs.js"></script>
13+
14+
<script src="js/app.js"></script>
15+
</head>
16+
<body ng-controller="numeralExample">
17+
<style>
18+
table {
19+
width: 100%;
20+
margin-top: 2rem;
21+
}
22+
23+
th {
24+
text-align: left;
25+
border-bottom: 1px solid #888;
26+
}
27+
28+
input {
29+
margin-left: 1rem;
30+
}
31+
</style>
32+
<h1>Angular-numeraljs Example: Changing Languages</h1>
33+
<div>
34+
<label>Test Number:
35+
<input type="text" ng-model="number" placeholder="Enter test number here" />
36+
</label>
37+
<label>Current Language:
38+
<select ng-model="currentLanguage">
39+
<option value="de">de</option>
40+
<option value="en">en</option>
41+
<option value="fr">fr</option>
42+
</select>
43+
</label>
44+
<label>Default Format:
45+
<input type="text" ng-model="defaultFormat" placeholder="Enter default format" />
46+
</div>
47+
48+
<table>
49+
<tr>
50+
<th>Name</th>
51+
<th>Format</th>
52+
<th>Value</th>
53+
</tr>
54+
<tr ng-repeat='format in formats'>
55+
<td>{{ format.name }}</td>
56+
<td>{{ format.format }}</td>
57+
<td>{{ number | numeraljs:format.format }}</td>
58+
</tr>
59+
</table>
60+
</body>
61+
</html>

example/changeLanguages/js/app.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
var app = angular.module('exampleApp', ['ngNumeraljs']);
2+
3+
app.config(['$numeraljsConfigProvider', function ($numeraljsConfigProvider) {
4+
// Load some additional languages
5+
$numeraljsConfigProvider.setLanguage('fr', {
6+
delimiters: {
7+
thousands: ' ',
8+
decimal: ','
9+
},
10+
abbreviations: {
11+
thousand: 'k',
12+
million: 'm',
13+
billion: 'b',
14+
trillion: 't'
15+
},
16+
ordinal : function (number) {
17+
return number === 1 ? 'er' : 'ème';
18+
},
19+
currency: {
20+
symbol: '€'
21+
}
22+
});
23+
24+
$numeraljsConfigProvider.setLanguage('de', {
25+
delimiters: {
26+
thousands: ' ',
27+
decimal: ','
28+
},
29+
abbreviations: {
30+
thousand: 'k',
31+
million: 'm',
32+
billion: 'b',
33+
trillion: 't'
34+
},
35+
ordinal: function (number) {
36+
return '.';
37+
},
38+
currency: {
39+
symbol: '€'
40+
}
41+
});
42+
43+
$numeraljsConfigProvider.setCurrentLanguage('en');
44+
}]);
45+
46+
app.controller('numeralExample', function ($scope, $numeraljsConfig) {
47+
$scope.formats = [{
48+
name: 'Default Format',
49+
}, {
50+
name: 'Number',
51+
format: '0,0'
52+
}, {
53+
name: 'Currency',
54+
format: '$0,0.00'
55+
},{
56+
name: 'Bytes',
57+
format: '0b'
58+
}, {
59+
name: 'Percentages',
60+
format: '0.0%'
61+
}, {
62+
name: 'Time',
63+
format: '00:00:00'
64+
}];
65+
66+
$scope.currentLanguage = 'en';
67+
68+
$scope.$watch('currentLanguage', function (language) {
69+
$numeraljsConfig.setCurrentLanguage(language);
70+
});
71+
72+
73+
$scope.defaultFormat = null;
74+
75+
$scope.$watch('defaultFormat', function (format) {
76+
if (format == null) return;
77+
$numeraljsConfig.setDefaultFormat(format);
78+
});
79+
});

package.json

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
11
{
22
"name": "angular-numeraljs",
33
"description": "AngularJS filter for Numeral.js: number formatting as a filter",
4-
"version": "1.1.6",
4+
"version": "1.2.0",
55
"homepage": "https://github.com/baumandm/angular-numeraljs",
66
"author": "Dave Bauman <baumandm@gmail.com>",
77
"repository": {
88
"type": "git",
99
"url": "git@github.com:baumandm/angular-numeraljs.git"
1010
},
11-
"licenses": [
12-
{
13-
"type": "MIT",
14-
"url": "http://mit-license.org"
15-
}
16-
],
11+
"bugs": {
12+
"url": "https://github.com/baumandm/angular-numeraljs/issues"
13+
},
14+
"license": "MIT",
1715
"dependencies": {
1816
"numeral": "~1.5"
1917
},
2018
"devDependencies": {
21-
"karma": "~0.12",
19+
"karma": "~0.13",
2220
"grunt": ">= 0.4.0",
23-
"grunt-karma": "~0.10",
21+
"grunt-karma": "~0.12",
2422
"grunt-contrib-concat": "*",
2523
"grunt-contrib-jshint": "*",
2624
"grunt-contrib-uglify": "*",
27-
"grunt-commonjs": "*",
25+
"grunt-commonjs": "0.2.0-rc7",
2826
"grunt-ng-annotate": "*",
27+
"jasmine-core": "~2.3.4",
2928
"karma-jasmine": "*",
30-
"karma-phantomjs-launcher": "~0.1"
29+
"karma-phantomjs-launcher": "~0.2",
30+
"phantomjs": "~1.9"
3131
},
3232
"scripts": {
3333
"test": "grunt karma:unit"

src/angular-numeraljs.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,15 @@ angular.module('ngNumeraljs', [])
2525
return {
2626
customFormat: function (name) {
2727
return formats[name] || name;
28-
}
28+
},
29+
30+
setCurrentLanguage: this.setCurrentLanguage,
31+
32+
setDefaultFormat: this.setDefaultFormat,
33+
34+
setFormat: this.setFormat,
35+
36+
setLanguage: this.setLanguage,
2937
};
3038
};
3139
})

test/unit/filtersSpec.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,4 +150,21 @@ describe('numeraljs filter', function () {
150150
});
151151
});
152152
});
153+
154+
describe('with runtime configuration', function () {
155+
var $config;
156+
157+
describe('when setting default format', function () {
158+
beforeEach(inject(function ($filter, $numeraljsConfig) {
159+
numeraljsFilter = $filter('numeraljs');
160+
$config = $numeraljsConfig;
161+
}));
162+
163+
it('should override the default format', function () {
164+
$config.setDefaultFormat('0.0 $');
165+
$config.setCurrentLanguage('en');
166+
expect(numeraljsFilter('1024.344')).toEqual('1024.3 $');
167+
});
168+
});
169+
});
153170
});

0 commit comments

Comments
 (0)