index.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /* eslint-disable no-nested-ternary */
  2. /* eslint-disable no-restricted-syntax */
  3. /* eslint-disable guard-for-in */
  4. /**
  5. * num 小于0,左缩进num*2个空格; 大于0,右缩进num*2个空格。
  6. * @param {string} str 代码
  7. * @param {number} num 缩进次数
  8. * @param {number} len 【可选】缩进单位,空格数
  9. */
  10. export function indent(str, num, len = 2) {
  11. if (num === 0) return str
  12. const isLeft = num < 0; const result = []; let reg; let
  13. spaces = ''
  14. if (isLeft) {
  15. num *= -1
  16. reg = new RegExp(`(^\\s{0,${num * len}})`, 'g')
  17. } else {
  18. for (let i = 0; i < num * len; i++) spaces += ' '
  19. }
  20. str.split('\n').forEach(line => {
  21. line = isLeft ? line.replace(reg, '') : spaces + line
  22. result.push(line)
  23. })
  24. return result.join('\n')
  25. }
  26. // 首字母大小
  27. export function titleCase(str) {
  28. return str.replace(/( |^)[a-z]/g, L => L.toUpperCase())
  29. }
  30. // 下划转驼峰
  31. export function camelCase(str) {
  32. return str.replace(/-[a-z]/g, str1 => str1.substr(-1).toUpperCase())
  33. }
  34. export function isNumberStr(str) {
  35. return /^[+-]?(0|([1-9]\d*))(\.\d+)?$/g.test(str)
  36. }
  37. export const exportDefault = 'export default '
  38. export const beautifierConf = {
  39. html: {
  40. indent_size: '2',
  41. indent_char: ' ',
  42. max_preserve_newlines: '-1',
  43. preserve_newlines: false,
  44. keep_array_indentation: false,
  45. break_chained_methods: false,
  46. indent_scripts: 'separate',
  47. brace_style: 'end-expand',
  48. space_before_conditional: true,
  49. unescape_strings: false,
  50. jslint_happy: false,
  51. end_with_newline: true,
  52. wrap_line_length: '110',
  53. indent_inner_html: true,
  54. comma_first: false,
  55. e4x: true,
  56. indent_empty_lines: true
  57. },
  58. js: {
  59. indent_size: '2',
  60. indent_char: ' ',
  61. max_preserve_newlines: '-1',
  62. preserve_newlines: false,
  63. keep_array_indentation: false,
  64. break_chained_methods: false,
  65. indent_scripts: 'normal',
  66. brace_style: 'end-expand',
  67. space_before_conditional: true,
  68. unescape_strings: false,
  69. jslint_happy: true,
  70. end_with_newline: true,
  71. wrap_line_length: '110',
  72. indent_inner_html: true,
  73. comma_first: false,
  74. e4x: true,
  75. indent_empty_lines: true
  76. }
  77. }
  78. function stringify(obj) {
  79. return JSON.stringify(obj, (key, val) => {
  80. if (typeof val === 'function') {
  81. return `${val}`
  82. }
  83. return val
  84. })
  85. }
  86. function parse(str) {
  87. JSON.parse(str, (k, v) => {
  88. if (v.indexOf && v.indexOf('function') > -1) {
  89. return eval(`(${v})`)
  90. }
  91. return v
  92. })
  93. }
  94. export function jsonClone(obj) {
  95. return parse(stringify(obj))
  96. }
  97. // 深拷贝对象
  98. export function deepClone(obj) {
  99. const _toString = Object.prototype.toString
  100. // null, undefined, non-object, function
  101. if (!obj || typeof obj !== 'object') {
  102. return obj
  103. }
  104. // DOM Node
  105. if (obj.nodeType && 'cloneNode' in obj) {
  106. return obj.cloneNode(true)
  107. }
  108. // Date
  109. if (_toString.call(obj) === '[object Date]') {
  110. return new Date(obj.getTime())
  111. }
  112. // RegExp
  113. if (_toString.call(obj) === '[object RegExp]') {
  114. const flags = []
  115. if (obj.global) { flags.push('g') }
  116. if (obj.multiline) { flags.push('m') }
  117. if (obj.ignoreCase) { flags.push('i') }
  118. return new RegExp(obj.source, flags.join(''))
  119. }
  120. const result = Array.isArray(obj) ? [] : obj.constructor ? new obj.constructor() : {}
  121. for (const key in obj) {
  122. result[key] = deepClone(obj[key])
  123. }
  124. return result
  125. }