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

34 lines
972 B
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 13:56:18 +08:00
} else if (type == 'W') {
name = list.length ? Number(list[list.length - 1].code.replace('W', '')) + 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);
}
const code = [(type || ''), name].join('');
let count = 0;
for (let index = 0; index < list.length; index++) {
count++;
if (list[index].code == code) {
this.getUID(type, list);
return;
}
}
if (count == list.length) {
return code;
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
}