rearrage_stuff
This commit is contained in:
33
resources/js_recipes/recipe - bottomless-javascript-class.md
Executable file
33
resources/js_recipes/recipe - bottomless-javascript-class.md
Executable file
@@ -0,0 +1,33 @@
|
||||
Got it — you’d like the chain of accessed properties to “remember” its path, so you can do things like:
|
||||
|
||||
```js
|
||||
console.log(foo.bar.baz.toString()); // "bar.baz"
|
||||
```
|
||||
|
||||
You can extend the Proxy trick for that:
|
||||
|
||||
```js
|
||||
function Bottomless(path = []) {
|
||||
return new Proxy(() => {}, {
|
||||
get(_, prop) {
|
||||
if (prop === "toString" || prop === "valueOf") {
|
||||
return () => path.join(".");
|
||||
}
|
||||
return Bottomless([...path, prop]);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
const foo = Bottomless();
|
||||
|
||||
console.log(foo.bar.baz.toString()); // "bar.baz"
|
||||
console.log(foo.hello.world.toString()); // "hello.world"
|
||||
```
|
||||
|
||||
⚡ Notes:
|
||||
|
||||
- `toString` (and `valueOf`) are trapped so you can stringify naturally.
|
||||
- The chain isn’t “real” objects anymore, but function proxies that track their path.
|
||||
- You could also add a `.path` property if you prefer structured access.
|
||||
|
||||
Want me to make it so it **still supports assignment** (`foo.bar = 123`) _and_ path stringifying?
|
||||
48
resources/js_recipes/recipe - find-element-width-in-chars.html
Executable file
48
resources/js_recipes/recipe - find-element-width-in-chars.html
Executable file
@@ -0,0 +1,48 @@
|
||||
<!-- CHATGPT EXAMPLE -->
|
||||
<!-- 1. Get the width of the container (in pixels). -->
|
||||
<!-- 2. Get the width of a single character in the monospaced font (this can be done by creating a temporary element with the same font and measuring its width).-->
|
||||
<!-- 3. Divide the container's width by the character's width to get the number of characters. -->
|
||||
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>Measure Div Width in Characters</title>
|
||||
<style>
|
||||
.monospaced-div {
|
||||
font-family: "Courier New", Courier, monospace; /* Monospaced font */
|
||||
width: 360px; /* Example width in pixels */
|
||||
border: 1px solid #333;
|
||||
padding: 10px;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="monospaced-div">This is a div with monospaced text.</div>
|
||||
|
||||
<script>
|
||||
function getMonospacedCharCount(div) {
|
||||
// Create a temporary span to get the width of one character
|
||||
const testChar = document.createElement("span");
|
||||
testChar.textContent = "0"; // Monospaced fonts use "0" for width
|
||||
testChar.style.fontFamily = window.getComputedStyle(div).fontFamily;
|
||||
testChar.style.visibility = "hidden"; // Hide the element
|
||||
document.body.appendChild(testChar);
|
||||
|
||||
const charWidth = testChar.offsetWidth; // Get width of a single character
|
||||
document.body.removeChild(testChar); // Remove the test element
|
||||
|
||||
// Get the width of the div and calculate how many characters fit
|
||||
const divWidth = div.offsetWidth;
|
||||
|
||||
// Return the number of characters that fit in the div width
|
||||
return Math.floor(divWidth / charWidth);
|
||||
}
|
||||
|
||||
const div = document.querySelector(".monospaced-div");
|
||||
const charCount = getMonospacedCharCount(div);
|
||||
console.log("Number of characters the div can hold:", charCount);
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
14
resources/js_recipes/recipe - xorshift32.js
Executable file
14
resources/js_recipes/recipe - xorshift32.js
Executable file
@@ -0,0 +1,14 @@
|
||||
const [XSgetSeed, XSgetNext, XSrand] = (() => {
|
||||
const m = 2 ** 32;
|
||||
const XSgetSeed = () => Math.floor(Math.random() * (m - 1)) + 1;
|
||||
const s = Uint32Array.of(XSgetSeed());
|
||||
return [XSgetSeed, XSgetNext, (seed) => XSgetNext(seed) / m];
|
||||
}
|
||||
|
||||
function XSgetNext(seed) {
|
||||
if (seed !== undefined) s[0] = seed;
|
||||
s[0] ^= s[0] << 13;
|
||||
s[0] ^= s[0] >>> 17;
|
||||
s[0] ^= s[0] << 5;
|
||||
return s[0];
|
||||
}
|
||||
Reference in New Issue
Block a user