-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.js
executable file
·92 lines (84 loc) · 2.13 KB
/
lib.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
/**
*
* the lib global
* @class lib
* @singleton
*/
var lib = {
UNKNOWN: 0,
BROWSER: 1,
NODE: 2,
WORKER: 3,
NODE_WEBKIT: 4,
env: 0, //takes value from above enumeration
/**
* converts any javascript variable to its JSON representation
* @param {Object} obj
* @returns {String}
* */
encode: function(obj, replacer, space) {
return JSON.stringify(obj, replacer, space);
},
/**
* parses a JSON string to its object representation
* @param {String} str
* @param {Boolean} safe if true, will not throw exception
* @returns {Object}
*/
decode: function(str, safe) {
try {
return JSON.parse(str);
} catch (e) {
if (safe !== true) throw e;
return {};
}
},
capitalize:function(string, all) {
if(all) return string.toUpperCase();
return string.charAt(0).toUpperCase() + string.slice(1);
},
/**
* The empty function, useful for undefined callbacks etc
* @returns {undefined}
*/
emptyFn: function() {},
callServer: function(opts) {
var request = require('request');
var opts = opts || {},
options = {};
options.url = opts.url || null,
options.method = opts.method || 'GET';
if (options.method === 'POST') {
var data = opts.data;
var headers = {
'User-Agent': 'request'
}
_.extend(options, {
headers: headers,
body: data,
json: true
});
}
request(options, function(error, response, body) {
if (!error && response.statusCode === 200) {
opts.success(body);
}
});
}
};
(function(root, factory) {
var env;
if (typeof window != 'undefined')
if (typeof process == 'undefined') env = lib.BROWSER;
else env = lib.NODE;
else if (typeof process != 'undefined') env = lib.NODE;
else if (typeof WorkerGlobalScope != 'undefined') env = lib.WORKER;
else env = lib.UNKNOWN;
if (env == lib.NODE) {
module.exports = factory(root, env);
} else factory(root, env);
})(this, function(root, env) {
lib.env = env;
lib.root = root;
return lib;
});