| 1 | 1 | var fs = require('fs'); |
| 2 | 1 | var path = require('path'); |
| 3 | 1 | var util = require('util'); |
| 4 | 1 | var crypto = require('crypto'); |
| 5 | 1 | var net = require('net'); |
| 6 | | |
| 7 | | /** |
| 8 | | * 由于非常依赖promise,所以将promise设置为全局变量 |
| 9 | | * @type {[type]} |
| 10 | | */ |
| 11 | 1 | global.Promise = require('es6-promise').Promise; |
| 12 | | |
| 13 | | /** |
| 14 | | * 动态创建一个类 |
| 15 | | * 提供了继承、扩展、调用父级别方法等方法 |
| 16 | | * @return {[type]} [description] |
| 17 | | */ |
| 18 | 1 | global.Class = function (prop, superCls) { |
| 19 | 4 | 'use strict'; |
| 20 | 4 | var cls = function () { |
| 21 | 4 | function T(args) { |
| 22 | 4 | for(var name in cls.__prop){ |
| 23 | 10 | var val = cls.__prop[name]; |
| 24 | 10 | this[name] = isObject(val) ? extend({}, val) : val; |
| 25 | | } |
| 26 | | //自动执行init方法 |
| 27 | 4 | if(isFunction(this.init)){ |
| 28 | | //获取init返回值,如果返回一个promise,可以让后续执行在then之后 |
| 29 | 4 | this.__initReturn = this.init.apply(this, args); |
| 30 | | } |
| 31 | 4 | return this; |
| 32 | | } |
| 33 | 4 | T.prototype = cls.prototype; |
| 34 | 4 | T.constructor = cls; |
| 35 | 4 | return new T(arguments); |
| 36 | | }; |
| 37 | | //类的属性,不放在原型上,实例化的时候调用 |
| 38 | 4 | cls.__prop = {}; |
| 39 | 4 | cls.extend = function(prop){ |
| 40 | 3 | if (isFunction(prop)) { |
| 41 | 1 | prop = prop(); |
| 42 | | } |
| 43 | 3 | if (isObject(prop)) { |
| 44 | 3 | for(var name in prop){ |
| 45 | 16 | var val = prop[name]; |
| 46 | 16 | if (isFunction(val)) { |
| 47 | 10 | this.prototype[name] = val; |
| 48 | | }else{ |
| 49 | 6 | cls.__prop[name] = isObject(val) ? extend({}, val) : val; |
| 50 | | } |
| 51 | | } |
| 52 | | } |
| 53 | 3 | return this; |
| 54 | | }; |
| 55 | 4 | cls.inherits = function(superCls){ |
| 56 | 3 | util.inherits(this, superCls); |
| 57 | | //将父级的属性复制到当前类上 |
| 58 | 3 | extend(cls.__prop, superCls.__prop); |
| 59 | 3 | return this; |
| 60 | | }; |
| 61 | 4 | if (superCls === true && isFunction(prop)) { |
| 62 | 1 | superCls = prop; |
| 63 | 1 | prop = undefined; |
| 64 | | } |
| 65 | 4 | if (isFunction(superCls)) { |
| 66 | 3 | cls.inherits(superCls); |
| 67 | | } |
| 68 | | //调用父级方法 |
| 69 | 4 | cls.prototype.super = cls.prototype.super_ = function(name, data){ |
| 70 | | //如果当前类没有这个方法,则直接返回。 |
| 71 | | //用于在a方法调用父级的b方法 |
| 72 | 6 | if (!this[name]) { |
| 73 | 1 | return; |
| 74 | | } |
| 75 | 5 | var super_ = this.constructor.super_; |
| 76 | | //如果父级没有这个方法,那么直接返回 |
| 77 | 5 | if (!isFunction(super_.prototype[name])) { |
| 78 | 1 | return; |
| 79 | | } |
| 80 | | //如果参数不是数组,自动转为数组 |
| 81 | 4 | if (!isArray(data)) { |
| 82 | 4 | data = [data]; |
| 83 | | } |
| 84 | 4 | while(1){ |
| 85 | 5 | if (this[name] === super_.prototype[name] && super_.super_) { |
| 86 | 1 | super_ = super_.super_; |
| 87 | | }else{ |
| 88 | 4 | break; |
| 89 | | } |
| 90 | | } |
| 91 | 4 | var method = super_.prototype[name]; |
| 92 | 4 | delete super_.prototype[name]; |
| 93 | 4 | var ret = method.apply(this, data); |
| 94 | 4 | super_.prototype[name] = method; |
| 95 | 4 | return ret; |
| 96 | | }; |
| 97 | 4 | if (prop) { |
| 98 | 3 | cls.extend(prop); |
| 99 | | } |
| 100 | 4 | return cls; |
| 101 | | }; |
| 102 | | /** |
| 103 | | * extend, from jquery,具有深度复制功能 |
| 104 | | * @return {[type]} [description] |
| 105 | | */ |
| 106 | 1 | global.extend = function(){ |
| 107 | 23 | 'use strict'; |
| 108 | 23 | var args = [].slice.call(arguments); |
| 109 | 23 | var deep = true; |
| 110 | 23 | var target = args.shift(); |
| 111 | 23 | if (isBoolean(target)) { |
| 112 | 8 | deep = target; |
| 113 | 8 | target = args.shift(); |
| 114 | | } |
| 115 | 23 | target = target || {}; |
| 116 | 23 | var length = args.length; |
| 117 | 23 | var options, name, src, copy, copyAsArray, clone; |
| 118 | 23 | for(var i = 0; i < length; i++){ |
| 119 | 23 | options = args[i] || {}; |
| 120 | 23 | if (isFunction(options)) { |
| 121 | 1 | options = options(); |
| 122 | | } |
| 123 | 23 | for(name in options){ |
| 124 | 34 | src = target[name]; |
| 125 | 34 | copy = options[name]; |
| 126 | 34 | if (src === copy) { |
| 127 | 2 | continue; |
| 128 | | } |
| 129 | 32 | if (deep && copy && (isObject(copy) || (copyAsArray = isArray(copy) ))) { |
| 130 | 6 | if (copyAsArray) { |
| 131 | 1 | copyAsArray = false; |
| 132 | 1 | clone = src && isArray(src) ? src : []; |
| 133 | | }else{ |
| 134 | 5 | clone = src && isObject(src) ? src : {}; |
| 135 | | } |
| 136 | 6 | target[name] = extend(deep, clone, copy); |
| 137 | 26 | }else if (copy !== undefined) { |
| 138 | 26 | target[name] = copy; |
| 139 | | } |
| 140 | | } |
| 141 | | } |
| 142 | 23 | return target; |
| 143 | | }; |
| 144 | | |
| 145 | | |
| 146 | | //Object上toString方法 |
| 147 | 1 | var toString = Object.prototype.toString; |
| 148 | | |
| 149 | | /** |
| 150 | | * 是否是boolean |
| 151 | | * @param {[type]} obj |
| 152 | | * @return {Boolean} |
| 153 | | */ |
| 154 | 1 | global.isBoolean = function(obj){ |
| 155 | 34 | 'use strict'; |
| 156 | 34 | return toString.call(obj) === '[object Boolean]'; |
| 157 | | }; |
| 158 | | /** |
| 159 | | * 是否是数字 |
| 160 | | * @param {[type]} obj [description] |
| 161 | | * @return {Boolean} [description] |
| 162 | | */ |
| 163 | 1 | global.isNumber = function(obj){ |
| 164 | 18 | 'use strict'; |
| 165 | 18 | return toString.call(obj) === '[object Number]'; |
| 166 | | }; |
| 167 | | /** |
| 168 | | * 是否是个对象 |
| 169 | | * @param {[type]} obj [description] |
| 170 | | * @return {Boolean} [description] |
| 171 | | */ |
| 172 | 1 | global.isObject = function(obj){ |
| 173 | 66 | 'use strict'; |
| 174 | 66 | return toString.call(obj) === '[object Object]'; |
| 175 | | }; |
| 176 | | /** |
| 177 | | * 是否是字符串 |
| 178 | | * @param {[type]} obj [description] |
| 179 | | * @return {Boolean} [description] |
| 180 | | */ |
| 181 | 1 | global.isString = function(obj){ |
| 182 | 12 | 'use strict'; |
| 183 | 12 | return toString.call(obj) === '[object String]'; |
| 184 | | }; |
| 185 | | /** |
| 186 | | * 是否是个function |
| 187 | | * @param {[type]} obj [description] |
| 188 | | * @return {Boolean} [description] |
| 189 | | */ |
| 190 | 1 | global.isFunction = function(obj){ |
| 191 | 65 | 'use strict'; |
| 192 | 65 | return typeof obj === 'function'; |
| 193 | | }; |
| 194 | | /** |
| 195 | | * 是否是日期 |
| 196 | | * @return {Boolean} [description] |
| 197 | | */ |
| 198 | 1 | global.isDate = function(obj){ |
| 199 | 2 | 'use strict'; |
| 200 | 2 | return util.isDate(obj); |
| 201 | | }; |
| 202 | | /** |
| 203 | | * 是否是正则 |
| 204 | | * @param {[type]} reg [description] |
| 205 | | * @return {Boolean} [description] |
| 206 | | */ |
| 207 | 1 | global.isRegexp = function(obj){ |
| 208 | 2 | 'use strict'; |
| 209 | 2 | return util.isRegExp(obj); |
| 210 | | }; |
| 211 | | /** |
| 212 | | * 是否是个错误 |
| 213 | | * @param {[type]} obj [description] |
| 214 | | * @return {Boolean} [description] |
| 215 | | */ |
| 216 | 1 | global.isError = function(obj){ |
| 217 | 2 | 'use strict'; |
| 218 | 2 | return util.isError(obj); |
| 219 | | }; |
| 220 | | /** |
| 221 | | * 判断对象是否为空 |
| 222 | | * @param {[type]} obj |
| 223 | | * @return {Boolean} |
| 224 | | */ |
| 225 | 1 | global.isEmpty = function(obj){ |
| 226 | 12 | 'use strict'; |
| 227 | 12 | if (isObject(obj)) { |
| 228 | 2 | var key; |
| 229 | 2 | for(key in obj){ |
| 230 | 1 | return false; |
| 231 | | } |
| 232 | 1 | return true; |
| 233 | 10 | }else if (isArray(obj)) { |
| 234 | 1 | return obj.length === 0; |
| 235 | 9 | }else if (isString(obj)) { |
| 236 | 2 | return obj.length === 0; |
| 237 | 7 | }else if (isNumber(obj)) { |
| 238 | 2 | return obj === 0; |
| 239 | 5 | }else if (obj === null || obj === undefined) { |
| 240 | 2 | return true; |
| 241 | 3 | }else if (isBoolean(obj)) { |
| 242 | 2 | return !obj; |
| 243 | | } |
| 244 | 1 | return false; |
| 245 | | }; |
| 246 | | /** |
| 247 | | * 是否是个标量 |
| 248 | | * @param {[type]} obj [description] |
| 249 | | * @return {Boolean} [description] |
| 250 | | */ |
| 251 | 1 | global.isScalar = function(obj){ |
| 252 | 1 | 'use strict'; |
| 253 | 1 | return isBoolean(obj) || isNumber(obj) || isString(obj); |
| 254 | | }; |
| 255 | | /** |
| 256 | | * 是否是个数组 |
| 257 | | * @type {Boolean} |
| 258 | | */ |
| 259 | 1 | global.isArray = Array.isArray; |
| 260 | | /** |
| 261 | | * 是否是IP |
| 262 | | * @type {Boolean} |
| 263 | | */ |
| 264 | 1 | global.isIP = net.isIP; |
| 265 | 1 | global.isIP4 = net.isIP4; |
| 266 | 1 | global.isIP6 = net.isIP6; |
| 267 | | /** |
| 268 | | * 是否是个文件 |
| 269 | | * @param {[type]} p [description] |
| 270 | | * @return {Boolean} [description] |
| 271 | | */ |
| 272 | 1 | global.isFile = function(p){ |
| 273 | 4 | 'use strict'; |
| 274 | 4 | if (!fs.existsSync(p)) { |
| 275 | 2 | return false; |
| 276 | | } |
| 277 | 2 | var stats = fs.statSync(p); |
| 278 | 2 | return stats.isFile(); |
| 279 | | }; |
| 280 | | /** |
| 281 | | * 是否是个目录 |
| 282 | | * @param {[type]} p [description] |
| 283 | | * @return {Boolean} [description] |
| 284 | | */ |
| 285 | 1 | global.isDir = function(p){ |
| 286 | 3 | 'use strict'; |
| 287 | 3 | if (!fs.existsSync(p)) { |
| 288 | 1 | return false; |
| 289 | | } |
| 290 | 2 | var stats = fs.statSync(p); |
| 291 | 2 | return stats.isDirectory(); |
| 292 | | }; |
| 293 | | /** |
| 294 | | * 是否是buffer |
| 295 | | * @type {Boolean} |
| 296 | | */ |
| 297 | 1 | global.isBuffer = Buffer.isBuffer; |
| 298 | | /** |
| 299 | | * 是否是个数字的字符串 |
| 300 | | * @param {[type]} obj [description] |
| 301 | | * @return {Boolean} [description] |
| 302 | | */ |
| 303 | 1 | var numberReg = /^((\d*\.?\d*(?:e[+-]?\d*(?:\d?\.?|\.?\d?)\d*)?)|(0[0-7]+)|(0x[0-9a-f]+))$/i; |
| 304 | 1 | global.isNumberString = function(obj){ |
| 305 | 3 | 'use strict'; |
| 306 | 3 | return numberReg.test(obj); |
| 307 | | }; |
| 308 | | /** |
| 309 | | * 判断是否是个promise |
| 310 | | * @param {[type]} obj [description] |
| 311 | | * @return {Boolean} [description] |
| 312 | | */ |
| 313 | 1 | global.isPromise = function(obj){ |
| 314 | 12 | 'use strict'; |
| 315 | 12 | return !!(obj && typeof obj.then === 'function'); |
| 316 | | }; |
| 317 | | |
| 318 | | /** |
| 319 | | * 判断一个文件或者目录是否可写 |
| 320 | | * @param {[type]} p [description] |
| 321 | | * @return {Boolean} [description] |
| 322 | | */ |
| 323 | 1 | global.isWritable = function(p){ |
| 324 | 3 | 'use strict'; |
| 325 | 3 | if (!fs.existsSync(p)) { |
| 326 | 1 | return false; |
| 327 | | } |
| 328 | 2 | var stats = fs.statSync(p); |
| 329 | 2 | var mode = stats.mode; |
| 330 | 2 | var uid = process.getuid ? process.getuid() : 0; |
| 331 | 2 | var gid = process.getgid ? process.getgid() : 0; |
| 332 | 2 | var owner = uid === stats.uid; |
| 333 | 2 | var group = gid === stats.gid; |
| 334 | 2 | return !!(owner && (mode & parseInt('00200', 8)) || |
| 335 | | group && (mode & parseInt('00020', 8)) || |
| 336 | | (mode & parseInt('00002', 8))); |
| 337 | | }; |
| 338 | | |
| 339 | | /** |
| 340 | | * 递归创建目录,同步模式 |
| 341 | | * @param {[type]} p [description] |
| 342 | | * @param {[type]} mode [description] |
| 343 | | * @return {[type]} [description] |
| 344 | | */ |
| 345 | 1 | global.mkdir = function(p, mode){ |
| 346 | 1 | 'use strict'; |
| 347 | 1 | mode = mode || '0777'; |
| 348 | 1 | if (fs.existsSync(p)) { |
| 349 | 0 | chmod(p, mode); |
| 350 | 0 | return true; |
| 351 | | } |
| 352 | 1 | var pp = path.dirname(p); |
| 353 | 1 | if (fs.existsSync(pp)) { |
| 354 | 1 | fs.mkdirSync(p, mode); |
| 355 | | }else{ |
| 356 | 0 | mkdir(pp, mode); |
| 357 | 0 | mkdir(p, mode); |
| 358 | | } |
| 359 | 1 | return true; |
| 360 | | }; |
| 361 | | /** |
| 362 | | * 修改目录或者文件权限 |
| 363 | | * @param {[type]} p [description] |
| 364 | | * @param {[type]} mode [description] |
| 365 | | * @return {[type]} [description] |
| 366 | | */ |
| 367 | 1 | global.chmod = function(p, mode){ |
| 368 | 0 | 'use strict'; |
| 369 | 0 | mode = mode || '0777'; |
| 370 | 0 | if (!fs.existsSync(p)) { |
| 371 | 0 | return true; |
| 372 | | } |
| 373 | 0 | return fs.chmodSync(p, mode); |
| 374 | | }; |
| 375 | | /** |
| 376 | | * 获取文件内容 |
| 377 | | * @param {[type]} file [description] |
| 378 | | * @return {[type]} [description] |
| 379 | | */ |
| 380 | 1 | global.getFileContent = function(file, encoding){ |
| 381 | 0 | 'use strict'; |
| 382 | 0 | if (!fs.existsSync(file)) { |
| 383 | 0 | return ''; |
| 384 | | } |
| 385 | 0 | return fs.readFileSync(file, { |
| 386 | | encoding: encoding || 'utf8' |
| 387 | | }); |
| 388 | | }; |
| 389 | | /** |
| 390 | | * 设置文件内容 |
| 391 | | * @param {[type]} file [description] |
| 392 | | * @param {[type]} data [description] |
| 393 | | * @return {[type]} [description] |
| 394 | | */ |
| 395 | 1 | global.setFileContent = function(file, data){ |
| 396 | 0 | 'use strict'; |
| 397 | 0 | return fs.writeFileSync(file, data); |
| 398 | | }; |
| 399 | | /** |
| 400 | | * 大写首字符 |
| 401 | | * @param {[type]} name [description] |
| 402 | | * @return {[type]} [description] |
| 403 | | */ |
| 404 | 1 | global.ucfirst = function(name){ |
| 405 | 6 | 'use strict'; |
| 406 | 6 | name = (name || '') + ''; |
| 407 | 6 | return name.substr(0,1).toUpperCase() + name.substr(1).toLowerCase(); |
| 408 | | }; |
| 409 | | /** |
| 410 | | * 获取字符串的md5 |
| 411 | | * @param {[type]} str [description] |
| 412 | | * @return {[type]} [description] |
| 413 | | */ |
| 414 | 1 | global.md5 = function(str){ |
| 415 | 2 | 'use strict'; |
| 416 | 2 | var instance = crypto.createHash('md5'); |
| 417 | 2 | instance.update(str + ''); |
| 418 | 2 | return instance.digest('hex'); |
| 419 | | }; |
| 420 | | /** |
| 421 | | * 生成一个promise,如果传入的参数是promise则直接返回 |
| 422 | | * @param {[type]} obj [description] |
| 423 | | * @return {[type]} [description] |
| 424 | | */ |
| 425 | 1 | global.getPromise = function(obj, reject){ |
| 426 | 7 | 'use strict'; |
| 427 | 7 | if (isPromise(obj)) { |
| 428 | 1 | return obj; |
| 429 | | } |
| 430 | 6 | if (reject) { |
| 431 | 2 | return Promise.reject(obj); |
| 432 | | } |
| 433 | 4 | return Promise.resolve(obj); |
| 434 | | }; |
| 435 | | /** |
| 436 | | * 生成一个defer对象 |
| 437 | | * @return {[type]} [description] |
| 438 | | */ |
| 439 | 1 | global.getDefer = function(){ |
| 440 | 1 | 'use strict'; |
| 441 | 1 | var deferred = {}; |
| 442 | 1 | deferred.promise = new Promise(function(resolve, reject){ |
| 443 | 1 | deferred.resolve = resolve; |
| 444 | 1 | deferred.reject = reject; |
| 445 | | }); |
| 446 | 1 | return deferred; |
| 447 | | }; |
| 448 | | /** |
| 449 | | * 快速生成一个object |
| 450 | | * @param {[type]} key [description] |
| 451 | | * @param {[type]} value [description] |
| 452 | | * @return {[type]} [description] |
| 453 | | */ |
| 454 | 1 | global.getObject = function(key, value){ |
| 455 | 5 | 'use strict'; |
| 456 | 5 | var obj = {}; |
| 457 | 5 | if (!isArray(key)) { |
| 458 | 2 | obj[key] = value; |
| 459 | 2 | return obj; |
| 460 | | } |
| 461 | 3 | key.forEach(function(item, i){ |
| 462 | 5 | obj[item] = value[i]; |
| 463 | | }); |
| 464 | 3 | return obj; |
| 465 | | }; |
| 466 | | /** |
| 467 | | * 将数组变成对象 |
| 468 | | * @param {[type]} arr [description] |
| 469 | | * @param {[type]} key [description] |
| 470 | | * @param {[type]} valueKeys [description] |
| 471 | | * @return {[type]} [description] |
| 472 | | */ |
| 473 | 1 | global.arrToObj = function(arr, key, valueKey){ |
| 474 | 4 | 'use strict'; |
| 475 | 4 | var result = {}; |
| 476 | 4 | var arrResult = []; |
| 477 | 4 | arr.forEach(function(item){ |
| 478 | 8 | var keyValue = item[key]; |
| 479 | 8 | if (valueKey === null) { |
| 480 | 4 | arrResult.push(keyValue); |
| 481 | 4 | }else if (valueKey) { |
| 482 | 2 | result[keyValue] = item[valueKey]; |
| 483 | | }else{ |
| 484 | 2 | result[keyValue] = item; |
| 485 | | } |
| 486 | | }) |
| 487 | 4 | return valueKey === null ? arrResult : result; |
| 488 | | } |