37 lines
1.0 KiB
JavaScript
37 lines
1.0 KiB
JavaScript
|
|
/**
|
|
* @public
|
|
* @param {string} type
|
|
* @return {string}
|
|
*/
|
|
export function getUID(type, list) {
|
|
// if (list && list.length) {
|
|
let name = '';
|
|
if (type == 'T') {
|
|
name = list.length ? Number(list[list.length - 1].code.replace('T', '')) + 1 : list.length + 1;
|
|
} else if (type == 'W') {
|
|
name = list.length ? Number(list[list.length - 1].code.replace('W', '')) + 1 : list.length + 1;
|
|
} else {
|
|
name = Math.floor((Math.random() * 100000) + 1);
|
|
}
|
|
function checkUid() {
|
|
let count = 0;
|
|
for (let index = 0; index < list.length; index++) {
|
|
count++;
|
|
if (list[index].code == [(type || ''), name].join('')) {
|
|
name++;
|
|
return checkUid();
|
|
}
|
|
}
|
|
return count == list.length;
|
|
}
|
|
if (checkUid()) {
|
|
return [(type || ''), name].join('');
|
|
}
|
|
// } else {
|
|
// return [(type || ''), Math.floor((Math.random() * 100000) + 1)].join('');
|
|
// }
|
|
// use Math.random to make id as unique as possible.
|
|
}
|
|
|