forked from grahamearley/FirestoreGoogleAppsScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtil.js
More file actions
59 lines (50 loc) · 2.02 KB
/
Util.js
File metadata and controls
59 lines (50 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/* eslint no-unused-vars: ["error", { "varsIgnorePattern": "_" }] */
/* globals UrlFetchApp, Utilities */
// RegEx test for root path references. Groups relative path for extraction.
var regexPath_ = /^projects\/.+?\/databases\/\(default\)\/documents\/(.+\/.+)$/
// RegEx test for testing for binary data by checking for non-printable characters.
// Parsing strings for binary data is completely dependent on the data being sent over.
var regexBinary_ = /[\x00-\x08\x0E-\x1F]/ // eslint-disable-line no-control-regex
// Assumes n is a Number.
function isInt_ (n) {
return n % 1 === 0
}
function isNumeric_ (val) {
return Number(parseFloat(val)) === val
}
function base64EncodeSafe_ (string) {
const encoded = Utilities.base64EncodeWebSafe(string)
return encoded.replace(/=/g, '')
}
function fetchObject_ (url, options) {
const response = UrlFetchApp.fetch(url, options)
const responseObj = JSON.parse(response.getContentText())
checkForError_(responseObj)
return responseObj
}
function checkForError_ (responseObj) {
if (responseObj['error']) {
throw new Error(responseObj['error']['message'])
}
if (Array.isArray(responseObj) && responseObj.length && responseObj[0]['error']) {
throw new Error(responseObj[0]['error']['message'])
}
}
function getCollectionFromPath_ (path) {
return getColDocFromPath_(path, false)
}
function getDocumentFromPath_ (path) {
return getColDocFromPath_(path, true)
}
function getColDocFromPath_ (path, isDocument) {
// Path defaults to empty string if it doesn't exist. Remove insignificant slashes.
const splitPath = (path || '').split('/').filter(function (p) {
return p
})
const len = splitPath.length
// Set item path to document if isDocument, otherwise set to collection if exists.
// This works because path is always in the format of "collection/document/collection/document/etc.."
const item = len && len & 1 ^ isDocument ? splitPath.splice(len - 1, 1)[0] : ''
// Remainder of path is in splitPath. Put back together and return.
return [splitPath.join('/'), item]
}