mirror of
https://github.com/penpot/penpot.git
synced 2025-02-09 00:28:20 -05:00
Move vendor's uuid module into uxbox.util.
This commit is contained in:
parent
967b67f0b1
commit
4607d92c31
11 changed files with 35 additions and 30 deletions
|
@ -7,7 +7,7 @@
|
||||||
(ns uxbox.common.workers
|
(ns uxbox.common.workers
|
||||||
"A lightweight layer on top of webworkers api."
|
"A lightweight layer on top of webworkers api."
|
||||||
(:require [beicon.core :as rx]
|
(:require [beicon.core :as rx]
|
||||||
[uuid.core :as uuid]
|
[uxbox.util.uuid :as uuid]
|
||||||
[uxbox.util.transit :as t]))
|
[uxbox.util.transit :as t]))
|
||||||
|
|
||||||
;; --- Implementation
|
;; --- Implementation
|
||||||
|
|
|
@ -8,7 +8,7 @@
|
||||||
(ns uxbox.main.data.colors
|
(ns uxbox.main.data.colors
|
||||||
(:require [clojure.set :as set]
|
(:require [clojure.set :as set]
|
||||||
[beicon.core :as rx]
|
[beicon.core :as rx]
|
||||||
[uuid.core :as uuid]
|
[uxbox.util.uuid :as uuid]
|
||||||
[uxbox.main.state :as st]
|
[uxbox.main.state :as st]
|
||||||
[uxbox.common.rstore :as rs]
|
[uxbox.common.rstore :as rs]
|
||||||
[uxbox.main.state.colors :as stc]
|
[uxbox.main.state.colors :as stc]
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
|
|
||||||
(ns uxbox.main.data.dashboard
|
(ns uxbox.main.data.dashboard
|
||||||
(:require [beicon.core :as rx]
|
(:require [beicon.core :as rx]
|
||||||
[uuid.core :as uuid]
|
[uxbox.util.uuid :as uuid]
|
||||||
[uxbox.common.rstore :as rs]
|
[uxbox.common.rstore :as rs]
|
||||||
[uxbox.common.router :as r]
|
[uxbox.common.router :as r]
|
||||||
[uxbox.main.state :as st]
|
[uxbox.main.state :as st]
|
||||||
|
|
|
@ -8,7 +8,7 @@
|
||||||
(ns uxbox.main.data.images
|
(ns uxbox.main.data.images
|
||||||
(:require [cuerdas.core :as str]
|
(:require [cuerdas.core :as str]
|
||||||
[beicon.core :as rx]
|
[beicon.core :as rx]
|
||||||
[uuid.core :as uuid]
|
[uxbox.util.uuid :as uuid]
|
||||||
[uxbox.common.rstore :as rs]
|
[uxbox.common.rstore :as rs]
|
||||||
[uxbox.main.state :as st]
|
[uxbox.main.state :as st]
|
||||||
[uxbox.common.repo :as rp]))
|
[uxbox.common.repo :as rp]))
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
|
|
||||||
(ns uxbox.main.data.shapes
|
(ns uxbox.main.data.shapes
|
||||||
(:require [beicon.core :as rx]
|
(:require [beicon.core :as rx]
|
||||||
[uuid.core :as uuid]
|
[uxbox.util.uuid :as uuid]
|
||||||
[uxbox.main.constants :as c]
|
[uxbox.main.constants :as c]
|
||||||
[uxbox.common.rstore :as rs]
|
[uxbox.common.rstore :as rs]
|
||||||
[uxbox.common.router :as r]
|
[uxbox.common.router :as r]
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
|
|
||||||
(ns uxbox.main.data.workspace
|
(ns uxbox.main.data.workspace
|
||||||
(:require [beicon.core :as rx]
|
(:require [beicon.core :as rx]
|
||||||
[uuid.core :as uuid]
|
[uxbox.util.uuid :as uuid]
|
||||||
[uxbox.main.constants :as c]
|
[uxbox.main.constants :as c]
|
||||||
[uxbox.common.rstore :as rs]
|
[uxbox.common.rstore :as rs]
|
||||||
[uxbox.common.schema :as sc]
|
[uxbox.common.schema :as sc]
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
(ns uxbox.main.state.shapes
|
(ns uxbox.main.state.shapes
|
||||||
"A collection of functions for manage shapes insinde the state."
|
"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.util.data :refer (index-of)]
|
||||||
[uxbox.common.geom :as geom]))
|
[uxbox.common.geom :as geom]))
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
;;
|
;;
|
||||||
;; Copyright (c) 2016 Andrey Antukh <niwi@niwi.nz>
|
;; Copyright (c) 2016 Andrey Antukh <niwi@niwi.nz>
|
||||||
|
|
||||||
(ns uuid.core
|
(ns uxbox.util.uuid
|
||||||
"Provides a UUID v4 uuid generation.
|
"Provides a UUID v4 uuid generation.
|
||||||
|
|
||||||
In difference with builtin `random-uuid` function this
|
In difference with builtin `random-uuid` function this
|
||||||
|
@ -13,7 +13,7 @@
|
||||||
|
|
||||||
If no high qualiry RNG, switches to the default Math based
|
If no high qualiry RNG, switches to the default Math based
|
||||||
RNG with proper waring in the console."
|
RNG with proper waring in the console."
|
||||||
(:require [uuid.impl :as impl]))
|
(:require [uxbox.util.uuid.impl :as impl]))
|
||||||
|
|
||||||
(defn v4
|
(defn v4
|
||||||
"Generate a v4 (random) UUID."
|
"Generate a v4 (random) UUID."
|
||||||
|
@ -21,5 +21,5 @@
|
||||||
(uuid (impl/v4)))
|
(uuid (impl/v4)))
|
||||||
|
|
||||||
(def random
|
(def random
|
||||||
"Alias for `uuid.core/v4`."
|
"Alias for `uxbox.util.uuid/v4`."
|
||||||
v4)
|
v4)
|
|
@ -7,11 +7,12 @@
|
||||||
*/
|
*/
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
goog.provide("uuid.impl");
|
goog.provide("uxbox.util.uuid.impl");
|
||||||
goog.require("uuid.rng");
|
goog.require("uxbox.util.uuid.rng");
|
||||||
|
|
||||||
goog.scope(function() {
|
goog.scope(function() {
|
||||||
const rng = uuid.rng;
|
const impl = uxbox.util.uuid.impl;
|
||||||
|
const rng = uxbox.util.uuid.rng;
|
||||||
const hexMap = [];
|
const hexMap = [];
|
||||||
|
|
||||||
for (let i = 0; i < 256; i++) {
|
for (let i = 0; i < 256; i++) {
|
||||||
|
@ -38,7 +39,7 @@ goog.scope(function() {
|
||||||
hexMap[buf[i++]]);
|
hexMap[buf[i++]]);
|
||||||
}
|
}
|
||||||
|
|
||||||
uuid.impl.v4 = function() {
|
impl.v4 = function() {
|
||||||
const buf = rng.getBytes(16);
|
const buf = rng.getBytes(16);
|
||||||
buf[6] = (buf[6] & 0x0f) | 0x40;
|
buf[6] = (buf[6] & 0x0f) | 0x40;
|
||||||
buf[8] = (buf[8] & 0x3f) | 0x80;
|
buf[8] = (buf[8] & 0x3f) | 0x80;
|
|
@ -8,35 +8,39 @@
|
||||||
|
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
goog.provide("uuid.rng");
|
goog.provide("uxbox.util.uuid.rng");
|
||||||
goog.require("cljs.core");
|
goog.require("cljs.core");
|
||||||
|
|
||||||
goog.scope(function() {
|
goog.scope(function() {
|
||||||
const global = goog.global;
|
const global = goog.global;
|
||||||
|
const rng = uxbox.util.uuid.rng;
|
||||||
|
|
||||||
|
|
||||||
// Check if nodejs rng is available (high quality);
|
// Check if nodejs rng is available (high quality);
|
||||||
if (cljs.core._STAR_target_STAR_ === "nodejs") {
|
if (cljs.core._STAR_target_STAR_ === "nodejs") {
|
||||||
const crypto = require("crypto");
|
const crypto = require("crypto");
|
||||||
|
|
||||||
uuid.rng.getBytes = function(n) {
|
rng.getBytes = function(n) {
|
||||||
return crypto.randomBytes(n);
|
return crypto.randomBytes(n);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if whatwg rng is available (high quality);
|
// Check if whatwg rng is available (high quality);
|
||||||
else if (global.crypto !== undefined &&
|
else if (global.crypto !== undefined &&
|
||||||
global.crypto.getRandomValues !== undefined) {
|
global.crypto.getRandomValues !== undefined) {
|
||||||
uuid.rng.getBytes = function(n) {
|
rng.getBytes = function(n) {
|
||||||
const buf = new Uint8Array(16);
|
const buf = new Uint8Array(16);
|
||||||
global.crypto.getRandomValues(buf);
|
global.crypto.getRandomValues(buf);
|
||||||
return buf;
|
return buf;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// Switch Back to the Math.random (low quality);
|
// Switch Back to the Math.random (low quality);
|
||||||
else {
|
else {
|
||||||
console.warn("No high quality RNG available, switching back to Math.random.");
|
console.warn("No high quality RNG available, switching back to Math.random.");
|
||||||
uuid.rng.getBytes = function(n) {
|
rng.getBytes = function(n) {
|
||||||
const buf = new Array(16);
|
const buf = new Array(n);
|
||||||
for (let i = 0, r; i < 16; i++) {
|
for (let i = 0, r; i < n; i++) {
|
||||||
if ((i & 0x03) === 0) { r = Math.random() * 0x100000000; }
|
if ((i & 0x03) === 0) { r = Math.random() * 0x100000000; }
|
||||||
buf[i] = r >>> ((i & 0x03) << 3) & 0xff;
|
buf[i] = r >>> ((i & 0x03) << 3) & 0xff;
|
||||||
}
|
}
|
|
@ -1,7 +1,7 @@
|
||||||
(ns uxbox.tests.shapes-state
|
(ns uxbox.tests.shapes-state
|
||||||
(:require [cljs.test :as t :include-macros true]
|
(:require [cljs.test :as t :include-macros true]
|
||||||
[cljs.pprint :refer (pprint)]
|
[cljs.pprint :refer (pprint)]
|
||||||
[uuid.core :as uuid]
|
[uxbox.util.uuid :as uuid]
|
||||||
[uxbox.main.state.shapes :as sh]))
|
[uxbox.main.state.shapes :as sh]))
|
||||||
|
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
|
@ -25,7 +25,7 @@
|
||||||
(assoc-in [:pages-by-id 1 :shapes] [2 1])
|
(assoc-in [:pages-by-id 1 :shapes] [2 1])
|
||||||
(assoc-in [:shapes-by-id 2] {:id 2 :page 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])]
|
(let [result (sh/duplicate-shapes initial [1])]
|
||||||
;; (pprint expected)
|
;; (pprint expected)
|
||||||
;; (pprint result)
|
;; (pprint result)
|
||||||
|
@ -44,7 +44,7 @@
|
||||||
(assoc-in [:shapes-by-id 1 :items] [5 4 2 3])
|
(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 4] {:id 4 :name "3" :page 1 :group 1})
|
||||||
(assoc-in [:shapes-by-id 5] {:id 5 :name "2" :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])]
|
(let [result (sh/duplicate-shapes initial [2 3])]
|
||||||
;; (pprint expected)
|
;; (pprint expected)
|
||||||
;; (pprint result)
|
;; (pprint result)
|
||||||
|
@ -65,7 +65,7 @@
|
||||||
(assoc-in [:pages-by-id 1 :shapes] [6 5 1 4])
|
(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 5] {:id 5 :name "4" :page 1})
|
||||||
(assoc-in [:shapes-by-id 6] {:id 6 :name "3" :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])]
|
(let [result (sh/duplicate-shapes initial [3 4])]
|
||||||
;; (pprint expected)
|
;; (pprint expected)
|
||||||
;; (pprint result)
|
;; (pprint result)
|
||||||
|
@ -86,7 +86,7 @@
|
||||||
:type :group
|
:type :group
|
||||||
:items [4]})
|
:items [4]})
|
||||||
(assoc-in [:shapes-by-id 4] {:id 4 :page 1 :group 3}))]
|
(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])]
|
(let [result (sh/duplicate-shapes initial [1])]
|
||||||
;; (pprint expected)
|
;; (pprint expected)
|
||||||
;; (pprint result)
|
;; (pprint result)
|
||||||
|
@ -324,7 +324,7 @@
|
||||||
(assoc-in [:shapes-by-id 2 :group] 4)
|
(assoc-in [:shapes-by-id 2 :group] 4)
|
||||||
(assoc-in [:shapes-by-id 4] {:type :group :name "Group 10"
|
(assoc-in [:shapes-by-id 4] {:type :group :name "Group 10"
|
||||||
:items [2] :id 4 :page 1}))]
|
: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)]
|
cljs.core/rand-int (constantly 10)]
|
||||||
(let [result (sh/group-shapes initial [2] 1)]
|
(let [result (sh/group-shapes initial [2] 1)]
|
||||||
(t/is (= result expected))))))
|
(t/is (= result expected))))))
|
||||||
|
@ -345,7 +345,7 @@
|
||||||
(assoc-in [:shapes-by-id 3 :group] 4)
|
(assoc-in [:shapes-by-id 3 :group] 4)
|
||||||
(assoc-in [:shapes-by-id 4] {:type :group :name "Group 10"
|
(assoc-in [:shapes-by-id 4] {:type :group :name "Group 10"
|
||||||
:items [2 3] :id 4 :page 1}))]
|
: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)]
|
cljs.core/rand-int (constantly 10)]
|
||||||
(let [result (sh/group-shapes initial [2 3] 1)]
|
(let [result (sh/group-shapes initial [2 3] 1)]
|
||||||
(t/is (= result expected))))))
|
(t/is (= result expected))))))
|
||||||
|
@ -366,7 +366,7 @@
|
||||||
(assoc-in [:shapes-by-id 3 :group] 4)
|
(assoc-in [:shapes-by-id 3 :group] 4)
|
||||||
(assoc-in [:shapes-by-id 4] {:type :group :name "Group 10"
|
(assoc-in [:shapes-by-id 4] {:type :group :name "Group 10"
|
||||||
:items [2 3] :id 4 :page 1}))]
|
: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)]
|
cljs.core/rand-int (constantly 10)]
|
||||||
(let [result (sh/group-shapes initial [2 3] 1)]
|
(let [result (sh/group-shapes initial [2 3] 1)]
|
||||||
(t/is (= result expected))))))
|
(t/is (= result expected))))))
|
||||||
|
@ -387,7 +387,7 @@
|
||||||
(assoc-in [:shapes-by-id 3 :items] [4])
|
(assoc-in [:shapes-by-id 3 :items] [4])
|
||||||
(assoc-in [:shapes-by-id 4] {:type :group :name "Group 10"
|
(assoc-in [:shapes-by-id 4] {:type :group :name "Group 10"
|
||||||
:items [2] :id 4 :page 1 :group 3}))]
|
: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)]
|
cljs.core/rand-int (constantly 10)]
|
||||||
(let [result (sh/group-shapes initial [2] 1)]
|
(let [result (sh/group-shapes initial [2] 1)]
|
||||||
(t/is (= result expected))))))
|
(t/is (= result expected))))))
|
||||||
|
@ -410,7 +410,7 @@
|
||||||
:items [1 2] :id 5 :page 1})
|
:items [1 2] :id 5 :page 1})
|
||||||
(update-in [:shapes-by-id] dissoc 3)
|
(update-in [:shapes-by-id] dissoc 3)
|
||||||
(update-in [:shapes-by-id] dissoc 4))]
|
(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)]
|
cljs.core/rand-int (constantly 10)]
|
||||||
(let [result (sh/group-shapes initial [1 2] 1)]
|
(let [result (sh/group-shapes initial [1 2] 1)]
|
||||||
(t/is (= result expected))))))
|
(t/is (= result expected))))))
|
||||||
|
|
Loading…
Add table
Reference in a new issue