@ecan-bi/tools 1.0.0 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.ts CHANGED
@@ -1,6 +1,22 @@
1
1
  // 指标库相关内容
2
2
  import indicators from './src/indicator'
3
+ // 通用函数
4
+ import runCode from './src/utils/runCode'
5
+ // 工作台专用
6
+ import useTransformChartDataByAttrKey from './src/hooks/useTransformChartDataByAttrKey'
7
+ import useTransformChartDataByAttrValue from './src/hooks/useTransformChartDataByAttrValue'
8
+ import useValueFormatter from './src/hooks/useValueFormatter'
9
+ import useVariablesInText from './src/hooks/useVariablesInText'
10
+ import transformProps from './src/utils/transformProps'
11
+ import { getIndicatorParams } from './src/utils/util'
3
12
 
4
13
  export default {
5
- ...indicators
14
+ ...indicators,
15
+ runCode,
16
+ useTransformChartDataByAttrKey,
17
+ useTransformChartDataByAttrValue,
18
+ useValueFormatter,
19
+ useVariablesInText,
20
+ transformProps,
21
+ getIndicatorParams
6
22
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ecan-bi/tools",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "General tools",
5
5
  "main": "index.ts",
6
6
  "scripts": {
@@ -13,7 +13,9 @@
13
13
  "license": "ISC",
14
14
  "dependencies": {
15
15
  "dayjs": "^1.11.13",
16
- "lodash-es": "^4.17.21",
17
- "vue": "3.2.27"
16
+ "lodash-es": "^4.17.21"
17
+ },
18
+ "devDependencies": {
19
+ "vue": "3.3.8"
18
20
  }
19
21
  }
@@ -0,0 +1,77 @@
1
+ interface DataItem {
2
+ name: string | number
3
+ value: string | number
4
+ type?: string
5
+ }
6
+
7
+ export type Data = DataItem []
8
+
9
+ export interface KeyTypeDataFieldNames {
10
+ name: string
11
+ types: {
12
+ label: string
13
+ value: string
14
+ } []
15
+ }
16
+
17
+ /**
18
+ * 图形转化数据
19
+ * @param data 数据
20
+ * @param dataFieldNames 字段映射
21
+ */
22
+ export const useTransformChartDataByAttrKey = (data: Data = [], dataFieldNames: KeyTypeDataFieldNames) => {
23
+ let { name = 'name', types = [] } = dataFieldNames || {}
24
+ name = name?.trim() || name
25
+ const set = new Set()
26
+ const map = new Map<string, any []>()
27
+ const dataLen = data.length
28
+ const typesLen = types.length
29
+ // 只有一条记录
30
+ if (dataLen === 1) {
31
+ const vs: any = []
32
+ // data 为一个值取第一个
33
+ for (let i = 0; i < typesLen; i++) {
34
+ const type = types[i]
35
+ const { label, value } = type
36
+ const v = { ...data[0], value: data[0][value] }
37
+ set.add(label)
38
+ // 值列表
39
+ vs.push(v)
40
+ }
41
+ const n = data[0][name] || ''
42
+ map.set(n, vs)
43
+ } else {
44
+ for (let i = 0; i < dataLen; i++) {
45
+ const item = data[i]
46
+ const x = item[name]
47
+ set.add(x)
48
+ for (let j = 0; j < typesLen; j++) {
49
+ const type = types[j]
50
+ const { label, value } = type
51
+ const v = { ...item, value: item[value] }
52
+ if (map.has(label)) {
53
+ const m: any = map.get(label)
54
+ m.push(v)
55
+ map.set(label, m)
56
+ } else {
57
+ map.set(label, [v])
58
+ }
59
+ }
60
+ }
61
+ }
62
+
63
+ const dimensions = Array.from(set)
64
+ const dataset: { [key:string]:any } [] = []
65
+ for (const [name, data] of map) {
66
+ dataset.push({
67
+ name,
68
+ data
69
+ })
70
+ }
71
+ return {
72
+ dimensions,
73
+ dataset
74
+ }
75
+ }
76
+
77
+ export default useTransformChartDataByAttrKey
@@ -0,0 +1,61 @@
1
+ interface DataItem {
2
+ name: string | number
3
+ value: string | number
4
+ type?: string
5
+ }
6
+
7
+ export type Data = DataItem []
8
+
9
+ export interface ValueTypeDataFieldNames {
10
+ name: string
11
+ value: string
12
+ type: string
13
+ }
14
+
15
+ /**
16
+ * 图形转化数据
17
+ * @param data 数据
18
+ * @param dataFieldNames 字段映射
19
+ */
20
+ export const useTransformChartDataByAttrValue = (data: Data, dataFieldNames?: ValueTypeDataFieldNames) => {
21
+ let { name = 'name', value = 'value', type = 'type' } = dataFieldNames || {}
22
+ // 处去字符串
23
+ name = name?.trim()
24
+ value = value?.trim()
25
+ type = type?.trim()
26
+ const set = new Set()
27
+ const map = new Map()
28
+ const len = data.length
29
+ for (let i = 0; i < len; i++) {
30
+ const item = data[i]
31
+ // x轴
32
+ const x = item[name] || ''
33
+ // y轴
34
+ const y = item[value] || ''
35
+ // 系列(类型)
36
+ const t = item[type] || ''
37
+ set.add(x)
38
+ if (map.has(t)) {
39
+ const arr = map.get(t)
40
+ // @ts-ignore
41
+ arr.push({ name: x, value: y, ...item })
42
+ map.set(t, arr)
43
+ } else {
44
+ // @ts-ignore
45
+ map.set(t, [{ name: x, value: y, ...item }])
46
+ }
47
+ }
48
+ const dimensions = Array.from(set)
49
+ const dataset: { [key:string]:any } [] = []
50
+ for (const [name, data] of map) {
51
+ dataset.push({
52
+ name,
53
+ data
54
+ })
55
+ }
56
+ return {
57
+ dimensions,
58
+ dataset
59
+ }
60
+ }
61
+ export default useTransformChartDataByAttrValue
@@ -0,0 +1,27 @@
1
+ import runCode from '../utils/runCode'
2
+ export const useValueFormatter = (formatter: string, value:string) => {
3
+ if (formatter == null) return value
4
+ const variables = (formatter).match(/\{.*\}/g)
5
+ if (variables == null || variables.length === 0) {
6
+ return value
7
+ }
8
+ const len = variables.length
9
+ for (let i = 0; i < len; i++) {
10
+ const variable = variables[i]
11
+ if (variable == null || variable === '') break
12
+ let statement = variable.slice(1, -1)?.trim()
13
+ statement = statement?.replace(/value/g, value)
14
+ try {
15
+ let result = statement
16
+ if ((/\(.*\)/g).test(statement) || (/[+/\\*]/g).test(statement)) {
17
+ result = runCode(statement)
18
+ }
19
+ formatter = formatter.replace(variable, result)
20
+ } catch (e) {
21
+ formatter = formatter.replace(variable, statement)
22
+ }
23
+ }
24
+ return formatter
25
+ }
26
+
27
+ export default useValueFormatter
@@ -0,0 +1,44 @@
1
+ import runCode from '../utils/runCode'
2
+ export const useVariablesInText = (
3
+ formatter: string = '',
4
+ { textData, codeData }: { textData?: { [key:string]: any }, codeData?: { [key:string]: any } } = {},
5
+ { useNewline = false, useSpace = false } = {}
6
+ ) => {
7
+ // 引入全局变量
8
+ const data = { ...textData }
9
+ const variables = (formatter || '').match(/\{(.+?)\}/g)
10
+ if (variables == null || variables.length === 0) {
11
+ return formatter
12
+ }
13
+ for (let i = 0; i < variables.length; i++) {
14
+ const variable = variables[i]
15
+ let statement = variable.slice(1, -1).trim()
16
+ const reg = /[\u4e00-\u9fa5a-z-_A-Z0-9]*/g
17
+ const textVariables: any = statement.match(reg)
18
+ for (let j = 0; j < textVariables.length; j++) {
19
+ const textVariable = textVariables[j]
20
+ const usedVariable = data[textVariable]
21
+ if (usedVariable != null) {
22
+ // 使用的变量
23
+ statement = statement.replace(textVariable, usedVariable)
24
+ }
25
+ }
26
+ if ((/\(.*\)/g).test(statement) || (/[+/\\*]/g).test(statement)) {
27
+ statement = runCode(statement, codeData)
28
+ }
29
+ formatter = formatter.replace(variable, statement)
30
+ }
31
+ // 替换换行
32
+ if (useNewline) {
33
+ formatter = formatter?.replace(/\n|\\n/g, '<br/>')
34
+ }
35
+ // 替换空格
36
+ if (useSpace) {
37
+ formatter = formatter?.replace(/\s{2,}?/g, '&nbsp;&nbsp;')
38
+ }
39
+ // 千分位格式化
40
+ formatter = formatter.replace(/Qfw\((.*?)\)/, (res, str) => textData?.Qfw ? textData?.Qfw(str) : str)
41
+ return formatter
42
+ }
43
+
44
+ export default useVariablesInText
package/src/indicator.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  import { unref } from 'vue'
2
2
  import dayjs from 'dayjs'
3
3
  import { cloneDeep } from 'lodash-es'
4
- import { hasOwn, lowerCaseIncludes, getConditions, getFormatStep } from './utils'
5
- import { MODEL_KEY_LIST } from './constant'
4
+ import { hasOwn, lowerCaseIncludes, getConditions, getFormatStep } from './utils/util'
5
+ import { MODEL_KEY_LIST } from './utils/constant'
6
6
 
7
7
  let modelValue = new Map()
8
8
  const excludeAttr = [
@@ -0,0 +1,26 @@
1
+ import dayjs from 'dayjs'
2
+ const runCode = (code: string, data?: { [key:string]: any }) => {
3
+ const useFn = {
4
+ dayjs,
5
+ ...data
6
+ }
7
+ let paramsStr = ''
8
+ const params: any = []
9
+ for (const key in useFn) {
10
+ paramsStr += `${key},`
11
+ params.push(useFn[key])
12
+ }
13
+ paramsStr = paramsStr.replace(/,$/, '')
14
+ const runFn = `function (${paramsStr}){return ${code}}`
15
+ let result
16
+ try {
17
+ // eslint-disable-next-line no-new-func
18
+ result = Function(`"use strict";return (${runFn})`)()(...params)
19
+ } catch (e) {
20
+ // console.error()
21
+ return code
22
+ }
23
+ return result
24
+ }
25
+
26
+ export default runCode