金诚优选前端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
1.5 KiB

  1. // 判断arr是否为一个数组,返回一个bool值
  2. function isArray(arr) {
  3. return Object.prototype.toString.call(arr) === '[object Array]';
  4. }
  5. // 深度克隆
  6. function deepClone(obj) {
  7. // 对常见的“非”值,直接返回原来值
  8. if ([null, undefined, NaN, false].includes(obj)) return obj;
  9. if (typeof obj !== "object" && typeof obj !== 'function') {
  10. //原始类型直接返回
  11. return obj;
  12. }
  13. var o = isArray(obj) ? [] : {};
  14. for (let i in obj) {
  15. if (obj.hasOwnProperty(i)) {
  16. o[i] = typeof obj[i] === "object" ? deepClone(obj[i]) : obj[i];
  17. }
  18. }
  19. return o;
  20. }
  21. function getUUid(len = 32, firstU = true, radix = null) {
  22. let chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
  23. let uuid = [];
  24. radix = radix || chars.length;
  25. if (len) {
  26. // 如果指定uuid长度,只是取随机的字符,0|x为位运算,能去掉x的小数位,返回整数位
  27. for (let i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix];
  28. } else {
  29. let r;
  30. // rfc4122标准要求返回的uuid中,某些位为固定的字符
  31. uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
  32. uuid[14] = '4';
  33. for (let i = 0; i < 36; i++) {
  34. if (!uuid[i]) {
  35. r = 0 | Math.random() * 16;
  36. uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
  37. }
  38. }
  39. }
  40. // 移除第一个字符,并用u替代,因为第一个字符为数值时,该guuid不能用作id或者class
  41. if (firstU) {
  42. uuid.shift();
  43. return 'u' + uuid.join('');
  44. } else {
  45. return uuid.join('');
  46. }
  47. }
  48. export {
  49. deepClone,
  50. getUUid
  51. };