Skip to content

Instantly share code, notes, and snippets.

View iwstkhr's full-sized avatar

Takahiro Iwasa iwstkhr

View GitHub Profile
@iwstkhr
iwstkhr / example.html
Created April 29, 2024 10:53
Fadein Animation in Tailwind CSS
<div class="animate-fade-in">
Fadein!
</div>
@iwstkhr
iwstkhr / main.ts
Created April 24, 2024 07:25
Downloading Data by Node.js
import fs from 'fs';
export async function download(url: string, file: string): Promise<void> {
const response = await fetch(url);
if (!response.ok) {
return;
}
const data = Buffer.from(await response.arrayBuffer());
fs.writeFileSync(file, data);
}
@iwstkhr
iwstkhr / app.py
Created April 13, 2024 17:12
Converting Japanese Hiragana to Katakana
def hiragana_to_katakana(src: str) -> str:
result = ''
for char in src:
code = ord(char)
result += chr(code + 96) if 12352 < code < 12439 else char
return result
print(hiragana_to_katakana('てすと'))
# It outputs 'テスト'.
@iwstkhr
iwstkhr / test_your_module.py
Created March 17, 2024 19:57
Mocking datetime in Python
from datetime import datetime
from pytest_mock import MockerFixture
def test_your_function(mocker: MockerFixture):
now = datetime.strptime('2024-01-02 12:34:56', '%Y-%m-%d %H:%M:%S')
mock = mocker.MagicMock(wraps=datetime)
mock.now.return_value = now
mocker.patch('src.your_module.datetime', new=mock)
@iwstkhr
iwstkhr / main.ts
Created October 31, 2023 13:32
Generating Hash Values from Texts
export async function getHashByUrl(url: string): Promise<string> {
// https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest
const uint8 = new TextEncoder().encode(url);
const digest = await crypto.subtle.digest('SHA-256', uint8);
return Array.from(new Uint8Array(digest)).map(b => b.toString(16).padStart(2,'0')).join('');
}
@iwstkhr
iwstkhr / example.html
Created September 5, 2023 14:40
Making Checkboxes Read-only by CSS
<input type="checkbox" style="pointer-events: none" checked/>Click me!
<input type="checkbox" checked/>Toggle me!