gcf-common-lib 0.7.0 → 0.8.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/index.js +22 -86
- package/index.ts +66 -88
- package/package.json +1 -1
- package/utils.js +114 -0
- package/utils.ts +57 -0
package/index.js
CHANGED
|
@@ -36,17 +36,20 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
36
36
|
}
|
|
37
37
|
};
|
|
38
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
-
exports.GcfCommon = void 0;
|
|
39
|
+
exports.GcfCommon = exports.storage = exports.pubSub = void 0;
|
|
40
|
+
var lodash_1 = require("lodash");
|
|
40
41
|
var storage_1 = require("@google-cloud/storage");
|
|
41
42
|
var pubsub_1 = require("@google-cloud/pubsub");
|
|
42
|
-
var
|
|
43
|
+
var utils_1 = require("./utils");
|
|
44
|
+
exports.pubSub = new pubsub_1.PubSub();
|
|
45
|
+
exports.storage = new storage_1.Storage();
|
|
43
46
|
var GcfCommon = /** @class */ (function () {
|
|
44
47
|
function GcfCommon() {
|
|
45
48
|
}
|
|
46
49
|
/**
|
|
47
50
|
*
|
|
48
|
-
* @param {!
|
|
49
|
-
* @param {!
|
|
51
|
+
* @param {!TEvent} event Event payload.
|
|
52
|
+
* @param {!TContext} context Metadata for the event.
|
|
50
53
|
* @param handler
|
|
51
54
|
* @param timeout Seconds
|
|
52
55
|
*/
|
|
@@ -56,7 +59,7 @@ var GcfCommon = /** @class */ (function () {
|
|
|
56
59
|
var _this = this;
|
|
57
60
|
return __generator(this, function (_a) {
|
|
58
61
|
return [2 /*return*/, Promise.race([
|
|
59
|
-
|
|
62
|
+
(0, utils_1.timeoutAfter)(timeout),
|
|
60
63
|
handler(event, context),
|
|
61
64
|
])
|
|
62
65
|
.then(function (res) { return __awaiter(_this, void 0, void 0, function () {
|
|
@@ -73,14 +76,20 @@ var GcfCommon = /** @class */ (function () {
|
|
|
73
76
|
});
|
|
74
77
|
}); })
|
|
75
78
|
.catch(function (err) { return __awaiter(_this, void 0, void 0, function () {
|
|
76
|
-
var fname,
|
|
79
|
+
var fname, response;
|
|
77
80
|
var _a, _b;
|
|
78
81
|
return __generator(this, function (_c) {
|
|
79
82
|
switch (_c.label) {
|
|
80
83
|
case 0:
|
|
81
84
|
fname = (_b = (_a = process === null || process === void 0 ? void 0 : process.env) === null || _a === void 0 ? void 0 : _a.K_SERVICE) !== null && _b !== void 0 ? _b : 'UNKNOWN';
|
|
82
|
-
|
|
83
|
-
|
|
85
|
+
response = {
|
|
86
|
+
error: {
|
|
87
|
+
name: err.name,
|
|
88
|
+
message: "GCF [" + fname + "]: " + err.message,
|
|
89
|
+
stack: err.stack,
|
|
90
|
+
},
|
|
91
|
+
};
|
|
92
|
+
return [4 /*yield*/, this.publish(event, context, response).catch(lodash_1.noop)];
|
|
84
93
|
case 1:
|
|
85
94
|
_c.sent();
|
|
86
95
|
throw err;
|
|
@@ -90,12 +99,6 @@ var GcfCommon = /** @class */ (function () {
|
|
|
90
99
|
});
|
|
91
100
|
});
|
|
92
101
|
};
|
|
93
|
-
/**
|
|
94
|
-
*
|
|
95
|
-
* @param {!Object} event Event payload.
|
|
96
|
-
* @param {!Object} context Metadata for the event.
|
|
97
|
-
* @param json
|
|
98
|
-
*/
|
|
99
102
|
GcfCommon.publish = function (event, context, json) {
|
|
100
103
|
return __awaiter(this, void 0, void 0, function () {
|
|
101
104
|
var topic;
|
|
@@ -113,23 +116,16 @@ var GcfCommon = /** @class */ (function () {
|
|
|
113
116
|
});
|
|
114
117
|
});
|
|
115
118
|
};
|
|
116
|
-
/**
|
|
117
|
-
*
|
|
118
|
-
* @param {!Object} event Event payload.
|
|
119
|
-
* @param {!Object} context Metadata for the event.
|
|
120
|
-
*/
|
|
121
119
|
GcfCommon.getTopic = function (event, context) {
|
|
122
120
|
var _a, _b, _c;
|
|
123
121
|
return __awaiter(this, void 0, void 0, function () {
|
|
124
|
-
var
|
|
122
|
+
var topicName, file, meta, topic;
|
|
125
123
|
return __generator(this, function (_d) {
|
|
126
124
|
switch (_d.label) {
|
|
127
125
|
case 0:
|
|
128
|
-
pubSub = new pubsub_1.PubSub();
|
|
129
126
|
topicName = (_a = event === null || event === void 0 ? void 0 : event.metadata) === null || _a === void 0 ? void 0 : _a.topic;
|
|
130
127
|
if (!(!topicName && ((_b = context === null || context === void 0 ? void 0 : context.resource) === null || _b === void 0 ? void 0 : _b.type) === 'storage#object')) return [3 /*break*/, 2];
|
|
131
|
-
|
|
132
|
-
file = storage.bucket(event.bucket).file(event.name);
|
|
128
|
+
file = exports.storage.bucket(event.bucket).file(event.name);
|
|
133
129
|
return [4 /*yield*/, file.getMetadata()];
|
|
134
130
|
case 1:
|
|
135
131
|
meta = (_d.sent())[0];
|
|
@@ -138,7 +134,7 @@ var GcfCommon = /** @class */ (function () {
|
|
|
138
134
|
_d.label = 2;
|
|
139
135
|
case 2:
|
|
140
136
|
if (!!(0, lodash_1.isEmpty)(topicName)) return [3 /*break*/, 4];
|
|
141
|
-
topic = pubSub.topic(topicName);
|
|
137
|
+
topic = exports.pubSub.topic(topicName);
|
|
142
138
|
return [4 /*yield*/, topic.setMetadata({ labels: { date: topicName.split('__')[1] } })];
|
|
143
139
|
case 3:
|
|
144
140
|
_d.sent();
|
|
@@ -148,23 +144,17 @@ var GcfCommon = /** @class */ (function () {
|
|
|
148
144
|
});
|
|
149
145
|
});
|
|
150
146
|
};
|
|
151
|
-
/**
|
|
152
|
-
*
|
|
153
|
-
* @param {!Object} event Event payload.
|
|
154
|
-
* @param {!Object} context Metadata for the event.
|
|
155
|
-
*/
|
|
156
147
|
GcfCommon.getOptions = function (event, context) {
|
|
157
148
|
var _a, _b, _c, _d, _e;
|
|
158
149
|
return __awaiter(this, void 0, void 0, function () {
|
|
159
|
-
var
|
|
150
|
+
var file, meta;
|
|
160
151
|
return __generator(this, function (_f) {
|
|
161
152
|
switch (_f.label) {
|
|
162
153
|
case 0:
|
|
163
154
|
if (!((_a = event === null || event === void 0 ? void 0 : event.metadata) === null || _a === void 0 ? void 0 : _a.options)) return [3 /*break*/, 1];
|
|
164
155
|
return [2 /*return*/, JSON.parse((_c = (_b = event === null || event === void 0 ? void 0 : event.metadata) === null || _b === void 0 ? void 0 : _b.options) !== null && _c !== void 0 ? _c : '{}')];
|
|
165
156
|
case 1:
|
|
166
|
-
|
|
167
|
-
file = storage.bucket(event.bucket).file(event.name);
|
|
157
|
+
file = exports.storage.bucket(event.bucket).file(event.name);
|
|
168
158
|
return [4 /*yield*/, file.getMetadata()];
|
|
169
159
|
case 2:
|
|
170
160
|
meta = (_f.sent())[0];
|
|
@@ -173,60 +163,6 @@ var GcfCommon = /** @class */ (function () {
|
|
|
173
163
|
});
|
|
174
164
|
});
|
|
175
165
|
};
|
|
176
|
-
/**
|
|
177
|
-
*
|
|
178
|
-
* @param s Google function time limit (max: 9 min)
|
|
179
|
-
*/
|
|
180
|
-
GcfCommon.delay = function (s) {
|
|
181
|
-
if (s === void 0) { s = 540; }
|
|
182
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
183
|
-
return __generator(this, function (_a) {
|
|
184
|
-
return [2 /*return*/, new Promise(function (resolve, reject) {
|
|
185
|
-
setTimeout(function () {
|
|
186
|
-
reject(new Error(s + " seconds timeout exceeded"));
|
|
187
|
-
}, s * 1000);
|
|
188
|
-
})];
|
|
189
|
-
});
|
|
190
|
-
});
|
|
191
|
-
};
|
|
192
|
-
//
|
|
193
|
-
GcfCommon.indexToA1 = function (idx) {
|
|
194
|
-
return this.colNumToA1(idx + 1);
|
|
195
|
-
};
|
|
196
|
-
GcfCommon.colNumToA1 = function (columnNumber) {
|
|
197
|
-
var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
|
198
|
-
// To store result (Excel column name)
|
|
199
|
-
var charIdxArr = [];
|
|
200
|
-
while (columnNumber > 0) {
|
|
201
|
-
// Find remainder
|
|
202
|
-
var rem = columnNumber % chars.length;
|
|
203
|
-
// If remainder is 0, then a
|
|
204
|
-
// 'Z' must be there in output
|
|
205
|
-
if (rem === 0) {
|
|
206
|
-
charIdxArr.push(chars.length - 1);
|
|
207
|
-
columnNumber = Math.floor(columnNumber / chars.length) - 1;
|
|
208
|
-
}
|
|
209
|
-
else { // If remainder is non-zero
|
|
210
|
-
charIdxArr.push(rem - 1);
|
|
211
|
-
columnNumber = Math.floor(columnNumber / chars.length);
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
// Reverse the string and print result
|
|
215
|
-
return charIdxArr.reverse().map(function (n) { return chars[n]; }).join('');
|
|
216
|
-
};
|
|
217
|
-
GcfCommon.A1ToIndex = function (value) {
|
|
218
|
-
return this.A1ToColNum(value) - 1;
|
|
219
|
-
};
|
|
220
|
-
GcfCommon.A1ToColNum = function (value) {
|
|
221
|
-
var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
|
222
|
-
var result = 0;
|
|
223
|
-
// tslint:disable-next-line:prefer-for-of
|
|
224
|
-
for (var i = 0; i < value.length; i++) {
|
|
225
|
-
result *= chars.length;
|
|
226
|
-
result += chars.indexOf(value[i]) + 1;
|
|
227
|
-
}
|
|
228
|
-
return result;
|
|
229
|
-
};
|
|
230
166
|
return GcfCommon;
|
|
231
167
|
}());
|
|
232
168
|
exports.GcfCommon = GcfCommon;
|
package/index.ts
CHANGED
|
@@ -1,19 +1,68 @@
|
|
|
1
|
-
import {File, Storage} from '@google-cloud/storage';
|
|
2
|
-
import {PubSub, Topic} from '@google-cloud/pubsub';
|
|
3
1
|
import {isEmpty, noop} from 'lodash';
|
|
2
|
+
import {File, Storage} from "@google-cloud/storage";
|
|
3
|
+
import {PubSub} from "@google-cloud/pubsub";
|
|
4
|
+
import {timeoutAfter} from "./utils";
|
|
5
|
+
|
|
6
|
+
export type TEvent = {
|
|
7
|
+
bucket: string;
|
|
8
|
+
contentType: string;
|
|
9
|
+
crc32c: string;
|
|
10
|
+
etag: string;
|
|
11
|
+
generation: string;
|
|
12
|
+
id: string;
|
|
13
|
+
kind: string; // 'storage#object'
|
|
14
|
+
md5Hash: string;
|
|
15
|
+
mediaLink: string;
|
|
16
|
+
metadata: { [key: string]: string, topic: string, options?: any };
|
|
17
|
+
metageneration: string;
|
|
18
|
+
name: string;
|
|
19
|
+
selfLink: string;
|
|
20
|
+
size: string;
|
|
21
|
+
storageClass: string;
|
|
22
|
+
timeCreated: string;
|
|
23
|
+
timeStorageClassUpdated: string;
|
|
24
|
+
updated: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export type TContext = {
|
|
28
|
+
eventId: string;
|
|
29
|
+
timestamp: string;
|
|
30
|
+
eventType: string; // 'google.storage.object.finalize'
|
|
31
|
+
resource: {
|
|
32
|
+
service: string; // 'storage.googleapis.com'
|
|
33
|
+
name: string;
|
|
34
|
+
type: string; // 'storage#object'
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export type TResponse = {
|
|
39
|
+
error?: {
|
|
40
|
+
name: string,
|
|
41
|
+
message: string,
|
|
42
|
+
stack: string,
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export const pubSub = new PubSub();
|
|
47
|
+
export const storage = new Storage();
|
|
4
48
|
|
|
5
49
|
export class GcfCommon {
|
|
6
50
|
|
|
7
51
|
/**
|
|
8
52
|
*
|
|
9
|
-
* @param {!
|
|
10
|
-
* @param {!
|
|
53
|
+
* @param {!TEvent} event Event payload.
|
|
54
|
+
* @param {!TContext} context Metadata for the event.
|
|
11
55
|
* @param handler
|
|
12
56
|
* @param timeout Seconds
|
|
13
57
|
*/
|
|
14
|
-
static async process(
|
|
58
|
+
static async process<T>(
|
|
59
|
+
event: TEvent,
|
|
60
|
+
context: TContext,
|
|
61
|
+
handler: (event: TEvent, context: TContext) => Promise<T>,
|
|
62
|
+
timeout = 535,
|
|
63
|
+
) {
|
|
15
64
|
return Promise.race([
|
|
16
|
-
|
|
65
|
+
timeoutAfter(timeout),
|
|
17
66
|
handler(event, context),
|
|
18
67
|
])
|
|
19
68
|
.then(async (res: any) => {
|
|
@@ -22,38 +71,31 @@ export class GcfCommon {
|
|
|
22
71
|
})
|
|
23
72
|
.catch(async (err: Error) => {
|
|
24
73
|
const fname = process?.env?.K_SERVICE ?? 'UNKNOWN';
|
|
25
|
-
const
|
|
26
|
-
|
|
74
|
+
const response: TResponse = {
|
|
75
|
+
error: {
|
|
76
|
+
name: err.name,
|
|
77
|
+
message: `GCF [${fname}]: ${err.message}`,
|
|
78
|
+
stack: err.stack,
|
|
79
|
+
},
|
|
80
|
+
};
|
|
81
|
+
await this.publish(event, context, response).catch(noop);
|
|
27
82
|
throw err;
|
|
28
83
|
})
|
|
29
84
|
;
|
|
30
85
|
}
|
|
31
86
|
|
|
32
|
-
|
|
33
|
-
*
|
|
34
|
-
* @param {!Object} event Event payload.
|
|
35
|
-
* @param {!Object} context Metadata for the event.
|
|
36
|
-
* @param json
|
|
37
|
-
*/
|
|
38
|
-
static async publish(event, context, json) {
|
|
87
|
+
static async publish(event: TEvent, context: TContext, json: TResponse) {
|
|
39
88
|
const topic = await this.getTopic(event, context);
|
|
40
89
|
console.log('publish:', topic?.name, json);
|
|
41
90
|
if (topic) return await topic.publishMessage({json});
|
|
42
91
|
}
|
|
43
92
|
|
|
44
|
-
|
|
45
|
-
*
|
|
46
|
-
* @param {!Object} event Event payload.
|
|
47
|
-
* @param {!Object} context Metadata for the event.
|
|
48
|
-
*/
|
|
49
|
-
static async getTopic(event, context): Promise<Topic | undefined> {
|
|
50
|
-
const pubSub = new PubSub();
|
|
93
|
+
static async getTopic(event: TEvent, context: TContext) {
|
|
51
94
|
|
|
52
95
|
/** t_{GUID}__{YYYY-MM-DD} */
|
|
53
96
|
let topicName: string | undefined = event?.metadata?.topic;
|
|
54
97
|
|
|
55
98
|
if (!topicName && context?.resource?.type === 'storage#object') {
|
|
56
|
-
const storage = new Storage();
|
|
57
99
|
const file: File = storage.bucket(event.bucket).file(event.name);
|
|
58
100
|
const [meta] = await file.getMetadata();
|
|
59
101
|
topicName = meta?.metadata?.topic;
|
|
@@ -67,77 +109,13 @@ export class GcfCommon {
|
|
|
67
109
|
}
|
|
68
110
|
}
|
|
69
111
|
|
|
70
|
-
|
|
71
|
-
*
|
|
72
|
-
* @param {!Object} event Event payload.
|
|
73
|
-
* @param {!Object} context Metadata for the event.
|
|
74
|
-
*/
|
|
75
|
-
static async getOptions(event, context) {
|
|
112
|
+
static async getOptions(event: TEvent, context: TContext) {
|
|
76
113
|
if (event?.metadata?.options) {
|
|
77
114
|
return JSON.parse(event?.metadata?.options ?? '{}');
|
|
78
115
|
} else {
|
|
79
|
-
const storage = new Storage();
|
|
80
116
|
const file: File = storage.bucket(event.bucket).file(event.name);
|
|
81
117
|
const [meta] = await file.getMetadata();
|
|
82
118
|
return JSON.parse(meta?.metadata?.options ?? '{}');
|
|
83
119
|
}
|
|
84
120
|
}
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
*
|
|
88
|
-
* @param s Google function time limit (max: 9 min)
|
|
89
|
-
*/
|
|
90
|
-
static async delay(s: number = 540) {
|
|
91
|
-
return new Promise<undefined>((resolve, reject) => {
|
|
92
|
-
setTimeout(() => {
|
|
93
|
-
reject(new Error(`${s} seconds timeout exceeded`));
|
|
94
|
-
}, s * 1000);
|
|
95
|
-
});
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
//
|
|
99
|
-
|
|
100
|
-
static indexToA1(idx: number) {
|
|
101
|
-
return this.colNumToA1(idx + 1);
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
static colNumToA1(columnNumber: number) {
|
|
105
|
-
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
|
106
|
-
|
|
107
|
-
// To store result (Excel column name)
|
|
108
|
-
const charIdxArr: number[] = [];
|
|
109
|
-
|
|
110
|
-
while (columnNumber > 0) {
|
|
111
|
-
// Find remainder
|
|
112
|
-
const rem = columnNumber % chars.length;
|
|
113
|
-
|
|
114
|
-
// If remainder is 0, then a
|
|
115
|
-
// 'Z' must be there in output
|
|
116
|
-
if (rem === 0) {
|
|
117
|
-
charIdxArr.push(chars.length - 1);
|
|
118
|
-
columnNumber = Math.floor(columnNumber / chars.length) - 1;
|
|
119
|
-
} else { // If remainder is non-zero
|
|
120
|
-
charIdxArr.push(rem - 1);
|
|
121
|
-
columnNumber = Math.floor(columnNumber / chars.length);
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
// Reverse the string and print result
|
|
126
|
-
return charIdxArr.reverse().map((n) => chars[n]).join('');
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
static A1ToIndex(value: string) {
|
|
130
|
-
return this.A1ToColNum(value) - 1;
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
static A1ToColNum(value: string) {
|
|
134
|
-
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
|
135
|
-
let result = 0;
|
|
136
|
-
// tslint:disable-next-line:prefer-for-of
|
|
137
|
-
for (let i = 0; i < value.length; i++) {
|
|
138
|
-
result *= chars.length;
|
|
139
|
-
result += chars.indexOf(value[i]) + 1;
|
|
140
|
-
}
|
|
141
|
-
return result;
|
|
142
|
-
}
|
|
143
121
|
}
|
package/package.json
CHANGED
package/utils.js
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (_) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.A1ToColNum = exports.A1ToIndex = exports.colNumToA1 = exports.indexToA1 = exports.timeoutAfter = void 0;
|
|
40
|
+
/**
|
|
41
|
+
*
|
|
42
|
+
* @param seconds Google function timeout limit (max: 9 min)
|
|
43
|
+
*/
|
|
44
|
+
function timeoutAfter(seconds) {
|
|
45
|
+
if (seconds === void 0) { seconds = 540; }
|
|
46
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
47
|
+
return __generator(this, function (_a) {
|
|
48
|
+
return [2 /*return*/, new Promise(function (resolve, reject) {
|
|
49
|
+
setTimeout(function () {
|
|
50
|
+
reject(new Error(seconds + " seconds timeout exceeded"));
|
|
51
|
+
}, seconds * 1000);
|
|
52
|
+
})];
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
exports.timeoutAfter = timeoutAfter;
|
|
57
|
+
//
|
|
58
|
+
function indexToA1(idx) {
|
|
59
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
60
|
+
return __generator(this, function (_a) {
|
|
61
|
+
return [2 /*return*/, this.colNumToA1(idx + 1)];
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
exports.indexToA1 = indexToA1;
|
|
66
|
+
function colNumToA1(columnNumber) {
|
|
67
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
68
|
+
var chars, charIdxArr, rem;
|
|
69
|
+
return __generator(this, function (_a) {
|
|
70
|
+
chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
|
71
|
+
charIdxArr = [];
|
|
72
|
+
while (columnNumber > 0) {
|
|
73
|
+
rem = columnNumber % chars.length;
|
|
74
|
+
// If remainder is 0, then a
|
|
75
|
+
// 'Z' must be there in output
|
|
76
|
+
if (rem === 0) {
|
|
77
|
+
charIdxArr.push(chars.length - 1);
|
|
78
|
+
columnNumber = Math.floor(columnNumber / chars.length) - 1;
|
|
79
|
+
}
|
|
80
|
+
else { // If remainder is non-zero
|
|
81
|
+
charIdxArr.push(rem - 1);
|
|
82
|
+
columnNumber = Math.floor(columnNumber / chars.length);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
// Reverse the string and print result
|
|
86
|
+
return [2 /*return*/, charIdxArr.reverse().map(function (n) { return chars[n]; }).join('')];
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
exports.colNumToA1 = colNumToA1;
|
|
91
|
+
function A1ToIndex(value) {
|
|
92
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
93
|
+
return __generator(this, function (_a) {
|
|
94
|
+
return [2 /*return*/, this.A1ToColNum(value) - 1];
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
exports.A1ToIndex = A1ToIndex;
|
|
99
|
+
function A1ToColNum(value) {
|
|
100
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
101
|
+
var chars, result, i;
|
|
102
|
+
return __generator(this, function (_a) {
|
|
103
|
+
chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
|
104
|
+
result = 0;
|
|
105
|
+
// tslint:disable-next-line:prefer-for-of
|
|
106
|
+
for (i = 0; i < value.length; i++) {
|
|
107
|
+
result *= chars.length;
|
|
108
|
+
result += chars.indexOf(value[i]) + 1;
|
|
109
|
+
}
|
|
110
|
+
return [2 /*return*/, result];
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
exports.A1ToColNum = A1ToColNum;
|
package/utils.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
*
|
|
3
|
+
* @param seconds Google function timeout limit (max: 9 min)
|
|
4
|
+
*/
|
|
5
|
+
export async function timeoutAfter(seconds: number = 540) {
|
|
6
|
+
return new Promise<undefined>((resolve, reject) => {
|
|
7
|
+
setTimeout(() => {
|
|
8
|
+
reject(new Error(`${seconds} seconds timeout exceeded`));
|
|
9
|
+
}, seconds * 1000);
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
//
|
|
14
|
+
|
|
15
|
+
export async function indexToA1(idx: number) {
|
|
16
|
+
return this.colNumToA1(idx + 1);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export async function colNumToA1(columnNumber: number) {
|
|
20
|
+
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
|
21
|
+
|
|
22
|
+
// To store result (Excel column name)
|
|
23
|
+
const charIdxArr: number[] = [];
|
|
24
|
+
|
|
25
|
+
while (columnNumber > 0) {
|
|
26
|
+
// Find remainder
|
|
27
|
+
const rem = columnNumber % chars.length;
|
|
28
|
+
|
|
29
|
+
// If remainder is 0, then a
|
|
30
|
+
// 'Z' must be there in output
|
|
31
|
+
if (rem === 0) {
|
|
32
|
+
charIdxArr.push(chars.length - 1);
|
|
33
|
+
columnNumber = Math.floor(columnNumber / chars.length) - 1;
|
|
34
|
+
} else { // If remainder is non-zero
|
|
35
|
+
charIdxArr.push(rem - 1);
|
|
36
|
+
columnNumber = Math.floor(columnNumber / chars.length);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Reverse the string and print result
|
|
41
|
+
return charIdxArr.reverse().map((n) => chars[n]).join('');
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export async function A1ToIndex(value: string) {
|
|
45
|
+
return this.A1ToColNum(value) - 1;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export async function A1ToColNum(value: string) {
|
|
49
|
+
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
|
50
|
+
let result = 0;
|
|
51
|
+
// tslint:disable-next-line:prefer-for-of
|
|
52
|
+
for (let i = 0; i < value.length; i++) {
|
|
53
|
+
result *= chars.length;
|
|
54
|
+
result += chars.indexOf(value[i]) + 1;
|
|
55
|
+
}
|
|
56
|
+
return result;
|
|
57
|
+
}
|