util.js 4.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /**
  2. * base64加密
  3. */
  4. var Base64 = {
  5. _keyStr:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
  6. encode:function(e){
  7. var t="";var n,r,i,s,o,u,a;var f=0;e=Base64._utf8_encode(e);while(f<e.length){n=e.charCodeAt(f++);r=e.charCodeAt(f++);i=e.charCodeAt(f++);s=n>>2;o=(n&3)<<4|r>>4;u=(r&15)<<2|i>>6;a=i&63;if(isNaN(r)){u=a=64}else if(isNaN(i)){a=64}t=t+this._keyStr.charAt(s)+this._keyStr.charAt(o)+this._keyStr.charAt(u)+this._keyStr.charAt(a)}return t},decode:function(e){var t="";var n,r,i;var s,o,u,a;var f=0;e=e.replace(/[^A-Za-z0-9+/=]/g,"");while(f<e.length){s=this._keyStr.indexOf(e.charAt(f++));o=this._keyStr.indexOf(e.charAt(f++));u=this._keyStr.indexOf(e.charAt(f++));a=this._keyStr.indexOf(e.charAt(f++));n=s<<2|o>>4;r=(o&15)<<4|u>>2;i=(u&3)<<6|a;t=t+String.fromCharCode(n);if(u!=64){t=t+String.fromCharCode(r)}if(a!=64){t=t+String.fromCharCode(i)}}t=Base64._utf8_decode(t);return t},_utf8_encode:function(e){e=e.replace(/rn/g,"n");var t="";for(var n=0;n<e.length;n++){var r=e.charCodeAt(n);if(r<128){t+=String.fromCharCode(r)}else if(r>127&&r<2048){t+=String.fromCharCode(r>>6|192);t+=String.fromCharCode(r&63|128)}else{t+=String.fromCharCode(r>>12|224);t+=String.fromCharCode(r>>6&63|128);t+=String.fromCharCode(r&63|128)}}return t},_utf8_decode:function(e){var t="";var n=0;var r=c1=c2=0;while(n<e.length){r=e.charCodeAt(n);if(r<128){t+=String.fromCharCode(r);n++}else if(r>191&&r<224){c2=e.charCodeAt(n+1);t+=String.fromCharCode((r&31)<<6|c2&63);n+=2}else{c2=e.charCodeAt(n+1);c3=e.charCodeAt(n+2);t+=String.fromCharCode((r&15)<<12|(c2&63)<<6|c3&63);n+=3}}return t
  8. },
  9. }
  10. /**
  11. * 表单对象赋值:
  12. * 对目标对象存在且源对象同样存在的属性,全部覆盖;
  13. * 目标对象不存在但是源对象存在的属性, 全部丢弃;
  14. * 目标对象存在但是源对象不存在的属性,如果是字符串赋值为空串,其余类型赋值为undefined
  15. */
  16. function recover (target, source) {
  17. if (target === undefined || target === null) { throw new TypeError('Cannot convert first argument to object') }
  18. var to = Object(target)
  19. if (source === undefined || source === null) { return to }
  20. var keysArray = Object.keys(Object(target))
  21. for (var nextIndex = 0, len = keysArray.length; nextIndex < len; nextIndex++) {
  22. var nextKey = keysArray[nextIndex]
  23. var desc = Object.getOwnPropertyDescriptor(target, nextKey)
  24. if (desc !== undefined && desc.enumerable) {
  25. if (to.hasOwnProperty(nextKey)) {
  26. if (to[nextKey] instanceof Array) {
  27. to[nextKey] = source[nextKey]
  28. } else if (to[nextKey] instanceof Object) {
  29. recover(to[nextKey], source[nextKey])
  30. } else if (source[nextKey] !== undefined) {
  31. to[nextKey] = source[nextKey]
  32. } else if (typeof (to[nextKey]) === 'string') {
  33. to[nextKey] = ''
  34. } else {
  35. to[nextKey] = undefined
  36. }
  37. }
  38. }
  39. }
  40. return to
  41. }
  42. /**
  43. * 表单对象赋值:
  44. * 对目标对象存在且源对象同样存在的属性,全部覆盖;
  45. * 目标对象不存在但是源对象存在的属性, 全部丢弃;
  46. * 目标对象存在但是源对象不存在的属性,保留目标对象的属性不做处理
  47. */
  48. function recoverNotNull (target, source) {
  49. if (target === undefined || target === null) { throw new TypeError('Cannot convert first argument to object') }
  50. var to = Object(target)
  51. if (source === undefined || source === null) { return to }
  52. var keysArray = Object.keys(Object(target))
  53. for (var nextIndex = 0, len = keysArray.length; nextIndex < len; nextIndex++) {
  54. var nextKey = keysArray[nextIndex]
  55. var desc = Object.getOwnPropertyDescriptor(target, nextKey)
  56. if (desc !== undefined && desc.enumerable) {
  57. if (to.hasOwnProperty(nextKey)) {
  58. if (to[nextKey] instanceof Array) {
  59. to[nextKey] = source[nextKey]
  60. } else if (to[nextKey] instanceof Object) {
  61. recover(to[nextKey], source[nextKey])
  62. } else if (source[nextKey] !== undefined) {
  63. to[nextKey] = source[nextKey]
  64. }
  65. }
  66. }
  67. }
  68. return to
  69. }
  70. // 驼峰转下划线
  71. function toLine (name) {
  72. if (name.indexOf('.') < 0) {
  73. return name.replace(/([A-Z])/g, '_$1').toLowerCase()
  74. } else {
  75. return name
  76. }
  77. }
  78. module.exports = {
  79. Base64:Base64,
  80. recover: recover,
  81. recoverNotNull: recoverNotNull,
  82. toLine: toLine
  83. }