@8ms/helpers 1.3.34 → 1.4.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.
@@ -16,6 +16,7 @@ const getDate_1 = __importDefault(require("../../date/getDate"));
16
16
  const getStringFromStream_1 = __importDefault(require("../../string/getStringFromStream"));
17
17
  const isResponse200_1 = __importDefault(require("../isResponse200"));
18
18
  const cloneDeep_1 = __importDefault(require("lodash/cloneDeep"));
19
+ const isFileExists_1 = __importDefault(require("./isFileExists"));
19
20
  exports.readFileDefault = {
20
21
  body: undefined,
21
22
  now: undefined,
@@ -38,31 +39,40 @@ exports.readFileDefault = {
38
39
  */
39
40
  const readFile = async ({ bucket, key, isJson }) => {
40
41
  let response = (0, cloneDeep_1.default)(exports.readFileDefault);
41
- const today = (0, getToday_1.default)({ setMidnight: false });
42
+ const today = (0, getToday_1.default)({
43
+ setMidnight: false
44
+ });
42
45
  response.now = (0, getUnixTime_1.default)(today);
43
- const { GetObjectCommand } = require('@aws-sdk/client-s3');
44
- const apiResponse = await global.awsS3Client.send(new GetObjectCommand({
45
- Bucket: bucket,
46
- Key: key,
47
- }));
48
- if ((0, isResponse200_1.default)({ apiResponse })) {
49
- let now = today;
50
- let modified = (0, getDate_1.default)({ input: apiResponse.LastModified, setMidnight: false });
51
- // Convert the date to unix
52
- response.modified.unix = (0, getUnixTime_1.default)(modified);
53
- // Compare now to unix
54
- response.modified.vsNow.months = (0, differenceInMonths_1.default)(now, modified);
55
- response.modified.vsNow.days = (0, differenceInDays_1.default)(now, modified);
56
- response.modified.vsNow.hours = (0, differenceInHours_1.default)(now, modified);
57
- response.modified.vsNow.minutes = (0, differenceInMinutes_1.default)(now, modified);
58
- // Compare now to midnight
59
- response.modified.vsMidnight.hours = (0, getHours_1.default)(now);
60
- response.modified.vsMidnight.minutes = (0, getMinutes_1.default)(now);
61
- // Convert stream into a string
62
- response.body = await (0, getStringFromStream_1.default)({ stream: apiResponse.Body });
63
- // If we need to return JSON, parse it. On error return undefined
64
- if (isJson) {
65
- response.body = JSON.parse(response.body) || undefined;
46
+ const doesFileExist = await (0, isFileExists_1.default)({
47
+ bucket,
48
+ key
49
+ });
50
+ // Only fetch the file if it exists
51
+ if (doesFileExist) {
52
+ const { GetObjectCommand } = require('@aws-sdk/client-s3');
53
+ const apiResponse = await global.awsS3Client.send(new GetObjectCommand({
54
+ Bucket: bucket,
55
+ Key: key,
56
+ }));
57
+ if ((0, isResponse200_1.default)({ apiResponse })) {
58
+ let now = today;
59
+ let modified = (0, getDate_1.default)({ input: apiResponse.LastModified, setMidnight: false });
60
+ // Convert the date to unix
61
+ response.modified.unix = (0, getUnixTime_1.default)(modified);
62
+ // Compare now to unix
63
+ response.modified.vsNow.months = (0, differenceInMonths_1.default)(now, modified);
64
+ response.modified.vsNow.days = (0, differenceInDays_1.default)(now, modified);
65
+ response.modified.vsNow.hours = (0, differenceInHours_1.default)(now, modified);
66
+ response.modified.vsNow.minutes = (0, differenceInMinutes_1.default)(now, modified);
67
+ // Compare now to midnight
68
+ response.modified.vsMidnight.hours = (0, getHours_1.default)(now);
69
+ response.modified.vsMidnight.minutes = (0, getMinutes_1.default)(now);
70
+ // Convert stream into a string
71
+ response.body = await (0, getStringFromStream_1.default)({ stream: apiResponse.Body });
72
+ // If we need to return JSON, parse it. On error return undefined
73
+ if (false !== isJson) {
74
+ response.body = JSON.parse(response.body) || undefined;
75
+ }
66
76
  }
67
77
  }
68
78
  return response;
@@ -0,0 +1,10 @@
1
+ type GetCurrency = {
2
+ currency: string;
3
+ input: any;
4
+ };
5
+ /**
6
+ * Use the International number formatting to return the currency value.
7
+ * https://www.freecodecamp.org/news/how-to-format-number-as-currency-in-javascript-one-line-of-code/
8
+ */
9
+ declare const getCurrency: ({ currency, input }: GetCurrency) => string;
10
+ export default getCurrency;
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ /**
4
+ * Use the International number formatting to return the currency value.
5
+ * https://www.freecodecamp.org/news/how-to-format-number-as-currency-in-javascript-one-line-of-code/
6
+ */
7
+ const getCurrency = ({ currency, input }) => {
8
+ const value = Number(input);
9
+ const currencyClean = currency.toUpperCase()
10
+ .trim();
11
+ let formatted = new Intl.NumberFormat('en-GB', {
12
+ style: 'currency',
13
+ currency: currencyClean,
14
+ }).format(value);
15
+ if ('USD' === currencyClean) {
16
+ formatted = formatted.replace('US$', '$');
17
+ }
18
+ return formatted;
19
+ };
20
+ exports.default = getCurrency;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@8ms/helpers",
3
3
  "license": "UNLICENSED",
4
- "version": "1.3.34",
4
+ "version": "1.4.1",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "git+https://github.com/8millionstories-organisation/8ms-helpers-ts.git"