Skip to content

Instantly share code, notes, and snippets.

View iwstkhr's full-sized avatar

Takahiro Iwasa iwstkhr

View GitHub Profile
@iwstkhr
iwstkhr / index.ts
Created December 6, 2024 20:55
rehype plugin to make target attributes "_blank"
import { Element, Root } from 'hast'
import { fromHtml } from 'hast-util-from-html';
import { selectAll } from 'hast-util-select';
interface Option {
excludes?: string[];
icon?: boolean;
iconClass?: string;
rel?: 'alternate' | 'author' | 'bookmark' | 'canonical' | 'dns-prefetch' | 'external' | 'help' | 'icon' | 'license' | 'manifest' | 'me' | 'modulepreload' | 'next' | 'nofollow' | 'noopener' | 'noreferrer' | 'opener' | 'pingback' | 'preconnect' | 'prefetch' | 'preload' | 'prerender' | 'prev' | 'privacy-policy' | 'search' | 'stylesheet' | 'tag' | 'terms-of-service';
}
@iwstkhr
iwstkhr / example.js
Last active May 22, 2024 16:28
Mocking JavaScript Date Class with Jest
// For timer mocks, see https://jestjs.io/docs/timer-mocks
// For setSystemTime, see https://jestjs.io/docs/jest-object#jestsetsystemtimenow-number--date
jest.useFakeTimers().setSystemTime(new Date('2023-01-02 12:34:56'));
@iwstkhr
iwstkhr / list_unused_sg.py
Created May 22, 2024 16:18
Listing Unused AWS Security Groups
# Run this scripts like the following:
# python list_unused_sg.py
#
# CLI Options
# --include-inbound: include inbound setting information
# --no-include-inbound: Do not include inbound setting information
# --include-outbound: include outbound setting information
# --no-include-outbound: Do not include outbound setting information
# --output: json or list
@iwstkhr
iwstkhr / inactive_deployments_deleter.py
Created May 22, 2024 16:08
Deleting All Inactive Deployments of AWS IoT Greengrass V2 Components
# Run this scripts like the following:
# python inactive_deployments_deleter.py --target-arn "arn:aws:iot:<AWS_REGION>:<AWS_ACCOUNT_ID>:thing/<YOUR_THING_NAME>"
import argparse
from time import sleep
import boto3
client = boto3.client('greengrassv2')
@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
Last active May 22, 2024 16:30
Downloading Data by Node.js
import fs from 'fs';
export async function download(url: string, file: string): Promise<void> {
// See https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
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!