Fork me on GitHub
#clojurescript
<
2020-08-20
>
Kasey Speakman01:08:22

Does random-uuid use browser's basic random? Or does it use crypto random APIs if avail?

Kasey Speakman01:08:31

I see in source it uses rand-int which uses Math/random but I'm not sure where that comes from.

Kasey Speakman01:08:09

k, so not great randomness then

Kasey Speakman01:08:31

In .NET there is also a Math module and I am not familiar with Java namespaces. So I wasn't sure if it had something ported from Java with same name Math or if it was using browser version. That tells me what I wanted to know, thanks.

3
Kasey Speakman02:08:08

Historically some browsers have not just had poor randomness but downright broken implementations of Math.random. Old versions of Chrome would initially repeat the same number many many times, for example. Collecting info for decisions.

lilactown02:08:26

Yeah afaict uuid are typically not meant to be cryptographically secure

dpsutton02:08:03

not sure anything from a browser can ever be so?

Kasey Speakman02:08:05

they are just meant to be unique more than random (not sure how much of a distinction that is). but when randomness is sketchy it won't be much of either.

Kasey Speakman02:08:51

in client-server apps, I have the server generate it. but this is client only offline.

Kasey Speakman02:08:55

previously was using uuid.js when doing this app in F#. https://www.npmjs.com/package/uuid

Kasey Speakman02:08:41

Probably keep using that.

lilactown04:08:55

Yep, that probably will do better

Kasey Speakman04:08:26

Is there an idiomatic way to return a vector with 1 or 2 elements, depending on whether the last element is nil or empty?

Rodrigo Martin04:08:40

Something like (into [] (filter (complement empty?) [x y]))? (not sure if it improves readability, tho)

Kasey Speakman05:08:50

managed to reduce it down to (not-empty (filterv some? [x (not-empty y)])) But not sure how readable that is.

Kasey Speakman05:08:06

The logical version of it is this

(cond (and (not-empty y)
           (some? x)) [x y]
      (some? x) [x]
      :else nil)
But repeats conditions. nested if is the other option

florinbraghis09:08:53

Assuming x and y are collections, you can do (vec (remove empty? [x y])), but that might not produce what you expect if x is also empty. I think the if version is better.

Kasey Speakman10:08:16

x is not a collection, it is a keyword that might be nil

Kasey Speakman10:08:41

Yeah, I went with if

Kasey Speakman04:08:01

Using if currently

Kasey Speakman04:08:41

(if (empty? y)
        [x]
        [x y])

dnolen16:08:44

I thought about cond-> but I think if is shorter/clearer

Kasey Speakman22:08:27

ended up looking like this:

(when (some? name)
      (if-let [params (not-empty path-params)]
        (when-let [converted (reduce convert params param-types)]
          [name converted])
        [name]))

Kasey Speakman22:08:51

The when in the middle is not accidental. I want it to return nil if the params cannot be converted.

Kasey Speakman22:08:52

Thanks all for the help. 🙂