~statianzo/janet-hashids

55277cd9659b6ce91b3c14df528868a1de2ceeda — Jason Staten 4 years ago
first
5 files changed, 122 insertions(+), 0 deletions(-)

A LICENSE
A hashids.janet
A project.janet
A test/consistent-shuffle.janet
A test/encode.janet
A  => LICENSE +24 -0
@@ 1,24 @@
This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to <http://unlicense.org/>

A  => hashids.janet +37 -0
@@ 1,37 @@
(def DEFAULT_ALPHABET "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890")

(def DEFAULT_SEPS "cfhistuCFHISTU")

(def DEFAULT_SALT "")

(def DEFAULT_MIN_LENGTH 0)

(def MIN_ALPHABET_LENGTH 16)

(def SEP_DIV (/ 7 2))

(def GUARD_DIV 12)


(defn consistent-shuffle [alphabet salt]
  (if (= "" salt) alphabet
    (let [alph-bytes (apply array (string/bytes alphabet))
          salt-bytes (string/bytes salt)]
      (var p 0)

      (loop [i :down [(- (length alph-bytes) 1) 0]]
        (let [v (mod i (length salt-bytes))
              n (get salt-bytes v)]
          (set p (+ p n))
          (def j (mod (+ n v p) i))
          (def a (get alph-bytes j))
          (def b (get alph-bytes i))
          (put alph-bytes i a)
          (put alph-bytes j b)
          ))

      (apply string/from-bytes alph-bytes))))


(defn encode [opts num]
  "j0gW")

A  => project.janet +10 -0
@@ 1,10 @@
(declare-project
  :name "hashids"
  :author "Jason Staten"
  :license "Unlicense"
  :version "0.0.1"
  :url "https://git.sr.ht/~statianzo/janet-hashids"
  :repo "git+https://git.sr.ht/~/statianzo/janet-hashids")

(declare-source
  :source ["hashids.janet"])

A  => test/consistent-shuffle.janet +18 -0
@@ 1,18 @@
(import ../hashids :as h)

(assert
  (= "abc" (h/consistent-shuffle "abc" "")))

(def salt "this is my salt")

(assert
  (= "ba" (h/consistent-shuffle "ab" salt)))

(assert
  (= "bca" (h/consistent-shuffle "abc" salt)))

(assert
  (= "cadb" (h/consistent-shuffle "abcd" salt)))




A  => test/encode.janet +33 -0
@@ 1,33 @@
(import ../hashids :as h)

(assert
  (= "j0gW" (h/encode {} 12345)))

#(assert
  #(= "jR" (h/encode {} 1)))



#@test "encode: single number" {

    #run ./bashids -e 12345

    #[[ "$output" == "j0gW" ]]

    #run ./bashids -e 1

    #[[ "$output" == "jR" ]]

    #run ./bashids -e 22

    #[[ "$output" == "Lw" ]]

    #run ./bashids -e 333

    #[[ "$output" == "Z0E" ]]

    #run ./bashids -e 9999

    #[[ "$output" == "w0rR" ]]

#}