본문 바로가기

Learn/Typescript

<노마드 TS 챌린지> 1주 차 회고

 

 

타입스크립트로 블록체인 만들기라는 챌린지에 도전한지 일주일이 지났다

 

처음에는 typescript에 대한 강의를 찾던 중 항상 관심이 가던 블록체인까지 만들 수 있단 말에 바로 신청을 했고,

2주 차 과정에 어떤 결과물이 나올지 기대가 되었다.

 

 신청한 와중에 다른 TS 강의를 수강하긴 하였지만, 새롭게 배운 개념들도 있었다.

 

generics type : 구체적인 타입을 지정하지 않고 다양한 인수와 리턴 값에 대한 타입을 지정해서 처리하는 것인데 꽤나 유용해보이고 실제로 써보니 편하기도 했다.

abstrct class : 이건 자주 쓸 것 같진 않다. class 위에 또 부모가 생기는 건데.. 음.. 아직은 활용방안이 잘 떠오르지 않아서 그런 걸 수도 있지만 어쨌든 새로운 개념 줍줍

 

 

 generics 과 class로 proptotype처럼 함수를 만들어봤다.

 

function superPrint<V>(a:V[]){
    return a[0]
}

const a = superPrint([1,2,3,4])
const b = superPrint([true,false,true])

function last<T>(arr:T[]){
    return arr[arr.length-1];
}

function prepend<T>(arr : T[],item : T){
    return [item,...arr]
}

function mix<T>(arr1:T[],arr1:T[]){
    return [...arr1,...arr2]
}

function count<T>(arr:T[]){
    return arr.length
}

function findIndex<T>(arr:T[],item:T){
    let answer = arr.indexOf(item)

    if(answer)return answer
    else if (answer === 0) return 0
    return null
}

function slice<T>(arr:T[],startIndex:number,endIndex?:number){
    return arr.slice(startIndex,endIndex)
}

 

 

Challenge goals:
타입스크립트의 클래스를 이용하여 Dict (딕셔너리. dictionary) 클래스를 만드세요. Dict 클래스는 아래와 같은 메소드들을 갖고 있어야 합니다.

add: 단어를 추가함.
get: 단어의 정의를 리턴함.
delete: 단어를 삭제함.
update: 단어를 업데이트 .
showAll: 사전 단어를 모두 보여줌.
count: 사전 단어들의 갯수를 리턴함.
upsert 단어를 업데이트 . 존재하지 않을시. 이를 추가함. (update + insert = upsert)
exists: 해당 단어가 사전에 존재하는지 여부를 알려줌.
bulkAdd: 다음과 같은 방식으로. 여러개의 단어를 한번에 추가할 있게 해줌. [{term:"김치", definition:"대박이네~"}, {term:"아파트", definition:"비싸네~"}]
bulkDelete: 다음과 같은 방식으로. 여러개의 단어를 한번에 삭제할 있게 해줌. ["김치", "아파트"]

type Words = {
    [key: string]: string;
    };

   
class Dict {
    private words: Words;
    constructor() {
    this.words = {};
    }
    add(word: Word) {
        if (this.words[word.term] === undefined) {
        this.words[word.term] = word.def;
        }
    }
    get(term:string){
        return this.words[term]
    }
    delete(term : string){
        if(this.words[term]) delete this.words[this.words.term]
    }
    update(word: Word) {
            if (this.words[word.term] !== undefined) {
            this.words[word.term] = word.def;
            }
    }
    showAll(){
        return Object.keys(this.words).forEach(term => console.log(term))
    }
    count(){
        return Object.keys(this.words).length
    }
    upsert(word: Word){
        this.words[word.term] = word.def
    }
    exists(term:string){
        if(this.words[term]) return true
        return false
    }
    bulkAdd(bluk: Word[]){
        bluk.forEach((word:Word)=>{
            if(this.words[word.term] === undefined)
            this.words[word.term] = word.def
        })
    }
    bulkDelete(bulk: string[]) {
        bulk.forEach((word: string) => {
            if(this.words[word])
            delete this.words[word]
        })
    }
}

class Word {
    constructor(
        public term: string,
        public def: string
    ) {}
}

 

 

'Learn > Typescript' 카테고리의 다른 글

Typescript로 블록체인 만들기  (0) 2023.07.20
Typescript tuple type  (0) 2023.06.30