This commit is contained in:
mehbark 2023-04-17 00:24:30 -04:00
parent a5b5f3ff40
commit d32e22ee2b
7 changed files with 413 additions and 135 deletions

View file

@ -1,5 +1,6 @@
{
"name": "davehash",
"homepage": "davehash/",
"version": "0.1.0",
"private": true,
"dependencies": {

View file

@ -4,11 +4,8 @@
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<meta name="theme-color" content="#e00707" />
<meta name="description" content="a cool set of hashing algorithms" />
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<!--
manifest.json provides metadata used when your web app is installed on a
@ -24,10 +21,16 @@
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
<title>davehash</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<noscript>
you need to enable javascript to run this app
<br />
i know that's not very cool
<br />
sorry
</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
@ -38,6 +41,5 @@
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
--></body>
</html>

View file

@ -1,6 +1,6 @@
{
"short_name": "React App",
"name": "Create React App Sample",
"short_name": "davehash",
"name": "davehash",
"icons": [
{
"src": "favicon.ico",
@ -18,7 +18,7 @@
"sizes": "512x512"
}
],
"start_url": ".",
"start_url": "./davehash/",
"display": "standalone",
"theme_color": "#000000",
"background_color": "#ffffff"

View file

@ -1,5 +1,6 @@
.App {
text-align: center;
display: inline-block;
}
.App-logo {
@ -14,14 +15,13 @@
}
.App-header {
background-color: #282c34;
background-color: white;
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
font-size: calc(10px + 2vmin);
color: white;
/* justify-content: center; */
font-size: 14px;
}
.App-link {

View file

@ -1,26 +1,65 @@
import React from 'react';
import logo from './logo.svg';
import './App.css';
import React, { useState } from "react";
import "./App.css";
import DaveHash, { hashes, HashFn, ShowHashDesc } from "./DaveHash";
function App() {
let [name, setName] = useState("");
let [yVowel, setYVowel] = useState(true);
let [size, setSize] = useState(10);
let [hash_fn, setHashFn] = useState(() => hashes[0].fn);
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<p>
Edit <code>src/App.tsx</code> and save to reload.
</p>
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
<div className="App App-header">
<input onChange={e => setName(e.target.value)} value={name}></input>
<div style={{ display: "flex" }}>
<p>Y is a vowel:</p>
<input
type="checkbox"
checked={yVowel}
onChange={_ => setYVowel(!yVowel)}
></input>
</div>
<div
style={{
display: "flex",
alignItems: "center",
}}
>
Learn React
</a>
</header>
<p>Sylladex size:</p>
<input
onChange={e => {
let new_size = parseInt(e.target.value, 10);
return setSize(new_size == new_size ? new_size : 0);
}}
value={size}
width="10vw"
></input>
</div>
<div
className="hash-fns"
children={hashes.map((h, i) => (
<div
key={i}
onClick={() => setHashFn(() => h.fn)}
className={
`${h.fn}` == `${hash_fn}` ? "active-hash-fn" : ""
}
>
<ShowHashDesc bla={h.bla} />
</div>
))}
></div>
<br />
<DaveHash
text={name}
y_vowel={yVowel}
sylladex_size={size}
hash_fn={hash_fn}
/>
</div>
);
}
// TODO: more hash functions (configurable) and, like, an actual sylladex thingy (optional detect collisions)
export default App;

177
src/DaveHash.tsx Normal file
View file

@ -0,0 +1,177 @@
import { Fragment } from "react";
export default function DaveHash({
text,
y_vowel = false,
sylladex_size = 10,
hash_fn = basic_hash,
}: {
text: string;
y_vowel?: boolean;
sylladex_size?: number;
hash_fn?: HashFn;
}): JSX.Element {
let letters = text.toUpperCase().split("");
let math_result = (
<span> {math({ letters, y_vowel, sylladex_size, hash_fn })}</span>
);
// children.push();
let letters__ = (
<div style={{ display: "block" }} className="davehash">
<Word letters={letters} y_vowel={y_vowel} hash_fn={hash_fn} />
{math_result}
</div>
);
return letters__;
}
function Word({
letters,
y_vowel,
hash_fn,
}: {
letters: string[];
y_vowel: boolean;
hash_fn: HashFn;
}): JSX.Element {
let children = letters.map((letter, i) => (
<Fragment key={i}>{Letter({ letter, y_vowel, hash_fn })}</Fragment>
));
return <Fragment children={children}></Fragment>;
}
function math({
letters,
y_vowel,
sylladex_size,
hash_fn,
}: {
letters: string[];
y_vowel: boolean;
sylladex_size: number;
hash_fn: HashFn;
}): string {
let nums: number[] = letters
.filter(is_letter)
.map(l => hash_fn(l, y_vowel).value);
let sum = nums.reduce((a, b) => a + b, 0);
return `(${
nums.length > 0 ? nums.join("+") : 0
} = ${sum}%${sylladex_size} = ${sum % sylladex_size})`;
}
type HashResult = { color: Color; value: number };
// prettier-ignore
type AsciiLetter =
| "A" | "B" | "C" | "D" | "E"
| "F" | "G" | "H" | "I" | "J"
| "K" | "L" | "M" | "N" | "O"
| "P" | "Q" | "R" | "S" | "T"
| "U" | "V" | "W" | "X" | "Y"
| "Z";
export type HashFn = (_: AsciiLetter, y_vowel: boolean) => HashResult;
type Color = string;
type HashLetterAssoc = [AsciiLetter, Color, number];
type HashInfo = { fn: HashFn; bla: HashLetterAssoc[] };
function Letter({
letter,
y_vowel,
hash_fn,
}: {
letter: string;
y_vowel: boolean;
hash_fn: HashFn;
}): JSX.Element {
return is_letter(letter) ? (
<span style={{ color: hash_fn(letter, y_vowel).color }}>{letter}</span>
) : (
<span className="space">{letter}</span>
);
}
const basic_hash: HashFn = (l, y_vowel) =>
is_vowel(l, y_vowel)
? { color: "red", value: 1 }
: { color: "blue", value: 2 };
function is_vowel(letter: AsciiLetter, y_vowel: boolean): boolean {
let vowels = "aeiou".split("");
if (y_vowel) {
vowels.push("y");
}
return !vowels.every(v => v != letter.toLowerCase());
}
function is_letter(letter: string): letter is AsciiLetter {
let letters = "QWERTYUIOPASDFGHJKLZXCVBNM".split("");
return !letters.every(v => v != letter);
}
const reversed_hash: HashFn = (l, y_vowel) =>
!is_vowel(l, y_vowel)
? { color: "red", value: 1 }
: { color: "blue", value: 2 };
export function ShowHashDesc({ bla }: { bla: HashLetterAssoc[] }): JSX.Element {
return (
<div
className="hash-desc"
children={bla.map((hla, i) => (
<Fragment key={i}>{ShowHashLetterAssoc({ hla })}</Fragment>
))}
></div>
);
}
function ShowHashLetterAssoc({ hla }: { hla: HashLetterAssoc }): JSX.Element {
return (
<p className="hash-letter-assoc">
<span style={{ color: hla[1] }}>{hla[0].toUpperCase()}</span>=
{hla[2]};<span style={{ color: "#00000000" }}> </span>
</p>
);
}
const scrabble_hash: HashFn = (l, _) => {
// prettier-ignore
let dict = {
A: 1, B: 3, C: 3, D: 2, E: 1,
F: 4, G: 2, H: 4, I: 1, J: 8,
K: 5, L: 1, M: 3, N: 1, O: 1,
P: 3, Q: 10, R: 1, S: 1, T: 1,
U: 1, V: 4, W: 4, X: 8, Y: 4,
Z: 10,
};
return { color: "#05d000", value: dict[l] };
};
export const hashes: HashInfo[] = [
{
fn: basic_hash,
bla: [
["C", "blue", 2],
["V", "red", 1],
],
},
{
fn: reversed_hash,
bla: [
["C", "red", 1],
["V", "blue", 2],
],
},
{
fn: scrabble_hash,
bla: "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
.split("")
.map(l => [
l as AsciiLetter,
"#05d000",
scrabble_hash(l as AsciiLetter, false).value,
]),
},
];

View file

@ -1,13 +1,72 @@
body {
margin: 0;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
sans-serif;
font-family: "Courier New", Courier, monospace;
font-weight: bold;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
white-space: pre;
}
#root {
margin-top: 5vh;
}
code {
font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
font-family: source-code-pro, Menlo, Monaco, Consolas, "Courier New",
monospace;
}
.vowel {
color: red;
}
.consonant {
color: blue;
}
.davehash {
font-size: 14px;
display: flex;
}
* {
margin: 0;
max-width: 100vw;
max-height: 100vh;
}
body {
overflow: hidden;
}
.space {
width: 0.75rem;
}
.hash-desc {
display: block;
cursor: pointer;
width: 40vmin;
margin-left: auto;
margin-right: auto;
/* text-overflow: ellipsis; */
overflow: hidden;
text-align: left;
/* max-height: 17px; */
}
.hash-letter-assoc {
display: inline-block;
}
/* .hash-letter-assoc::after {
content: " ";
} */
.active-hash-fn {
background-color: #e7e7e7;
}
input[type="checkbox"] {
cursor: pointer;
}