rt-sim-training-client/src/jmapNew/utils/Uid.js

38 lines
1.0 KiB
JavaScript
Raw Normal View History

2019-11-29 12:51:58 +08:00
/**
* @public
* @param {string} type
* @return {string}
*/
2019-12-06 10:31:19 +08:00
export function getUID(type, list) {
2019-12-06 13:03:45 +08:00
// if (list && list.length) {
let name = '';
if (type == 'T') {
name = list.length ? Number(list[list.length - 1].code.replace('T', '')) + 1 : list.length + 1;
2019-12-06 10:31:19 +08:00
} else {
2019-12-06 13:03:45 +08:00
name = Math.floor((Math.random() * 100000) + 1);
}
2019-12-09 17:48:46 +08:00
// else if (type == 'W') {
// name = list.length ? Number(list[list.length - 1].code.replace('W', '')) + 1 : list.length + 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();
}
2019-12-06 13:03:45 +08:00
}
return count == list.length;
2019-12-06 13:03:45 +08:00
}
if (checkUid()) {
return [(type || ''), name].join('');
2019-12-06 10:31:19 +08:00
}
2019-12-06 13:03:45 +08:00
// } else {
// return [(type || ''), Math.floor((Math.random() * 100000) + 1)].join('');
// }
2019-12-05 16:17:54 +08:00
// use Math.random to make id as unique as possible.
2019-11-29 12:51:58 +08:00
}