Skip to content

Instantly share code, notes, and snippets.

View CometstarMH's full-sized avatar
💭
Mehmehmeh

CometstarMH

💭
Mehmehmeh
View GitHub Profile
@CometstarMH
CometstarMH / yarn-run.sh
Last active May 21, 2024 08:04
dialog yarn run
#!/bin/zsh
if [ ! -e package.json ]; then
echo 'package.json does not exist'
exit 1
fi
if [ $(jq -r '.scripts' package.json) = null ]; then
echo 'package.json has no scripts'
exit 2
fi
if [ $(jq -r '.scripts|length' package.json) -eq 0 ]; then
@CometstarMH
CometstarMH / ctorToFunc.ts
Created May 19, 2024 07:13
Typescript: Factory function that accepts a class (ctor function) and the parameters of its ctor
function ctorToFunc<C extends new (...args: any[]) => any, R extends Bar>(
cls: C extends new (...args: any[]) => R ? C : never,
...args: C extends new (...args: infer P) => R ? P : never
): InstanceType<C> {
return new cls(...args);
}
// Constructor Type Literals: https://github.com/Microsoft/TypeScript/blob/v2.6.1/doc/spec.md#3.8.9
// What `class` logically does: https://exploringjs.com/tackling-ts/ch_classes-as-values.html#type-operator-typeof
@CometstarMH
CometstarMH / getKonvaStageWithContexts.jsx
Last active December 7, 2021 10:06
Bridge provided React contexts such that context consumers inside Konva Stage can use the context values that are provided outside.
// Licensed under the MIT license.
import React from 'react';
import { Stage } from 'react-konva';
/**
* Bridge provided React contexts such that context consumers inside Konva Stage
* can use the context values that are provided outside.
*
* Do NOT call this function inside render methods. This function should be
@CometstarMH
CometstarMH / rlines.py
Created June 23, 2020 16:50
Read a *binary* file by lines with mixed EOL in reversed order. No empty lines are skipped. Not unlike https://stackoverflow.com/a/23646049
from itertools import islice
import io
import re
def rlines(f, BLOCK_SIZE = 1024):
f.seek(0, io.SEEK_END)
block_end_byte = f.tell()
block_number = -1
blocks = []
while block_end_byte > 0:
import math
adjlists = {
'A': ['B', 'D', 'H'],
'B': ['A', 'C', 'D'],
'C': ['B', 'D', 'F'],
'D': ['A', 'B', 'C', 'E'],
'E': ['D', 'F', 'H'],
'F': ['C', 'E', 'G'],
'G': ['F', 'H'],
'H': ['A', 'E', 'G']
@CometstarMH
CometstarMH / to_char_dsinterval.sql
Created February 28, 2019 02:41
PL/SQL function to convert dsinterval to varchar2 with "smart" format
create function to_char_dsinterval(period DSINTERVAL_UNCONSTRAINED, fmt varchar2) return varchar2 is
/*
====================================================================================================
Filename : to_char_dsinterval.sql
Author : Bob Lee
Date : 28-Feb-2019
Usage : convert dsinterval to varchar2, specifies format in fmt
recognized format elements are as follows, in regex and case-insensitive:
^s Add head plus/minus sign
s$ Add tail plus/minus sign
@CometstarMH
CometstarMH / format_query_result.sql
Last active February 1, 2019 04:01
Oracle PL/SQL: Format the result of a given SQL query statement in a SQL*Plus-ish manner.
create or replace function format_query_result (
sql_stmt varchar2,
col_formats dbms_sql.varchar2a,
col_lengths dbms_sql.Number_Table,
show_full_col_name boolean default true
) return dbms_sql.varchar2a
authid current_user
is
--TODO: need extra param to allow self-defined column spec
@CometstarMH
CometstarMH / ConstantTimeStringComp.cs
Last active February 1, 2019 03:57
Preventing Timing Attacks by Double HMAC String Comparison with a Random Key in C#. Just in case I forget how to write one.
using System;
using System.Security.Cryptography;
using System.Text;
using System.Linq;
public class Program
{
// adapted from https://paragonie.com/blog/2015/11/preventing-timing-attacks-on-string-comparison-with-double-hmac-strategy
static bool SecuredStringComparison(string x, string y)
{