@sb1/ffe-formatters 5.0.1 → 5.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/es/formatCurrency.js +1 -1
- package/es/formatDistance.js +3 -2
- package/es/formatNumber.js +3 -2
- package/es/formatPercentage.js +1 -1
- package/es/internal/parseNumber.js +4 -1
- package/lib/formatCurrency.js +1 -1
- package/lib/formatDistance.js +3 -2
- package/lib/formatNumber.js +3 -2
- package/lib/formatPercentage.js +1 -1
- package/lib/internal/parseNumber.js +4 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -12,7 +12,7 @@ npm install --save @sb1/ffe-formatters
|
|
|
12
12
|
|
|
13
13
|
## Usage
|
|
14
14
|
|
|
15
|
-
Full documentation on formatter usage is available at https://design.sparebank1.no/
|
|
15
|
+
Full documentation on formatter usage is available at https://design.sparebank1.no/Komponenter/Formatters.
|
|
16
16
|
|
|
17
17
|
Import the formatter(s) you need the following way:
|
|
18
18
|
|
package/es/formatCurrency.js
CHANGED
|
@@ -14,7 +14,7 @@ import { parseNumber } from './internal/parseNumber';
|
|
|
14
14
|
import { NON_BREAKING_SPACE } from './internal/unicode';
|
|
15
15
|
export var formatCurrency = function (amount, opts) {
|
|
16
16
|
var locale = opts.locale;
|
|
17
|
-
var _a = __assign({ prefix
|
|
17
|
+
var _a = __assign({}, opts), _b = _a.prefix, prefix = _b === void 0 ? "kr".concat(NON_BREAKING_SPACE) : _b, _c = _a.postfix, postfix = _c === void 0 ? ',–' : _c;
|
|
18
18
|
var parsed = parseNumber(amount, locale);
|
|
19
19
|
// A ',-' postfix is not allowed for amounts with øre, see:
|
|
20
20
|
// http://www.sprakradet.no/svardatabase/sporsmal-og-svar/kronebelop-rekkjefolgje-komma-og-strek/
|
package/es/formatDistance.js
CHANGED
|
@@ -2,9 +2,10 @@ import { formatNumber } from './formatNumber';
|
|
|
2
2
|
import { parseNumber } from './internal/parseNumber';
|
|
3
3
|
import { NON_BREAKING_SPACE } from './internal/unicode';
|
|
4
4
|
export var formatDistance = function (distance, opts) {
|
|
5
|
-
if (opts === void 0) { opts = {
|
|
5
|
+
if (opts === void 0) { opts = {}; }
|
|
6
|
+
var _a = opts.unit, unit = _a === void 0 ? 'km' : _a;
|
|
6
7
|
var toFormat = parseNumber(distance, 'nb');
|
|
7
8
|
return toFormat === null
|
|
8
9
|
? ''
|
|
9
|
-
: "".concat(formatNumber(toFormat, { locale: 'nb' })).concat(NON_BREAKING_SPACE).concat(
|
|
10
|
+
: "".concat(formatNumber(toFormat, { locale: 'nb' })).concat(NON_BREAKING_SPACE).concat(unit);
|
|
10
11
|
};
|
package/es/formatNumber.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { parseNumber } from './internal/parseNumber';
|
|
2
2
|
export var formatNumber = function (number, opts) {
|
|
3
3
|
var _a = opts.decimals, decimals = _a === void 0 ? 0 : _a, locale = opts.locale;
|
|
4
|
-
var
|
|
4
|
+
var supportedLocale = locale === 'en' ? 'en' : 'nb';
|
|
5
|
+
var toFormat = parseNumber(number, supportedLocale);
|
|
5
6
|
if (typeof toFormat !== 'number') {
|
|
6
7
|
return number;
|
|
7
8
|
}
|
|
8
|
-
return new Intl.NumberFormat(
|
|
9
|
+
return new Intl.NumberFormat(supportedLocale, {
|
|
9
10
|
maximumFractionDigits: decimals,
|
|
10
11
|
minimumFractionDigits: decimals,
|
|
11
12
|
}).format(toFormat);
|
package/es/formatPercentage.js
CHANGED
|
@@ -5,7 +5,7 @@ export var formatPercentage = function (amount, opts) {
|
|
|
5
5
|
maxDecimals: 2,
|
|
6
6
|
}; }
|
|
7
7
|
if (typeof amount !== 'number') {
|
|
8
|
-
return '';
|
|
8
|
+
return "".concat(amount.replace('.', ',')).concat(NON_BREAKING_SPACE, "%");
|
|
9
9
|
}
|
|
10
10
|
var percentage = amount.toLocaleString('nb-NO', {
|
|
11
11
|
minimumFractionDigits: opts.minDecimals,
|
|
@@ -23,7 +23,10 @@ var NumberParser = /** @class */ (function () {
|
|
|
23
23
|
};
|
|
24
24
|
return NumberParser;
|
|
25
25
|
}());
|
|
26
|
+
var nbParser = new NumberParser('nb');
|
|
27
|
+
var enParser = new NumberParser('en');
|
|
26
28
|
export var parseNumber = function (number, locale) {
|
|
27
|
-
var
|
|
29
|
+
var parser = locale === 'en' ? enParser : nbParser;
|
|
30
|
+
var parsed = parser.parse("".concat(number).replace(/\s/g, ''));
|
|
28
31
|
return Number.isNaN(parsed) ? null : parsed;
|
|
29
32
|
};
|
package/lib/formatCurrency.js
CHANGED
|
@@ -17,7 +17,7 @@ var parseNumber_1 = require("./internal/parseNumber");
|
|
|
17
17
|
var unicode_1 = require("./internal/unicode");
|
|
18
18
|
var formatCurrency = function (amount, opts) {
|
|
19
19
|
var locale = opts.locale;
|
|
20
|
-
var _a = __assign({ prefix
|
|
20
|
+
var _a = __assign({}, opts), _b = _a.prefix, prefix = _b === void 0 ? "kr".concat(unicode_1.NON_BREAKING_SPACE) : _b, _c = _a.postfix, postfix = _c === void 0 ? ',–' : _c;
|
|
21
21
|
var parsed = (0, parseNumber_1.parseNumber)(amount, locale);
|
|
22
22
|
// A ',-' postfix is not allowed for amounts with øre, see:
|
|
23
23
|
// http://www.sprakradet.no/svardatabase/sporsmal-og-svar/kronebelop-rekkjefolgje-komma-og-strek/
|
package/lib/formatDistance.js
CHANGED
|
@@ -5,10 +5,11 @@ var formatNumber_1 = require("./formatNumber");
|
|
|
5
5
|
var parseNumber_1 = require("./internal/parseNumber");
|
|
6
6
|
var unicode_1 = require("./internal/unicode");
|
|
7
7
|
var formatDistance = function (distance, opts) {
|
|
8
|
-
if (opts === void 0) { opts = {
|
|
8
|
+
if (opts === void 0) { opts = {}; }
|
|
9
|
+
var _a = opts.unit, unit = _a === void 0 ? 'km' : _a;
|
|
9
10
|
var toFormat = (0, parseNumber_1.parseNumber)(distance, 'nb');
|
|
10
11
|
return toFormat === null
|
|
11
12
|
? ''
|
|
12
|
-
: "".concat((0, formatNumber_1.formatNumber)(toFormat, { locale: 'nb' })).concat(unicode_1.NON_BREAKING_SPACE).concat(
|
|
13
|
+
: "".concat((0, formatNumber_1.formatNumber)(toFormat, { locale: 'nb' })).concat(unicode_1.NON_BREAKING_SPACE).concat(unit);
|
|
13
14
|
};
|
|
14
15
|
exports.formatDistance = formatDistance;
|
package/lib/formatNumber.js
CHANGED
|
@@ -4,11 +4,12 @@ exports.formatNumber = void 0;
|
|
|
4
4
|
var parseNumber_1 = require("./internal/parseNumber");
|
|
5
5
|
var formatNumber = function (number, opts) {
|
|
6
6
|
var _a = opts.decimals, decimals = _a === void 0 ? 0 : _a, locale = opts.locale;
|
|
7
|
-
var
|
|
7
|
+
var supportedLocale = locale === 'en' ? 'en' : 'nb';
|
|
8
|
+
var toFormat = (0, parseNumber_1.parseNumber)(number, supportedLocale);
|
|
8
9
|
if (typeof toFormat !== 'number') {
|
|
9
10
|
return number;
|
|
10
11
|
}
|
|
11
|
-
return new Intl.NumberFormat(
|
|
12
|
+
return new Intl.NumberFormat(supportedLocale, {
|
|
12
13
|
maximumFractionDigits: decimals,
|
|
13
14
|
minimumFractionDigits: decimals,
|
|
14
15
|
}).format(toFormat);
|
package/lib/formatPercentage.js
CHANGED
|
@@ -8,7 +8,7 @@ var formatPercentage = function (amount, opts) {
|
|
|
8
8
|
maxDecimals: 2,
|
|
9
9
|
}; }
|
|
10
10
|
if (typeof amount !== 'number') {
|
|
11
|
-
return '';
|
|
11
|
+
return "".concat(amount.replace('.', ',')).concat(unicode_1.NON_BREAKING_SPACE, "%");
|
|
12
12
|
}
|
|
13
13
|
var percentage = amount.toLocaleString('nb-NO', {
|
|
14
14
|
minimumFractionDigits: opts.minDecimals,
|
|
@@ -26,8 +26,11 @@ var NumberParser = /** @class */ (function () {
|
|
|
26
26
|
};
|
|
27
27
|
return NumberParser;
|
|
28
28
|
}());
|
|
29
|
+
var nbParser = new NumberParser('nb');
|
|
30
|
+
var enParser = new NumberParser('en');
|
|
29
31
|
var parseNumber = function (number, locale) {
|
|
30
|
-
var
|
|
32
|
+
var parser = locale === 'en' ? enParser : nbParser;
|
|
33
|
+
var parsed = parser.parse("".concat(number).replace(/\s/g, ''));
|
|
31
34
|
return Number.isNaN(parsed) ? null : parsed;
|
|
32
35
|
};
|
|
33
36
|
exports.parseNumber = parseNumber;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sb1/ffe-formatters",
|
|
3
|
-
"version": "5.0.
|
|
3
|
+
"version": "5.0.3",
|
|
4
4
|
"description": "JavaScript general use string formatters",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ffe"
|
|
@@ -34,5 +34,5 @@
|
|
|
34
34
|
"publishConfig": {
|
|
35
35
|
"access": "public"
|
|
36
36
|
},
|
|
37
|
-
"gitHead": "
|
|
37
|
+
"gitHead": "58de7e398d74d58063d329c8bb05535e6d0ba0af"
|
|
38
38
|
}
|