Skip to content

Commit

Permalink
feat(id): produce UUID with generateId. move to /utils/Identifier.js
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.
And moved the function to its own `/utils/Identifier.js` file for ease of maintenance.

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

var generateId = function() {
return Math.trunc(Math.random() * 1000);
};
import { generateId } from "./utils/Identifier";

/**
* @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 "./utils/Identifier";

/**
* @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 "./../utils/Identifier";
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
15 changes: 15 additions & 0 deletions src/utils/Identifier.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* 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}`;
}

0 comments on commit bebc5e4

Please sign in to comment.