├── .gitignore ├── COPYING ├── README.md ├── project.clj └── src └── cljs_uuid_utils └── core.cljs /.gitignore: -------------------------------------------------------------------------------- 1 | pom.xml 2 | *jar 3 | /lib/ 4 | /classes/ 5 | /targets/ 6 | .lein-deps-sum 7 | .lein-failures 8 | .lein-repl-history 9 | target/* 10 | .idea/* 11 | cljs-uuid-utils.iml 12 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | Eclipse Public License - v 1.0 2 | 3 | THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE 4 | PUBLIC LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF 5 | THE PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT. 6 | 7 | 1. DEFINITIONS 8 | 9 | "Contribution" means: 10 | 11 | a) in the case of the initial Contributor, the initial code and 12 | documentation distributed under this Agreement, and 13 | 14 | b) in the case of each subsequent Contributor: 15 | 16 | i) changes to the Program, and 17 | 18 | ii) additions to the Program; 19 | 20 | where such changes and/or additions to the Program originate from and 21 | are distributed by that particular Contributor. A Contribution 22 | 'originates' from a Contributor if it was added to the Program by such 23 | Contributor itself or anyone acting on such Contributor's 24 | behalf. Contributions do not include additions to the Program which: 25 | (i) are separate modules of software distributed in conjunction with 26 | the Program under their own license agreement, and (ii) are not 27 | derivative works of the Program. 28 | 29 | "Contributor" means any person or entity that distributes the Program. 30 | 31 | "Licensed Patents" mean patent claims licensable by a Contributor 32 | which are necessarily infringed by the use or sale of its Contribution 33 | alone or when combined with the Program. 34 | 35 | "Program" means the Contributions distributed in accordance with this 36 | Agreement. 37 | 38 | "Recipient" means anyone who receives the Program under this 39 | Agreement, including all Contributors. 40 | 41 | 2. GRANT OF RIGHTS 42 | 43 | a) Subject to the terms of this Agreement, each Contributor hereby 44 | grants Recipient a non-exclusive, worldwide, royalty-free copyright 45 | license to reproduce, prepare derivative works of, publicly display, 46 | publicly perform, distribute and sublicense the Contribution of such 47 | Contributor, if any, and such derivative works, in source code and 48 | object code form. 49 | 50 | b) Subject to the terms of this Agreement, each Contributor hereby 51 | grants Recipient a non-exclusive, worldwide, royalty-free patent 52 | license under Licensed Patents to make, use, sell, offer to sell, 53 | import and otherwise transfer the Contribution of such Contributor, if 54 | any, in source code and object code form. This patent license shall 55 | apply to the combination of the Contribution and the Program if, at 56 | the time the Contribution is added by the Contributor, such addition 57 | of the Contribution causes such combination to be covered by the 58 | Licensed Patents. The patent license shall not apply to any other 59 | combinations which include the Contribution. No hardware per se is 60 | licensed hereunder. 61 | 62 | c) Recipient understands that although each Contributor grants the 63 | licenses to its Contributions set forth herein, no assurances are 64 | provided by any Contributor that the Program does not infringe the 65 | patent or other intellectual property rights of any other entity. Each 66 | Contributor disclaims any liability to Recipient for claims brought by 67 | any other entity based on infringement of intellectual property rights 68 | or otherwise. As a condition to exercising the rights and licenses 69 | granted hereunder, each Recipient hereby assumes sole responsibility 70 | to secure any other intellectual property rights needed, if any. For 71 | example, if a third party patent license is required to allow 72 | Recipient to distribute the Program, it is Recipient's responsibility 73 | to acquire that license before distributing the Program. 74 | 75 | d) Each Contributor represents that to its knowledge it has sufficient 76 | copyright rights in its Contribution, if any, to grant the copyright 77 | license set forth in this Agreement. 78 | 79 | 3. REQUIREMENTS 80 | 81 | A Contributor may choose to distribute the Program in object code form 82 | under its own license agreement, provided that: 83 | 84 | a) it complies with the terms and conditions of this Agreement; and 85 | 86 | b) its license agreement: 87 | 88 | i) effectively disclaims on behalf of all Contributors all warranties 89 | and conditions, express and implied, including warranties or 90 | conditions of title and non-infringement, and implied warranties or 91 | conditions of merchantability and fitness for a particular purpose; 92 | 93 | ii) effectively excludes on behalf of all Contributors all liability 94 | for damages, including direct, indirect, special, incidental and 95 | consequential damages, such as lost profits; 96 | 97 | iii) states that any provisions which differ from this Agreement are 98 | offered by that Contributor alone and not by any other party; and 99 | 100 | iv) states that source code for the Program is available from such 101 | Contributor, and informs licensees how to obtain it in a reasonable 102 | manner on or through a medium customarily used for software exchange. 103 | 104 | When the Program is made available in source code form: 105 | 106 | a) it must be made available under this Agreement; and 107 | 108 | b) a copy of this Agreement must be included with each copy of the Program. 109 | 110 | Contributors may not remove or alter any copyright notices contained 111 | within the Program. 112 | 113 | Each Contributor must identify itself as the originator of its 114 | Contribution, if any, in a manner that reasonably allows subsequent 115 | Recipients to identify the originator of the Contribution. 116 | 117 | 4. COMMERCIAL DISTRIBUTION 118 | 119 | Commercial distributors of software may accept certain 120 | responsibilities with respect to end users, business partners and the 121 | like. While this license is intended to facilitate the commercial use 122 | of the Program, the Contributor who includes the Program in a 123 | commercial product offering should do so in a manner which does not 124 | create potential liability for other Contributors. Therefore, if a 125 | Contributor includes the Program in a commercial product offering, 126 | such Contributor ("Commercial Contributor") hereby agrees to defend 127 | and indemnify every other Contributor ("Indemnified Contributor") 128 | against any losses, damages and costs (collectively "Losses") arising 129 | from claims, lawsuits and other legal actions brought by a third party 130 | against the Indemnified Contributor to the extent caused by the acts 131 | or omissions of such Commercial Contributor in connection with its 132 | distribution of the Program in a commercial product offering. The 133 | obligations in this section do not apply to any claims or Losses 134 | relating to any actual or alleged intellectual property 135 | infringement. In order to qualify, an Indemnified Contributor must: a) 136 | promptly notify the Commercial Contributor in writing of such claim, 137 | and b) allow the Commercial Contributor tocontrol, and cooperate with 138 | the Commercial Contributor in, the defense and any related settlement 139 | negotiations. The Indemnified Contributor may participate in any such 140 | claim at its own expense. 141 | 142 | For example, a Contributor might include the Program in a commercial 143 | product offering, Product X. That Contributor is then a Commercial 144 | Contributor. If that Commercial Contributor then makes performance 145 | claims, or offers warranties related to Product X, those performance 146 | claims and warranties are such Commercial Contributor's responsibility 147 | alone. Under this section, the Commercial Contributor would have to 148 | defend claims against the other Contributors related to those 149 | performance claims and warranties, and if a court requires any other 150 | Contributor to pay any damages as a result, the Commercial Contributor 151 | must pay those damages. 152 | 153 | 5. NO WARRANTY 154 | 155 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS 156 | PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 157 | KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY 158 | WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY 159 | OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is solely 160 | responsible for determining the appropriateness of using and 161 | distributing the Program and assumes all risks associated with its 162 | exercise of rights under this Agreement , including but not limited to 163 | the risks and costs of program errors, compliance with applicable 164 | laws, damage to or loss of data, programs or equipment, and 165 | unavailability or interruption of operations. 166 | 167 | 6. DISCLAIMER OF LIABILITY 168 | 169 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT NOR 170 | ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, 171 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING 172 | WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF 173 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 174 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OR 175 | DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED 176 | HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 177 | 178 | 7. GENERAL 179 | 180 | If any provision of this Agreement is invalid or unenforceable under 181 | applicable law, it shall not affect the validity or enforceability of 182 | the remainder of the terms of this Agreement, and without further 183 | action by the parties hereto, such provision shall be reformed to the 184 | minimum extent necessary to make such provision valid and enforceable. 185 | 186 | If Recipient institutes patent litigation against any entity 187 | (including a cross-claim or counterclaim in a lawsuit) alleging that 188 | the Program itself (excluding combinations of the Program with other 189 | software or hardware) infringes such Recipient's patent(s), then such 190 | Recipient's rights granted under Section 2(b) shall terminate as of 191 | the date such litigation is filed. 192 | 193 | All Recipient's rights under this Agreement shall terminate if it 194 | fails to comply with any of the material terms or conditions of this 195 | Agreement and does not cure such failure in a reasonable period of 196 | time after becoming aware of such noncompliance. If all Recipient's 197 | rights under this Agreement terminate, Recipient agrees to cease use 198 | and distribution of the Program as soon as reasonably 199 | practicable. However, Recipient's obligations under this Agreement and 200 | any licenses granted by Recipient relating to the Program shall 201 | continue and survive. 202 | 203 | Everyone is permitted to copy and distribute copies of this Agreement, 204 | but in order to avoid inconsistency the Agreement is copyrighted and 205 | may only be modified in the following manner. The Agreement Steward 206 | reserves the right to publish new versions (including revisions) of 207 | this Agreement from time to time. No one other than the Agreement 208 | Steward has the right to modify this Agreement. The Eclipse Foundation 209 | is the initial Agreement Steward. The Eclipse Foundation may assign 210 | the responsibility to serve as the Agreement Steward to a suitable 211 | separate entity. Each new version of the Agreement will be given a 212 | distinguishing version number. The Program (including Contributions) 213 | may always be distributed subject to the version of the Agreement 214 | under which it was received. In addition, after a new version of the 215 | Agreement is published, Contributor may elect to distribute the 216 | Program (including its Contributions) under the new version. Except as 217 | expressly stated in Sections 2(a) and 2(b) above, Recipient receives 218 | no rights or licenses to the intellectual property of any Contributor 219 | under this Agreement, whether expressly, by implication, estoppel or 220 | otherwise. All rights in the Program not expressly granted under this 221 | Agreement are reserved. 222 | 223 | This Agreement is governed by the laws of the State of Washington and 224 | the intellectual property laws of the United States of America. No 225 | party to this Agreement will bring a legal action under this Agreement 226 | more than one year after the cause of action arose. Each party waives 227 | its rights to a jury trial in any resulting litigation. 228 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # cljs-uuid-utils 2 | 3 | This is an official fork of https://github.com/franks42/cljs-uuid-utils in 4 | order to switch maintainers. Thanks Frank Siebenlist. 5 | 6 | PLEASE NOTE THAT THE LEIN COORDINATES AND NAMESPACE HAVE CHANGED (it is no 7 | longer a single segment namespace). 8 | 9 | ClojureScript micro-library with an implementation of a type 4, random UUID generator 10 | compatible with RFC-4122 and cljs.core/UUID (make-random-uuid), 11 | a getter function to obtain the uuid string representation from a UUID-instance 12 | (uuid-string), a uuid-string conformance validating predicate (valid-uuid?), 13 | and a UUID factory from uuid-string with conformance validation (make-uuid-from). 14 | An implementation of a squuid (semi-sequential uuid) generator is also included (make-random-squuid). 15 | 16 | ## Installation 17 | 18 | Add the following to your `project.clj` dependencies: 19 | 20 | ```clojure 21 | [com.lucasbradstreet/cljs-uuid-utils "1.0.2"] 22 | ``` 23 | 24 | ## Usage 25 | 26 | Require cljs-uuid-utils.core namespace: 27 | 28 | ```clojure 29 | (ns myapp 30 | (:require [cljs-uuid-utils.core :as uuid])) 31 | ``` 32 | 33 | REPL examples: 34 | 35 | ```clojure 36 | 37 | ClojureScript:cljs.user> (def r (uuid/make-random-uuid)) 38 | #uuid "ec9b1b11-74b0-48a4-989c-7e939fd37dec" 39 | ClojureScript:cljs.user> (type r) 40 | cljs.core/UUID 41 | 42 | ClojureScript:cljs.user> (uuid/uuid-string r) 43 | "ec9b1b11-74b0-48a4-989c-7e939fd37dec" 44 | 45 | ClojureScript:cljs.user> (uuid/valid-uuid? "NO-WAY") 46 | nil 47 | ClojureScript:cljs.user> (uuid/valid-uuid? "ec9b1b11-74b0-48a4-989c-7e939fd37dec") 48 | "ec9b1b11-74b0-48a4-989c-7e939fd37dec" 49 | ClojureScript:cljs.user> (uuid/valid-uuid? r) 50 | "ec9b1b11-74b0-48a4-989c-7e939fd37dec" 51 | 52 | ClojureScript:cljs.user> (uuid/make-uuid-from "ec9b1b11-74b0-48a4-989c-7e939fd37dec") 53 | #uuid "ec9b1b11-74b0-48a4-989c-7e939fd37dec" 54 | ClojureScript:cljs.user> (uuid/make-uuid-from "NO-WAY") 55 | nil 56 | ClojureScript:cljs.user> (uuid/make-uuid-from r) 57 | #uuid "ec9b1b11-74b0-48a4-989c-7e939fd37dec" 58 | ClojureScript:cljs.user> (uuid/make-uuid-from (UUID. "NO-WAY")) 59 | nil 60 | ClojureScript:cljs.user> (uuid/make-uuid-from (UUID. "ec9b1b11-74b0-48a4-989c-7e939fd37dec")) 61 | #uuid "ec9b1b11-74b0-48a4-989c-7e939fd37dec" 62 | 63 | ClojureScript:cljs.user> (uuid/make-random-squuid) 64 | #uuid "53c5d080-6b6f-4c58-bf42-cebddef27890" 65 | ClojureScript:cljs.user> (uuid/make-random-squuid) 66 | #uuid "53c5d084-bc2b-47ec-add4-c1d667c8ea11" 67 | 68 | ``` 69 | 70 | 71 | ## License 72 | 73 | Copyright (C) 2012 Frank Siebenlist and Lucas Bradstreet 74 | 75 | Distributed under the Eclipse Public License, the same as Clojure. 76 | -------------------------------------------------------------------------------- /project.clj: -------------------------------------------------------------------------------- 1 | (defproject com.lucasbradstreet/cljs-uuid-utils "1.0.2" 2 | :description "ClojureScript micro-library with an implementation of a type 4, random UUID generator compatible with RFC-4122 and cljs.core/UUID (make-random-uuid), a uuid-string conformance validating predicate (valid-uuid?), and a UUID factory from uuid-string with conformance validation (make-uuid-from)." 3 | :url "https://github.com/lbradstreet/cljs-uuid-utils" 4 | :license {:name "Eclipse Public License - v 1.0" 5 | :url "http://www.eclipse.org/legal/epl-v10.html" 6 | :distribution :repo 7 | :comments "See the notice in README.md or details in COPYING"}) 8 | -------------------------------------------------------------------------------- /src/cljs_uuid_utils/core.cljs: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) Frank Siebenlist. All rights reserved. 2 | ;; The use and distribution terms for this software are covered by the 3 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 4 | ;; which can be found in the file COPYING at the root of this distribution. 5 | ;; By using this software in any fashion, you are agreeing to be bound by 6 | ;; the terms of this license. 7 | ;; You must not remove this notice, or any other, from this software. 8 | 9 | (ns cljs-uuid-utils.core 10 | "ClojureScript micro-library with an implementation of a type 4, random UUID generator compatible with RFC-4122 and cljs.core/UUID (make-random-uuid), a getter function to obtain the uuid string representation from a UUID-instance (uuid-string), a uuid-string conformance validating predicate (valid-uuid?), and a UUID factory from uuid-string with conformance validation (make-uuid-from)." 11 | (:require [clojure.string :as string])) 12 | 13 | ;; see https://gist.github.com/4159427 for some background 14 | 15 | 16 | ;; Future UUID-implementations may chose a different internal representation of the UUID-instance 17 | ;; The trivial uuid-string function hides those UUID-internals. 18 | ;; Further motivation for uuid-string are related to interop thru json or with existing databases. 19 | 20 | (defn uuid-string 21 | "(uuid-string a-uuid) => uuid-str 22 | Arguments and Values: 23 | a-uuid --- a cljs.core/UUID instance. 24 | uuid-str --- returns a string representation of the UUID instance 25 | Description: 26 | Returns the string representation of the UUID instance in the format of, 27 | \"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\" similarly to java.util.UUID/toString. 28 | Note that this is different from cljs.core/UUID's EDN string-format. 29 | Examples: 30 | (def u (make-random-uuid)) => #uuid \"305e764d-b451-47ae-a90d-5db782ac1f2e\" 31 | (uuid-string u) => \"305e764d-b451-47ae-a90d-5db782ac1f2e\"" 32 | [a-uuid] 33 | (str (.-uuid a-uuid))) 34 | 35 | 36 | (defn make-random-squuid 37 | "(make-random-squuid) => new-uuid 38 | Arguments and Values: 39 | new-squuid --- new type 4 (pseudo randomly generated) cljs.core/UUID instance. 40 | Description: 41 | Returns pseudo randomly generated, semi-sequential SQUUID. 42 | See http://docs.datomic.com/clojure/#datomic.api/squuid 43 | Returns a UUID where the most significant 32 bits are the current time since epoch in seconds. 44 | like: xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx as per http://www.ietf.org/rfc/rfc4122.txt. 45 | Examples: 46 | (make-random-squuid) => #uuid \"305e764d-b451-47ae-a90d-5db782ac1f2e\" 47 | (type (make-random-squuid)) => cljs.core/UUID" 48 | [] 49 | (letfn [(top-32-bits [] (.toString (int (/ (.getTime (js/Date.)) 1000)) 16)) 50 | (f [] (.toString (rand-int 16) 16)) 51 | (g [] (.toString (bit-or 0x8 (bit-and 0x3 (rand-int 15))) 16))] 52 | (UUID.(string/join (concat 53 | (top-32-bits) "-" 54 | (repeatedly 4 f) "-4" 55 | (repeatedly 3 f) "-" 56 | (g) (repeatedly 3 f) "-" 57 | (repeatedly 12 f))) nil))) 58 | 59 | 60 | (defn make-random-uuid 61 | "(make-random-uuid) => new-uuid 62 | Arguments and Values: 63 | new-uuid --- new type 4 (pseudo randomly generated) cljs.core/UUID instance. 64 | Description: 65 | Returns pseudo randomly generated UUID, 66 | like: xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx as per http://www.ietf.org/rfc/rfc4122.txt. 67 | Examples: 68 | (make-random-uuid) => #uuid \"305e764d-b451-47ae-a90d-5db782ac1f2e\" 69 | (type (make-random-uuid)) => cljs.core/UUID" 70 | [] 71 | (letfn [(f [] (.toString (rand-int 16) 16)) 72 | (g [] (.toString (bit-or 0x8 (bit-and 0x3 (rand-int 15))) 16))] 73 | (UUID.(string/join (concat 74 | (repeatedly 8 f) "-" 75 | (repeatedly 4 f) "-4" 76 | (repeatedly 3 f) "-" 77 | (g) (repeatedly 3 f) "-" 78 | (repeatedly 12 f))) nil))) 79 | 80 | 81 | (def ^:private uuid-regex 82 | (let [x "[0-9a-fA-F]"] (re-pattern (str 83 | "^" x x x x x x x x "-" x x x x "-" x x x x "-" x x x x "-" x x x x x x x x x x x x "$")))) 84 | 85 | 86 | (defn valid-uuid? 87 | "(valid-uuid? maybe-uuid) => truthy-falsy 88 | Arguments and Values: 89 | maybe-uuid --- string or UUID-instance that may represent a conformant UUID. 90 | truthy-falsy --- Returns either the conforming UUID-string (truthy) or nil (falsy). 91 | Description: 92 | Predicate to test whether a string representation conforms to a 93 | \"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\" format where each x is a hexadecimal character. 94 | Input can be a maybe-uuid string or a cljs.core/UUID instance. 95 | Note that the current \"cljs.core/UUID.\" constructor does not check for any conformance. 96 | Examples: 97 | (valid-uuid? \"NO-WAY\") => nil 98 | (valid-uuid? \"4d7332e7-e4c6-4ca5-af91-86336c825e25\") => \"4d7332e7-e4c6-4ca5-af91-86336c825e25\" 99 | (valid-uuid? (UUID. \"4d7332e7-e4c6-4ca5-af91-86336c825e25\")) => \"4d7332e7-e4c6-4ca5-af91-86336c825e25\" 100 | (valid-uuid? (UUID. \"YES-WAY\")) => nil" 101 | [maybe-uuid] 102 | (let [maybe-uuid-str (cond 103 | (= (type maybe-uuid) cljs.core/UUID) (uuid-string maybe-uuid) 104 | (string? maybe-uuid) maybe-uuid 105 | :true false)] 106 | (when maybe-uuid-str (re-find uuid-regex maybe-uuid-str)))) 107 | 108 | 109 | ;; java equivalent "java.util.UUID/fromString" throws: IllegalArgumentException Invalid UUID string: ffa2a001-9eec-4224-a64d java.util.UUID.fromString 110 | ;; make-uuid-from should probably throw an exception also instead of silently returning nil... 111 | 112 | (defn make-uuid-from 113 | "(make-uuid-from maybe-uuid maybe-uuid) => uuid-or-nil 114 | Arguments and Values: 115 | maybe-uuid --- string or UUID-instance that may represent a conformant UUID. 116 | uuid-or-nil --- Returns either a cljs.core/UUID instance or nil. 117 | Description: 118 | Returns a cljs.core/UUID instance for a conformant UUID-string representation, or nil. 119 | Input can be a string or a cljs.core/UUID instance. 120 | Note that if the input UUID-instance is not valid, nil is returned. 121 | Examples: 122 | (make-uuid-from \"NO-WAY\") => nil 123 | (make-uuid-from \"4d7332e7-e4c6-4ca5-af91-86336c825e25\") => #uuid \"4d7332e7-e4c6-4ca5-af91-86336c825e25\" 124 | (make-uuid-from (UUID. \"4d7332e7-e4c6-4ca5-af91-86336c825e25\")) => #uuid \"4d7332e7-e4c6-4ca5-af91-86336c825e25\" 125 | (make-uuid-from (UUID. \"YES-WAY\")) => nil" 126 | [maybe-uuid] 127 | (when-let [uuid (valid-uuid? maybe-uuid)] 128 | (if (= (type maybe-uuid) cljs.core/UUID) 129 | maybe-uuid 130 | (UUID. uuid nil)))) 131 | --------------------------------------------------------------------------------