@sb1/ffe-formatters 4.2.8 → 5.0.0
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/es/formatCurrency.js +4 -2
- package/es/formatDate.js +0 -3
- package/es/formatDistance.js +4 -5
- package/es/formatNumber.js +6 -17
- package/es/internal/parseNumber.js +27 -8
- package/es/tsconfig.esm.tsbuildinfo +1 -0
- package/es/types.js +1 -0
- package/lib/formatCurrency.js +4 -2
- package/lib/formatDate.js +0 -3
- package/lib/formatDistance.js +4 -5
- package/lib/formatNumber.js +6 -17
- package/lib/internal/parseNumber.js +27 -8
- package/lib/tsconfig.cjs.tsbuildinfo +1 -0
- package/lib/types.js +2 -0
- package/package.json +2 -6
- package/types/formatCurrency.d.ts +3 -1
- package/types/formatDate.d.ts +1 -1
- package/types/formatNumber.d.ts +5 -1
- package/types/internal/parseNumber.d.ts +2 -1
- package/types/tsconfig.types.tsbuildinfo +1 -0
- package/types/types.d.ts +1 -0
package/es/formatCurrency.js
CHANGED
|
@@ -13,15 +13,17 @@ import { formatNumber } from './formatNumber';
|
|
|
13
13
|
import { parseNumber } from './internal/parseNumber';
|
|
14
14
|
import { NON_BREAKING_SPACE } from './internal/unicode';
|
|
15
15
|
export var formatCurrency = function (amount, opts) {
|
|
16
|
+
var locale = opts.locale;
|
|
16
17
|
var _a = __assign({ prefix: "kr".concat(NON_BREAKING_SPACE), postfix: ',–' }, opts), prefix = _a.prefix, postfix = _a.postfix;
|
|
17
|
-
var parsed = parseNumber(amount);
|
|
18
|
+
var parsed = parseNumber(amount, locale);
|
|
18
19
|
// A ',-' postfix is not allowed for amounts with øre, see:
|
|
19
20
|
// http://www.sprakradet.no/svardatabase/sporsmal-og-svar/kronebelop-rekkjefolgje-komma-og-strek/
|
|
20
21
|
if (typeof parsed === 'number' && parsed % 1 !== 0) {
|
|
21
22
|
var decimalPostfix = ![',–', ',-'].includes(postfix) ? postfix : '';
|
|
22
23
|
return "".concat(prefix).concat(formatNumber(amount, {
|
|
23
24
|
decimals: 2,
|
|
25
|
+
locale: locale,
|
|
24
26
|
})).concat(decimalPostfix);
|
|
25
27
|
}
|
|
26
|
-
return "".concat(prefix).concat(formatNumber(amount)).concat(postfix);
|
|
28
|
+
return "".concat(prefix).concat(formatNumber(amount, { locale: locale })).concat(postfix);
|
|
27
29
|
};
|
package/es/formatDate.js
CHANGED
|
@@ -1,8 +1,5 @@
|
|
|
1
1
|
var ensureTwoDigits = function (d) { return "00".concat(d).slice(-2); };
|
|
2
2
|
export var formatDate = function (timestamp) {
|
|
3
|
-
if (!(Number.isInteger(timestamp) || timestamp instanceof Date)) {
|
|
4
|
-
return null;
|
|
5
|
-
}
|
|
6
3
|
var date = timestamp instanceof Date ? timestamp : new Date(timestamp);
|
|
7
4
|
var dd = ensureTwoDigits(date.getDate());
|
|
8
5
|
var mm = ensureTwoDigits(date.getMonth() + 1);
|
package/es/formatDistance.js
CHANGED
|
@@ -3,9 +3,8 @@ import { parseNumber } from './internal/parseNumber';
|
|
|
3
3
|
import { NON_BREAKING_SPACE } from './internal/unicode';
|
|
4
4
|
export var formatDistance = function (distance, opts) {
|
|
5
5
|
if (opts === void 0) { opts = { unit: 'km' }; }
|
|
6
|
-
var toFormat = parseNumber(distance);
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
return "".concat(formatNumber(toFormat)).concat(NON_BREAKING_SPACE).concat(opts.unit);
|
|
6
|
+
var toFormat = parseNumber(distance, 'nb');
|
|
7
|
+
return toFormat === null
|
|
8
|
+
? ''
|
|
9
|
+
: "".concat(formatNumber(toFormat, { locale: 'nb' })).concat(NON_BREAKING_SPACE).concat(opts.unit);
|
|
11
10
|
};
|
package/es/formatNumber.js
CHANGED
|
@@ -1,23 +1,12 @@
|
|
|
1
|
-
var __assign = (this && this.__assign) || function () {
|
|
2
|
-
__assign = Object.assign || function(t) {
|
|
3
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
4
|
-
s = arguments[i];
|
|
5
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
6
|
-
t[p] = s[p];
|
|
7
|
-
}
|
|
8
|
-
return t;
|
|
9
|
-
};
|
|
10
|
-
return __assign.apply(this, arguments);
|
|
11
|
-
};
|
|
12
|
-
import { numberFormat } from 'underscore.string';
|
|
13
|
-
import { NON_BREAKING_SPACE } from './internal/unicode';
|
|
14
1
|
import { parseNumber } from './internal/parseNumber';
|
|
15
2
|
export var formatNumber = function (number, opts) {
|
|
16
|
-
|
|
17
|
-
var
|
|
18
|
-
var toFormat = parseNumber(number);
|
|
3
|
+
var _a = opts.decimals, decimals = _a === void 0 ? 0 : _a, locale = opts.locale;
|
|
4
|
+
var toFormat = parseNumber(number, locale);
|
|
19
5
|
if (typeof toFormat !== 'number') {
|
|
20
6
|
return number;
|
|
21
7
|
}
|
|
22
|
-
return
|
|
8
|
+
return new Intl.NumberFormat(locale === 'en' ? 'en' : 'nb', {
|
|
9
|
+
maximumFractionDigits: decimals,
|
|
10
|
+
minimumFractionDigits: decimals,
|
|
11
|
+
}).format(toFormat);
|
|
23
12
|
};
|
|
@@ -1,10 +1,29 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
// https://observablehq.com/@mbostock/localized-number-parsing
|
|
2
|
+
var NumberParser = /** @class */ (function () {
|
|
3
|
+
function NumberParser(locale) {
|
|
4
|
+
var _a, _b;
|
|
5
|
+
var parts = new Intl.NumberFormat(locale).formatToParts(12345.6);
|
|
6
|
+
var numerals = new Intl.NumberFormat(locale, { useGrouping: false })
|
|
7
|
+
.format(9876543210)
|
|
8
|
+
.split('')
|
|
9
|
+
.reverse();
|
|
10
|
+
var index = new Map(numerals.map(function (d, i) { return [d, i]; }));
|
|
11
|
+
this._group = new RegExp("[".concat((_a = parts.find(function (d) { return d.type === 'group'; })) === null || _a === void 0 ? void 0 : _a.value, "]"), 'g');
|
|
12
|
+
this._decimal = new RegExp("[".concat((_b = parts.find(function (d) { return d.type === 'decimal'; })) === null || _b === void 0 ? void 0 : _b.value, "]"));
|
|
13
|
+
this._numeral = new RegExp("[".concat(numerals.join(''), "]"), 'g');
|
|
14
|
+
this._index = function (d) { return "".concat(index.get(d)); };
|
|
4
15
|
}
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
16
|
+
NumberParser.prototype.parse = function (input) {
|
|
17
|
+
var string = input
|
|
18
|
+
.trim()
|
|
19
|
+
.replace(this._group, '')
|
|
20
|
+
.replace(this._decimal, '.')
|
|
21
|
+
.replace(this._numeral, this._index);
|
|
22
|
+
return string ? +string : Number.NaN;
|
|
23
|
+
};
|
|
24
|
+
return NumberParser;
|
|
25
|
+
}());
|
|
26
|
+
export var parseNumber = function (number, locale) {
|
|
27
|
+
var parsed = new NumberParser(locale).parse("".concat(number).replace(/\s/g, ''));
|
|
28
|
+
return Number.isNaN(parsed) ? null : parsed;
|
|
10
29
|
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"root":["../src/formatAccountNumber.ts","../src/formatCurrency.ts","../src/formatDate.ts","../src/formatDistance.ts","../src/formatFodselsnummer.ts","../src/formatNumber.ts","../src/formatPercentage.ts","../src/index.ts","../src/types.ts","../src/internal/parseNumber.ts","../src/internal/unicode.ts"],"version":"5.6.3"}
|
package/es/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/lib/formatCurrency.js
CHANGED
|
@@ -16,16 +16,18 @@ var formatNumber_1 = require("./formatNumber");
|
|
|
16
16
|
var parseNumber_1 = require("./internal/parseNumber");
|
|
17
17
|
var unicode_1 = require("./internal/unicode");
|
|
18
18
|
var formatCurrency = function (amount, opts) {
|
|
19
|
+
var locale = opts.locale;
|
|
19
20
|
var _a = __assign({ prefix: "kr".concat(unicode_1.NON_BREAKING_SPACE), postfix: ',–' }, opts), prefix = _a.prefix, postfix = _a.postfix;
|
|
20
|
-
var parsed = (0, parseNumber_1.parseNumber)(amount);
|
|
21
|
+
var parsed = (0, parseNumber_1.parseNumber)(amount, locale);
|
|
21
22
|
// A ',-' postfix is not allowed for amounts with øre, see:
|
|
22
23
|
// http://www.sprakradet.no/svardatabase/sporsmal-og-svar/kronebelop-rekkjefolgje-komma-og-strek/
|
|
23
24
|
if (typeof parsed === 'number' && parsed % 1 !== 0) {
|
|
24
25
|
var decimalPostfix = ![',–', ',-'].includes(postfix) ? postfix : '';
|
|
25
26
|
return "".concat(prefix).concat((0, formatNumber_1.formatNumber)(amount, {
|
|
26
27
|
decimals: 2,
|
|
28
|
+
locale: locale,
|
|
27
29
|
})).concat(decimalPostfix);
|
|
28
30
|
}
|
|
29
|
-
return "".concat(prefix).concat((0, formatNumber_1.formatNumber)(amount)).concat(postfix);
|
|
31
|
+
return "".concat(prefix).concat((0, formatNumber_1.formatNumber)(amount, { locale: locale })).concat(postfix);
|
|
30
32
|
};
|
|
31
33
|
exports.formatCurrency = formatCurrency;
|
package/lib/formatDate.js
CHANGED
|
@@ -3,9 +3,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.formatDate = void 0;
|
|
4
4
|
var ensureTwoDigits = function (d) { return "00".concat(d).slice(-2); };
|
|
5
5
|
var formatDate = function (timestamp) {
|
|
6
|
-
if (!(Number.isInteger(timestamp) || timestamp instanceof Date)) {
|
|
7
|
-
return null;
|
|
8
|
-
}
|
|
9
6
|
var date = timestamp instanceof Date ? timestamp : new Date(timestamp);
|
|
10
7
|
var dd = ensureTwoDigits(date.getDate());
|
|
11
8
|
var mm = ensureTwoDigits(date.getMonth() + 1);
|
package/lib/formatDistance.js
CHANGED
|
@@ -6,10 +6,9 @@ var parseNumber_1 = require("./internal/parseNumber");
|
|
|
6
6
|
var unicode_1 = require("./internal/unicode");
|
|
7
7
|
var formatDistance = function (distance, opts) {
|
|
8
8
|
if (opts === void 0) { opts = { unit: 'km' }; }
|
|
9
|
-
var toFormat = (0, parseNumber_1.parseNumber)(distance);
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
return "".concat((0, formatNumber_1.formatNumber)(toFormat)).concat(unicode_1.NON_BREAKING_SPACE).concat(opts.unit);
|
|
9
|
+
var toFormat = (0, parseNumber_1.parseNumber)(distance, 'nb');
|
|
10
|
+
return toFormat === null
|
|
11
|
+
? ''
|
|
12
|
+
: "".concat((0, formatNumber_1.formatNumber)(toFormat, { locale: 'nb' })).concat(unicode_1.NON_BREAKING_SPACE).concat(opts.unit);
|
|
14
13
|
};
|
|
15
14
|
exports.formatDistance = formatDistance;
|
package/lib/formatNumber.js
CHANGED
|
@@ -1,27 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __assign = (this && this.__assign) || function () {
|
|
3
|
-
__assign = Object.assign || function(t) {
|
|
4
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
-
s = arguments[i];
|
|
6
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
-
t[p] = s[p];
|
|
8
|
-
}
|
|
9
|
-
return t;
|
|
10
|
-
};
|
|
11
|
-
return __assign.apply(this, arguments);
|
|
12
|
-
};
|
|
13
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
3
|
exports.formatNumber = void 0;
|
|
15
|
-
var underscore_string_1 = require("underscore.string");
|
|
16
|
-
var unicode_1 = require("./internal/unicode");
|
|
17
4
|
var parseNumber_1 = require("./internal/parseNumber");
|
|
18
5
|
var formatNumber = function (number, opts) {
|
|
19
|
-
|
|
20
|
-
var
|
|
21
|
-
var toFormat = (0, parseNumber_1.parseNumber)(number);
|
|
6
|
+
var _a = opts.decimals, decimals = _a === void 0 ? 0 : _a, locale = opts.locale;
|
|
7
|
+
var toFormat = (0, parseNumber_1.parseNumber)(number, locale);
|
|
22
8
|
if (typeof toFormat !== 'number') {
|
|
23
9
|
return number;
|
|
24
10
|
}
|
|
25
|
-
return
|
|
11
|
+
return new Intl.NumberFormat(locale === 'en' ? 'en' : 'nb', {
|
|
12
|
+
maximumFractionDigits: decimals,
|
|
13
|
+
minimumFractionDigits: decimals,
|
|
14
|
+
}).format(toFormat);
|
|
26
15
|
};
|
|
27
16
|
exports.formatNumber = formatNumber;
|
|
@@ -1,14 +1,33 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.parseNumber = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
// https://observablehq.com/@mbostock/localized-number-parsing
|
|
5
|
+
var NumberParser = /** @class */ (function () {
|
|
6
|
+
function NumberParser(locale) {
|
|
7
|
+
var _a, _b;
|
|
8
|
+
var parts = new Intl.NumberFormat(locale).formatToParts(12345.6);
|
|
9
|
+
var numerals = new Intl.NumberFormat(locale, { useGrouping: false })
|
|
10
|
+
.format(9876543210)
|
|
11
|
+
.split('')
|
|
12
|
+
.reverse();
|
|
13
|
+
var index = new Map(numerals.map(function (d, i) { return [d, i]; }));
|
|
14
|
+
this._group = new RegExp("[".concat((_a = parts.find(function (d) { return d.type === 'group'; })) === null || _a === void 0 ? void 0 : _a.value, "]"), 'g');
|
|
15
|
+
this._decimal = new RegExp("[".concat((_b = parts.find(function (d) { return d.type === 'decimal'; })) === null || _b === void 0 ? void 0 : _b.value, "]"));
|
|
16
|
+
this._numeral = new RegExp("[".concat(numerals.join(''), "]"), 'g');
|
|
17
|
+
this._index = function (d) { return "".concat(index.get(d)); };
|
|
7
18
|
}
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
19
|
+
NumberParser.prototype.parse = function (input) {
|
|
20
|
+
var string = input
|
|
21
|
+
.trim()
|
|
22
|
+
.replace(this._group, '')
|
|
23
|
+
.replace(this._decimal, '.')
|
|
24
|
+
.replace(this._numeral, this._index);
|
|
25
|
+
return string ? +string : Number.NaN;
|
|
26
|
+
};
|
|
27
|
+
return NumberParser;
|
|
28
|
+
}());
|
|
29
|
+
var parseNumber = function (number, locale) {
|
|
30
|
+
var parsed = new NumberParser(locale).parse("".concat(number).replace(/\s/g, ''));
|
|
31
|
+
return Number.isNaN(parsed) ? null : parsed;
|
|
13
32
|
};
|
|
14
33
|
exports.parseNumber = parseNumber;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"root":["../src/formatAccountNumber.ts","../src/formatCurrency.ts","../src/formatDate.ts","../src/formatDistance.ts","../src/formatFodselsnummer.ts","../src/formatNumber.ts","../src/formatPercentage.ts","../src/index.ts","../src/types.ts","../src/internal/parseNumber.ts","../src/internal/unicode.ts"],"version":"5.6.3"}
|
package/lib/types.js
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sb1/ffe-formatters",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "5.0.0",
|
|
4
4
|
"description": "JavaScript general use string formatters",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ffe"
|
|
@@ -27,10 +27,6 @@
|
|
|
27
27
|
"test": "jest",
|
|
28
28
|
"test:watch": "jest --watch"
|
|
29
29
|
},
|
|
30
|
-
"dependencies": {
|
|
31
|
-
"@types/underscore.string": "^0.0.41",
|
|
32
|
-
"underscore.string": "^3.3.4"
|
|
33
|
-
},
|
|
34
30
|
"devDependencies": {
|
|
35
31
|
"@sb1/ffe-buildtool": "^0.9.0",
|
|
36
32
|
"eslint": "^8.57.0"
|
|
@@ -38,5 +34,5 @@
|
|
|
38
34
|
"publishConfig": {
|
|
39
35
|
"access": "public"
|
|
40
36
|
},
|
|
41
|
-
"gitHead": "
|
|
37
|
+
"gitHead": "d753b8a8ecbc46fdfd2bb8a3c916819a186414ce"
|
|
42
38
|
}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
+
import { Locale } from './types';
|
|
1
2
|
interface Opts {
|
|
2
3
|
prefix?: string;
|
|
3
4
|
postfix?: string;
|
|
5
|
+
locale: Locale;
|
|
4
6
|
}
|
|
5
|
-
export declare const formatCurrency: (amount: number | string, opts
|
|
7
|
+
export declare const formatCurrency: (amount: number | string, opts: Opts) => string;
|
|
6
8
|
export {};
|
package/types/formatDate.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const formatDate: (timestamp: Date | number) => string
|
|
1
|
+
export declare const formatDate: (timestamp: Date | number) => string;
|
package/types/formatNumber.d.ts
CHANGED
|
@@ -1 +1,5 @@
|
|
|
1
|
-
|
|
1
|
+
import { Locale } from './types';
|
|
2
|
+
export declare const formatNumber: (number: number | string | null | undefined, opts: {
|
|
3
|
+
locale: Locale;
|
|
4
|
+
decimals?: number;
|
|
5
|
+
}) => string | number | null | undefined;
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
import { Locale } from '../types';
|
|
2
|
+
export declare const parseNumber: (number: number | string | null | undefined, locale: Locale) => number | null;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"root":["../src/formatAccountNumber.ts","../src/formatCurrency.ts","../src/formatDate.ts","../src/formatDistance.ts","../src/formatFodselsnummer.ts","../src/formatNumber.ts","../src/formatPercentage.ts","../src/index.ts","../src/types.ts","../src/internal/parseNumber.ts","../src/internal/unicode.ts"],"version":"5.6.3"}
|
package/types/types.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type Locale = 'nb' | 'nn' | 'en';
|