Skip to content

Instantly share code, notes, and snippets.

@seanchas116
seanchas116 / toIconifyIcon.ts
Created February 10, 2023 05:19
Generate IconifyIcon from SVG data
function toIconifyIcon(svg: string): IconifyIcon {
const body = svg.replace(/<svg[^>]*>/, "").replace(/<\/svg>/, "");
const width = svg.match(/width="(\d+)"/)?.[1];
const height = svg.match(/height="(\d+)"/)?.[1];
return {
body: `<g fill="currentColor">${body}</g>`,
width: width ? Number.parseInt(width) : undefined,
height: height ? Number.parseInt(height) : undefined,
};
@seanchas116
seanchas116 / ListDiff.ts
Created January 14, 2021 09:36
Virtual DOM like list diff using keys
export type ListDiff<T> =
| { type: "insert"; value: T; before?: T }
| { type: "remove"; value: T };
/**
* Virtual DOM like diff algorhtim for arrays
*/
export function listDiff<T extends { key: string }>(
before: T[],
after: T[]
@seanchas116
seanchas116 / firebase-mobx.ts
Created September 21, 2017 10:35
Firebase value as custom MobX observable
import { Atom } from 'mobx'
import * as firebase from 'firebase'
// Custom observable for Firebase value
// see https://mobx.js.org/refguide/extending.html
export class FirebaseWatcher<T> {
private atom: Atom
private ref?: firebase.database.Reference
private _value: T
@seanchas116
seanchas116 / index.html
Last active April 12, 2016 11:47
Simple list data-binding utility idea
<div class="todo-list">
<div class="todo">
<input type="checkbox" class="done">
<h3 class="title">Title</h3>
</div>
</div>
@seanchas116
seanchas116 / pubnet-proxy.rb
Last active December 27, 2015 03:39
titech-pubnetに接続中の場合プロキシ環境変数を設定するだけのスクリプト(Macのみ) シェルでeval `ruby pubnet-proxy.rb`する
def get_ssid
airport = '/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I'
`#{airport}`.split("\n").each do |line|
words = line.split
if words[0] == 'SSID:'
return words[1]
end
end
@seanchas116
seanchas116 / deferred-map.cc
Last active December 23, 2015 06:59
Deferred "map" function for ranges
#include <type_traits>
#include <vector>
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>
template <typename TImpl>
class Enumerator
{
@seanchas116
seanchas116 / extension-method-macro.cpp
Last active December 23, 2015 03:59
A set of C++ macros for defining extension methods
#include <vector>
#include <iostream>
#include <type_traits>
#include <boost/preprocessor.hpp>
#define GET_TYPE(n, array) \
BOOST_PP_TUPLE_ELEM(2, 0, BOOST_PP_ARRAY_ELEM(n, array))
#define GET_ARG(n, array) \
BOOST_PP_TUPLE_ELEM(2, 1, BOOST_PP_ARRAY_ELEM(n, array))
class DoNotation
class DSL
def initialize(&block)
@first_lets = []
@proc_sources = []
@variables = Hash[]
@seanchas116
seanchas116 / parslet-arithmetic.rb
Last active December 21, 2015 13:58
parse and calculate arithmetic expressions with Parslet
require 'parslet'
require 'pp'
class Parser < Parslet::Parser
rule(:lparen) { str('(') >> space? }
rule(:rparen) { str(')') >> space? }
rule(:space) { match('\s').repeat(1) }
rule(:space?) { space.maybe }
@seanchas116
seanchas116 / gist:6306349
Last active December 21, 2015 12:29
僕の考えた最弱な言語
x = 5
y = 10
add = (x, y) =>
x + y
end
z = add x, y
times = (i, fn) =>
iteration = (j) =>
switch j < i