rt-sim-training-client/src/jmapNew/utils/Uid.js
2019-12-20 13:48:37 +08:00

60 lines
1.6 KiB
JavaScript

/**
* @public
* @param {string} type
* @return {string}
*/
export function getUID(type, list) {
// 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;
} else {
name = Math.floor((Math.random() * 100000) + 1);
}
// else if (type == 'W') {
// name = list.length ? Number(list[list.length - 1].code.replace('W', '')) + 1 : list.length + 1;
// }
function checkUid(name) {
for (let index = 0; index < list.length; index++) {
if (list[index].code == [(type || ''), name].join('')) {
name++;
return checkUid(name);
}
}
return name;
}
const nameNum = checkUid(name);
if (nameNum) {
return [(type || ''), nameNum].join('');
}
// } else {
// return [(type || ''), Math.floor((Math.random() * 100000) + 1)].join('');
// }
// use Math.random to make id as unique as possible.
}
var base = 0;
/**
* @public
* @param {string} type
* @return {string}
*/
export function setUID(type) {
// use Math.random to make id as unique as possible.
return [(type || ''), base++, Math.random().toFixed(5)].join('_');
}