TypeScript 仕事納め

# 背景
三か月やってきた AWS 関連業務が一旦終わり、完全に別業務をすることになった。

ってことで、とりあえず、今現在の個人的チートシートを作って仕事納めをしておこう。

# 個人的チートシート

```typescript
// 環境
// 入れるべきもの
// - AWS なら
// -- AWS Toolkit: SAMやるときにはあると便利
// - prettier
// - ESLint < 多分入れたほうが良いはずだけど・・そこまでいかなかった

// generals
console.log("format: ", Number((10.556).toFixed(2))); // round

// Codes
import dayjs from "dayjs";
console.info = function () {}; // silent console

// Chain Methods 便利 C# 好きならそのままな感覚
let variable = "can be modified. do not use 'var' like in Kotlin";
const constant = "constant value";

// object
let loginData = {
  [constant]: `${variable} formatter`, // dinamic property name by value of constants/variables
};
console.log({ loginData });
// merge
const merged = {
  variable,
  ...{ Name: constant },
  ...{ values: [{ Name: constant }] },
}; // able to apply to array
console.log({ merged });
// if dinamic merge, see https://qiita.com/baku2san/items/4a472afe8f479279cb1c

// collection
const sampleData: { Name: string; Value?: number | string }[] = [
  { Name: "koge", Value: "1" },
  { Name: "hoge", Value: 15 },
  { Name: "hmm" },
];
const records = sampleData.filter((f) => f.Name == "mached");
const record = records[0]?.Value ?? 0;
const sliced = sampleData.slice(1, 3);
console.log({ userPoolName: sliced });
const splitted = sliced[0].Name.split("o")[1] ?? "nothing";
console.log({ splitted });
const numbers = Array.from({ length: 5 }, (_, f) => f);
console.log({ numbers });
console.log({ sampleData });
const sorted = sampleData.sort(
  (a, b) => Number(b.Value ?? 0) - Number(a.Value ?? 0)
);
const reversed = sorted.reverse();
console.log({ reversed });
const selected = sampleData.map((m) => {
  // like C# Linq .Select()
  return { Nickname: `Nick ${m.Name}`, Value: m.Value ?? 0 };
});
console.log({ selected });
const aggregated = sampleData // like C# linq .Aggregate()
  .map((m) => Number(m.Value ?? 0))
  .reduce((a, b) => a + b);
console.log({ aggregated });

const users3 = [
  { id: 1, name: "Hoge", age: 15 },
  { id: 2, name: "Hoge2", age: 35 },
  { id: 3, name: "Hoge3", age: 45 },
];
const users4 = [
  { id: 1, name2: "Hoge", age: 15 },
  { id: 2, name2: "Hoge2", age: 35 },
  { id: 5, name2: "Hoge3", age: 45 },
];
let data3: any[] = [];
users3.map(async (m) => {
  data3.push({
    key: m.id,
    ...m,
  });
});
let data4: any[] = [];
users4.map(async (m) => {
  data4.push({
    key: m.id,
    ...m,
  });
});
let distincted = [...data3, ...data4].reduce((obj, it) => {
  obj[it.key] = it;
  return obj;
}, {});
console.log({ distincted });

// function
function getInfo(userID: string): string | undefined {
  if (!userID) {
    console.warn(`${getInfo.name}(): function name can be .`);
    return "";
  }
}

// date
const getTime = Date.now(); // equals to getTime()
const currentDate = new Date(new Date().getTime()); // Unix Epoch time as 13 digits
console.info("toDateString());", currentDate.toDateString());
console.info("toISOString());", currentDate.toISOString()); // これが文字列取るには良さそう ISO 8601
console.info("toJSON());", currentDate.toJSON());
console.info("toLocaleDateString());", currentDate.toLocaleDateString());
console.info("toLocaleString());", currentDate.toLocaleString());
console.info("toLocaleTimeString());", currentDate.toLocaleTimeString());
console.info("toString());", currentDate.toString());
console.info("toTimeString());", currentDate.toTimeString());
console.info("toUTCString());", currentDate.toUTCString());

console.info("getDate());", currentDate.getDate());
console.info("getDay());", currentDate.getDay()); // 曜日
console.info("getFullYear", currentDate.getFullYear());
console.info("getHours())", currentDate.getHours());
console.info("getMonth())", currentDate.getMonth()); // begins with 0. must be add 1.
console.info("getTime());", currentDate.getTime());
console.info("getTimezone", currentDate.getTimezoneOffset());
console.info("getUTCDate(", currentDate.getUTCDate());
const intlNumberFormat = new Intl.NumberFormat("ja-JP", {
  minimumIntegerDigits: 2,
});
const formatByIntlNumberFormat = (date: Date) => {
  return `${date.getFullYear()}年${intlNumberFormat.format(
    date.getMonth() + 1
  )}月${intlNumberFormat.format(date.getDate())}日`;
};
const formatByDayjs = (date: Date) => {
  return dayjs(date).format("YYYY年MM付DD日");
};
const formatByLocaleString = (date: Date) => {
  return date.toLocaleString("ja-jp", {
    year: "numeric",
    month: "2-digit", // numeric: 一桁/ long|short|narrow: "月"
    day: "2-digit", // numeric: 一桁
    hour: "2-digit", // numeric: 一桁
    minute: "numeric", // 2-digit: 差はない？
    second: "numeric", // 2-digit: 差はない？
    timeZone: "Asia/Tokyo",
  });
};
console.log({
  intlNumberFormat: formatByIntlNumberFormat(currentDate),
  dayjs: formatByDayjs(currentDate),
  tolocal: formatByLocaleString(currentDate),
});
// Temporal can do the same thing and more faster probablly
```

# あとがき
次触ることがあれば・・役立つかもね


C# みたいに TypeScript/JavaScript が意外に面白かった。

やっぱり新しいことは楽しい。

これからも好奇心を大切にしようと改めて思いましたとさ :laughing: