forked from bergur/pg-parameterize
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
38 lines (30 loc) · 794 Bytes
/
index.js
File metadata and controls
38 lines (30 loc) · 794 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
'use strict';
const ordinal = function(string) {
let index = 1;
while(string.indexOf('?') !== -1) {
string = string.replace('?','$'+index);
index++;
}
return string;
}
const flatten = function(arr) {
return arr.reduce(function(flattened,item) {
item.forEach(function(value) {
flattened.push(value);
});
return flattened;
},[])
}
const tuple = function(arr, ordinal) {
const tuple = arr.map(function(item) {
return '(' + item.map(value => '?').join(',') +')';
}).join(',');
if (!ordinal) {
return tuple;
} else {
return this.ordinal(tuple);
}
}
exports.ordinal = ordinal;
exports.flatten = flatten;
exports.tuple = tuple;