Skip to content

Instantly share code, notes, and snippets.

View useStep.ts
import { useState } from 'react'
type Steps = {
[step: string]: string
}
export const useStep = <T extends Steps>({
steps,
initialStep,
}: {
@matsuby
matsuby / cumulativeSumObjects.ts
Last active Apr 8, 2021
時系列データの累積和
View cumulativeSumObjects.ts
/**
* cumulativeSumObjects({
* 20200101: {a: 1, b: 2, c: 3},
* 20200102: {b: 1, c: 2, d: 3},
* 20200103: {c: 1, d: 2, e: 3},
* })
* => {
* 20200101: {a: 1, b: 2, c: 3, d: 0, e: 0},
* 20200102: {a: 1, b: 3, c: 5, d: 3, e: 0},
* 20200103: {a: 1, b: 3, c: 6, d: 5, e: 3},
@matsuby
matsuby / sumObjects.ts
Last active Apr 8, 2021
オブジェクトの和
View sumObjects.ts
/**
* sumObjects([
* {a: 1, b: 2},
* {b: 3, c: 4},
* ])
* => {a: 1, b: 5, c: 4}
*/
const sumObjects = (
objects: Record<string, number>[]
): Record<string, number> =>
@matsuby
matsuby / combinations.ts
Last active Oct 27, 2020
combinations.ts
View combinations.ts
/**
* Returns an array of all combinations of elements from all arrays.
*
* @examples
* combinations([1], [2, 3], [4, 5, 6])
* => "[[1,2,4],[1,2,5],[1,2,6],[1,3,4],[1,3,5],[1,3,6]]"
*/
export const combinations = <T extends unknown>(...arrays: T[][]): T[] => {
const result: T[] = []
const _combinations = (n: number, current: T[]) => {
@matsuby
matsuby / calcPieChartValues.js
Created Nov 29, 2018
JavaScript calculation pie chart values.
View calcPieChartValues.js
{
const calcPieChartValues = (values, numDegits = null) => {
const totalValue = values.reduce((a, c) => a + c);
const deg2rad = (deg) => deg * Math.PI / 180;
return values.map((value, i) => {
const data = {};
data.index = i;
data.value = value;
data.rate = value / totalValue;
@matsuby
matsuby / index.html
Last active Oct 24, 2018
d3を使って円から円を直線で結ぶサンプル
View index.html
<!doctype html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>d3を使って円から円を直線で結ぶサンプル</title>
</head>
<body>
<div id="container"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
<script>
@matsuby
matsuby / fibonacci.js
Last active Oct 21, 2018
JavaScript one-liner function: fibonacci (get single fibonacci number)
View fibonacci.js
// =========================================================================
// https://en.wikipedia.org/wiki/Fibonacci_number#List_of_Fibonacci_numbers
// =========================================================================
// for ES2015
{
const fibonacci = Object.assign(function f(n){return +n!==parseInt(n)||!isFinite(n)?NaN:Math.abs(n)>1476?Math.sign(n)*Infinity:(n>=0?1:~n%2?-1:1)*(f[Math.abs(n)]||(f[Math.abs(n)-1]=f(Math.abs(n)-1))+f[Math.abs(n)-2])},{"-2":-1,"-1":1,0:0,1:1});
const positiveIntegers = [...[...Array(21).keys()], 100, 999, 1476, 1477, 1478, 1479];
@matsuby
matsuby / groupArr2d_groupConcatArr2d.js
Last active Oct 18, 2018
JavaScript one-liner function: groupArr2d and groupConcatArr2d (>=ES2017)
View groupArr2d_groupConcatArr2d.js
// for ES2017
{
const groupArr2d = (arr2d, {swap=false} = {}) => arr2d.reduce((a,c)=>Object.assign(a,{[c[+swap]]:a[c[+swap]]?[...a[c[+swap]],c[+!swap]]:[c[+!swap]]}),{});
const groupConcatArr2d = (arr2d, {swap=false, glue=","} = {}) => Object.assign({},...Object.entries(arr2d.reduce((a,c)=>Object.assign(a,{[c[+swap]]:a[c[+swap]]?[...a[c[+swap]],c[+!swap]]:[c[+!swap]]}),{})).map(e=>({[e[0]]:e[1].join(glue)})));
// two-dimensional arrays can represent object.
// in each array, the first value can be regarded as a key, and the next value can be regarded as a value.
// `Object.entries(obj)` is easy to understand as an example.
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries#Examples
@matsuby
matsuby / parseQuery_buildQuery.js
Last active Oct 18, 2018
JavaScript one-liner function: parseQuery and buildQuery (>=ES2017)
View parseQuery_buildQuery.js
// for ES2017
{
// "name=matsuby&age=27" => {name: "matsuby", age: "27"}
const parseQuery = qs => typeof qs!=="string"?{}:Object.assign({},...Object.entries([...new URLSearchParams(qs)].reduce((a,c)=>Object.assign(a,{[c[0]]:a[c[0]]?[...a[c[0]],c[1]]:[c[1]]}),{})).map(e=>({[e[0]]:e[1].join(",")})));
// {name: "matsuby", age: "27"} => "name=matsuby&age=27"
const buildQuery = qo => new URLSearchParams(Object.entries(qo).map(kv=>kv[1].split(",").map(v=>[kv[0],v])).reduce((a,c)=>a.concat(c),[])).toString();
const testParams = [
@matsuby
matsuby / isLeapYear.js
Last active Oct 16, 2018
JavaScript one-liner function: isLeapYear (Proleptic Gregorian calendar)
View isLeapYear.js
// =================================================================
// https://en.wikipedia.org/wiki/Proleptic_Gregorian_calendar
// =================================================================
// for ES2015
{
const isLeapYear = y => !!(+y===parseInt(y)&&!y.pop&&(y%4)^(y%100)|!(y%400));
const testParams = [
// expect: true (is leap)