Skip to content

Instantly share code, notes, and snippets.

View genki's full-sized avatar

Genki Takiuchi genki

View GitHub Profile
@genki
genki / uint8array_serializer.ts
Last active February 28, 2024 07:11
The Uint8Array serializer over valid UTF-16 string.
// pack bytes into valid UTF-16 string
//
// strategy:
//
// * using ESC as the escape character
// * if there is ESC in the bytes, double it
// * if there is unmatched surrogate pair, mark it by the escape character
//
// 0x007f: escape, because it's rare but still only one utf-8 byte.
// To escape itself, use 0x007f 0x08ff (two bytes utf-8)
@genki
genki / outof.ts
Last active January 21, 2024 21:16
`outof`, the valibot utility method, that is a type guard for the `Output` type of the schema as like as the `is` does for `Input` type. In addition, you can use the parsed value if you specify `then` arg.
const outof = <T extends BaseSchema, R,
O = T extends BaseSchema<any, infer O> ? O : never,
>(schema:T, value:unknown, then?:(value:O) => R): value is O => {
const {issues, output} = safeParse(schema, value);
if (issues && issues.length > 0) return false;
if (then) then(output as O);
return true;
};
// USAGE example
export const waiter: {
<T, R, A extends any[]>(
w:Waitable<T>, done:(x:T, ...args:A) => Promise<R>|R
): (...args:A) => Promise<R>;
<R, A extends any[], T=void|undefined>(
w:Waitable<T>, done:(...args:A) => Promise<R>|R
): (...args:A) => Promise<R>;
} = <T, R, A extends any[]>(w:Waitable<T>, done:{
(x:T, ...args:A):Promise<R>|R;
(...args:A):Promise<R>|R;
@genki
genki / submit.ts
Created August 31, 2023 10:17
`submit(formStore, onSubmit$)` method that enables the `modular-forms/qwik` to submit the form equivalent to simulate the manual submit.
import type {
FormStore, FieldValues, ResponseData, FormErrors, FieldPath, FieldArrayPath,
SetResponseOptions,
} from '@modular-forms/qwik';
import {
getValues, validate, setResponse, FormError, setError,
} from '@modular-forms/qwik';
import { type Maybe } from '~/utils';
type ErrorResponseOptions = SetResponseOptions &
The following variables are available, where appropriate:
.Bl -column "XXXXXXXXXXXXXXXXXXX" "XXXXX"
.It Sy "Variable name" Ta Sy "Alias" Ta Sy "Replaced with"
.It Li "alternate_on" Ta "" Ta "If pane is in alternate screen"
.It Li "alternate_saved_x" Ta "" Ta "Saved cursor X in alternate screen"
.It Li "alternate_saved_y" Ta "" Ta "Saved cursor Y in alternate screen"
.It Li "buffer_name" Ta "" Ta "Name of buffer"
.It Li "buffer_sample" Ta "" Ta "Sample of start of buffer"
.It Li "buffer_size" Ta "" Ta "Size of the specified buffer in bytes"
.It Li "client_activity" Ta "" Ta "Integer time client last had activity"
@genki
genki / mr
Last active January 25, 2023 01:25
mr: Move files relatively
#!/usr/bin/ruby
opts, files = ARGV.partition {|a| a =~ /^-/}
first, *middle, last = files
base = File.expand_path File.dirname(first)
to = File.expand_path File.join base, last
system ['mv', *opts, first, *middle, to].join ' '
@genki
genki / chatgpt.rb
Created December 5, 2022 06:38
Talk with ChatGPT from CLI.
#!/usr/bin/ruby
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'shellwords'
end
CHATGPT_URI = "https://chat.openai.com/chat"
def get_page_info
@genki
genki / indent_for_many_args.c
Created November 2, 2022 05:32
My preference is style-2.
// style-1
scanner->sis = grn_scan_info_build(ctx,
scanner->expr,
&(scanner->n_sis),
op,
record_exist);
// style-2
scanner->sis =
grn_scan_info_build
@genki
genki / find_unused.js
Created July 3, 2022 00:09
Find unused indexes out of mongodb
db.<collections>.aggregate({$indexStats:{}})._batch.reduce((h,i) => {h[i.spec.name] = i.accesses.ops; return h}, {})
require "em-synchrony"
def foo
fiber = Fiber.current
EM::Timer.new 3 do
fiber.resume "foo"
end
Fiber.yield
end