index.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /**
  2. * 对象类
  3. */
  4. export class ObjectManager {
  5. constructor() {}
  6. /**
  7. * 对象转url参数
  8. * @param {*} data,对象
  9. * @param {*} isPrefix,是否自动加上"?"
  10. * 参考:uview
  11. **/
  12. queryParams = (data = {}, isPrefix = true, arrayFormat = 'brackets') => {
  13. let prefix = isPrefix ? '?' : ''
  14. let _result = []
  15. if (['indices', 'brackets', 'repeat', 'comma'].indexOf(arrayFormat) == -1) arrayFormat = 'brackets';
  16. for (let key in data) {
  17. let value = data[key]
  18. // 去掉为空的参数
  19. if (['', undefined, null].indexOf(value) >= 0) {
  20. continue;
  21. }
  22. // 如果值为数组,另行处理
  23. if (value.constructor === Array) {
  24. // e.g. {ids: [1, 2, 3]}
  25. switch (arrayFormat) {
  26. case 'indices':
  27. // 结果: ids[0]=1&ids[1]=2&ids[2]=3
  28. for (let i = 0; i < value.length; i++) {
  29. _result.push(key + '[' + i + ']=' + value[i])
  30. }
  31. break;
  32. case 'brackets':
  33. // 结果: ids[]=1&ids[]=2&ids[]=3
  34. value.forEach(_value => {
  35. _result.push(key + '[]=' + _value)
  36. })
  37. break;
  38. case 'repeat':
  39. // 结果: ids=1&ids=2&ids=3
  40. value.forEach(_value => {
  41. _result.push(key + '=' + _value)
  42. })
  43. break;
  44. case 'comma':
  45. // 结果: ids=1,2,3
  46. let commaStr = "";
  47. value.forEach(_value => {
  48. commaStr += (commaStr ? "," : "") + _value;
  49. })
  50. _result.push(key + '=' + commaStr)
  51. break;
  52. default:
  53. value.forEach(_value => {
  54. _result.push(key + '[]=' + _value)
  55. })
  56. }
  57. } else {
  58. _result.push(key + '=' + value)
  59. }
  60. }
  61. return _result.length ? prefix + _result.join('&') : ''
  62. }
  63. /**
  64. * @description 克隆对象
  65. * @param {object} obj 需要深度克隆的对象
  66. * @returns {*} 克隆后的对象或者原值(不是对象)
  67. */
  68. clone = (obj, cache = new WeakMap()) => {
  69. if (obj === null || typeof obj !== 'object') return obj;
  70. if (cache.has(obj)) return cache.get(obj);
  71. let clone;
  72. if (obj instanceof Date) {
  73. clone = new Date(obj.getTime());
  74. } else if (obj instanceof RegExp) {
  75. clone = new RegExp(obj);
  76. } else if (obj instanceof Map) {
  77. clone = new Map(Array.from(obj, ([key, value]) => [key, this.clone(value, cache)]));
  78. } else if (obj instanceof Set) {
  79. clone = new Set(Array.from(obj, value => this.clone(value, cache)));
  80. } else if (Array.isArray(obj)) {
  81. clone = obj.map(value => this.clone(value, cache));
  82. } else if (Object.prototype.toString.call(obj) === '[object Object]') {
  83. clone = Object.create(Object.getPrototypeOf(obj));
  84. cache.set(obj, clone);
  85. for (const [key, value] of Object.entries(obj)) {
  86. clone[key] = this.clone(value, cache);
  87. }
  88. } else {
  89. clone = Object.assign({}, obj);
  90. }
  91. cache.set(obj, clone);
  92. return clone;
  93. }
  94. /**
  95. * @description JS对象深度合并
  96. * @param {object} target 需要拷贝的对象
  97. * @param {object} source 拷贝的来源对象
  98. * @returns {object|boolean} 深度合并后的对象或者false(入参有不是对象)
  99. */
  100. merge = (target = {}, source = {}) => {
  101. target = this.clone(target)
  102. if (typeof target !== 'object' || target === null || typeof source !== 'object' || source === null) return target;
  103. const merged = Array.isArray(target) ? target.slice() : Object.assign({}, target);
  104. for (const prop in source) {
  105. if (!source.hasOwnProperty(prop)) continue;
  106. const sourceValue = source[prop];
  107. const targetValue = merged[prop];
  108. if (sourceValue instanceof Date) {
  109. merged[prop] = new Date(sourceValue);
  110. } else if (sourceValue instanceof RegExp) {
  111. merged[prop] = new RegExp(sourceValue);
  112. } else if (sourceValue instanceof Map) {
  113. merged[prop] = new Map(sourceValue);
  114. } else if (sourceValue instanceof Set) {
  115. merged[prop] = new Set(sourceValue);
  116. } else if (typeof sourceValue === 'object' && sourceValue !== null) {
  117. merged[prop] = deepMerge(targetValue, sourceValue);
  118. } else {
  119. merged[prop] = sourceValue;
  120. }
  121. }
  122. return merged;
  123. }
  124. /**
  125. * @description 获取某个对象下的属性,用于通过类似'a.b.c'的形式去获取一个对象的的属性的形式
  126. * @param {object} obj 对象
  127. * @param {string} key 需要获取的属性字段
  128. * @returns {*}
  129. */
  130. getProperty = (obj, key) => {
  131. if (!obj) {
  132. return
  133. }
  134. if (typeof key !== 'string' || key === '') {
  135. return ''
  136. }
  137. if (key.indexOf('.') !== -1) {
  138. const keys = key.split('.')
  139. let firstObj = obj[keys[0]] || {}
  140. for (let i = 1; i < keys.length; i++) {
  141. if (firstObj) {
  142. firstObj = firstObj[keys[i]]
  143. }
  144. }
  145. return firstObj
  146. }
  147. return obj[key]
  148. }
  149. /**
  150. * @description 设置对象的属性值,如果'a.b.c'的形式进行设置
  151. * @param {object} obj 对象
  152. * @param {string} key 需要设置的属性
  153. * @param {string} value 设置的值
  154. */
  155. setProperty = (obj, key, value) => {
  156. if (!obj) {
  157. return
  158. }
  159. // 递归赋值
  160. const inFn = function(_obj, keys, v) {
  161. // 最后一个属性key
  162. if (keys.length === 1) {
  163. _obj[keys[0]] = v
  164. return
  165. }
  166. // 0~length-1个key
  167. while (keys.length > 1) {
  168. const k = keys[0]
  169. if (!_obj[k] || (typeof _obj[k] !== 'object')) {
  170. _obj[k] = {}
  171. }
  172. const key = keys.shift()
  173. // 自调用判断是否存在属性,不存在则自动创建对象
  174. inFn(_obj[k], keys, v)
  175. }
  176. }
  177. if (typeof key !== 'string' || key === '') {
  178. } else if (key.indexOf('.') !== -1) { // 支持多层级赋值操作
  179. const keys = key.split('.')
  180. inFn(obj, keys, value)
  181. } else {
  182. obj[key] = value
  183. }
  184. }
  185. /**
  186. * 是否是对象
  187. * @param {Object} value
  188. */
  189. isObject(value) {
  190. return Object.prototype.toString.call(value) === '[object Object]'
  191. }
  192. /**
  193. * 移除空值
  194. */
  195. trim = (obj) => {
  196. let map = {};
  197. for (const key in obj) {
  198. if (Object.hasOwnProperty.call(obj, key)) {
  199. // 如果是字符串要 trim
  200. const value = (typeof obj[key] === 'string') ? obj[key].trim() : obj[key];
  201. if (!!value || value === 0) {
  202. map[key] = value;
  203. }
  204. }
  205. }
  206. return map;
  207. }
  208. /**
  209. * 将对象按照键名排序
  210. * @param obj 要处理的对象
  211. * @param type 排序方式,asc:升序,desc:降序
  212. * v1.0.1
  213. */
  214. sort = (obj, type = "asc") => {
  215. // 获取键名
  216. let newParams = {};
  217. let keys = Object.keys(obj).sort();
  218. if (type == "desc") {
  219. keys.reverse();
  220. }
  221. for (let i = 0; i < keys.length; i++) {
  222. let value = obj[keys[i]];
  223. newParams[keys[i]] = value;
  224. }
  225. return newParams;
  226. }
  227. }