-
Notifications
You must be signed in to change notification settings - Fork 0
/
KeyStore.ts
52 lines (45 loc) · 1.07 KB
/
KeyStore.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import camelCase from 'camelcase'
import * as uuid from 'shortid'
/**
* 唯一名称生成器
*/
export default class KeyStore {
keys: Array<[string, number]>
static format = {
/**
* 格式化为驼峰结构
* @param {*} str
*/
camelCase (str: string, options?) {
return camelCase(str, options)
}
}
constructor () {
this.keys = []
}
/**
* 根据传入名称,生成唯一值
* @param {String} name 名称
* @param {Function} format 格式化方法
* @param {Any} options 格式化方法 参数
*/
generate(name?: string, format?: Function, ...options) {
if (!name) return KeyStore.uuid()
let exist = this.keys.filter(item => item[0] === name)[0]
let rtnStr = name
if (exist === undefined) {
this.keys.push([name, 1])
} else {
let count = exist[1]
rtnStr = `${name.toLowerCase()}${count}`
exist[1] = count + 1
}
return typeof format === 'function' ? format(rtnStr, ...options) : rtnStr
}
/**
* 生成uuid
*/
static uuid () {
return uuid.generate()
}
}