3 minute read

Tags: , , , ,

開個新的 戰場

讓自己頭腦可以回顧溫習

工作上遇到的好玩事務~

eslint + auto fix vsc

  • eslint: 快樂好文章
    • 安裝
      • npm install --global eslint
    • cd 到你要導入的開發環境下 root 位置
      • eslint --init
    • 都正常後會在root位置下幫你新增一個 eslintrc.json(我是選json格式)
  • VSC 專案的 root 開一個 .vscode folder

    • 然後加入 settings.json
     {
         "editor.codeActionsOnSave": {
             "source.fixAll.eslint": true
           },
           "eslint.validate": ["javascript"]
     }
    

Go

Type Assertions

x.(T) asserts that x is not nil and that the value stored in x is og type T.

  • ex:

     package main
          
     import "fmt"
     import "math"
          
     func main() {
         shapes := []Shape{
             Circle{1.0},
             Square{1.772453},
             Rectangle{5, 10},
             Triangle{10, 4, 7},
         }
         for _, v := range shapes {
             fmt.Println(v, "\tArea:", v.Area())
             if t, ok := v.(Triangle); ok {
                 fmt.Println("Angles:", t.Angles())
             }
         }
     }
          
     type Shape interface {
         Area() float64
     }
     type Circle struct {
         Radius float64
     }
     type Triangle struct {
         A, B, C float64 // lengths of the sides of a triangle.
     }
     type Rectangle struct {
         A, B float64
     }
     type Square struct {
         A float64
     }
          
     func (t Circle) Area() float64 {
         return math.Pi * t.Radius * t.Radius
     }
          
     // Heron's Formula for the area of a triangle
     func (t Triangle) Area() float64 {
         p := (t.A + t.B + t.C) / 2.0 // perimeter half
         return math.Sqrt(p * (p - t.A) * (p - t.B) * (p - t.C))
     }
     func (t Rectangle) Area() float64 {
         return t.A * t.B
     }
          
     func (t Square) Area() float64 {
         return t.A * t.A
     }
          
     func (t Circle) String() string {
         return fmt.Sprint("Circle (Radius: ", t.Radius, ")")
     }
     func (t Triangle) String() string {
         return fmt.Sprint("Triangle (Sides: ", t.A, ", ", t.B, ", ", t.C, ")")
     }
     func (t Rectangle) String() string {
         return fmt.Sprint("Rectangle (Sides: ", t.A, ", ", t.B, ")")
     }
     func (t Square) String() string {
         return fmt.Sprint("Square (Sides: ", t.A, ")")
     }
          
     func (t Triangle) Angles() []float64 {
         return []float64{angle(t.B, t.C, t.A), angle(t.A, t.C, t.B), angle(t.A, t.B, t.C)}
     }
     func angle(a, b, c float64) float64 {
         return math.Acos((a*a+b*b-c*c)/(2*a*b)) * 180.0 / math.Pi
     }
    
  • reference:



cap vs len

  • cap tells you the capacity of the underlying array
  • len tells you how many items are in the array

  • ex:

     s := make([]int, 0, 3)
     for i := 0; i < 5; i++ {
         s = append(s, i)
         fmt.Printf("cap %v, len %v, %p\n", cap(s), len(s), s)
     }
    
     cap 3, len 1, 0x1040e130
     cap 3, len 2, 0x1040e130
     cap 3, len 3, 0x1040e130
     cap 8, len 4, 0x10432220
     cap 8, len 5, 0x10432220
    
  • reference:



xrom inner join 用法

JavaScript

reduce MDN: reduce()

~~~js
let input = [{id: 1, companyName: "company14", companyId: 14, flActive: true, purchaseMonth: "2019-12-15T00:00:00", year: 2019, month: "December"},
{id: 2, companyName: "company5", companyId: 5, flActive: true, purchaseMonth: "2019-12-15T00:00:00", year: 2019, month: "December"},
{id: 3, companyName: "company13", companyId: 13, flActive: true, purchaseMonth: "2019-11-15T00:00:00", year: 2019, month: "November"},
{id: 4, companyName: "company14", companyId: 14, flActive: true, purchaseMonth: "2019-11-15T00:00:00", year: 2019, month: "December"},
{id: 5, companyName: "company5", companyId: 5, flActive: true, purchaseMonth: "2019-10-15T00:00:00", year: 2019, month: "October"},
{id: 6, companyName: "company14", companyId: 14, flActive: true, purchaseMonth: "2020-09-15T00:00:00", year: 2020, month: "September"},
{id: 7, companyName: "company7", companyId: 7, flActive: true, purchaseMonth: "2020-09-15T00:00:00", year: 2020, month: "September"}]

let months = { "09": "September", "10": "October", "11": "November", "12": "December" }

let result = input.reduce((state,current) => {
   let {companyName, year, month} = current;
   
   let company = state[companyName] || (state[companyName] = {});
   let yearObj = company[year] || (company[year] = {});
   let monthArr = yearObj[month] || (yearObj[month] = []);
   monthArr.push(current);
   
   return state;
}, {});

console.log(result);
~~~


React

route with path params

  • 在Route 中的寫法
     <Route  exact path="/pathName/:id" component={ Wooh }/>
    
  • 在 components 拿 path params
    • 招數一
         const Wooh = ({match}) => {
                return (
                  <div>
                id: {match.params.id}
              </div>
            )
         }
      
    • 招數二
        import { useParams } from "react-router";
         const Wooh = () => {
               let {id} = useParams()
            return (
                <div>
                id: {id}
              </div>
            )
      
  • reference