-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.js
146 lines (114 loc) · 3.29 KB
/
util.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
/**
* @author Marenin Alex
* October 2013
*/
var crypto = require( 'crypto' );
exports.CONFIG_DIR = './config/';
exports.noop = function(){};
/**
* @param {string} route
* @param {Object} data
* @return string
*/
exports.format = function( route, data ){
var placeholders = route.match( /:\w+/g ) || [],
res = route,
i;
for ( i = 0; i < placeholders.length; i++ )
res = res.replace( new RegExp(placeholders[i], 'g'), data[placeholders[i].substr(1)] );
return res;
};
exports.formatString = function( format, data ){
var placeholders = format.match( /\{\{\w+\}\}/g ) || [],
res = format,
i;
for ( i = 0; i < placeholders.length; i++ )
res = res.replace( new RegExp(placeholders[i], 'g'), data[placeholders[i].replace(/[\{\}]/g, '') || ''] );
return res;
};
/**
* @param {Object|null} destination
* @param {Object} source
* @return Object
*/
exports.mixin = function mixin( destination, source ){
var res = destination ? destination : {};
if ( source )
for ( var k in source ) if ( source.hasOwnProperty(k) )
res[k] = source[k];
return res;
};
/**
* @param {Object} original
* @param {Object} newSample
* @return Object|null
*/
exports.objectDiff = function( original, newSample ){
var diff = {},
count = 0;
for ( var k in newSample ) if ( newSample.hasOwnProperty(k) )
if ( original[k] !== newSample[k] ){
diff[k] = newSample[k];
count ++;
}
return count ? diff : false;
};
/**
* @param {string} configName
* @param {string?} configDir
* @return Object
*/
exports.getConfig = function( configName, configDir ){
var dir = configDir || exports.CONFIG_DIR,
common = requireConfig( 'common' );
return exports.mixin( common, requireConfig(configName) );
function requireConfig( name ){
var conf = require( dir + name + '.js' );
return conf._extends
? exports.mixin( requireConfig(conf._extends), conf )
: conf;
}
};
/**
* @param {long} ts
* @param {string?} delimiter
* @returns {string}
*/
exports.timestampToDate = function( ts, delimiter ){
var d = new Date( ts ),
day = makeTwoDigit( d.getDate() ),
month = makeTwoDigit( d.getMonth() + 1 ),
year = d.getFullYear(),
hour = makeTwoDigit( d.getHours() ),
minute = makeTwoDigit( d.getMinutes() ),
del = delimiter || '.';
return year + del + month + del + day + ' ' + hour + ':' + minute;
};
/**
* @param {string} password
* @returns {string}
*/
exports.passwordHash = function( password ){
return crypto.createHash( 'md5' ).update(
crypto.createHash( 'sha1' ).update( password ).digest( 'hex' ) + 'salt'
).digest( 'hex' );
};
exports.getRemoteIpFromRequest = function( req ){
var ip = req.ip;
if ( ip == '127.0.0.1' )
return false;
else
return ip;
};
exports.trim = function( val ){
return String( val ).trim();
};
exports.generateId = function( length ){
var p1 = cutRight( Math.random() * 100000, parseInt(length / 2) ),
p2 = cutRight( Date.now() * (Number(p1[0]) || 1), parseInt(length / 2) );
return p1 + p2;
};
function cutRight( str, count ){
var s = String( str );
return s.substring( s.length - count );
}