@becollective/utils 1.2.0 → 1.3.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/bundle.js +43 -1
- package/index.js +5 -2
- package/package.json +12 -3
- package/{tests → src}/date-time.test.js +2 -2
- package/src/money.js +37 -0
- package/src/money.test.js +99 -0
- package/testUtils/setup.js +21 -0
- package/tests/setup.js +0 -9
- /package/{date-time.js → src/date-time.js} +0 -0
- /package/{password.js → src/password.js} +0 -0
- /package/{tests → src}/password.test.js +0 -0
package/bundle.js
CHANGED
|
@@ -84,9 +84,51 @@ var isUnderSixteen = function isUnderSixteen(dateOfBirth) {
|
|
|
84
84
|
return isNaN(age) ? false : age < 16;
|
|
85
85
|
};
|
|
86
86
|
|
|
87
|
+
var _require = require('@becollective/constants'),
|
|
88
|
+
currencies = _require.currencies; // We should probably throw and error on bad input instead of falling back to a default.
|
|
89
|
+
// We're making an assumption that whoever is calling this function can safely work
|
|
90
|
+
// with AUD. But this assumption removes a lot of error handling elsewhere.
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
var getCurrencyFromCurrencyCode = function getCurrencyFromCurrencyCode(currencyCode) {
|
|
94
|
+
if (typeof currencyCode !== 'string' || !currencies[currencyCode]) {
|
|
95
|
+
console.error(new Error("Invalid currencyCode: ".concat(currencyCode, ". Falling back to 'AUD'.")));
|
|
96
|
+
return currencies.AUD;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return currencies[currencyCode];
|
|
100
|
+
};
|
|
101
|
+
var makeMoneyString = function makeMoneyString(_ref) {
|
|
102
|
+
var inputAmount = _ref.amount,
|
|
103
|
+
currencyCode = _ref.currencyCode,
|
|
104
|
+
_ref$rounded = _ref.rounded,
|
|
105
|
+
rounded = _ref$rounded === void 0 ? false : _ref$rounded,
|
|
106
|
+
_ref$showCurrencyCode = _ref.showCurrencyCode,
|
|
107
|
+
showCurrencyCode = _ref$showCurrencyCode === void 0 ? false : _ref$showCurrencyCode;
|
|
108
|
+
var amount = Number(inputAmount);
|
|
109
|
+
|
|
110
|
+
if (isNaN(amount)) {
|
|
111
|
+
console.error(new Error("Invalid amount: ".concat(inputAmount, ". Falling back to 0.")));
|
|
112
|
+
amount = 0;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
var currency = getCurrencyFromCurrencyCode(currencyCode);
|
|
116
|
+
var minorUnit = currency.minorUnit,
|
|
117
|
+
symbol = currency.symbol;
|
|
118
|
+
var decimalPlaces = rounded ? 0 : minorUnit;
|
|
119
|
+
var localeAmount = amount.toLocaleString(undefined, {
|
|
120
|
+
minimumFractionDigits: decimalPlaces,
|
|
121
|
+
maximumFractionDigits: decimalPlaces
|
|
122
|
+
});
|
|
123
|
+
var currencyCodeString = showCurrencyCode ? " ".concat(currency.code) : '';
|
|
124
|
+
return "".concat(symbol).concat(localeAmount).concat(currencyCodeString);
|
|
125
|
+
};
|
|
126
|
+
|
|
87
127
|
var util = {
|
|
88
128
|
password: password,
|
|
89
129
|
isUnderSixteen: isUnderSixteen,
|
|
90
|
-
getAge: getAge
|
|
130
|
+
getAge: getAge,
|
|
131
|
+
getCurrencyFromCurrencyCode: getCurrencyFromCurrencyCode,
|
|
132
|
+
makeMoneyString: makeMoneyString
|
|
91
133
|
};
|
|
92
134
|
module.exports = util;
|
package/index.js
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
|
-
import { password } from './password';
|
|
2
|
-
import { isUnderSixteen, getAge } from './date-time';
|
|
1
|
+
import { password } from './src/password';
|
|
2
|
+
import { isUnderSixteen, getAge } from './src/date-time';
|
|
3
|
+
import { getCurrencyFromCurrencyCode, makeMoneyString } from './src/money';
|
|
3
4
|
|
|
4
5
|
const util = {
|
|
5
6
|
password,
|
|
6
7
|
isUnderSixteen,
|
|
7
8
|
getAge,
|
|
9
|
+
getCurrencyFromCurrencyCode,
|
|
10
|
+
makeMoneyString,
|
|
8
11
|
};
|
|
9
12
|
|
|
10
13
|
module.exports = util;
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@becollective/utils",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"description": "Common utilities",
|
|
5
5
|
"main": "bundle.js",
|
|
6
6
|
"scripts": {
|
|
7
|
-
"test": "NODE_ENV=localtest npm run build && ./node_modules/.bin/jest
|
|
7
|
+
"test": "NODE_ENV=localtest npm run build && ./node_modules/.bin/jest",
|
|
8
8
|
"build": "./node_modules/.bin/rollup -c",
|
|
9
|
-
"
|
|
9
|
+
"prepare": "npm run build; npm test"
|
|
10
10
|
},
|
|
11
11
|
"author": "",
|
|
12
12
|
"license": "ISC",
|
|
@@ -19,6 +19,15 @@
|
|
|
19
19
|
"rollup-plugin-node-resolve": "^3.4.0"
|
|
20
20
|
},
|
|
21
21
|
"dependencies": {
|
|
22
|
+
"@becollective/constants": "^3.3.2",
|
|
22
23
|
"moment": "^2.24.0"
|
|
24
|
+
},
|
|
25
|
+
"jest": {
|
|
26
|
+
"testMatch": [
|
|
27
|
+
"**/*.test.js"
|
|
28
|
+
],
|
|
29
|
+
"setupFiles": [
|
|
30
|
+
"./testUtils/setup.js"
|
|
31
|
+
]
|
|
23
32
|
}
|
|
24
33
|
}
|
|
@@ -33,14 +33,14 @@ describe('isUnderSixteen', () => {
|
|
|
33
33
|
test('Is not U16 if dob is 16 years ago', () => {
|
|
34
34
|
const dob = moment(refPoint);
|
|
35
35
|
|
|
36
|
-
expect(isUnderSixteen(dob.subtract(1, '
|
|
36
|
+
expect(isUnderSixteen(dob.subtract(1, 'minute').utc().format())).toBe(false);
|
|
37
37
|
expect(isUnderSixteen(dob.subtract(100, 'year').utc().format())).toBe(false);
|
|
38
38
|
});
|
|
39
39
|
|
|
40
40
|
test('Is U16 if dob is less than 16 years ago', () => {
|
|
41
41
|
const dob = moment(refPoint);
|
|
42
42
|
|
|
43
|
-
expect(isUnderSixteen(dob.add(1, '
|
|
43
|
+
expect(isUnderSixteen(dob.add(1, 'minute').utc().format())).toBe(true);
|
|
44
44
|
expect(isUnderSixteen(dob.add(100, 'year').utc().format())).toBe(true);
|
|
45
45
|
});
|
|
46
46
|
|
package/src/money.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
const { currencies } = require('@becollective/constants');
|
|
2
|
+
|
|
3
|
+
// We should probably throw and error on bad input instead of falling back to a default.
|
|
4
|
+
// We're making an assumption that whoever is calling this function can safely work
|
|
5
|
+
// with AUD. But this assumption removes a lot of error handling elsewhere.
|
|
6
|
+
export const getCurrencyFromCurrencyCode = currencyCode => {
|
|
7
|
+
if (typeof currencyCode !== 'string' || !currencies[currencyCode]) {
|
|
8
|
+
console.error(new Error(`Invalid currencyCode: ${currencyCode}. Falling back to 'AUD'.`));
|
|
9
|
+
return currencies.AUD;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
return currencies[currencyCode];
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export const makeMoneyString = ({
|
|
16
|
+
amount: inputAmount,
|
|
17
|
+
currencyCode,
|
|
18
|
+
rounded = false,
|
|
19
|
+
showCurrencyCode = false,
|
|
20
|
+
}) => {
|
|
21
|
+
let amount = Number(inputAmount);
|
|
22
|
+
if (isNaN(amount)) {
|
|
23
|
+
console.error(new Error(`Invalid amount: ${inputAmount}. Falling back to 0.`));
|
|
24
|
+
amount = 0;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const currency = getCurrencyFromCurrencyCode(currencyCode);
|
|
28
|
+
const { minorUnit, symbol } = currency;
|
|
29
|
+
|
|
30
|
+
const decimalPlaces = rounded ? 0 : minorUnit;
|
|
31
|
+
const localeAmount = amount.toLocaleString(undefined, {
|
|
32
|
+
minimumFractionDigits: decimalPlaces,
|
|
33
|
+
maximumFractionDigits: decimalPlaces,
|
|
34
|
+
});
|
|
35
|
+
const currencyCodeString = showCurrencyCode ? ` ${currency.code}` : '';
|
|
36
|
+
return `${symbol}${localeAmount}${currencyCodeString}`;
|
|
37
|
+
};
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
const { currencies } = require('@becollective/constants');
|
|
2
|
+
|
|
3
|
+
const { getCurrencyFromCurrencyCode, makeMoneyString } = require('../bundle.js');
|
|
4
|
+
|
|
5
|
+
describe('getCurrencyFromCurrencyCode', () => {
|
|
6
|
+
test('Returns the correct currency', () => {
|
|
7
|
+
expect(getCurrencyFromCurrencyCode('AUD')).toMatchObject(currencies.AUD);
|
|
8
|
+
expect(getCurrencyFromCurrencyCode('NZD')).toMatchObject(currencies.NZD);
|
|
9
|
+
expect(getCurrencyFromCurrencyCode('GBP')).toMatchObject(currencies.GBP);
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
test('Returns AUD currency if currencyCode not found or invalid', () => {
|
|
13
|
+
const bogusCurrencyCodes = [
|
|
14
|
+
undefined,
|
|
15
|
+
1234,
|
|
16
|
+
'some junk that is not a currency code',
|
|
17
|
+
{ foo: 'bar' },
|
|
18
|
+
];
|
|
19
|
+
bogusCurrencyCodes.forEach(bogusCurrencyCode => {
|
|
20
|
+
expect(getCurrencyFromCurrencyCode(bogusCurrencyCode)).toMatchObject(currencies.AUD);
|
|
21
|
+
});
|
|
22
|
+
});
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
describe('makeMoneyString', () => {
|
|
26
|
+
test('Outputs a rounded money string', () => {
|
|
27
|
+
expect(
|
|
28
|
+
makeMoneyString({
|
|
29
|
+
amount: 123.05,
|
|
30
|
+
rounded: true,
|
|
31
|
+
currencyCode: 'AUD',
|
|
32
|
+
})
|
|
33
|
+
).toEqual('$123');
|
|
34
|
+
|
|
35
|
+
expect(
|
|
36
|
+
makeMoneyString({
|
|
37
|
+
amount: 123.5,
|
|
38
|
+
rounded: true,
|
|
39
|
+
currencyCode: 'AUD',
|
|
40
|
+
})
|
|
41
|
+
).toEqual('$124');
|
|
42
|
+
|
|
43
|
+
expect(
|
|
44
|
+
makeMoneyString({
|
|
45
|
+
amount: 123.95,
|
|
46
|
+
rounded: true,
|
|
47
|
+
currencyCode: 'AUD',
|
|
48
|
+
})
|
|
49
|
+
).toEqual('$124');
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
test('Outputs a money string with currency code', () => {
|
|
53
|
+
expect(
|
|
54
|
+
makeMoneyString({
|
|
55
|
+
amount: 123.05,
|
|
56
|
+
currencyCode: 'NZD',
|
|
57
|
+
showCurrencyCode: true,
|
|
58
|
+
})
|
|
59
|
+
).toEqual('$123.05 NZD');
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
test('Outputs a money string for AUD if bad input is provided', () => {
|
|
63
|
+
const bogusCurrencyCodes = [
|
|
64
|
+
undefined,
|
|
65
|
+
1234,
|
|
66
|
+
'some junk that is not a currency code',
|
|
67
|
+
{ foo: 'bar' },
|
|
68
|
+
];
|
|
69
|
+
bogusCurrencyCodes.forEach(bogusCurrencyCode => {
|
|
70
|
+
expect(
|
|
71
|
+
makeMoneyString({
|
|
72
|
+
amount: 123.05,
|
|
73
|
+
currencyCode: bogusCurrencyCode,
|
|
74
|
+
showCurrencyCode: true,
|
|
75
|
+
})
|
|
76
|
+
).toEqual('$123.05 AUD');
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
test('Gracefully handles non-Number `amount` input', () => {
|
|
81
|
+
expect(
|
|
82
|
+
makeMoneyString({
|
|
83
|
+
amount: '100.25',
|
|
84
|
+
rounded: true,
|
|
85
|
+
currencyCode: 'GBP',
|
|
86
|
+
})
|
|
87
|
+
).toEqual('£100');
|
|
88
|
+
|
|
89
|
+
const bogusAmountValues = [undefined, null, 'some junk that is not a number', { foo: 'bar' }];
|
|
90
|
+
bogusAmountValues.forEach(bogusAmountValue => {
|
|
91
|
+
expect(
|
|
92
|
+
makeMoneyString({
|
|
93
|
+
amount: bogusAmountValue,
|
|
94
|
+
currencyCode: 'GBP',
|
|
95
|
+
})
|
|
96
|
+
).toEqual('£0.00');
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
});
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
process.env.NODE_ENV = 'localtest';
|
|
2
|
+
|
|
3
|
+
let hasListened = false;
|
|
4
|
+
if(!hasListened) {
|
|
5
|
+
hasListened = true;
|
|
6
|
+
process.on('unhandledRejection', (err) => {
|
|
7
|
+
console.debug(err.stack);
|
|
8
|
+
});
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
global.console = {
|
|
12
|
+
// These could be called in parts of the code for good reason but pollute the test output.
|
|
13
|
+
// So suppress the while testing.
|
|
14
|
+
// Ref: https://stackoverflow.com/questions/44467657/jest-better-way-to-disable-console-inside-unit-tests
|
|
15
|
+
log: jest.fn(),
|
|
16
|
+
error: jest.fn(),
|
|
17
|
+
warn: jest.fn(),
|
|
18
|
+
info: jest.fn(),
|
|
19
|
+
// Use console.debug if you need to debug while testing
|
|
20
|
+
debug: console.debug,
|
|
21
|
+
};
|
package/tests/setup.js
DELETED
|
File without changes
|
|
File without changes
|
|
File without changes
|