Skip to content

Instantly share code, notes, and snippets.

@nexpr
nexpr / image.md
Last active July 10, 2022 08:17
UIライブラリで見かけるようなinputの左上にラベルついてるやつ

image

image

@nexpr
nexpr / fn.php
Created July 9, 2022 04:58
PHPのアロー関数で代入やif分岐できない制限を回避
<?php
$let = fn(...$a) => array_pop($a)(...$a);
$v1 = 10;
$let($v1 + 1, fn($v2) =>
$let($v2 + 1, fn($v3) =>
$let($v3 + 1, fn($v4) =>
var_dump($v1, $v2, $v3, $v4)
@nexpr
nexpr / ReSort.js
Created July 7, 2022 15:08
ユーザーが操作して要素を並び替えられるコンポーネント/Shift+↑↓やShift+クリック
const ReSort = ({ items, onChange, keySelector, itemRender }) => {
const [selected, setSelected] = useState(null)
const onClick = (item) => (event) => {
if (!selected) {
setSelected(item)
} else if (selected === item) {
setSelected(null)
} else {
const selected_index = items.indexOf(selected)
@nexpr
nexpr / HexInput.js
Created June 17, 2022 10:58
hex input react component
import { useState } from "react"
const toHexString = (num) => {
return num.toString(16).toUpperCase()
}
const fromHexString = (hex) => {
return hex ? parseInt(hex, 16) : 0
}
@nexpr
nexpr / dom-example.js
Created June 16, 2022 14:04
create dom element module
import dom from "./dom.js"
// 1
const div = dom.div({ className: "foo" }, [
dom.input(),
dom.input({ type: "checkbox" })
])
document.body.append(div)
@nexpr
nexpr / crlf2lf.py
Created April 25, 2022 13:27
CRLFをLFに一括変換
import sys
import glob
for arg in sys.argv[1:]:
for path in glob.glob(arg, recursive=True):
with open(path, "rb") as f:
text1 = f.read().decode()
text2 = text1.replace("\r\n", "\n")
@nexpr
nexpr / example.js
Last active March 19, 2022 06:08
JavaScript pattern matching
const A = gen()
const B = gen()
const a = A({ v: 0 })
const b = B({ v: 1 })
class C {}
const c = new C()
const values = [
new Date(),
@nexpr
nexpr / lit-emotion.html
Created January 26, 2022 14:13
lit-html + emotion example
<!DOCTYPE html>
<meta charset="utf-8" />
<script>
// for emotion
window.process = { env: {} }
</script>
<script type="module">
import { html, render } from "https://unpkg.com/lit-html@2.1.1/lit-html.js"
import { css, cx } from "https://unpkg.com/@emotion/css@11.7.1/dist/emotion-css.esm.js?module"
<!DOCTYPE html>
<meta charset="utf-8" />
<style>
.flex-container {
display: flex;
flex-flow: row wrap;
gap: 20px;
}
<!DOCTYPE html>
<meta charset="utf-8" />
<style>
body {
margin: 0;
}
.control {
display: inline-block;