Skip to content

Instantly share code, notes, and snippets.

View tonkotsuboy's full-sized avatar
😸
I want another myself.

Takeshi Kano tonkotsuboy

😸
I want another myself.
View GitHub Profile
@tonkotsuboy
tonkotsuboy / mathClamp.ts
Created November 9, 2021 09:27
Math.clamp()
/**
* Math.clamp()
*/
export const mathClamp = (x: number, lower: number, upper: number): number => {
return Math.min(Math.max(x, lower), upper)
}
@tonkotsuboy
tonkotsuboy / validateDate.test.ts
Created July 26, 2021 07:53
dayjsを使った日付のバリデート
import { validateDate } from "./validateDate";
describe("有効な日付のテスト", () => {
it.each(["2021/01/01", "2021/12/31", "2021/02/01", "2000/02/29"])(
"%sは正しい日付である",
(date) => {
expect(validateDate(date, "YYYY/MM/DD")).toBe(true);
}
);
@tonkotsuboy
tonkotsuboy / foo.test.js
Created May 13, 2021 02:44
beforeEach nest
describe("describe 1", () => {
beforeEach(() => {
console.log("before each 1");
});
describe("describe 2", () => {
beforeEach(() => {
console.log("before each 2");
});
it("test1", () => {});
@tonkotsuboy
tonkotsuboy / findLastIndex.js
Last active October 28, 2021 01:15
findLastIndex demo
const myArray = [
{ name: 'ギータ', age: 32 },
{ name: 'マッチ', age: 37 },
{ name: 'ケイゾー', age: 37 },
{ name: '高谷', age: 39 },
];
// myArrayから、ageが37の最後の要素を探し、名前を取得
const targetName = myArray.findLast(member => {
return member.age === 37;
@tonkotsuboy
tonkotsuboy / myTest.mjs
Last active March 30, 2021 05:03
Top level await is already available in Node.js.
// this sample can be run with: $ node myTest.mjs
console.log("こんにちは");
await new Promise(resolve => {
setTimeout(() => {
console.log("2秒経ちました");
resolve();
}, 2000);
});
@tonkotsuboy
tonkotsuboy / MyClass.js
Last active September 21, 2021 02:51
class fields and methods
class MyClass {
// public field
foo = "public field: 寿司";
// private field
#bar = "private field: ラーメン";
// public static field
static qux = "public static field: うどん";
// private static field
static #corge = "private static field: 麻婆豆腐";
@tonkotsuboy
tonkotsuboy / parseOgImage.ts
Created August 9, 2020 10:44
parse og:image
const parseOgImage = (htmlString: string): string | null => {
return htmlString.match(/property="og:image" content="(?<ogimage>.*)"/iu)
?.groups.ogimage ?? null;
};
@tonkotsuboy
tonkotsuboy / Fukuoka.js
Created July 15, 2020 05:06
Chrome 84のプライベートメソッドとアクセサ
class Fukuoka {
get #ramen() {
return "ラーメン";
}
#sayHello() {
console.log(`${this.#ramen}はえらい美味しかばい`);
}
constructor() {
@tonkotsuboy
tonkotsuboy / 名前つきキャプチャー.js
Created July 3, 2020 05:47
名前つきキャプチャー
const regrex = /よく(?<food>.*)食う(?<animal>.*)だ/u;
const result1 = "隣の客はよく柿食う客だ".match(regrex);
console.log(result1.groups.food);
console.log(result1.groups.animal);
const result2 = "隣の客はよく牡蠣食う吉田拓郎だ".match(regrex);
console.log(result2.groups.food);
console.log(result2.groups.animal);
@tonkotsuboy
tonkotsuboy / oneLinerArray.js
Created June 27, 2020 09:55
配列の最初の要素、長さ、最終要素をワンライナーで取得する
// 配列の最初の要素、長さ、最終要素をワンライナーで取得する
// 参考: https://twitter.com/argyleink/status/1139415654132436993
// 対象の配列
const myArray = ["😸", "🐶", "🐟", "🦆", "🐳", "🦉"];
// firstに最初の要素、lに長さ, lastに最終要素
const { 0: first, length: l, [l-1]: last} = myArray;
// 確認のため出力。結果は{first: "😸", l: 6, last: "🦉"}