Skip to content

Instantly share code, notes, and snippets.

Avatar
🍵

Shuhei Kagawa shuhei

🍵
View GitHub Profile
@shuhei
shuhei / replace-text.js
Last active Nov 21, 2020
Replacing text in a document
View replace-text.js
// Recursive
function replaceText(node, from, to) {
switch (node.nodeType) {
case Node.TEXT_NODE:
node.textContent = node.textContent.replace(from, to);
break;
case Node.ELEMENT_NODE:
for (const childNode of node.childNodes) {
replaceText(childNode, from, to);
}
@shuhei
shuhei / .gitignore
Last active Oct 19, 2020
An example `gulpfile.js` for CoffeeScript and Browserify. Note that this uses gulp-browserify's master. Update this as soon as 0.2.6 is out.
View .gitignore
/node_modules
/build
@shuhei
shuhei / Main.java
Created Feb 23, 2020
Java int overflow
View Main.java
public class Main {
public static void main(String[] args) {
System.out.println(Integer.MIN_VALUE); // -2147483648
System.out.println(Integer.MAX_VALUE); // 2147483647
System.out.println(Integer.MAX_VALUE + 1); // -2147483648
System.out.println(-Integer.MIN_VALUE); // -2147483648
System.out.println(-(Integer.MIN_VALUE + 1)); // 2147483647
}
}
@shuhei
shuhei / README.md
Last active Feb 12, 2020
Firefox http2 beacon bug
View README.md

Create a key pair.

openssl req -x509 -newkey rsa:2048 -nodes -sha256 -subj '/CN=localhost' \
  -keyout localhost-privkey.pem -out localhost-cert.pem

Run the server.

@shuhei
shuhei / convert-textile.js
Created Jan 25, 2020
Convert textile files into markdown files
View convert-textile.js
const { promises: fs } = require("fs");
const textile = require("textile-js");
const prettier = require("prettier");
const path = require("path");
async function main() {
const postsDir = path.resolve("source", "_posts");
const files = (await fs.readdir(postsDir)).filter(file =>
file.endsWith(".textile")
);
@shuhei
shuhei / monty-hall.js
Last active Aug 25, 2019
Monty Hall Problem
View monty-hall.js
const doors = [0, 1, 2];
const tries = 100000;
let pickHasCar = 0;
let theOtherHasCar = 0;
for (let i = 0; i < tries; i++) {
const car = doors[random(3)];
const pick = doors[random(3)];
// Open a door that doesn't have the car.
@shuhei
shuhei / 1-basic.js
Last active Aug 8, 2019
An idea for testing custom React hooks
View 1-basic.js
const React = require("react");
const { mount } = require("enzyme");
// A custom hook to test
function useCounter(initial) {
const [count, setCount] = React.useState(initial);
const increment = React.useCallback(() => setCount(c => c + 1), []);
const reset = React.useCallback(() => setCount(initial), [initial]);
return { count, increment, reset };
}
@shuhei
shuhei / README.md
Created Jul 3, 2019
gzip chunkSize and readable/writableHighWatermark
View README.md

Not much difference...

default: chunkSize 16KB, hwm: 16KB x 20.80 ops/sec ±1.65% (51 runs sampled)
chunkSize 128KB, hwm: 16KB x 22.86 ops/sec ±1.07% (55 runs sampled)
chunkSize 128KB, hwm: 128KB x 21.54 ops/sec ±8.46% (52 runs sampled)
chunkSize 16KB, hwm: 128KB x 21.48 ops/sec ±1.87% (52 runs sampled)
Fastest is [ 'chunkSize 128KB, hwm: 16KB', 'chunkSize 128KB, hwm: 128KB' ]
@shuhei
shuhei / README.md
Last active Jul 3, 2019
jobqueue in JavaScript
View README.md

jobqueue

This is a somewhat evil restaurant. When it has available seats, customers can have seats immediately and stay as long as they want. When the restaurant is full, customers need to wait on a queue. The strange thing is that new customers are led to the head of the queue. If you are the first customer who starts a queue, be prepared to wait for a long time because new customers join in front of you. And the waiter will tell you to leave the queue because you waited too long (timeout) or a new customer is joining the queue but the queue is too long (stack full)!

Implementations

@shuhei
shuhei / couple.rb
Created May 9, 2016
愛を生む二人を探して
View couple.rb
# Extract meaningful bits.
# https://en.wikipedia.org/wiki/UTF-8#Description
def code_bits(bits)
bits.each_slice(8).with_index.map do |byte, i|
i == 0 ? byte[4..-1] : byte[2..-1]
end.flatten
end
# Returns unicode charactor from bits.
def bits_to_char(bits)
You can’t perform that action at this time.