0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-23 23:18:48 -05:00

Move vendor's uuid module into uxbox.util.

This commit is contained in:
Andrey Antukh 2016-06-21 19:50:09 +03:00
parent 967b67f0b1
commit 4607d92c31
No known key found for this signature in database
GPG key ID: 4DFEBCB8316A8B95
11 changed files with 35 additions and 30 deletions

View file

@ -7,7 +7,7 @@
(ns uxbox.common.workers
"A lightweight layer on top of webworkers api."
(:require [beicon.core :as rx]
[uuid.core :as uuid]
[uxbox.util.uuid :as uuid]
[uxbox.util.transit :as t]))
;; --- Implementation

View file

@ -8,7 +8,7 @@
(ns uxbox.main.data.colors
(:require [clojure.set :as set]
[beicon.core :as rx]
[uuid.core :as uuid]
[uxbox.util.uuid :as uuid]
[uxbox.main.state :as st]
[uxbox.common.rstore :as rs]
[uxbox.main.state.colors :as stc]

View file

@ -7,7 +7,7 @@
(ns uxbox.main.data.dashboard
(:require [beicon.core :as rx]
[uuid.core :as uuid]
[uxbox.util.uuid :as uuid]
[uxbox.common.rstore :as rs]
[uxbox.common.router :as r]
[uxbox.main.state :as st]

View file

@ -8,7 +8,7 @@
(ns uxbox.main.data.images
(:require [cuerdas.core :as str]
[beicon.core :as rx]
[uuid.core :as uuid]
[uxbox.util.uuid :as uuid]
[uxbox.common.rstore :as rs]
[uxbox.main.state :as st]
[uxbox.common.repo :as rp]))

View file

@ -7,7 +7,7 @@
(ns uxbox.main.data.shapes
(:require [beicon.core :as rx]
[uuid.core :as uuid]
[uxbox.util.uuid :as uuid]
[uxbox.main.constants :as c]
[uxbox.common.rstore :as rs]
[uxbox.common.router :as r]

View file

@ -7,7 +7,7 @@
(ns uxbox.main.data.workspace
(:require [beicon.core :as rx]
[uuid.core :as uuid]
[uxbox.util.uuid :as uuid]
[uxbox.main.constants :as c]
[uxbox.common.rstore :as rs]
[uxbox.common.schema :as sc]

View file

@ -1,6 +1,6 @@
(ns uxbox.main.state.shapes
"A collection of functions for manage shapes insinde the state."
(:require [uuid.core :as uuid]
(:require [uxbox.util.uuid :as uuid]
[uxbox.util.data :refer (index-of)]
[uxbox.common.geom :as geom]))

View file

@ -4,7 +4,7 @@
;;
;; Copyright (c) 2016 Andrey Antukh <niwi@niwi.nz>
(ns uuid.core
(ns uxbox.util.uuid
"Provides a UUID v4 uuid generation.
In difference with builtin `random-uuid` function this
@ -13,7 +13,7 @@
If no high qualiry RNG, switches to the default Math based
RNG with proper waring in the console."
(:require [uuid.impl :as impl]))
(:require [uxbox.util.uuid.impl :as impl]))
(defn v4
"Generate a v4 (random) UUID."
@ -21,5 +21,5 @@
(uuid (impl/v4)))
(def random
"Alias for `uuid.core/v4`."
"Alias for `uxbox.util.uuid/v4`."
v4)

View file

@ -7,11 +7,12 @@
*/
"use strict";
goog.provide("uuid.impl");
goog.require("uuid.rng");
goog.provide("uxbox.util.uuid.impl");
goog.require("uxbox.util.uuid.rng");
goog.scope(function() {
const rng = uuid.rng;
const impl = uxbox.util.uuid.impl;
const rng = uxbox.util.uuid.rng;
const hexMap = [];
for (let i = 0; i < 256; i++) {
@ -38,7 +39,7 @@ goog.scope(function() {
hexMap[buf[i++]]);
}
uuid.impl.v4 = function() {
impl.v4 = function() {
const buf = rng.getBytes(16);
buf[6] = (buf[6] & 0x0f) | 0x40;
buf[8] = (buf[8] & 0x3f) | 0x80;

View file

@ -8,35 +8,39 @@
"use strict";
goog.provide("uuid.rng");
goog.provide("uxbox.util.uuid.rng");
goog.require("cljs.core");
goog.scope(function() {
const global = goog.global;
const rng = uxbox.util.uuid.rng;
// Check if nodejs rng is available (high quality);
if (cljs.core._STAR_target_STAR_ === "nodejs") {
const crypto = require("crypto");
uuid.rng.getBytes = function(n) {
rng.getBytes = function(n) {
return crypto.randomBytes(n);
};
}
// Check if whatwg rng is available (high quality);
else if (global.crypto !== undefined &&
global.crypto.getRandomValues !== undefined) {
uuid.rng.getBytes = function(n) {
rng.getBytes = function(n) {
const buf = new Uint8Array(16);
global.crypto.getRandomValues(buf);
return buf;
};
}
// Switch Back to the Math.random (low quality);
else {
console.warn("No high quality RNG available, switching back to Math.random.");
uuid.rng.getBytes = function(n) {
const buf = new Array(16);
for (let i = 0, r; i < 16; i++) {
rng.getBytes = function(n) {
const buf = new Array(n);
for (let i = 0, r; i < n; i++) {
if ((i & 0x03) === 0) { r = Math.random() * 0x100000000; }
buf[i] = r >>> ((i & 0x03) << 3) & 0xff;
}

View file

@ -1,7 +1,7 @@
(ns uxbox.tests.shapes-state
(:require [cljs.test :as t :include-macros true]
[cljs.pprint :refer (pprint)]
[uuid.core :as uuid]
[uxbox.util.uuid :as uuid]
[uxbox.main.state.shapes :as sh]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
@ -25,7 +25,7 @@
(assoc-in [:pages-by-id 1 :shapes] [2 1])
(assoc-in [:shapes-by-id 2] {:id 2 :page 1}))]
(with-redefs [uuid.core/random (constantly 2)]
(with-redefs [uxbox.util.uuid/random (constantly 2)]
(let [result (sh/duplicate-shapes initial [1])]
;; (pprint expected)
;; (pprint result)
@ -44,7 +44,7 @@
(assoc-in [:shapes-by-id 1 :items] [5 4 2 3])
(assoc-in [:shapes-by-id 4] {:id 4 :name "3" :page 1 :group 1})
(assoc-in [:shapes-by-id 5] {:id 5 :name "2" :page 1 :group 1}))]
(with-redefs [uuid.core/random (constantly-inc 4)]
(with-redefs [uxbox.util.uuid/random (constantly-inc 4)]
(let [result (sh/duplicate-shapes initial [2 3])]
;; (pprint expected)
;; (pprint result)
@ -65,7 +65,7 @@
(assoc-in [:pages-by-id 1 :shapes] [6 5 1 4])
(assoc-in [:shapes-by-id 5] {:id 5 :name "4" :page 1})
(assoc-in [:shapes-by-id 6] {:id 6 :name "3" :page 1}))]
(with-redefs [uuid.core/random (constantly-inc 5)]
(with-redefs [uxbox.util.uuid/random (constantly-inc 5)]
(let [result (sh/duplicate-shapes initial [3 4])]
;; (pprint expected)
;; (pprint result)
@ -86,7 +86,7 @@
:type :group
:items [4]})
(assoc-in [:shapes-by-id 4] {:id 4 :page 1 :group 3}))]
(with-redefs [uuid.core/random (constantly-inc 3)]
(with-redefs [uxbox.util.uuid/random (constantly-inc 3)]
(let [result (sh/duplicate-shapes initial [1])]
;; (pprint expected)
;; (pprint result)
@ -324,7 +324,7 @@
(assoc-in [:shapes-by-id 2 :group] 4)
(assoc-in [:shapes-by-id 4] {:type :group :name "Group 10"
:items [2] :id 4 :page 1}))]
(with-redefs [uuid.core/random (constantly 4)
(with-redefs [uxbox.util.uuid/random (constantly 4)
cljs.core/rand-int (constantly 10)]
(let [result (sh/group-shapes initial [2] 1)]
(t/is (= result expected))))))
@ -345,7 +345,7 @@
(assoc-in [:shapes-by-id 3 :group] 4)
(assoc-in [:shapes-by-id 4] {:type :group :name "Group 10"
:items [2 3] :id 4 :page 1}))]
(with-redefs [uuid.core/random (constantly 4)
(with-redefs [uxbox.util.uuid/random (constantly 4)
cljs.core/rand-int (constantly 10)]
(let [result (sh/group-shapes initial [2 3] 1)]
(t/is (= result expected))))))
@ -366,7 +366,7 @@
(assoc-in [:shapes-by-id 3 :group] 4)
(assoc-in [:shapes-by-id 4] {:type :group :name "Group 10"
:items [2 3] :id 4 :page 1}))]
(with-redefs [uuid.core/random (constantly 4)
(with-redefs [uxbox.util.uuid/random (constantly 4)
cljs.core/rand-int (constantly 10)]
(let [result (sh/group-shapes initial [2 3] 1)]
(t/is (= result expected))))))
@ -387,7 +387,7 @@
(assoc-in [:shapes-by-id 3 :items] [4])
(assoc-in [:shapes-by-id 4] {:type :group :name "Group 10"
:items [2] :id 4 :page 1 :group 3}))]
(with-redefs [uuid.core/random (constantly 4)
(with-redefs [uxbox.util.uuid/random (constantly 4)
cljs.core/rand-int (constantly 10)]
(let [result (sh/group-shapes initial [2] 1)]
(t/is (= result expected))))))
@ -410,7 +410,7 @@
:items [1 2] :id 5 :page 1})
(update-in [:shapes-by-id] dissoc 3)
(update-in [:shapes-by-id] dissoc 4))]
(with-redefs [uuid.core/random (constantly 5)
(with-redefs [uxbox.util.uuid/random (constantly 5)
cljs.core/rand-int (constantly 10)]
(let [result (sh/group-shapes initial [1 2] 1)]
(t/is (= result expected))))))