Skip to content

Commit

Permalink
feat: 添加处理数据的方法
Browse files Browse the repository at this point in the history
  • Loading branch information
novlan1 committed Jun 15, 2022
1 parent b50d350 commit 0b5cba7
Show file tree
Hide file tree
Showing 3 changed files with 291 additions and 134 deletions.
1 change: 1 addition & 0 deletions src/base/list/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './list'
export * from './parse-list'
134 changes: 0 additions & 134 deletions src/base/list/list.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { getUnitPreviousRatio } from '../number'

/**
* 拉平数组
*
Expand Down Expand Up @@ -40,135 +38,3 @@ export function shuffle(array) {
}
return arr
}

/**
* 判断数组是否全部相等
* @param list - 数组
* @returns 是否全部相等
*
* @example
* ```ts
* isListAllEqual([0, 0, 0]) // true
* isListAllEqual([0, 0, 2]) // false
* ```
*/
export function isListAllEqual(list = []) {
if (!list.length) return true
const value = list[0]
// eslint-disable-next-line no-restricted-syntax
for (const item of list.slice(1)) {
if (item !== value) return false
}

return true
}

/**
* 获取对象的value列表,并输出大对象形式
* @param {*} data
* @returns
*
* @example
*
* ```ts
* const data = [
* {
* Project: 'xx',
* Request: 111,
* Score: 111
* },
* {
* Project: 'yy',
* Request: 333,
* Score: 555
* }]
*
* getKeyValuesMap(data)
*
* // 结果为
* {
* Project: ['xx', 'yy'],
Request: [111, 333],
Score: [111, 555],
}
```
*/
export function getKeyValuesMap(data: Array<any> = []) {
if (!data.length) return {}
const keys = Object.keys(data[0])
const keyValueMap = {}

data.forEach(item => {
keys.forEach(key => {
if (keyValueMap[key]) {
keyValueMap[key].push(item[key])
} else {
keyValueMap[key] = [item[key]]
}
})
})
return keyValueMap
}

/**
* 获取相对上次的比例
* @param data
*
* [{
* Project: { value: 'mj-match', name: 'Project' },
* Request: {
value: 854,
name: 'Request',
idx: 19,
lastIdx: 19,
isMax: false,
isMin: false,
isSecondMax: false,
isSecondMin: true
},
}
}]
* @param preDataMap
*
* {
'mj-match': {
Project: 'mj-match',
Request: 4,
Score: 91.81,
FirstLoadTime: 178,
WholePageTime: 1035,
ParseDomTime: 484,
DNSLinkTime: 0,
DOMTime: 414,
TCP: 0,
HTTP: 275,
BackEnd: 60,
CGIFailNum: 0,
ErrorLogNum: 0,
CGIRequestNum: 83
},
}
*/
export function getPreviousRatio(
data = [],
preDataMap = {},
uniqKey = 'Project',
) {
data.forEach(item => {
Object.keys(item).forEach(key => {
const obj = item[key] as any

if (typeof obj.value === 'number') {
const uniqVal = (item[uniqKey] as any).value
const preValue = preDataMap?.[uniqVal]?.[key]

if (preValue === undefined) {
obj.ratio = ''
} else {
obj.ratio = getUnitPreviousRatio(obj.value, preValue)
obj.previousValue = preValue
}
}
})
})
}
Loading

0 comments on commit 0b5cba7

Please sign in to comment.