Skip to content

Instantly share code, notes, and snippets.

@yano3nora
yano3nora / object-key-by-value.ts
Last active July 1, 2023 08:03
[ts: Get object key by own value] #ts
import { objectKeys } from 'libs/utils/object-keys'
/**
* @example
* const obj = { one: 1 }
* const key = objectKeyByValue(obj, 1) // 'one' | undefined
*/
export const objectKeyByValue = <
T extends Record<PropertyKey, U>,
U = unknown
@yano3nora
yano3nora / object-swap.ts
Last active January 28, 2023 07:46
[js: Swap (invert) object's keys and values] #js
/**
* @link https://ultimatecourses.com/blog/reverse-object-keys-and-values-in-javascript
* @example
* const swapped = objectSwap({ x: 1, y: 2 }) // { 1: 'x', 2: 'y' }
* ^^^^^^^ infer Record<1 | 2, 'x' | 'y'>
*/
export const objectSwap = <
T extends PropertyKey,
U extends PropertyKey,
>(object: Record<T, U>) => Object.fromEntries(
@yano3nora
yano3nora / object-has.ts
Created January 26, 2023 17:45
[ts: Object.hasOwn] Force infer object prop with check by Object.hasOwn. #ts
/**
* Force infer object prop with check by Object.hasOwn
*
* @link https://github.com/microsoft/TypeScript/issues/47450#issuecomment-1013737149
* @link https://javascript.plainenglish.io/in-vs-hasown-vs-hasownproperty-in-javascript-885771d2d100
*
* @example
* const myObject = { hello: 'world' }
* const input = prompt('enter key')!
*
@yano3nora
yano3nora / striped-list-by-entities.tsx
Last active March 29, 2023 09:52
[js: striped-list] list component by render prop pattern.
import React, { ReactNode } from 'react'
import { Box, Divider, VStack } from '@chakra-ui/layout'
import { ChakraProps } from '@chakra-ui/system'
/**
* ↓ みたいなやつ作りたい時用
*
* item1
* -------
* item2
@yano3nora
yano3nora / nodemailer.md
Created November 28, 2022 04:15
[js: nodemailer] Send e-mails with Node.JS – easy as cake! #js
@yano3nora
yano3nora / maildev.md
Last active April 2, 2024 04:20
[js: maildev] SMTP Server + Web Interface for viewing and testing emails during development. #js #dev
@yano3nora
yano3nora / group-by.ts
Created October 26, 2022 03:03
[js: group-by] #js
/**
* @link https://qiita.com/nagtkk/items/e1cc3f929b61b1882bd1
* @example
* const result = groupBy(users, user => user.role)
* .filter(([role, _users]) => role === 'admin')
*/
export const groupBy = <K, V>(
array: readonly V[],
getKey: (cur: V, idx: number, src: readonly V[]) => K
): [K, V[]][] =>
@yano3nora
yano3nora / react-player.md
Last active January 25, 2024 06:21
[js: react-player] #js

Overview

github.com/cookpete/react-player

  • react でそのまま使える video, audio 再生プレイヤー
  • youtube などの映像サービスに広く対応
  • 普通に cdn に置いてあるファイルを読んだりとかも ok
$ npm i react-player
@yano3nora
yano3nora / inferable-object-keys.ts
Last active March 15, 2023 04:37
[js: object / keys - Object.fromEntries] Create Object by Arrays. #js
/**
* type inferable Object.keys
*
* NOTE key が number な型定義だと number[] になりつつ
* 「 js の object key は全て string 」という仕様で
* string[] になるので、後続で map(Number) など必要
*/
export const objectKeys = <T extends object>(target: T): (keyof T)[] => (
Object.keys(target).map(key => key as keyof T)
)