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

Commit 3d5af97

Browse files
committed
2.0.0 upgrade
Upgrade to use Numeral.js 2.x. Breaking changes ahead
1 parent 23cef0f commit 3d5af97

File tree

19 files changed

+24145
-13383
lines changed

19 files changed

+24145
-13383
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
# Changelog
22

3+
## 2.0.0
4+
5+
* Upgrade to Numeral.js 2.x, with several breaking changes
6+
* $numeraljsConfigProvider's interface has been modified to better match Numeral.js
7+
* `$numeraljsConfigProvider.setCurrentLanguage(lang)` has been renamed to `locale(locale)`
8+
* `$numeraljsConfigProvider.setDefaultFormat(format)` has been renamed to `defaultFormat(format)`
9+
* `$numeraljsConfigProvider.setFormat(name, formatString)` has been renamed to `namedFormat(name, formatString)`
10+
* `$numeraljsConfigProvider.setLanguage(lang, def)` has been renamed to `register('locale', name, def)`
11+
* Fixed indentation to match EditorConfig settings
12+
* Upgraded Karma & related test dependencies
13+
314
## 1.3.0
415

516
* Remove the separate CommonJS build in favor of a single UMD module.

LICENSE

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
The MIT License
2-
Copyright (c) 2013-2016 Dave Bauman
2+
Copyright (c) 2013-2017 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
@@ -18,4 +18,3 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1818
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1919
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2020
THE SOFTWARE.
21-

README.md

Lines changed: 88 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,19 @@
44

55
This is an Angular.js filter that applies [Numeral.js](http://numeraljs.com/) formatting.
66

7+
The latest version of this library uses Numeral.js 2.x branch. There are several breaking changes in Numeral.js, as well as breaking changes in this library. If you depend on Numeral.js 1.x and cannot upgrade, please use the latest 1.x release (and corresponding `1.x` branch).
8+
9+
## Breaking Changes in 2.x
10+
11+
For details on breaking changes in Numeral.js itself, please see its [changelog](https://github.com/adamwdraper/Numeral-js/tree/master#200).
12+
13+
In addition, angular-numeraljs has the following breaking changes from in 1.3.0 to 2.0.0:
14+
15+
* `$numeraljsConfigProvider.setCurrentLanguage(lang)` has been renamed to `locale(locale)`
16+
* `$numeraljsConfigProvider.setDefaultFormat(format)` has been renamed to `defaultFormat(format)`
17+
* `$numeraljsConfigProvider.setFormat(name, formatString)` has been renamed to `namedFormat(name, formatString)`
18+
* `$numeraljsConfigProvider.setLanguage(lang, def)` has been renamed to `register('locale', name, def)`
19+
720
## How to Use
821

922
1. Include Numeral.js in your project
@@ -43,11 +56,11 @@ Numeral.js must be already loaded in the browser prior to using `$numeraljsConfi
4356

4457
### Named Formats
4558

46-
`$numeraljsConfigProvider.setFormat(name, formatString)` - defines a named format which can be used in place of the format string in the filter.
59+
`$numeraljsConfigProvider.namedFormat(name, formatString)` - defines a named format which can be used in place of the format string in the filter.
4760

4861
```js
4962
app.config(['$numeraljsConfigProvider', function ($numeraljsConfigProvider) {
50-
$numeraljsConfigProvider.setFormat('currency', '$ 0,0.00');
63+
$numeraljsConfigProvider.namedFormat('currency', '$ 0,0.00');
5164
}]);
5265
```
5366

@@ -63,11 +76,11 @@ In markup,
6376

6477
Numeral.js defines the default format as '0,0', so this format is used if none is provided to the filter.
6578

66-
`$numeraljsConfigProvider.setDefaultFormat(format)` - overrides the built-in default format.
79+
`$numeraljsConfigProvider.defaultFormat(format)` - overrides the built-in default format.
6780

6881
```js
6982
app.config(['$numeraljsConfigProvider', function ($numeraljsConfigProvider) {
70-
$numeraljsConfigProvider.setDefaultFormat('0.0 $');
83+
$numeraljsConfigProvider.defaultFormat('0.0 $');
7184
}]);
7285
```
7386

@@ -79,44 +92,84 @@ In markup,
7992
</p>
8093
```
8194

82-
### Custom Languages
95+
### Custom Locales and Formats
8396

84-
`$numeraljsConfigProvider.setLanguage(langId, definition)` - adds new language definitions to Numeral.js. See the available list here: [languages](https://github.com/adamwdraper/Numeral-js/tree/master/languages).
97+
`$numeraljsConfigProvider.register(type, name, definition)` - adds new locale or format definitions to Numeral.js. `type` must be either `'locale'` or `'format'`. For complete details, please refer to the Numeral.js [documentation](http://numeraljs.com/#locales).
8598

8699
```js
87100
app.config(['$numeraljsConfigProvider', function ($numeraljsConfigProvider) {
88-
var language = {
89-
delimiters: {
90-
thousands: ' ',
91-
decimal: ','
92-
},
93-
abbreviations: {
94-
thousand: 'k',
95-
million: 'm',
96-
billion: 'b',
97-
trillion: 't'
98-
},
99-
ordinal: function (number) {
100-
return '.';
101-
},
102-
currency: {
103-
symbol: ''
104-
}
105-
};
106-
107-
$numeraljsConfigProvider.setLanguage('de', language);
101+
// Register a new Locale
102+
$numeraljsConfigProvider.register('locale', 'de', {
103+
delimiters: {
104+
thousands: ' ',
105+
decimal: ','
106+
},
107+
abbreviations: {
108+
thousand: 'k',
109+
million: 'm',
110+
billion: 'b',
111+
trillion: 't'
112+
},
113+
ordinal: function (number) {
114+
return '.';
115+
},
116+
currency: {
117+
symbol: ''
118+
}
119+
});
120+
121+
// Register a new Format
122+
$numeraljsConfigProvider.register('format', 'percentage', {
123+
regexps: {
124+
format: /(%)/,
125+
unformat: /(%)/
126+
},
127+
format: function(value, format, roundingFunction) {
128+
var space = numeral._.includes(format, ' %') ? ' ' : '',
129+
output;
130+
131+
value = value * 100;
132+
133+
// check for space before %
134+
format = format.replace(/\s?\%/, '');
135+
136+
output = numeral._.numberToFormat(value, format, roundingFunction);
137+
138+
if (numeral._.includes(output, ')')) {
139+
output = output.split('');
140+
141+
output.splice(-1, 0, space + '%');
142+
143+
output = output.join('');
144+
} else {
145+
output = output + space + '%';
146+
}
147+
148+
return output;
149+
},
150+
unformat: function(string) {
151+
return numeral._.stringToNumber(string) * 0.01;
152+
}
153+
});
154+
155+
$numeraljsConfigProvider.defaultFormat('0%');
156+
$numeraljsConfigProvider.locale('de');
108157
}]);
109158
```
110159

111-
Languages can be loaded directly into Numeral.js as well, e.g. by loading the [language files](https://github.com/adamwdraper/Numeral-js/tree/master/languages) after Numeral.js is loaded. Angular-numeraljs can use these languages even if they are not set via this provider.
160+
Please note that registering a new format will add new formatting functionality, e.g. adding a new character to the format string. If you want to add a short name for a specific format string, see `namedFormat()` above.
161+
162+
See a list of available locales here: [locale](https://github.com/adamwdraper/Numeral-js/tree/master/locales).
163+
164+
Locales or formats can be loaded directly into Numeral.js as well, e.g. by loading the [locale files](https://github.com/adamwdraper/Numeral-js/tree/master/locales) after Numeral.js is loaded. Angular-numeraljs can use these locales or formats even if they are not set via this provider.
112165

113-
### Select Language
166+
### Select Locale
114167

115-
`$numeraljsConfigProvider.setCurrentLanguage(langId)` - selects the current language. The language must be loaded either by `$numeraljsConfigProvider.setLanguage()` or by loading the Numeral.js language file.
168+
`$numeraljsConfigProvider.locale(locale)` - selects the current locale. The locale must be loaded either by `$numeraljsConfigProvider.register()` or by loading the Numeral.js locale file.
116169

117170
```js
118171
app.config(['$numeraljsConfigProvider', function ($numeraljsConfigProvider) {
119-
$numeraljsConfigProvider.setCurrentLanguage('de');
172+
$numeraljsConfigProvider.locale('de');
120173
}]);
121174
```
122175

@@ -125,7 +178,8 @@ app.config(['$numeraljsConfigProvider', function ($numeraljsConfigProvider) {
125178
It is possible to change all of the configurations at runtime by injecting `$numeraljsConfig`:
126179

127180
app.controller('numeralExample', function ($scope, $numeraljsConfig) {
128-
$numeraljsConfig.setCurrentLanguage($scope.language);
181+
$numeraljsConfig.defaultFormat('0,0.0');
182+
$numeraljsConfig.locale($scope.locale);
129183
});
130184

131185
This may be useful for websites with a language switcher, saved user preferences, etc.
@@ -145,19 +199,19 @@ There are several examples in the `example/` folder which can be used for refere
145199
This filter can be installed via Bower with the following dependency in the `bower.json` file.
146200

147201
"dependencies": {
148-
"angular-numeraljs": "^1.0"
202+
"angular-numeraljs": "^2.0"
149203
}
150204

151205
## Browserify
152206

153207
This project is published in NPM as `angular-numeraljs`.
154208

155209
"dependencies": {
156-
"angular-numeraljs": "^1.0"
210+
"angular-numeraljs": "^2.0"
157211
}
158212

159213
The `example/browserify` folder has a working example with Browserify and Grunt. To build this project, install [Grunt](http://gruntjs.com/) and [Browserify](http://browserify.org/) and run the following:
160-
214+
161215
cd example/browserify
162216
npm install
163217
grunt build
@@ -181,4 +235,3 @@ Then open `example/browserify/dist/index.html` in a browser.
181235
4. Tests are automatically run during the build, but they can be run manually as well
182236

183237
grunt test
184-

dist/angular-numeraljs.js

Lines changed: 31 additions & 35 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.3.2 - 2016-12-27
3+
* @version v2.0.0 - 2017-02-13
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
@@ -23,49 +23,45 @@
2323
}(this, function (numeral) {
2424
return angular.module('ngNumeraljs', [])
2525
.provider('$numeraljsConfig', function () {
26-
var formats = {};
26+
var formats = {};
2727

28-
this.setFormat = function (name, format) {
29-
formats[name] = format;
30-
};
28+
this.defaultFormat = function (format) {
29+
numeral.defaultFormat(format);
30+
};
3131

32-
this.setDefaultFormat = function (format) {
33-
numeral.defaultFormat(format);
34-
};
32+
this.locale = function (locale) {
33+
numeral.locale(locale);
34+
};
3535

36-
this.setLanguage = function (lang, def) {
37-
numeral.language(lang, def);
38-
};
39-
40-
this.setCurrentLanguage = function (lang) {
41-
numeral.language(lang);
42-
};
36+
this.namedFormat = function (name, format) {
37+
formats[name] = format;
38+
};
4339

44-
this.$get = function () {
45-
return {
46-
customFormat: function (name) {
47-
return formats[name] || name;
48-
},
40+
this.register = function (type, name, def) {
41+
numeral.register(type, name, def);
42+
};
4943

50-
setCurrentLanguage: this.setCurrentLanguage,
51-
52-
setDefaultFormat: this.setDefaultFormat,
53-
54-
setFormat: this.setFormat,
55-
56-
setLanguage: this.setLanguage,
57-
};
44+
this.$get = function () {
45+
return {
46+
customFormat: function (name) {
47+
return formats[name] || name;
48+
},
49+
defaultFormat: this.defaultFormat,
50+
locale: this.locale,
51+
register: this.register,
52+
namedFormat: this.namedFormat
5853
};
54+
};
5955
})
6056
.filter('numeraljs', ['$numeraljsConfig', function ($numeraljsConfig) {
61-
return function (input, format) {
62-
if (input == null) {
63-
return input;
64-
}
57+
return function (input, format) {
58+
if (input == null) {
59+
return input;
60+
}
6561

66-
format = $numeraljsConfig.customFormat(format);
62+
format = $numeraljsConfig.customFormat(format);
6763

68-
return numeral(input).format(format);
69-
};
64+
return numeral(input).format(format);
65+
};
7066
}]);
7167
}));

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/changeLanguages/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<meta name="description" content="Changing languages example for the Angular-numeraljs module">
88
<meta name="viewport" content="width=device-width, initial-scale=1">
99

10-
<script src="../../test/lib/numeral.js"></script>
10+
<script src="../../node_modules/numeral/numeral.js"></script>
1111
<script src="../../test/lib/angular/angular.js"></script>
1212
<script src="../../src/angular-numeraljs.js"></script>
1313

example/changeLanguages/js/app.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ var app = angular.module('exampleApp', ['ngNumeraljs']);
22

33
app.config(['$numeraljsConfigProvider', function ($numeraljsConfigProvider) {
44
// Load some additional languages
5-
$numeraljsConfigProvider.setLanguage('fr', {
5+
$numeraljsConfigProvider.register('locale', 'fr', {
66
delimiters: {
77
thousands: ' ',
88
decimal: ','
@@ -21,7 +21,7 @@ app.config(['$numeraljsConfigProvider', function ($numeraljsConfigProvider) {
2121
}
2222
});
2323

24-
$numeraljsConfigProvider.setLanguage('de', {
24+
$numeraljsConfigProvider.register('locale', 'de', {
2525
delimiters: {
2626
thousands: ' ',
2727
decimal: ','
@@ -40,7 +40,7 @@ app.config(['$numeraljsConfigProvider', function ($numeraljsConfigProvider) {
4040
}
4141
});
4242

43-
$numeraljsConfigProvider.setCurrentLanguage('en');
43+
$numeraljsConfigProvider.locale('en');
4444
}]);
4545

4646
app.controller('numeralExample', function ($scope, $numeraljsConfig) {
@@ -66,14 +66,14 @@ app.controller('numeralExample', function ($scope, $numeraljsConfig) {
6666
$scope.currentLanguage = 'en';
6767

6868
$scope.$watch('currentLanguage', function (language) {
69-
$numeraljsConfig.setCurrentLanguage(language);
69+
$numeraljsConfig.locale(language);
7070
});
7171

7272

7373
$scope.defaultFormat = null;
7474

7575
$scope.$watch('defaultFormat', function (format) {
7676
if (format == null) return;
77-
$numeraljsConfig.setDefaultFormat(format);
77+
$numeraljsConfig.defaultFormat(format);
7878
});
79-
});
79+
});

0 commit comments

Comments
 (0)