@8ms/helpers 1.10.0 → 1.12.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/date/getDate.js CHANGED
@@ -20,13 +20,14 @@ const getDate = (props) => {
20
20
  // Convert the input into string
21
21
  const dateString = props.input.toString();
22
22
  const regexes = {
23
- ymdNoDash: new RegExp('^[0-9]{8}$'),
24
23
  ymdDash: new RegExp('^[0-9]{4}-[0-9]{2}-[0-9]{2}$'),
24
+ ymdNumber: new RegExp('^[0-9]{8}$'),
25
25
  ymdHisDash: new RegExp('^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}$'),
26
+ ymdHisNumber: new RegExp('^[0-9]{14}$'),
26
27
  };
27
28
  // console.log('regexes.ymdHisDash.test(dateString) ',regexes.ymdHisDash.test(dateString));
28
29
  // 20220412
29
- if (regexes.ymdNoDash.test(dateString)) {
30
+ if (regexes.ymdNumber.test(dateString)) {
30
31
  const year = Number(dateString.substring(0, 4));
31
32
  const month = Number(dateString.substring(4, 6)) - 1;
32
33
  const day = Number(dateString.substring(6, 8));
@@ -76,6 +77,25 @@ const getDate = (props) => {
76
77
  // Resolve timezone offset
77
78
  date = (0, date_fns_1.addMinutes)(date, date.getTimezoneOffset() * -1);
78
79
  }
80
+ // 20220412010101
81
+ else if (regexes.ymdHisNumber.test(dateString)) {
82
+ const year = Number(dateString.substring(0, 4));
83
+ const month = Number(dateString.substring(4, 6)) - 1;
84
+ const day = Number(dateString.substring(6, 8));
85
+ const hours = Number(dateString.substring(8, 10));
86
+ const minutes = Number(dateString.substring(10, 12));
87
+ const seconds = Number(dateString.substring(12, 14));
88
+ date = new Date();
89
+ // Reset the date to the 1st initially at 1am
90
+ date.setDate(1);
91
+ date.setHours(1, 0, 0, 0);
92
+ date.setFullYear(year);
93
+ date.setMonth(month);
94
+ date.setDate(day);
95
+ date.setHours(hours, minutes, seconds);
96
+ // Resolve timezone offset
97
+ date = (0, date_fns_1.addMinutes)(date, date.getTimezoneOffset() * -1);
98
+ }
79
99
  // Unsure date format, try anything
80
100
  else {
81
101
  date = new Date(dateString);
@@ -1,8 +1,9 @@
1
1
  type GetClient = {
2
+ location?: string;
2
3
  projectId?: string;
3
4
  };
4
5
  /**
5
6
  * Create a new client if the project id differs from the global.
6
7
  */
7
- declare const getClient: ({ projectId }: GetClient) => any;
8
+ declare const getClient: (props: GetClient) => any;
8
9
  export default getClient;
@@ -3,14 +3,14 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  /**
4
4
  * Create a new client if the project id differs from the global.
5
5
  */
6
- const getClient = ({ projectId }) => {
6
+ const getClient = (props) => {
7
7
  let client = global.googleBigQueryClient;
8
- if (undefined !== projectId && projectId !== global.googleBigQueryDefaultProjectId) {
8
+ if (props?.projectId && props?.projectId !== global.googleBigQueryDefaultProjectId) {
9
9
  const { BigQuery } = require('@google-cloud/bigquery');
10
10
  client = new BigQuery({
11
11
  credentials: global.googleBigQueryDefault.credentials,
12
- location: "europe-west2",
13
- projectId: projectId,
12
+ location: props?.location || "europe-west2",
13
+ projectId: props?.projectId,
14
14
  });
15
15
  }
16
16
  return client;
@@ -0,0 +1,12 @@
1
+ type GetTables = {
2
+ datasetId?: string;
3
+ projectId?: string;
4
+ };
5
+ type Tables = {
6
+ id: string;
7
+ };
8
+ /**
9
+ * Retrieve all the Tables from a given Project/Dataset.
10
+ */
11
+ declare const getTables: (props: GetTables) => Promise<Tables[]>;
12
+ export default getTables;
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const getClient_1 = __importDefault(require("./getClient"));
7
+ /**
8
+ * Retrieve all the Tables from a given Project/Dataset.
9
+ */
10
+ const getTables = async (props) => {
11
+ let response = [];
12
+ const client = (0, getClient_1.default)({
13
+ projectId: props.projectId,
14
+ });
15
+ const tables = await client.dataset(props.datasetId).getTables();
16
+ if (tables?.[0]) {
17
+ for (let i = 0; i < tables[0].length; i++) {
18
+ response.push({
19
+ id: tables[0][i].id,
20
+ });
21
+ }
22
+ }
23
+ return response;
24
+ };
25
+ exports.default = getTables;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@8ms/helpers",
3
3
  "license": "UNLICENSED",
4
- "version": "1.10.0",
4
+ "version": "1.12.0",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "git+https://github.com/8millionstories-organisation/8ms-helpers-ts.git"