Skip to content

Instantly share code, notes, and snippets.

@maruware
maruware / array-util.ts
Created November 20, 2018 04:02
TypeScript: find map value with promise
const findMapPromises = async <T, S> (arr: T[], predicate: (value: T, index: number, obj: T[]) => Promise<S>): Promise<S> => {
for (let i = 0; i < arr.length; i++) {
try {
const r = await predicate(arr[i], i, arr)
return r
} catch (e) {
// next.
}
}
return null
@maruware
maruware / Link.tsx
Last active June 14, 2023 02:51
custom Link component with material-ui + react-router-dom on TypeScript
@maruware
maruware / basic-auth-s3-cloudfront-lambda-allinone.yaml
Last active April 17, 2019 11:02
Basic auth ss3+cloudfront
AWSTemplateFormatVersion: '2010-09-09'
Description: Static contents distribution using S3 and CloudFront with basic authentication by lambda@Edge
Parameters:
AuthUser:
Description: ID for basic authentication
Type: String
Default: user2018
AuthPass:
Description: Password for basic authentication
Type: String
@maruware
maruware / koa-socketio.ts
Last active April 18, 2019 09:23
koa + socket.io
import { BaseContext } from 'koa'
declare module 'koa' {
interface BaseContext {
io: SocketIO.Server
}
}
export default (io: SocketIO.Server) => async (
ctx: BaseContext,
@maruware
maruware / svg_to_png.ts
Created July 23, 2019 09:29
SVG to PNG through Canvas
export const svgToPng = async (svg: SVGSVGElement) => {
const w = svg.getAttribute('width')
const h = svg.getAttribute('height')
if (!w || !h) {
throw new Error('Undefined svg size')
}
const width = parseInt(w)
const height = parseInt(h)
@maruware
maruware / base64.go
Last active August 2, 2019 12:16
[Go] Base64 to io.Reader
func DecodeBase64(b64Str string) io.Reader {
r := strings.NewReader(b64Str)
img := base64.NewDecoder(base64.StdEncoding, r)
}
@maruware
maruware / convert_jpeg.go
Last active August 7, 2019 05:48
[Go] Convert JPEG
func ConvertToJpeg(src io.Reader, dst io.Writer, quality int) error {
img, _, err := image.Decode(src)
if err != nil {
return err
}
opts := &jpeg.Options{Quality: quality}
err = jpeg.Encode(dst, img, opts)
if err != nil {
return err
@maruware
maruware / bearer_auth_client.go
Created August 7, 2019 08:34
[go] BearerAuthClient
package test_helper
import (
"fmt"
"io"
"net/http"
)
type BearerAuthClient struct {
token string
@maruware
maruware / PlaceholderImage.tsx
Created August 9, 2019 09:02
materil-ui placeholder image
import React from 'react'
import { makeStyles } from '@material-ui/core/styles'
import ImageIcon from '@material-ui/icons/Image'
interface PlaceHolderImageProps {
width: number
height: number
}
@maruware
maruware / QRCodeImage.tsx
Last active August 14, 2019 07:30
React QRCode Element
import React, { useState, useEffect } from 'react'
import QRCode from 'qrcode'
export interface QRCodeImageProps {
value: string
}
const QRCodeImage: React.FC<
QRCodeImageProps & React.ImgHTMLAttributes<HTMLImageElement>
> = ({ value, ...rest }) => {
const [dataUrl, setDataUrl] = useState('')