@sb1/ffe-formatters 4.1.5 → 4.2.1

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.
@@ -1,34 +1,36 @@
1
1
  import { NON_BREAKING_SPACE } from './internal/unicode';
2
- function foreignAccountNumber(accountNumber) {
3
- var formattedAccountNumber = '';
4
- for (var i = 0; i < accountNumber.length; i++) {
5
- var isFourthIteration = (i + 1) % 4 === 0;
6
- formattedAccountNumber += accountNumber[i].valueOf();
7
- if (isFourthIteration && i + 1 < accountNumber.length) {
8
- formattedAccountNumber += NON_BREAKING_SPACE;
2
+ var foreignAccountNumber = function (accountNumber) {
3
+ var formattedAccountNumber = '';
4
+ for (var i = 0; i < accountNumber.length; i++) {
5
+ var isFourthIteration = (i + 1) % 4 === 0;
6
+ formattedAccountNumber += accountNumber[i].valueOf();
7
+ if (isFourthIteration && i + 1 < accountNumber.length) {
8
+ formattedAccountNumber += NON_BREAKING_SPACE;
9
+ }
9
10
  }
10
- }
11
- return formattedAccountNumber;
12
- }
13
- function domesticAccountNumber(accountNumber) {
14
- var formattedAccountNumber = '';
15
- if (accountNumber.length > 0) {
16
- formattedAccountNumber += accountNumber.substring(0, 4);
17
- }
18
- if (accountNumber.length > 4) {
19
- formattedAccountNumber += NON_BREAKING_SPACE + accountNumber.substring(4, 6);
20
- }
21
- if (accountNumber.length > 6) {
22
- formattedAccountNumber += NON_BREAKING_SPACE + accountNumber.substring(6);
23
- }
24
- return formattedAccountNumber;
25
- }
26
- export default function formatAccountNumber(accountNumber) {
27
- if (!accountNumber) {
28
- return accountNumber;
29
- }
30
- if (accountNumber.length <= 11) {
31
- return domesticAccountNumber(accountNumber);
32
- }
33
- return foreignAccountNumber(accountNumber);
34
- }
11
+ return formattedAccountNumber;
12
+ };
13
+ var domesticAccountNumber = function (accountNumber) {
14
+ var formattedAccountNumber = '';
15
+ if (accountNumber.length > 0) {
16
+ formattedAccountNumber += accountNumber.substring(0, 4);
17
+ }
18
+ if (accountNumber.length > 4) {
19
+ formattedAccountNumber +=
20
+ NON_BREAKING_SPACE + accountNumber.substring(4, 6);
21
+ }
22
+ if (accountNumber.length > 6) {
23
+ formattedAccountNumber +=
24
+ NON_BREAKING_SPACE + accountNumber.substring(6);
25
+ }
26
+ return formattedAccountNumber;
27
+ };
28
+ export var formatAccountNumber = function (accountNumber) {
29
+ if (!accountNumber) {
30
+ return accountNumber;
31
+ }
32
+ if (accountNumber.length <= 11) {
33
+ return domesticAccountNumber(accountNumber);
34
+ }
35
+ return foreignAccountNumber(accountNumber);
36
+ };
@@ -1,28 +1,27 @@
1
- function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
2
- function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
3
- function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
4
- function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
5
- function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
6
- function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
7
- import formatNumber from './formatNumber';
8
- import parseNumber from './internal/parseNumber';
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 { formatNumber } from './formatNumber';
13
+ import { parseNumber } from './internal/parseNumber';
9
14
  import { NON_BREAKING_SPACE } from './internal/unicode';
10
- export default function formatCurrency(amount) {
11
- var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
12
- var _prefix$postfix$opts = _objectSpread({
13
- prefix: "kr".concat(NON_BREAKING_SPACE),
14
- postfix: ',–'
15
- }, opts),
16
- prefix = _prefix$postfix$opts.prefix,
17
- postfix = _prefix$postfix$opts.postfix;
18
-
19
- // A ',-' postfix is not allowed for amounts with øre, see:
20
- // http://www.sprakradet.no/svardatabase/sporsmal-og-svar/kronebelop-rekkjefolgje-komma-og-strek/
21
- if (parseNumber(amount) % 1 !== 0) {
22
- var decimalPostfix = ![',–', ',-'].includes(postfix) ? postfix : '';
23
- return "".concat(prefix).concat(formatNumber(amount, {
24
- decimals: 2
25
- })).concat(decimalPostfix);
26
- }
27
- return "".concat(prefix).concat(formatNumber(amount)).concat(postfix);
28
- }
15
+ export var formatCurrency = function (amount, opts) {
16
+ var _a = __assign({ prefix: "kr".concat(NON_BREAKING_SPACE), postfix: ',–' }, opts), prefix = _a.prefix, postfix = _a.postfix;
17
+ var parsed = parseNumber(amount);
18
+ // A ',-' postfix is not allowed for amounts with øre, see:
19
+ // http://www.sprakradet.no/svardatabase/sporsmal-og-svar/kronebelop-rekkjefolgje-komma-og-strek/
20
+ if (typeof parsed === 'number' && parsed % 1 !== 0) {
21
+ var decimalPostfix = ![',–', ',-'].includes(postfix) ? postfix : '';
22
+ return "".concat(prefix).concat(formatNumber(amount, {
23
+ decimals: 2,
24
+ })).concat(decimalPostfix);
25
+ }
26
+ return "".concat(prefix).concat(formatNumber(amount)).concat(postfix);
27
+ };
package/es/formatDate.js CHANGED
@@ -1,13 +1,11 @@
1
- var ensureTwoDigits = function ensureTwoDigits(d) {
2
- return "00".concat(d).slice(-2);
1
+ var ensureTwoDigits = function (d) { return "00".concat(d).slice(-2); };
2
+ export var formatDate = function (timestamp) {
3
+ if (!(Number.isInteger(timestamp) || timestamp instanceof Date)) {
4
+ return null;
5
+ }
6
+ var date = timestamp instanceof Date ? timestamp : new Date(timestamp);
7
+ var dd = ensureTwoDigits(date.getDate());
8
+ var mm = ensureTwoDigits(date.getMonth() + 1);
9
+ var yyyy = date.getFullYear();
10
+ return "".concat(dd, ".").concat(mm, ".").concat(yyyy);
3
11
  };
4
- export default function formatDate(timestamp) {
5
- if (!(Number.isInteger(timestamp) || timestamp instanceof Date)) {
6
- return null;
7
- }
8
- var date = timestamp instanceof Date ? timestamp : new Date(timestamp);
9
- var dd = ensureTwoDigits(date.getDate());
10
- var mm = ensureTwoDigits(date.getMonth() + 1);
11
- var yyyy = date.getFullYear();
12
- return "".concat(dd, ".").concat(mm, ".").concat(yyyy);
13
- }
@@ -1,14 +1,11 @@
1
- import formatNumber from './formatNumber';
2
- import parseNumber from './internal/parseNumber';
1
+ import { formatNumber } from './formatNumber';
2
+ import { parseNumber } from './internal/parseNumber';
3
3
  import { NON_BREAKING_SPACE } from './internal/unicode';
4
- var defaults = {
5
- unit: 'km'
4
+ export var formatDistance = function (distance, opts) {
5
+ if (opts === void 0) { opts = { unit: 'km' }; }
6
+ var toFormat = parseNumber(distance);
7
+ if (toFormat !== 0 && !toFormat) {
8
+ return '';
9
+ }
10
+ return "".concat(formatNumber(toFormat)).concat(NON_BREAKING_SPACE).concat(opts.unit);
6
11
  };
7
- export default function formatDistance(distance) {
8
- var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaults;
9
- var toFormat = parseNumber(distance);
10
- if (toFormat !== 0 && !toFormat) {
11
- return '';
12
- }
13
- return "".concat(formatNumber(toFormat)).concat(NON_BREAKING_SPACE).concat(opts.unit);
14
- }
@@ -1,7 +1,7 @@
1
1
  import { NON_BREAKING_SPACE } from './internal/unicode';
2
- export default function formatFodselsnummer(ssn) {
3
- if (!ssn || ssn.length !== 11) {
4
- return ssn;
5
- }
6
- return "".concat(ssn.substring(0, 6)).concat(NON_BREAKING_SPACE).concat(ssn.substring(6, 11));
7
- }
2
+ export var formatFodselsnummer = function (ssn) {
3
+ if (!ssn || ssn.length !== 11) {
4
+ return ssn;
5
+ }
6
+ return "".concat(ssn.substring(0, 6)).concat(NON_BREAKING_SPACE).concat(ssn.substring(6, 11));
7
+ };
@@ -1,25 +1,23 @@
1
- function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
2
- function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
3
- function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
4
- function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
5
- function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
6
- function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
7
- import numberFormat from 'underscore.string/numberFormat';
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';
8
13
  import { NON_BREAKING_SPACE } from './internal/unicode';
9
- import parseNumber from './internal/parseNumber';
10
- export default function formatNumber(number) {
11
- var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
12
- var _decimals$thousandSep = _objectSpread({
13
- decimals: 0,
14
- thousandSeparator: NON_BREAKING_SPACE,
15
- decimalMark: ','
16
- }, opts),
17
- decimals = _decimals$thousandSep.decimals,
18
- thousandSeparator = _decimals$thousandSep.thousandSeparator,
19
- decimalMark = _decimals$thousandSep.decimalMark;
20
- var toFormat = parseNumber(number);
21
- if (typeof toFormat !== 'number') {
22
- return number;
23
- }
24
- return "".concat(numberFormat(toFormat, decimals, decimalMark, thousandSeparator));
25
- }
14
+ import { parseNumber } from './internal/parseNumber';
15
+ export var formatNumber = function (number, opts) {
16
+ if (opts === void 0) { opts = {}; }
17
+ var _a = __assign({ decimals: 0, thousandSeparator: NON_BREAKING_SPACE, decimalMark: ',' }, opts), decimals = _a.decimals, thousandSeparator = _a.thousandSeparator, decimalMark = _a.decimalMark;
18
+ var toFormat = parseNumber(number);
19
+ if (typeof toFormat !== 'number') {
20
+ return number;
21
+ }
22
+ return "".concat(numberFormat(toFormat, decimals, decimalMark, thousandSeparator));
23
+ };
@@ -1,16 +1,15 @@
1
1
  import { NON_BREAKING_SPACE } from './internal/unicode';
2
- var defaults = {
3
- minDecimals: 0,
4
- maxDecimals: 2
2
+ export var formatPercentage = function (amount, opts) {
3
+ if (opts === void 0) { opts = {
4
+ minDecimals: 0,
5
+ maxDecimals: 2,
6
+ }; }
7
+ if (typeof amount !== 'number') {
8
+ return '';
9
+ }
10
+ var percentage = amount.toLocaleString('nb-NO', {
11
+ minimumFractionDigits: opts.minDecimals,
12
+ maximumFractionDigits: opts.maxDecimals,
13
+ });
14
+ return "".concat(percentage.replace('.', ',')).concat(NON_BREAKING_SPACE, "%");
5
15
  };
6
- export default function formatPercentage(amount) {
7
- var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaults;
8
- if (typeof amount !== 'number') {
9
- return '';
10
- }
11
- var percentage = amount.toLocaleString('nb-NO', {
12
- minimumFractionDigits: opts.minDecimals,
13
- maximumFractionDigits: opts.maxDecimals
14
- });
15
- return "".concat(percentage.replace('.', ',')).concat(NON_BREAKING_SPACE, "%");
16
- }
package/es/index.js CHANGED
@@ -1,7 +1,7 @@
1
- export { default as formatAccountNumber } from './formatAccountNumber';
2
- export { default as formatCurrency } from './formatCurrency';
3
- export { default as formatDate } from './formatDate';
4
- export { default as formatDistance } from './formatDistance';
5
- export { default as formatNumber } from './formatNumber';
6
- export { default as formatPercentage } from './formatPercentage';
7
- export { default as formatFodselsnummer } from './formatFodselsnummer';
1
+ export { formatAccountNumber } from './formatAccountNumber';
2
+ export { formatCurrency } from './formatCurrency';
3
+ export { formatDate } from './formatDate';
4
+ export { formatDistance } from './formatDistance';
5
+ export { formatNumber } from './formatNumber';
6
+ export { formatPercentage } from './formatPercentage';
7
+ export { formatFodselsnummer } from './formatFodselsnummer';
@@ -1,13 +1,10 @@
1
- export default function parseNumber(number) {
2
- if (!number || typeof number === 'number') {
3
- return number;
4
- }
5
- var parsed = number;
6
- if (typeof number === 'string') {
7
- parsed = parseFloat(number.replace(/[^\d,.-]/g, '').replace(/,/g, '.'));
8
- }
9
- if (Number.isNaN(parsed) || typeof parsed !== 'number') {
10
- return null;
11
- }
12
- return parsed;
13
- }
1
+ export var parseNumber = function (number) {
2
+ if (!number || typeof number === 'number') {
3
+ return number;
4
+ }
5
+ var parsed = parseFloat(number.replace(/[^\d,.-]/g, '').replace(/,/g, '.'));
6
+ if (Number.isNaN(parsed)) {
7
+ return null;
8
+ }
9
+ return parsed;
10
+ };
@@ -1,2 +1 @@
1
- export var NON_BREAKING_SPACE = "\xA0";
2
- export var SOFT_HYPHEN = "\xAD";
1
+ export var NON_BREAKING_SPACE = '\u00A0';
@@ -1,40 +1,40 @@
1
1
  "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = formatAccountNumber;
7
- var _unicode = require("./internal/unicode");
8
- function foreignAccountNumber(accountNumber) {
9
- var formattedAccountNumber = '';
10
- for (var i = 0; i < accountNumber.length; i++) {
11
- var isFourthIteration = (i + 1) % 4 === 0;
12
- formattedAccountNumber += accountNumber[i].valueOf();
13
- if (isFourthIteration && i + 1 < accountNumber.length) {
14
- formattedAccountNumber += _unicode.NON_BREAKING_SPACE;
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.formatAccountNumber = void 0;
4
+ var unicode_1 = require("./internal/unicode");
5
+ var foreignAccountNumber = function (accountNumber) {
6
+ var formattedAccountNumber = '';
7
+ for (var i = 0; i < accountNumber.length; i++) {
8
+ var isFourthIteration = (i + 1) % 4 === 0;
9
+ formattedAccountNumber += accountNumber[i].valueOf();
10
+ if (isFourthIteration && i + 1 < accountNumber.length) {
11
+ formattedAccountNumber += unicode_1.NON_BREAKING_SPACE;
12
+ }
15
13
  }
16
- }
17
- return formattedAccountNumber;
18
- }
19
- function domesticAccountNumber(accountNumber) {
20
- var formattedAccountNumber = '';
21
- if (accountNumber.length > 0) {
22
- formattedAccountNumber += accountNumber.substring(0, 4);
23
- }
24
- if (accountNumber.length > 4) {
25
- formattedAccountNumber += _unicode.NON_BREAKING_SPACE + accountNumber.substring(4, 6);
26
- }
27
- if (accountNumber.length > 6) {
28
- formattedAccountNumber += _unicode.NON_BREAKING_SPACE + accountNumber.substring(6);
29
- }
30
- return formattedAccountNumber;
31
- }
32
- function formatAccountNumber(accountNumber) {
33
- if (!accountNumber) {
34
- return accountNumber;
35
- }
36
- if (accountNumber.length <= 11) {
37
- return domesticAccountNumber(accountNumber);
38
- }
39
- return foreignAccountNumber(accountNumber);
40
- }
14
+ return formattedAccountNumber;
15
+ };
16
+ var domesticAccountNumber = function (accountNumber) {
17
+ var formattedAccountNumber = '';
18
+ if (accountNumber.length > 0) {
19
+ formattedAccountNumber += accountNumber.substring(0, 4);
20
+ }
21
+ if (accountNumber.length > 4) {
22
+ formattedAccountNumber +=
23
+ unicode_1.NON_BREAKING_SPACE + accountNumber.substring(4, 6);
24
+ }
25
+ if (accountNumber.length > 6) {
26
+ formattedAccountNumber +=
27
+ unicode_1.NON_BREAKING_SPACE + accountNumber.substring(6);
28
+ }
29
+ return formattedAccountNumber;
30
+ };
31
+ var formatAccountNumber = function (accountNumber) {
32
+ if (!accountNumber) {
33
+ return accountNumber;
34
+ }
35
+ if (accountNumber.length <= 11) {
36
+ return domesticAccountNumber(accountNumber);
37
+ }
38
+ return foreignAccountNumber(accountNumber);
39
+ };
40
+ exports.formatAccountNumber = formatAccountNumber;
@@ -1,35 +1,31 @@
1
1
  "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = formatCurrency;
7
- var _formatNumber = _interopRequireDefault(require("./formatNumber"));
8
- var _parseNumber = _interopRequireDefault(require("./internal/parseNumber"));
9
- var _unicode = require("./internal/unicode");
10
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
- function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
12
- function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
13
- function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
14
- function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
15
- function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
16
- function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
17
- function formatCurrency(amount) {
18
- var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
19
- var _prefix$postfix$opts = _objectSpread({
20
- prefix: "kr".concat(_unicode.NON_BREAKING_SPACE),
21
- postfix: ',–'
22
- }, opts),
23
- prefix = _prefix$postfix$opts.prefix,
24
- postfix = _prefix$postfix$opts.postfix;
25
-
26
- // A ',-' postfix is not allowed for amounts with øre, see:
27
- // http://www.sprakradet.no/svardatabase/sporsmal-og-svar/kronebelop-rekkjefolgje-komma-og-strek/
28
- if ((0, _parseNumber.default)(amount) % 1 !== 0) {
29
- var decimalPostfix = ![',–', ',-'].includes(postfix) ? postfix : '';
30
- return "".concat(prefix).concat((0, _formatNumber.default)(amount, {
31
- decimals: 2
32
- })).concat(decimalPostfix);
33
- }
34
- return "".concat(prefix).concat((0, _formatNumber.default)(amount)).concat(postfix);
35
- }
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
+ Object.defineProperty(exports, "__esModule", { value: true });
14
+ exports.formatCurrency = void 0;
15
+ var formatNumber_1 = require("./formatNumber");
16
+ var parseNumber_1 = require("./internal/parseNumber");
17
+ var unicode_1 = require("./internal/unicode");
18
+ var formatCurrency = function (amount, opts) {
19
+ 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
+ // A ',-' postfix is not allowed for amounts with øre, see:
22
+ // http://www.sprakradet.no/svardatabase/sporsmal-og-svar/kronebelop-rekkjefolgje-komma-og-strek/
23
+ if (typeof parsed === 'number' && parsed % 1 !== 0) {
24
+ var decimalPostfix = ![',–', ',-'].includes(postfix) ? postfix : '';
25
+ return "".concat(prefix).concat((0, formatNumber_1.formatNumber)(amount, {
26
+ decimals: 2,
27
+ })).concat(decimalPostfix);
28
+ }
29
+ return "".concat(prefix).concat((0, formatNumber_1.formatNumber)(amount)).concat(postfix);
30
+ };
31
+ exports.formatCurrency = formatCurrency;
package/lib/formatDate.js CHANGED
@@ -1,19 +1,15 @@
1
1
  "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = formatDate;
7
- var ensureTwoDigits = function ensureTwoDigits(d) {
8
- return "00".concat(d).slice(-2);
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.formatDate = void 0;
4
+ var ensureTwoDigits = function (d) { return "00".concat(d).slice(-2); };
5
+ var formatDate = function (timestamp) {
6
+ if (!(Number.isInteger(timestamp) || timestamp instanceof Date)) {
7
+ return null;
8
+ }
9
+ var date = timestamp instanceof Date ? timestamp : new Date(timestamp);
10
+ var dd = ensureTwoDigits(date.getDate());
11
+ var mm = ensureTwoDigits(date.getMonth() + 1);
12
+ var yyyy = date.getFullYear();
13
+ return "".concat(dd, ".").concat(mm, ".").concat(yyyy);
9
14
  };
10
- function formatDate(timestamp) {
11
- if (!(Number.isInteger(timestamp) || timestamp instanceof Date)) {
12
- return null;
13
- }
14
- var date = timestamp instanceof Date ? timestamp : new Date(timestamp);
15
- var dd = ensureTwoDigits(date.getDate());
16
- var mm = ensureTwoDigits(date.getMonth() + 1);
17
- var yyyy = date.getFullYear();
18
- return "".concat(dd, ".").concat(mm, ".").concat(yyyy);
19
- }
15
+ exports.formatDate = formatDate;
@@ -1,21 +1,15 @@
1
1
  "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = formatDistance;
7
- var _formatNumber = _interopRequireDefault(require("./formatNumber"));
8
- var _parseNumber = _interopRequireDefault(require("./internal/parseNumber"));
9
- var _unicode = require("./internal/unicode");
10
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
- var defaults = {
12
- unit: 'km'
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.formatDistance = void 0;
4
+ var formatNumber_1 = require("./formatNumber");
5
+ var parseNumber_1 = require("./internal/parseNumber");
6
+ var unicode_1 = require("./internal/unicode");
7
+ var formatDistance = function (distance, opts) {
8
+ if (opts === void 0) { opts = { unit: 'km' }; }
9
+ var toFormat = (0, parseNumber_1.parseNumber)(distance);
10
+ if (toFormat !== 0 && !toFormat) {
11
+ return '';
12
+ }
13
+ return "".concat((0, formatNumber_1.formatNumber)(toFormat)).concat(unicode_1.NON_BREAKING_SPACE).concat(opts.unit);
13
14
  };
14
- function formatDistance(distance) {
15
- var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaults;
16
- var toFormat = (0, _parseNumber.default)(distance);
17
- if (toFormat !== 0 && !toFormat) {
18
- return '';
19
- }
20
- return "".concat((0, _formatNumber.default)(toFormat)).concat(_unicode.NON_BREAKING_SPACE).concat(opts.unit);
21
- }
15
+ exports.formatDistance = formatDistance;
@@ -1,13 +1,11 @@
1
1
  "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = formatFodselsnummer;
7
- var _unicode = require("./internal/unicode");
8
- function formatFodselsnummer(ssn) {
9
- if (!ssn || ssn.length !== 11) {
10
- return ssn;
11
- }
12
- return "".concat(ssn.substring(0, 6)).concat(_unicode.NON_BREAKING_SPACE).concat(ssn.substring(6, 11));
13
- }
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.formatFodselsnummer = void 0;
4
+ var unicode_1 = require("./internal/unicode");
5
+ var formatFodselsnummer = function (ssn) {
6
+ if (!ssn || ssn.length !== 11) {
7
+ return ssn;
8
+ }
9
+ return "".concat(ssn.substring(0, 6)).concat(unicode_1.NON_BREAKING_SPACE).concat(ssn.substring(6, 11));
10
+ };
11
+ exports.formatFodselsnummer = formatFodselsnummer;
@@ -1,32 +1,27 @@
1
1
  "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = formatNumber;
7
- var _numberFormat = _interopRequireDefault(require("underscore.string/numberFormat"));
8
- var _unicode = require("./internal/unicode");
9
- var _parseNumber = _interopRequireDefault(require("./internal/parseNumber"));
10
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
- function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
12
- function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
13
- function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
14
- function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
15
- function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
16
- function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
17
- function formatNumber(number) {
18
- var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
19
- var _decimals$thousandSep = _objectSpread({
20
- decimals: 0,
21
- thousandSeparator: _unicode.NON_BREAKING_SPACE,
22
- decimalMark: ','
23
- }, opts),
24
- decimals = _decimals$thousandSep.decimals,
25
- thousandSeparator = _decimals$thousandSep.thousandSeparator,
26
- decimalMark = _decimals$thousandSep.decimalMark;
27
- var toFormat = (0, _parseNumber.default)(number);
28
- if (typeof toFormat !== 'number') {
29
- return number;
30
- }
31
- return "".concat((0, _numberFormat.default)(toFormat, decimals, decimalMark, thousandSeparator));
32
- }
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
+ Object.defineProperty(exports, "__esModule", { value: true });
14
+ exports.formatNumber = void 0;
15
+ var underscore_string_1 = require("underscore.string");
16
+ var unicode_1 = require("./internal/unicode");
17
+ var parseNumber_1 = require("./internal/parseNumber");
18
+ var formatNumber = function (number, opts) {
19
+ if (opts === void 0) { opts = {}; }
20
+ var _a = __assign({ decimals: 0, thousandSeparator: unicode_1.NON_BREAKING_SPACE, decimalMark: ',' }, opts), decimals = _a.decimals, thousandSeparator = _a.thousandSeparator, decimalMark = _a.decimalMark;
21
+ var toFormat = (0, parseNumber_1.parseNumber)(number);
22
+ if (typeof toFormat !== 'number') {
23
+ return number;
24
+ }
25
+ return "".concat((0, underscore_string_1.numberFormat)(toFormat, decimals, decimalMark, thousandSeparator));
26
+ };
27
+ exports.formatNumber = formatNumber;
@@ -1,22 +1,19 @@
1
1
  "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = formatPercentage;
7
- var _unicode = require("./internal/unicode");
8
- var defaults = {
9
- minDecimals: 0,
10
- maxDecimals: 2
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.formatPercentage = void 0;
4
+ var unicode_1 = require("./internal/unicode");
5
+ var formatPercentage = function (amount, opts) {
6
+ if (opts === void 0) { opts = {
7
+ minDecimals: 0,
8
+ maxDecimals: 2,
9
+ }; }
10
+ if (typeof amount !== 'number') {
11
+ return '';
12
+ }
13
+ var percentage = amount.toLocaleString('nb-NO', {
14
+ minimumFractionDigits: opts.minDecimals,
15
+ maximumFractionDigits: opts.maxDecimals,
16
+ });
17
+ return "".concat(percentage.replace('.', ',')).concat(unicode_1.NON_BREAKING_SPACE, "%");
11
18
  };
12
- function formatPercentage(amount) {
13
- var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaults;
14
- if (typeof amount !== 'number') {
15
- return '';
16
- }
17
- var percentage = amount.toLocaleString('nb-NO', {
18
- minimumFractionDigits: opts.minDecimals,
19
- maximumFractionDigits: opts.maxDecimals
20
- });
21
- return "".concat(percentage.replace('.', ',')).concat(_unicode.NON_BREAKING_SPACE, "%");
22
- }
19
+ exports.formatPercentage = formatPercentage;
package/lib/index.js CHANGED
@@ -1,55 +1,17 @@
1
1
  "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- Object.defineProperty(exports, "formatAccountNumber", {
7
- enumerable: true,
8
- get: function get() {
9
- return _formatAccountNumber.default;
10
- }
11
- });
12
- Object.defineProperty(exports, "formatCurrency", {
13
- enumerable: true,
14
- get: function get() {
15
- return _formatCurrency.default;
16
- }
17
- });
18
- Object.defineProperty(exports, "formatDate", {
19
- enumerable: true,
20
- get: function get() {
21
- return _formatDate.default;
22
- }
23
- });
24
- Object.defineProperty(exports, "formatDistance", {
25
- enumerable: true,
26
- get: function get() {
27
- return _formatDistance.default;
28
- }
29
- });
30
- Object.defineProperty(exports, "formatFodselsnummer", {
31
- enumerable: true,
32
- get: function get() {
33
- return _formatFodselsnummer.default;
34
- }
35
- });
36
- Object.defineProperty(exports, "formatNumber", {
37
- enumerable: true,
38
- get: function get() {
39
- return _formatNumber.default;
40
- }
41
- });
42
- Object.defineProperty(exports, "formatPercentage", {
43
- enumerable: true,
44
- get: function get() {
45
- return _formatPercentage.default;
46
- }
47
- });
48
- var _formatAccountNumber = _interopRequireDefault(require("./formatAccountNumber"));
49
- var _formatCurrency = _interopRequireDefault(require("./formatCurrency"));
50
- var _formatDate = _interopRequireDefault(require("./formatDate"));
51
- var _formatDistance = _interopRequireDefault(require("./formatDistance"));
52
- var _formatNumber = _interopRequireDefault(require("./formatNumber"));
53
- var _formatPercentage = _interopRequireDefault(require("./formatPercentage"));
54
- var _formatFodselsnummer = _interopRequireDefault(require("./formatFodselsnummer"));
55
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.formatFodselsnummer = exports.formatPercentage = exports.formatNumber = exports.formatDistance = exports.formatDate = exports.formatCurrency = exports.formatAccountNumber = void 0;
4
+ var formatAccountNumber_1 = require("./formatAccountNumber");
5
+ Object.defineProperty(exports, "formatAccountNumber", { enumerable: true, get: function () { return formatAccountNumber_1.formatAccountNumber; } });
6
+ var formatCurrency_1 = require("./formatCurrency");
7
+ Object.defineProperty(exports, "formatCurrency", { enumerable: true, get: function () { return formatCurrency_1.formatCurrency; } });
8
+ var formatDate_1 = require("./formatDate");
9
+ Object.defineProperty(exports, "formatDate", { enumerable: true, get: function () { return formatDate_1.formatDate; } });
10
+ var formatDistance_1 = require("./formatDistance");
11
+ Object.defineProperty(exports, "formatDistance", { enumerable: true, get: function () { return formatDistance_1.formatDistance; } });
12
+ var formatNumber_1 = require("./formatNumber");
13
+ Object.defineProperty(exports, "formatNumber", { enumerable: true, get: function () { return formatNumber_1.formatNumber; } });
14
+ var formatPercentage_1 = require("./formatPercentage");
15
+ Object.defineProperty(exports, "formatPercentage", { enumerable: true, get: function () { return formatPercentage_1.formatPercentage; } });
16
+ var formatFodselsnummer_1 = require("./formatFodselsnummer");
17
+ Object.defineProperty(exports, "formatFodselsnummer", { enumerable: true, get: function () { return formatFodselsnummer_1.formatFodselsnummer; } });
@@ -1,19 +1,14 @@
1
1
  "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = parseNumber;
7
- function parseNumber(number) {
8
- if (!number || typeof number === 'number') {
9
- return number;
10
- }
11
- var parsed = number;
12
- if (typeof number === 'string') {
13
- parsed = parseFloat(number.replace(/[^\d,.-]/g, '').replace(/,/g, '.'));
14
- }
15
- if (Number.isNaN(parsed) || typeof parsed !== 'number') {
16
- return null;
17
- }
18
- return parsed;
19
- }
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseNumber = void 0;
4
+ var parseNumber = function (number) {
5
+ if (!number || typeof number === 'number') {
6
+ return number;
7
+ }
8
+ var parsed = parseFloat(number.replace(/[^\d,.-]/g, '').replace(/,/g, '.'));
9
+ if (Number.isNaN(parsed)) {
10
+ return null;
11
+ }
12
+ return parsed;
13
+ };
14
+ exports.parseNumber = parseNumber;
@@ -1,8 +1,4 @@
1
1
  "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.SOFT_HYPHEN = exports.NON_BREAKING_SPACE = void 0;
7
- var NON_BREAKING_SPACE = exports.NON_BREAKING_SPACE = "\xA0";
8
- var SOFT_HYPHEN = exports.SOFT_HYPHEN = "\xAD";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.NON_BREAKING_SPACE = void 0;
4
+ exports.NON_BREAKING_SPACE = '\u00A0';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sb1/ffe-formatters",
3
- "version": "4.1.5",
3
+ "version": "4.2.1",
4
4
  "description": "JavaScript general use string formatters",
5
5
  "keywords": [
6
6
  "ffe"
@@ -21,22 +21,23 @@
21
21
  "url": "ssh://git@github.com:SpareBank1/designsystem.git"
22
22
  },
23
23
  "scripts": {
24
- "build": "ffe-buildtool babel",
24
+ "build": "ffe-buildtool tsc",
25
25
  "watch": "ffe-buildtool babel-watch",
26
- "lint": "eslint src",
27
- "lint:fix": "eslint src --fix",
26
+ "lint": "eslint src --ext ts,tsx",
27
+ "lint:fix": "eslint src --fix --ext ts,tsx",
28
28
  "test": "jest",
29
29
  "test:watch": "jest --watch"
30
30
  },
31
31
  "dependencies": {
32
+ "@types/underscore.string": "^0.0.41",
32
33
  "underscore.string": "^3.3.4"
33
34
  },
34
35
  "devDependencies": {
35
- "@sb1/ffe-buildtool": "^0.6.1",
36
+ "@sb1/ffe-buildtool": "^0.6.2",
36
37
  "eslint": "^8.57.0"
37
38
  },
38
39
  "publishConfig": {
39
40
  "access": "public"
40
41
  },
41
- "gitHead": "12285ee4ecea525ee235bc88aee2ffe38c32f0d1"
42
+ "gitHead": "9ac84c1d8650f7626876a864cc0dc73a67fa241b"
42
43
  }
@@ -0,0 +1 @@
1
+ export declare const formatAccountNumber: (accountNumber: string) => string;
@@ -0,0 +1,6 @@
1
+ interface Opts {
2
+ prefix?: string;
3
+ postfix?: string;
4
+ }
5
+ export declare const formatCurrency: (amount: number | string, opts?: Opts) => string;
6
+ export {};
@@ -0,0 +1 @@
1
+ export declare const formatDate: (timestamp: Date | number) => string | null;
@@ -0,0 +1,5 @@
1
+ interface Opts {
2
+ unit?: string;
3
+ }
4
+ export declare const formatDistance: (distance: string | number, opts?: Opts) => string;
5
+ export {};
@@ -0,0 +1 @@
1
+ export declare const formatFodselsnummer: (ssn: string) => string;
@@ -0,0 +1 @@
1
+ export declare const formatNumber: (number: number | string | null | undefined, opts?: {}) => string | number | null | undefined;
@@ -0,0 +1,6 @@
1
+ interface Opts {
2
+ minDecimals?: number;
3
+ maxDecimals?: number;
4
+ }
5
+ export declare const formatPercentage: (amount: number | string, opts?: Opts) => string;
6
+ export {};
package/types/index.d.ts CHANGED
@@ -1,31 +1,7 @@
1
- declare module '@sb1/ffe-formatters' {
2
- export function formatAccountNumber(accountNumber: string): string;
3
-
4
- export function formatCurrency(
5
- amount: number | string,
6
- opts?: { prefix?: string; postfix?: string },
7
- ): string;
8
-
9
- export function formatDate(timestamp: number | Date): string;
10
-
11
- export function formatDistance(
12
- distance: number | string,
13
- opts?: { unit?: string },
14
- ): string;
15
-
16
- export function formatNumber(
17
- number: number | string,
18
- opts?: {
19
- decimals?: number;
20
- thousandSeparator?: string;
21
- decimalMark?: string;
22
- },
23
- ): string;
24
-
25
- export function formatPercentage(
26
- amount: number,
27
- opts?: { minDecimals?: number; maxDecimals?: number },
28
- ): string;
29
-
30
- export function formatFodselsnummer(ssn: string): string;
31
- }
1
+ export { formatAccountNumber } from './formatAccountNumber';
2
+ export { formatCurrency } from './formatCurrency';
3
+ export { formatDate } from './formatDate';
4
+ export { formatDistance } from './formatDistance';
5
+ export { formatNumber } from './formatNumber';
6
+ export { formatPercentage } from './formatPercentage';
7
+ export { formatFodselsnummer } from './formatFodselsnummer';
@@ -0,0 +1 @@
1
+ export declare const parseNumber: (number: number | string | null | undefined) => string | number | null | undefined;
@@ -0,0 +1 @@
1
+ export declare const NON_BREAKING_SPACE = "\u00A0";
package/es/index.d.js DELETED
File without changes
package/lib/index.d.js DELETED
@@ -1 +0,0 @@
1
- "use strict";