commit 55277cd9659b6ce91b3c14df528868a1de2ceeda Author: Jason Staten Date: Wed May 20 08:51:47 2020 -0600 first diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..68a49da --- /dev/null +++ b/LICENSE @@ -0,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 diff --git a/hashids.janet b/hashids.janet new file mode 100644 index 0000000..e5284d9 --- /dev/null +++ b/hashids.janet @@ -0,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") diff --git a/project.janet b/project.janet new file mode 100644 index 0000000..8662d95 --- /dev/null +++ b/project.janet @@ -0,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"]) diff --git a/test/consistent-shuffle.janet b/test/consistent-shuffle.janet new file mode 100644 index 0000000..647394e --- /dev/null +++ b/test/consistent-shuffle.janet @@ -0,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))) + + + diff --git a/test/encode.janet b/test/encode.janet new file mode 100644 index 0000000..0798cac --- /dev/null +++ b/test/encode.janet @@ -0,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" ]] + +#}