gcf-common-lib 0.5.4 → 0.7.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 +72 -8
- package/index.ts +64 -2
- package/package.json +8 -3
package/index.js
CHANGED
|
@@ -119,34 +119,60 @@ var GcfCommon = /** @class */ (function () {
|
|
|
119
119
|
* @param {!Object} context Metadata for the event.
|
|
120
120
|
*/
|
|
121
121
|
GcfCommon.getTopic = function (event, context) {
|
|
122
|
-
var _a, _b;
|
|
122
|
+
var _a, _b, _c;
|
|
123
123
|
return __awaiter(this, void 0, void 0, function () {
|
|
124
124
|
var pubSub, topicName, storage, file, meta, topic;
|
|
125
|
-
return __generator(this, function (
|
|
126
|
-
switch (
|
|
125
|
+
return __generator(this, function (_d) {
|
|
126
|
+
switch (_d.label) {
|
|
127
127
|
case 0:
|
|
128
128
|
pubSub = new pubsub_1.PubSub();
|
|
129
|
-
|
|
129
|
+
topicName = (_a = event === null || event === void 0 ? void 0 : event.metadata) === null || _a === void 0 ? void 0 : _a.topic;
|
|
130
|
+
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];
|
|
130
131
|
storage = new storage_1.Storage();
|
|
131
132
|
file = storage.bucket(event.bucket).file(event.name);
|
|
132
133
|
return [4 /*yield*/, file.getMetadata()];
|
|
133
134
|
case 1:
|
|
134
|
-
meta = (
|
|
135
|
-
topicName = (
|
|
135
|
+
meta = (_d.sent())[0];
|
|
136
|
+
topicName = (_c = meta === null || meta === void 0 ? void 0 : meta.metadata) === null || _c === void 0 ? void 0 : _c.topic;
|
|
136
137
|
console.log('topic:', topicName);
|
|
137
|
-
|
|
138
|
+
_d.label = 2;
|
|
138
139
|
case 2:
|
|
139
140
|
if (!!(0, lodash_1.isEmpty)(topicName)) return [3 /*break*/, 4];
|
|
140
141
|
topic = pubSub.topic(topicName);
|
|
141
142
|
return [4 /*yield*/, topic.setMetadata({ labels: { date: topicName.split('__')[1] } })];
|
|
142
143
|
case 3:
|
|
143
|
-
|
|
144
|
+
_d.sent();
|
|
144
145
|
return [2 /*return*/, topic];
|
|
145
146
|
case 4: return [2 /*return*/];
|
|
146
147
|
}
|
|
147
148
|
});
|
|
148
149
|
});
|
|
149
150
|
};
|
|
151
|
+
/**
|
|
152
|
+
*
|
|
153
|
+
* @param {!Object} event Event payload.
|
|
154
|
+
* @param {!Object} context Metadata for the event.
|
|
155
|
+
*/
|
|
156
|
+
GcfCommon.getOptions = function (event, context) {
|
|
157
|
+
var _a, _b, _c, _d, _e;
|
|
158
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
159
|
+
var storage, file, meta;
|
|
160
|
+
return __generator(this, function (_f) {
|
|
161
|
+
switch (_f.label) {
|
|
162
|
+
case 0:
|
|
163
|
+
if (!((_a = event === null || event === void 0 ? void 0 : event.metadata) === null || _a === void 0 ? void 0 : _a.options)) return [3 /*break*/, 1];
|
|
164
|
+
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
|
+
case 1:
|
|
166
|
+
storage = new storage_1.Storage();
|
|
167
|
+
file = storage.bucket(event.bucket).file(event.name);
|
|
168
|
+
return [4 /*yield*/, file.getMetadata()];
|
|
169
|
+
case 2:
|
|
170
|
+
meta = (_f.sent())[0];
|
|
171
|
+
return [2 /*return*/, JSON.parse((_e = (_d = meta === null || meta === void 0 ? void 0 : meta.metadata) === null || _d === void 0 ? void 0 : _d.options) !== null && _e !== void 0 ? _e : '{}')];
|
|
172
|
+
}
|
|
173
|
+
});
|
|
174
|
+
});
|
|
175
|
+
};
|
|
150
176
|
/**
|
|
151
177
|
*
|
|
152
178
|
* @param s Google function time limit (max: 9 min)
|
|
@@ -163,6 +189,44 @@ var GcfCommon = /** @class */ (function () {
|
|
|
163
189
|
});
|
|
164
190
|
});
|
|
165
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
|
+
};
|
|
166
230
|
return GcfCommon;
|
|
167
231
|
}());
|
|
168
232
|
exports.GcfCommon = GcfCommon;
|
package/index.ts
CHANGED
|
@@ -50,9 +50,9 @@ export class GcfCommon {
|
|
|
50
50
|
const pubSub = new PubSub();
|
|
51
51
|
|
|
52
52
|
/** t_{GUID}__{YYYY-MM-DD} */
|
|
53
|
-
let topicName: string | undefined;
|
|
53
|
+
let topicName: string | undefined = event?.metadata?.topic;
|
|
54
54
|
|
|
55
|
-
if (context?.resource?.type === 'storage#object') {
|
|
55
|
+
if (!topicName && context?.resource?.type === 'storage#object') {
|
|
56
56
|
const storage = new Storage();
|
|
57
57
|
const file: File = storage.bucket(event.bucket).file(event.name);
|
|
58
58
|
const [meta] = await file.getMetadata();
|
|
@@ -67,6 +67,22 @@ export class GcfCommon {
|
|
|
67
67
|
}
|
|
68
68
|
}
|
|
69
69
|
|
|
70
|
+
/**
|
|
71
|
+
*
|
|
72
|
+
* @param {!Object} event Event payload.
|
|
73
|
+
* @param {!Object} context Metadata for the event.
|
|
74
|
+
*/
|
|
75
|
+
static async getOptions(event, context) {
|
|
76
|
+
if (event?.metadata?.options) {
|
|
77
|
+
return JSON.parse(event?.metadata?.options ?? '{}');
|
|
78
|
+
} else {
|
|
79
|
+
const storage = new Storage();
|
|
80
|
+
const file: File = storage.bucket(event.bucket).file(event.name);
|
|
81
|
+
const [meta] = await file.getMetadata();
|
|
82
|
+
return JSON.parse(meta?.metadata?.options ?? '{}');
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
70
86
|
/**
|
|
71
87
|
*
|
|
72
88
|
* @param s Google function time limit (max: 9 min)
|
|
@@ -78,4 +94,50 @@ export class GcfCommon {
|
|
|
78
94
|
}, s * 1000);
|
|
79
95
|
});
|
|
80
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
|
+
}
|
|
81
143
|
}
|
package/package.json
CHANGED
|
@@ -1,8 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "gcf-common-lib",
|
|
3
3
|
"description": "",
|
|
4
|
-
"version": "0.
|
|
5
|
-
"publishConfig": {
|
|
4
|
+
"version": "0.7.0",
|
|
5
|
+
"publishConfig": {
|
|
6
|
+
"access": "public",
|
|
7
|
+
"branches": [
|
|
8
|
+
"master"
|
|
9
|
+
]
|
|
10
|
+
},
|
|
6
11
|
"engines": {},
|
|
7
12
|
"scripts": {
|
|
8
13
|
"test": "echo \"Error: no test specified\" || exit 0"
|
|
@@ -13,7 +18,7 @@
|
|
|
13
18
|
},
|
|
14
19
|
"dependencies": {
|
|
15
20
|
"@google-cloud/pubsub": "^2.18.4",
|
|
16
|
-
"@google-cloud/storage": "^5.
|
|
21
|
+
"@google-cloud/storage": "^5.17.0",
|
|
17
22
|
"lodash": "^4.17.21"
|
|
18
23
|
},
|
|
19
24
|
"devDependencies": {
|