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

48 lines
1.4 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') {
if (list.length) {
const str = list[list.length - 1].code.substr(0, 1);
if (str == 'T') {
name = Number(list[list.length - 1].code.replace('T', '')) + 1;
} else {
name = 1;
}
} else {
name = list.length + 1;
}
// 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;
// }
2019-12-18 14:47:20 +08:00
function checkUid(name) {
for (let index = 0; index < list.length; index++) {
if (list[index].code == [(type || ''), name].join('')) {
name++;
2019-12-18 14:47:20 +08:00
return checkUid(name);
}
2019-12-06 13:03:45 +08:00
}
2019-12-18 14:47:20 +08:00
return name;
2019-12-06 13:03:45 +08:00
}
2019-12-18 14:47:20 +08:00
const nameNum = checkUid(name);
if (nameNum) {
return [(type || ''), nameNum].join('');
2019-12-06 10:31:19 +08:00
}
2019-12-18 14:47:20 +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
}