forked from XeCycle/pg-template-tag
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsrc.js
More file actions
42 lines (40 loc) · 959 Bytes
/
src.js
File metadata and controls
42 lines (40 loc) · 959 Bytes
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
class SqlLiteral {
constructor(parts, values) {
this._parts = parts;
this._values = values;
}
getText(starting=1) {
return this._parts.reduce((prev, curr, i) => {
var child = this._values[i-1];
var mid;
if (child instanceof SqlLiteral) {
mid = child.getText(starting);
starting += child.values.length;
}
else mid = "$" + (starting++);
return prev+mid+curr;
});
}
get text() {
return this.getText();
}
get values() {
return this._values.reduce((prev, curr) => prev.concat(
curr instanceof SqlLiteral ? curr.values : [curr]
), []);
}
}
export default
function SQLTag(parts, ...values) {
return new SqlLiteral(parts, values);
}
export
function join(array, separator) {
separator = separator || ",";
let parts = [""];
for (let i = 0; i < array.length-1; i++) {
parts.push(separator);
}
parts.push("");
return new SqlLiteral(parts, array);
}