You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Copy file name to clipboardExpand all lines: README.md
+88-35Lines changed: 88 additions & 35 deletions
Original file line number
Diff line number
Diff line change
@@ -4,6 +4,19 @@
4
4
5
5
This is an Angular.js filter that applies [Numeral.js](http://numeraljs.com/) formatting.
6
6
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
+
7
20
## How to Use
8
21
9
22
1. Include Numeral.js in your project
@@ -43,11 +56,11 @@ Numeral.js must be already loaded in the browser prior to using `$numeraljsConfi
43
56
44
57
### Named Formats
45
58
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.
47
60
48
61
```js
49
62
app.config(['$numeraljsConfigProvider', function ($numeraljsConfigProvider) {
`$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).
85
98
86
99
```js
87
100
app.config(['$numeraljsConfigProvider', function ($numeraljsConfigProvider) {
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
+
returnnumeral._.stringToNumber(string) *0.01;
152
+
}
153
+
});
154
+
155
+
$numeraljsConfigProvider.defaultFormat('0%');
156
+
$numeraljsConfigProvider.locale('de');
108
157
}]);
109
158
```
110
159
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.
112
165
113
-
### Select Language
166
+
### Select Locale
114
167
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.
116
169
117
170
```js
118
171
app.config(['$numeraljsConfigProvider', function ($numeraljsConfigProvider) {
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
145
199
This filter can be installed via Bower with the following dependency in the `bower.json` file.
146
200
147
201
"dependencies": {
148
-
"angular-numeraljs": "^1.0"
202
+
"angular-numeraljs": "^2.0"
149
203
}
150
204
151
205
## Browserify
152
206
153
207
This project is published in NPM as `angular-numeraljs`.
154
208
155
209
"dependencies": {
156
-
"angular-numeraljs": "^1.0"
210
+
"angular-numeraljs": "^2.0"
157
211
}
158
212
159
213
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
+
161
215
cd example/browserify
162
216
npm install
163
217
grunt build
@@ -181,4 +235,3 @@ Then open `example/browserify/dist/index.html` in a browser.
181
235
4. Tests are automatically run during the build, but they can be run manually as well
0 commit comments