plain |
function isUUID(str) {
return (
str.length === 36 &&
str[8] === '-' &&
str[13] === '-' &&
str[18] === '-' &&
str[23] === '-'
);
}
function parseComputedNodeID(id) {
const last36str = id.slice(id.length - 36);
if (isUUID(last36str)) {
return {
ownerNodeID: id.slice(0, Math.max(id.length - 36 - 1, 0)),
source: last36str,
};
}
const lastHypenIndex = id.lastIndexOf('-');
if (lastHypenIndex === -1) {
return {
ownerNodeID: '',
source: id,
};
}
return {
ownerNodeID: id.slice(0, lastHypenIndex),
source: id.slice(lastHypenIndex + 1),
};
}
ids.forEach(parseComputedNodeID)
| ready |
regex | function parseComputedNodeID(id) {
const uuidPattern =
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
if (!id || id.length === 0) {
return null;
}
const segments = id.split('-');
if (segments.length <= 1) {
return null;
}
for (let i = 0; i < segments.length; i++) {
const lastSegments = segments.slice(segments.length - i - 1);
const potentialId = lastSegments.join('-');
if (uuidPattern.test(potentialId)) {
return {
source: potentialId,
ownerNodeID: segments.slice(0, segments.length - i - 1).join('-'),
};
}
}
return {
source: segments[segments.length - 1],
ownerNodeID: segments.slice(0, segments.length - 1).join('-'),
};
}
ids.forEach(parseComputedNodeID)
| ready |