Skip to content

Instantly share code, notes, and snippets.

View naoki-sawada's full-sized avatar
🎄

Naoki Sawada naoki-sawada

🎄
View GitHub Profile
import hexRgb from "hex-rgb";
// see: https://www.w3.org/TR/WCAG20/#relativeluminancedef
export function isDarkColor(hexColor: string): boolean {
const { red, green, blue } = hexRgb(hexColor);
const [r, g, b] = [red / 255, green / 255, blue / 255].map((v) =>
v <= 0.03928 ? v / 12.92 : Math.pow((v + 0.055) / 1.055, 2.4)
);
@naoki-sawada
naoki-sawada / main.go
Created May 17, 2022 14:18
Go generics example
package main
import "fmt"
type Base interface {
getID() int64
}
type Person struct {
ID int64
@naoki-sawada
naoki-sawada / pre-request.js
Last active June 5, 2022 09:05
The pre-request script to automatically renewal the JWT in Postman.
function parseJwt (token) {
const base64Url = token.split('.')[1];
const base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
const jsonPayload = decodeURIComponent(atob(base64).split('').map((c) => {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
}).join(''));
return JSON.parse(jsonPayload);
};
function isValidToken(token) {
@naoki-sawada
naoki-sawada / App.tsx
Last active May 24, 2021 11:33
React hook and fetch example code.
import React, { useCallback, useEffect, useState } from "react";
import axios, { AxiosRequestConfig } from "axios";
class FetchError extends Error {
constructor({ name, message }: { name: string, message: string }) {
super(message);
this.name = name;
}
}
@naoki-sawada
naoki-sawada / grid.html
Created February 11, 2021 04:11
Grid layout example
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="initial-scale=1.0, width=device-width, shrink-to-fit=no" />
<style>
body {
display: grid;
grid-template-columns: [menu] 25% [content] auto [end];
@naoki-sawada
naoki-sawada / json-usage.go
Last active June 15, 2019 16:56
json with go
package main
import (
"encoding/json"
"fmt"
)
type response1 struct {
Page int `json:"page"`
Fruits []string `json:"fruits"`
@naoki-sawada
naoki-sawada / docker-compose.yaml
Created February 17, 2019 13:02
Example for Elasticsearch and Kibana use with docker-compose
version: '2'
services:
elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:6.6.0
ports:
- "9200:9200"
- "9300:9300"
kibana:
image: docker.elastic.co/kibana/kibana:6.6.0
ports:
@naoki-sawada
naoki-sawada / README.md
Last active November 14, 2018 14:42
Readme scaffold
@naoki-sawada
naoki-sawada / use-async-await-without-function.js
Last active September 4, 2018 14:38
Use async await without function
(async () => {
try {
await createConnection();
console.log("connected!");
} catch (e) {
console.error(e);
}
})();
@naoki-sawada
naoki-sawada / client.js
Last active August 30, 2024 00:40
Simple socket.io room and auth example
const io = require('socket.io-client');
const socket = io('http://localhost:3000', {
transportOptions: {
polling: {
extraHeaders: {
'Authorization': 'Bearer abc',
},
},
},