This repository has been archived by the owner on Sep 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
index.js
156 lines (131 loc) · 2.98 KB
/
index.js
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/**
* Module dependencies.
*/
var assert = require('assert');
var fs = require('fs');
/**
* Reserved word map.
*/
var txt = fs.readFileSync(__dirname + '/reserved.txt', 'utf8');
var reserved = txt.split('\n').reduce(function(map, word){
map[word.toLowerCase()] = true;
return map;
}, {});
/**
* Expose `format()`.
*/
exports = module.exports = format;
/**
* Format a string.
*
* @param {String} fmt
* @param {Mixed} ...
* @return {String}
* @api public
*/
function format(fmt) {
var i = 1;
var args = arguments;
return fmt.replace(/%([%sILQ])/g, function(_, type){
if ('%' == type) return '%';
var arg = args[i++];
switch (type) {
case 's': return exports.string(arg);
case 'I': return exports.ident(arg);
case 'L': return exports.literal(arg);
case 'Q': return exports.dollarQuotedString(arg);
}
});
}
/**
* Format as string.
*
* @param {Mixed} val
* @return {String}
* @api public
*/
exports.string = function(val){
return null == val ? '' : String(val);
};
/**
* Dollar-Quoted String Constants
*/
var randomTags = [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'g', 'j', 'k',
'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't'];
/**
* produces a random number from a given range
*
* @param {Number} start start of random numbers range
* @param {Number} end end of random numbers range (inclusive)
* @return {Number}
* @api private
*/
function random(start, end) {
var range = end - start;
return Math.floor((Math.random() * range) + start);
}
/**
* Format as dollar quoted string.
* see: http://www.postgresql.org/docs/8.3/interactive/sql-syntax-lexical.html#SQL-SYNTAX-DOLLAR-QUOTING
*
* @param {Mixed} val
* @return {String}
* @api public
*/
exports.dollarQuotedString = function(val) {
if (val === undefined || val === null || val === '') return '';
var randomTag = '$'+ randomTags[ random(0, randomTags.length) ] +'$';
return randomTag + val + randomTag;
}
/**
* Format as identifier.
*
* @param {Mixed} val
* @return {String}
* @api public
*/
exports.ident = function(val){
assert(null != val, 'identifier required');
return validIdent(val) ? val : quoteIdent(val);
};
/**
* Format as literal.
*
* @param {Mixed} val
* @return {String}
* @api public
*/
exports.literal = function(val){
if (null == val) return 'NULL';
if (Array.isArray(val)) {
var vals = val.map(exports.literal)
return "(" + vals.join(", ") + ")"
}
var backslash = ~val.indexOf('\\');
var prefix = backslash ? 'E' : '';
val = val.replace(/'/g, "''");
val = val.replace(/\\/g, '\\\\');
return prefix + "'" + val + "'";
};
/**
* Check if `id` is a valid unquoted identifier.
*
* @param {String} id
* @return {Boolean}
* @api private
*/
function validIdent(id) {
if (reserved[id]) return false;
return /^[a-z_][a-z0-9_$]*$/i.test(id);
}
/**
* Quote identifier.
*
* @param {String} id
* @return {String}
* @api private
*/
function quoteIdent(id) {
id = id.replace(/"/g, '""');
return '"' + id + '"';
}