Skip to content

Instantly share code, notes, and snippets.

@tompng
tompng / generator.rb
Last active Jul 30, 2020
めでたい
View generator.rb
require 'chunky_png'
img = ChunkyPNG::Image.from_file 'medetai.png'
imgdata = (1...img.height-1).map{|y|img.width.times.map{|x|img[x,y]>>8==0}}
compressed = imgdata.flatten.chunk(&:itself).map{|a,b|b.size}
puts (0..76).min_by{|i|compressed.map{|a|(a+i).chr}.to_json.size rescue 9999}
data = "3%U&T%+&>,.&6%=&)&->-&%%0&.&-&$.*5($(3/&/&-63''\#$&&-5%1&+'&&'&1&(&#%*&%0'%2&(*$&)&/&+$.%'(.%3%'&$*+&.%9&9%3&&%&(,&-&8&:%3&%&&'-&.%7&&#6&$%.&%%%)+&/'5%'%5%$%.&%+,(1)1%(&5)7''-6++&)2)&C)>'*&-/@"
data = "4&V'U&,'?-/'7&>'*'.?.'&&1'/'.'%/+6)%)40'0'.74(($%''.6&2',(''('2')'$&+'&1(&3')+%'*'0',%/&()/&4&('%+,'/&:':&4''&')-'.'9';&4'&''(.'/&8''$7'%&/'&&&*,'0(6&(&6&%&/'&,-)2*2&)'6*8((.7,,'*3*'D*?(+'.0A"
compressed = data.chars.map{_1.ord-34}
puts compressed.map.with_index{|a,i|[i%2]*a}.flatten.each_slice(120).map(&:join)
View interpolatever.ts
export const boardsPath = '/boards(.:format)'
export const boardPath = '/boards/:id(.:format)'
type Routes = {
[boardsPath]: {
GET: { params: { format: 'json' | 'html'; color?: string }; response: { a: string } }
POST: { params: { color: string; size: number }; response: { b: string } }
DELETE: { params: {}; response: { c: string } }
}
[boardPath]: {
View host.rb
require 'socket'
require 'securerandom'
require 'digest'
# require 'io/console'
require_relative './util'
# $> << '>'
password = 'password' #STDIN.noecho { STDIN.gets }.strip
server_host, server_port = ARGV[0].split(':')
local_port = ARGV[1]
socket = TCPSocket.new server_host, server_port.to_i
View type1.ts
export const fooPath = 'aa/:id/:foo/:bar'
type AllMethodPathAndParams = {
[fooPath]: {
GET: { id: number }
POST: undefined
}
}
function f<M extends 'GET' | 'POST', K extends keyof AllMethodPathAndParams>(
View ika_diacriticalmark.rb
def render(w, h)
subchar_none = "\u030d"
# Mac Terminal Courier
mac_terminal_courier = [3, 11, "\u031a"]
mac_chrome_courier = [3, 11, "\u0342"]
mac_slack = [2, 14, "\u0342"]
line_interval, subcahr_per_line, subchar_fill = mac_terminal_courier
((h + subcahr_per_line)/2).times do |y1|
View method_path_params_type.ts
type MethodPathParams = {
get: {
'foo/:fooId': {
fooId: number
foo?: number
}
'bar/:baz': {
baz: number
bar?: number
}
View unsafe_key_lock.rb
require'set'
keys=('0000'..'9999').to_a
sets = Set.new keys
queue=[]
k = keys.first
sets.delete k
list = k.chars.map(&:to_i)
k = k[1..]
n = 0
loop do
@tompng
tompng / svg_path_extaract_xy.rb
Created Jun 2, 2020
extract xy from svg path
View svg_path_extaract_xy.rb
def hoge s
hoges = s.scan(/[A-Za-z]|-?\d*\.\d*|\d+/).map{|a|a =~ /[\d.]/ ? a.to_f : a}
p hoges
x=y=0
out=[]
while hoges.first
begin
c = hoges.shift
case c
when 'M'
View context_with_selector.tsx
import React, { createContext, useRef, useEffect, useContext, useState } from 'react'
type Selector<T, U = any> = (value: T) => U
function shallowEqual(a: unknown, b: unknown): boolean {
if (a === b) return true
if (typeof a === 'object' && typeof b === 'object') {
if (a == null || b == null) return a === b
if (Array.isArray(a) || Array.isArray(b)) {
if (!Array.isArray(a) || !Array.isArray(b) || a.length !== b.length) return false
return a.every((v, i) => v == b[i])
}
View sound.js
function waveToDataURL(wave){
const f = n => '%' + (n >> 4).toString(16) + (n & 0xf).toString(16)
const f2 = n => f(n & 0xff) + f(n >> 8)
const f4 = n => f2(n & 0xffff) + f2(n >> 16)
return 'data:audio/wav,' + [
'RIFF',
f4(wave.length * 2 + 32),
'WAVEfmt%20',
f4(16),
f4(0x10001),
You can’t perform that action at this time.