string.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. class CutString {
  2. constructor(string, limit) {
  3. // temparary node to parse the html tags in the string
  4. this.tempDiv = document.createElement('div')
  5. this.tempDiv.id = 'TempNodeForTest'
  6. this.tempDiv.innerHTML = string
  7. // while parsing text no of characters parsed
  8. this.charCount = 0
  9. this.limit = limit
  10. }
  11. cut() {
  12. var newDiv = document.createElement('div')
  13. this.searchEnd(this.tempDiv, newDiv)
  14. return newDiv.innerHTML
  15. }
  16. searchEnd(parseDiv, newParent) {
  17. var ele
  18. var newEle
  19. for (var j = 0; j < parseDiv.childNodes.length; j++) {
  20. ele = parseDiv.childNodes[j]
  21. // not text node
  22. if (ele.nodeType != 3) {
  23. newEle = ele.cloneNode(true)
  24. newParent.appendChild(newEle)
  25. if (ele.childNodes.length === 0) continue
  26. newEle.innerHTML = ''
  27. var res = this.searchEnd(ele, newEle)
  28. if (res) return res
  29. else {
  30. continue
  31. }
  32. }
  33. if (ele.nodeValue.length + this.charCount >= this.limit) {
  34. newEle = ele.cloneNode(true)
  35. newEle.nodeValue = ele.nodeValue.substr(0, this.limit - this.charCount)
  36. newParent.appendChild(newEle)
  37. return true
  38. }
  39. newEle = ele.cloneNode(true)
  40. newParent.appendChild(newEle)
  41. this.charCount += ele.nodeValue.length
  42. }
  43. return false
  44. }
  45. }
  46. export function defaults(newVal, oldVal, callback) {
  47. let value
  48. if (newVal === null || newVal === undefined) {
  49. value = oldVal
  50. } else {
  51. value = newVal
  52. }
  53. if (callback) {
  54. value = callback(value)
  55. }
  56. return value
  57. }
  58. /**
  59. * html字符串裁剪
  60. * @param {String} html
  61. * @param {Number} length
  62. */
  63. export function htmlCut(html, length) {
  64. return new CutString(html, length).cut()
  65. }
  66. export function timeFormat(second) {
  67. //秒
  68. var str = '' //不支持大于60分钟的时间哟
  69. var minute = parseInt(second / 60)
  70. if (minute < 10) str += '0'
  71. str += minute
  72. second = parseInt(second % 60) + ''
  73. if (second.length == 1) second = '0' + second
  74. str = str + ':' + second
  75. return str
  76. }
  77. export function monthDayFormat(date) {
  78. //秒
  79. if (date.indexOf('T') != -1) {
  80. return date.substring(5, 10)
  81. }
  82. }
  83. /**
  84. * html字符串转文本
  85. * @param {String} html
  86. */
  87. export const html2Text = (function () {
  88. const div = document.createElement('div')
  89. return function (html) {
  90. if (!html) {
  91. return ''
  92. }
  93. div.innerHTML = html
  94. return div.innerText
  95. }
  96. })()
  97. /**
  98. * 计算字符串长度(英文占1个字符,中文汉字占2个字符)
  99. * @param {*} str
  100. */
  101. export function getCharLength(str = '') {
  102. let len = 0
  103. for (let i = 0; i < str.length; i++) {
  104. if (str.charCodeAt(i) > 127 || str.charCodeAt(i) == 94) {
  105. len += 2
  106. } else {
  107. len++
  108. }
  109. }
  110. return len
  111. }
  112. /**
  113. * 添加url请求参数
  114. * @param {String} url 地址
  115. * @param {Object} params 参数列表
  116. */
  117. export function addQueryParams(url, params = {}) {
  118. if (!url) {
  119. return ''
  120. }
  121. let has = url.indexOf('?') > -1
  122. let arr = []
  123. for (let key in params) {
  124. if (!url.match('&' + key + '|\\?' + key)) {
  125. arr.push(`${key}=${params[key]}`)
  126. }
  127. }
  128. if (arr.length) {
  129. return `${url}${has ? '&' : '?'}${arr.join('&')}`
  130. }
  131. return url
  132. }
  133. export function randomWord(randomFlag, min, max) {
  134. let str = ''
  135. let range = min
  136. let arr = [
  137. '0',
  138. '1',
  139. '2',
  140. '3',
  141. '4',
  142. '5',
  143. '6',
  144. '7',
  145. '8',
  146. '9',
  147. 'a',
  148. 'b',
  149. 'c',
  150. 'd',
  151. 'e',
  152. 'f',
  153. 'g',
  154. 'h',
  155. 'i',
  156. 'j',
  157. 'k',
  158. 'l',
  159. 'm',
  160. 'n',
  161. 'o',
  162. 'p',
  163. 'q',
  164. 'r',
  165. 's',
  166. 't',
  167. 'u',
  168. 'v',
  169. 'w',
  170. 'x',
  171. 'y',
  172. 'z',
  173. 'A',
  174. 'B',
  175. 'C',
  176. 'D',
  177. 'E',
  178. 'F',
  179. 'G',
  180. 'H',
  181. 'I',
  182. 'J',
  183. 'K',
  184. 'L',
  185. 'M',
  186. 'N',
  187. 'O',
  188. 'P',
  189. 'Q',
  190. 'R',
  191. 'S',
  192. 'T',
  193. 'U',
  194. 'V',
  195. 'W',
  196. 'X',
  197. 'Y',
  198. 'Z',
  199. ]
  200. // 随机产生
  201. if (randomFlag) {
  202. range = Math.round(Math.random() * (max - min)) + min
  203. }
  204. for (var i = 0; i < range; i++) {
  205. let pos = Math.round(Math.random() * (arr.length - 1))
  206. str += arr[pos]
  207. }
  208. return str
  209. }
  210. /**
  211. * 密码加密
  212. * @param {String} pwd
  213. */
  214. export function password(pwd) {
  215. const NUM = 2
  216. const front = randomWord(false, 8)
  217. const middle = randomWord(false, 8)
  218. const end = randomWord(false, 8)
  219. let str = Base64.encode(pwd)
  220. let str1 = str.substring(0, NUM)
  221. let str2 = str.substring(NUM)
  222. return front + str2 + middle + str1 + end
  223. }
  224. /**
  225. * 删除输入法表情符号
  226. * @param {*} content
  227. */
  228. export function removeEmoji(content = '') {
  229. return content.replace(/[\ud800-\udbff][\udc00-\udfff]/g, function (char) {
  230. if (char.length === 2) {
  231. return ''
  232. } else {
  233. return char
  234. }
  235. })
  236. }