Skip to content

Commit

Permalink
feat(id): produce UUID with generateId
Browse files Browse the repository at this point in the history
This is the replace the much simpler but prone to problem simple (and small) random Integer generation.

Close gwenaelp#14
  • Loading branch information
TonyMasse committed Dec 12, 2020
1 parent 977f26c commit 9bb9ab2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
18 changes: 15 additions & 3 deletions src/DiagramModel.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
import DiagramNode from "./DiagramNode";

var generateId = function() {
return Math.trunc(Math.random() * 1000);
};
/**
* Generate a UUID (v1)
* @param {Integer} c clock-seq-and-reserved clock-seq-low
* @return {String} The UUID
* http://www.rfcreader.com/#rfc4122_line385 allows random instead of MAC address
* https://www.famkruithof.net/uuid/uuidgen
* https://realityripple.com/Tools/UnUUID/
*/
export function generateId(c = 9999) {
const t = ((Date.now() + 12219292800000) * 1e4).toString(16);
const n = crypto.getRandomValues(new Uint8Array(6)).reduce((sum, x, i) => {
return sum + (i === 0 ? x | 1 : x).toString(16).padStart(2, "0");
}, "");
return `${t.slice(-8)}-${t.slice(-12, -8)}-1${t.slice(0, 3)}-${c}-${n}`;
}

/**
* @class DiagramModel
Expand Down
4 changes: 1 addition & 3 deletions src/DiagramNode.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
var generateId = function() {
return Math.trunc(Math.random() * 1000);
};
import { generateId } from "./DiagramModel";

/**
* @class DiagramNode
Expand Down
10 changes: 3 additions & 7 deletions src/components/Diagram.vue
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,7 @@ import DiagramNode from "./DiagramNode";
import DiagramLink from "./DiagramLink";
import DiagramPort from "./DiagramPort";
var generateId = function() {
return Math.trunc(Math.random() * 1000);
};
import { generateId } from "./../DiagramModel";
function getAbsoluteXY(element) {
var viewportElement = document.documentElement;
Expand Down Expand Up @@ -253,12 +251,10 @@ export default {
links[this.draggedItem.linkIndex].points[
this.draggedItem.pointIndex
].x =
coords.x;
].x = coords.x;
links[this.draggedItem.linkIndex].points[
this.draggedItem.pointIndex
].y =
coords.y;
].y = coords.y;
this.updateLinksPositions();
} else {
let coords = this.convertXYtoViewPort(this.mouseX, this.mouseY);
Expand Down

0 comments on commit 9bb9ab2

Please sign in to comment.