-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
321 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,5 @@ writeup | |
output | ||
node_modules | ||
.purs-repl | ||
dist/out.js | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
require('$PSCI')['$main'](); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<!doctype html> | ||
|
||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
|
||
<title>URLNote</title> | ||
|
||
<style> | ||
html, body { | ||
height: 100%; | ||
margin: 0px; | ||
text-align: center; | ||
} | ||
|
||
textarea { | ||
display: block; | ||
margin-left: auto; | ||
margin-right: auto; | ||
margin-top: 10px; | ||
width: 50%; | ||
height: 50%; | ||
border: 3px solid #cccccc; | ||
font-size: 3 rem; | ||
} | ||
</style> | ||
|
||
</head> | ||
|
||
<body> | ||
<h1>URLNote</h1> | ||
<textarea id="input" placeholder="Type your note here"></textarea> | ||
<b>Share this URL:</b> <br/> | ||
<input id="output" readonly type="url" size="50"></input> | ||
<script src="out.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
const PurescriptPlugin = require("esbuild-plugin-purescript"); | ||
const path = require("path"); | ||
|
||
const watch = process.env.DEV ? { | ||
onRebuild(error, result) { | ||
if (error) console.error('watch build failed:', error) | ||
else console.log('watch build succeeded:', result) | ||
} | ||
} : null; | ||
|
||
require('esbuild').build({ | ||
entryPoints: ['src/index.js'], | ||
bundle: true, | ||
outfile: 'dist/out.js', | ||
plugins: [PurescriptPlugin()], | ||
watch, | ||
minify: true | ||
}).catch(() => process.exit(1)) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
module URLNote.BaseConversion where | ||
|
||
import Prelude (class Eq, map, (<<<)) | ||
import Data.Array ((!!), range, zip, length) | ||
import Data.HashMap (HashMap, fromArray, lookup) | ||
import Data.Maybe (Maybe) | ||
import Data.Hashable (class Hashable, hash) | ||
import Data.String.CodePoints (CodePoint, codePointFromChar, singleton, toCodePointArray) | ||
|
||
-- DIGIT | ||
|
||
newtype Digit = Digit CodePoint | ||
|
||
derive instance eqDigit :: Eq Digit | ||
|
||
instance hashableDigit :: Hashable Digit where | ||
hash (Digit cp) = (hash <<< singleton) cp | ||
|
||
-- ALPHABET | ||
|
||
data Alphabet = Alphabet Int (HashMap Int Digit) (HashMap Digit Int) | ||
|
||
fromCodePointArray :: Array CodePoint -> Alphabet | ||
fromCodePointArray arr = Alphabet n indexToDigitMap digitToIndexMap | ||
where n = length arr | ||
digits = map Digit arr | ||
indices = range 0 n | ||
indexToDigitMap = fromArray (zip indices digits) | ||
digitToIndexMap = fromArray (zip digits indices) | ||
|
||
fromString :: String -> Alphabet | ||
fromString = fromCodePointArray <<< toCodePointArray | ||
|
||
indexToDigit :: Alphabet -> Int -> Maybe Digit | ||
indexToDigit (Alphabet _ indexToDigitMap _) index = lookup index indexToDigitMap | ||
|
||
digitToIndex :: Alphabet -> Digit -> Maybe Int | ||
digitToIndex (Alphabet _ _ digitToIndexMap) index = lookup index digitToIndexMap |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
module URLNote.Main where | ||
|
||
import Prelude (otherwise, (<<<), (==), (+), (*), map) | ||
import URLNote.BaseConversion (Alphabet(..), Digit(..), fromString) | ||
import URLNote.BaseConversion as BaseConversion | ||
import Data.String.CodePoints (codePointFromChar, fromCodePointArray, toCodePointArray) | ||
import Data.Char (fromCharCode) | ||
import Data.Array (cons, range) | ||
import Data.Array as Array | ||
import Data.HashMap (lookup) | ||
import Data.BigInt (BigInt, fromInt, toNumber, rem, quot, pow) | ||
import Data.Int (floor) | ||
import Data.Maybe (Maybe(..), fromJust) | ||
import Partial.Unsafe (unsafePartial) | ||
|
||
unsnoc :: String -> Maybe {init :: String, last :: Digit} | ||
unsnoc "" = Nothing | ||
unsnoc string = case (Array.unsnoc <<< toCodePointArray) string of | ||
Just {init, last} -> Just {init: (fromCodePointArray init), last: Digit last} | ||
Nothing -> Nothing | ||
|
||
base83_alphabet = fromString "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ012345689-._:/?#[]@!$&'()*+,;=" :: Alphabet | ||
base256_alphabet = BaseConversion.fromCodePointArray (map (unsafePartial (codePointFromChar <<< fromJust <<< fromCharCode)) (range 0 255)) :: Alphabet | ||
base10FFFF_alphabet = BaseConversion.fromCodePointArray (map (unsafePartial (codePointFromChar <<< fromJust <<< fromCharCode)) (range 0 0x10FFFF)) :: Alphabet | ||
|
||
|
||
-- BigInt constants | ||
bi0 = fromInt 0 :: BigInt | ||
bi1 = fromInt 1 :: BigInt | ||
bi83 = fromInt 83 :: BigInt | ||
bi256 = fromInt 256 :: BigInt | ||
|
||
encode :: String -> String | ||
encode string = (toBase83 <<< fromBase10FFFF) string | ||
|
||
baseEncoder :: Alphabet -> (BigInt -> String) | ||
baseEncoder (Alphabet base indexToDigitMap _) = encode | ||
where encode :: BigInt -> String | ||
encode num = fromCodePointArray (map (\(Digit x) -> x) (encode' num [])) | ||
encode' :: BigInt -> Array Digit -> Array Digit | ||
encode' num digits | ||
| num == bi0 = digits | ||
| otherwise = | ||
let | ||
nextDigit = unsafePartial (fromJust (lookup (floor (toNumber (rem num biBase))) indexToDigitMap)) | ||
in | ||
encode' (quot num biBase) (cons nextDigit digits) | ||
biBase = (fromInt base) | ||
|
||
baseDecoder :: Alphabet -> (String -> BigInt) | ||
baseDecoder (Alphabet base _ digitToIndexMap) = decode | ||
where decode :: String -> BigInt | ||
decode string = decode' string bi0 bi0 | ||
decode' :: String -> BigInt -> BigInt -> BigInt | ||
decode' string index sum = case unsnoc string of | ||
Just {init, last} -> let | ||
nextIndex = unsafePartial (fromJust (lookup last digitToIndexMap)) | ||
in | ||
decode' init (index + bi1) (sum + (fromInt nextIndex) * (pow biBase index)) | ||
Nothing -> sum | ||
biBase = (fromInt base) | ||
|
||
decode :: String -> String | ||
decode string = (toBase10FFFF <<< fromBase83) string | ||
|
||
toBase83 :: BigInt -> String | ||
toBase83 = baseEncoder base83_alphabet | ||
|
||
toBase10FFFF :: BigInt -> String | ||
toBase10FFFF = baseEncoder base10FFFF_alphabet | ||
|
||
fromBase83 :: String -> BigInt | ||
fromBase83 = baseDecoder base83_alphabet | ||
|
||
fromBase10FFFF :: String -> BigInt | ||
fromBase10FFFF = baseDecoder base10FFFF_alphabet |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { encode, decode } from "./URLNote/Main.purs" | ||
|
||
document.getElementById("output").value = window.location.href; | ||
|
||
document.getElementById("input").addEventListener("input", (ev) => { | ||
window.location.hash = encode(ev.target.value); | ||
document.getElementById("output").value = window.location.href; | ||
}); | ||
|
||
const updateTextArea = () => { | ||
document.getElementById("input").value = decode(window.location.hash.substr(1)); | ||
} | ||
|
||
window.addEventListener("load", updateTextArea); | ||
window.addEventListener("hashchange", updateTextArea); |
Oops, something went wrong.