Front-end/타입스크립트로 블록체인 만들기

HW - Class

탱 'ㅅ' 2024. 1. 20. 23:55

1st try

interface Word {
    term: string
    definition: string
}

class Dictionary {
    constructor(
        private dictionary: Word[]
    ) {}

    add(term: string, definition: string) {
        if (this.exists(term)) return
        this.dictionary.push({term, definition})
    }
    get(term: string) { 
        if (!this.exists(term)) return
        return this.dictionary.find(v => v.term === term)
    }
    delete(term: string) { 
        if (!this.exists(term)) return
        const index = this.dictionary.findIndex(v => v.term === term)
        this.dictionary.splice(index, 1)
    }
    update(term: string, definition: string) { 
        if (!this.exists(term)) return
        const index = this.dictionary.findIndex(v => v.term === term)
        this.dictionary[index].definition = definition
    }
    showAll() { // 모든 단어
        if (this.dictionary.length === 0) return 
        return this.dictionary.map(v => v.term).join(', ')
    }
    count() { // 단어 총 개수
        return this.dictionary.length
    }
    upsert(term: string, definition: string) { // 업데이트하는데 없으면 추가
        if (this.exists(term)) {
            this.update(term, definition)
        } else {
            this.add(term, definition)
        }
    }
    exists(term: string) {
        // return this.dictionary.map(v => v.term).includes(term)
        return this.dictionary.some(v => v.term === term)
    }
    bulkAdd(words: Word[]) { // 일괄 추가
        this.dictionary = {...this.dictionary, ...words}
    }
    bulkDelete(terms: string[]) { // 일괄 삭제
        this.dictionary.filter(v => !terms.includes(v.term))
    }
}

class Dict extends Dictionary {

}

 

 

 

2nd try

interface Words {
    [term: string]: string
}

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

class Dictionary {
    private dictionary: Words
    constructor() {
        this.dictionary = {}
        // { 
        //     "김치": "대박이네~", 
        //     "아파트": "비싸네~", 
        //     ..
        // }
    }

    exists(term: string) {
        return this.dictionary[term] !== undefined
    }
    add(word: Word) {
        if (this.exists(word.term)) return
        this.dictionary[word.term] = word.definiton
    }
    get(term: string) { 
        if (!this.exists(term)) return
        return this.dictionary[term]
    }
    update(word: Word) { // ?
        if (!this.exists(word.term)) return
        this.dictionary[word.term] = word.definiton
    }
    delete(term: string) { 
        if (!this.exists(term)) return
        delete this.dictionary[term]
    }
    showAll() {
        console.log(Object.keys(this.dictionary).join(', '))
    }
    count() {
        return Object.entries(this.dictionary).length
    }
    upsert(word: Word) {
        if (this.exists(word.term)) {
            this.update(word)
        } else {
            this.add(word)
        }
    }
    bulkAdd(words: Word[]) {
        words.forEach(word => this.add(word))
    }
    bulkDelete(terms: string[]) {
        terms.forEach(term => this.delete(term))
    }
}

let kimchi = new Word("김치", "대박이네~")
const apt = new Word("아파트", "비싸네~")

const dictionary = new Dictionary()
console.log(dictionary)
console.log(dictionary.exists("김치")) // exists

dictionary.add(kimchi) // add
console.log(dictionary)

console.log(dictionary.get("김치")) // get

kimchi = new Word("김치", "한국 전통 음식")
dictionary.update(kimchi) // update
console.log(dictionary)

dictionary.upsert(apt) // upsert
console.log(dictionary)

dictionary.showAll() // showAll
console.log(dictionary.count()) // count

dictionary.delete("김치") // delete
console.log(dictionary)

const butter = new Word("버터", "앙버터는 실패 없죠")
const bread = new Word("빵", "도넛보다 위험한 존재")
dictionary.bulkAdd([butter, bread]) // bulkAdd
console.log(dictionary)

dictionary.bulkDelete(["아파트", "빵"]) // bulkDelete
console.log(dictionary)

'Front-end > 타입스크립트로 블록체인 만들기' 카테고리의 다른 글

[TS] simple blockchain  (0) 2024.01.08
[TS] tsconfig.json  (0) 2024.01.04
[TS] Polymorphism + Generic Type + Interface  (1) 2024.01.04
[TS] Interfaces  (1) 2024.01.02
[TS] Classes  (1) 2024.01.02