util.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /**
  2. * base64加密
  3. */
  4. var Base64 = {
  5. _keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
  6. encode: function(e) {
  7. var t = "";
  8. var n, r, i, s, o, u, a;
  9. var f = 0;
  10. e = Base64._utf8_encode(e);
  11. while (f < e.length) {
  12. n = e.charCodeAt(f++);
  13. r = e.charCodeAt(f++);
  14. i = e.charCodeAt(f++);
  15. s = n >> 2;
  16. o = (n & 3) << 4 | r >> 4;
  17. u = (r & 15) << 2 | i >> 6;
  18. a = i & 63;
  19. if (isNaN(r)) {
  20. u = a = 64
  21. } else if (isNaN(i)) {
  22. a = 64
  23. }
  24. t = t + this._keyStr.charAt(s) + this._keyStr.charAt(o) + this._keyStr.charAt(u) + this._keyStr
  25. .charAt(a)
  26. }
  27. return t
  28. },
  29. decode: function(e) {
  30. var t = "";
  31. var n, r, i;
  32. var s, o, u, a;
  33. var f = 0;
  34. e = e.replace(/[^A-Za-z0-9+/=]/g, "");
  35. while (f < e.length) {
  36. s = this._keyStr.indexOf(e.charAt(f++));
  37. o = this._keyStr.indexOf(e.charAt(f++));
  38. u = this._keyStr.indexOf(e.charAt(f++));
  39. a = this._keyStr.indexOf(e.charAt(f++));
  40. n = s << 2 | o >> 4;
  41. r = (o & 15) << 4 | u >> 2;
  42. i = (u & 3) << 6 | a;
  43. t = t + String.fromCharCode(n);
  44. if (u != 64) {
  45. t = t + String.fromCharCode(r)
  46. }
  47. if (a != 64) {
  48. t = t + String.fromCharCode(i)
  49. }
  50. }
  51. t = Base64._utf8_decode(t);
  52. return t
  53. },
  54. _utf8_encode: function(e) {
  55. e = e.replace(/rn/g, "n");
  56. var t = "";
  57. for (var n = 0; n < e.length; n++) {
  58. var r = e.charCodeAt(n);
  59. if (r < 128) {
  60. t += String.fromCharCode(r)
  61. } else if (r > 127 && r < 2048) {
  62. t += String.fromCharCode(r >> 6 | 192);
  63. t += String.fromCharCode(r & 63 | 128)
  64. } else {
  65. t += String.fromCharCode(r >> 12 | 224);
  66. t += String.fromCharCode(r >> 6 & 63 | 128);
  67. t += String.fromCharCode(r & 63 | 128)
  68. }
  69. }
  70. return t
  71. },
  72. _utf8_decode: function(e) {
  73. var t = "";
  74. var n = 0;
  75. var r = c1 = c2 = 0;
  76. while (n < e.length) {
  77. r = e.charCodeAt(n);
  78. if (r < 128) {
  79. t += String.fromCharCode(r);
  80. n++
  81. } else if (r > 191 && r < 224) {
  82. c2 = e.charCodeAt(n + 1);
  83. t += String.fromCharCode((r & 31) << 6 | c2 & 63);
  84. n += 2
  85. } else {
  86. c2 = e.charCodeAt(n + 1);
  87. c3 = e.charCodeAt(n + 2);
  88. t += String.fromCharCode((r & 15) << 12 | (c2 & 63) << 6 | c3 & 63);
  89. n += 3
  90. }
  91. }
  92. return t
  93. },
  94. }
  95. /**
  96. * 表单对象赋值:
  97. * 对目标对象存在且源对象同样存在的属性,全部覆盖;
  98. * 目标对象不存在但是源对象存在的属性, 全部丢弃;
  99. * 目标对象存在但是源对象不存在的属性,如果是字符串赋值为空串,其余类型赋值为undefined
  100. */
  101. function recover(target, source) {
  102. if (target === undefined || target === null) {
  103. throw new TypeError('Cannot convert first argument to object')
  104. }
  105. var to = Object(target)
  106. if (source === undefined || source === null) {
  107. return to
  108. }
  109. var keysArray = Object.keys(Object(target))
  110. for (var nextIndex = 0, len = keysArray.length; nextIndex < len; nextIndex++) {
  111. var nextKey = keysArray[nextIndex]
  112. var desc = Object.getOwnPropertyDescriptor(target, nextKey)
  113. if (desc !== undefined && desc.enumerable) {
  114. if (to.hasOwnProperty(nextKey)) {
  115. if (to[nextKey] instanceof Array) {
  116. to[nextKey] = source[nextKey]
  117. } else if (to[nextKey] instanceof Object) {
  118. recover(to[nextKey], source[nextKey])
  119. } else if (source[nextKey] !== undefined) {
  120. to[nextKey] = source[nextKey]
  121. } else if (typeof(to[nextKey]) === 'string') {
  122. to[nextKey] = ''
  123. } else {
  124. to[nextKey] = undefined
  125. }
  126. }
  127. }
  128. }
  129. return to
  130. }
  131. /**
  132. * 表单对象赋值:
  133. * 对目标对象存在且源对象同样存在的属性,全部覆盖;
  134. * 目标对象不存在但是源对象存在的属性, 全部丢弃;
  135. * 目标对象存在但是源对象不存在的属性,保留目标对象的属性不做处理
  136. */
  137. function recoverNotNull(target, source) {
  138. if (target === undefined || target === null) {
  139. throw new TypeError('Cannot convert first argument to object')
  140. }
  141. var to = Object(target)
  142. if (source === undefined || source === null) {
  143. return to
  144. }
  145. var keysArray = Object.keys(Object(target))
  146. for (var nextIndex = 0, len = keysArray.length; nextIndex < len; nextIndex++) {
  147. var nextKey = keysArray[nextIndex]
  148. var desc = Object.getOwnPropertyDescriptor(target, nextKey)
  149. if (desc !== undefined && desc.enumerable) {
  150. if (to.hasOwnProperty(nextKey)) {
  151. if (to[nextKey] instanceof Array) {
  152. to[nextKey] = source[nextKey]
  153. } else if (to[nextKey] instanceof Object) {
  154. recover(to[nextKey], source[nextKey])
  155. } else if (source[nextKey] !== undefined) {
  156. to[nextKey] = source[nextKey]
  157. }
  158. }
  159. }
  160. }
  161. return to
  162. }
  163. // 驼峰转下划线
  164. function toLine(name) {
  165. if (name.indexOf('.') < 0) {
  166. return name.replace(/([A-Z])/g, '_$1').toLowerCase()
  167. } else {
  168. return name
  169. }
  170. }
  171. // 使用正则表达式判断文件名或URL是否为图片格式
  172. function isImageFormat(filename) {
  173. const imageFormats = /\.(jpeg|jpg|gif|png|bmp|svg)$/i;
  174. return imageFormats.test(filename);
  175. }
  176. module.exports = {
  177. Base64: Base64,
  178. recover: recover,
  179. recoverNotNull: recoverNotNull,
  180. toLine: toLine,
  181. isImageFormat: isImageFormat,
  182. }