ts 實現 Dictionary 字典

非泛型版

namespace demo {
  /**
   * 字典類
   * let dictionary = new Dictionary(); // new一個對象
   * // 設置屬性
   * dictionary.Set('gandalf', '[email protected]');
   * dictionary.Set('john', '[email protected]');
   * dictionary.Set('tyrion', '[email protected]');
   * // 調用
   * console.log(dictionary.Size());
   * console.log(dictionary.Values());
   * console.log(dictionary.Get('tyrion'));
   */
  export class Dictionary {
    /**
     * 字典項目
     */
    private items = {}

    /**
     * 驗證指定鍵是否在字典中
     * @param key 鍵
     * @returns 是否存在
     */
    public Has(key: any): boolean {
      return key in this.items
    }

    /**
     * 設置鍵值
     * @param key 鍵
     * @param value 值
     */
    public Set(key: any, value: any): void {
      this.items[key] = value
    }

    /**
     * 移除指定鍵
     * @param key 鍵
     * @returns 是否移除成功
     */
    public Remove(key: any): boolean {
      if (this.Has(key)) {
        delete this.items[key]
        return true
      }
      return false
    }

    /**
     * 查找特定鍵的值
     * @param key 鍵
     * @returns 值
     */
    public Get(key: any): any {
      return this.Has(key) ? this.items[key] : undefined
    }

    /**
     * 獲取字典所有的鍵
     * @returns 鍵數組
     */
    public Keys(): Array<any> {
      let values = new Array()//存到數組中返回
      for (let k in this.items) {
        if (this.Has(k)) {
          values.push(this.items[k])
        }
      }
      return values
    }

    /**
     * 獲取字典所有的值
     * @returns 值數組
     */
    public Values(): Array<any> {
      // 存到數組中返回
      let values = new Array()
      for (let k in this.items) {
        if (this.Has(k)) {
          values.push(this.items[k])
        }
      }
      return values
    }

    /**
     * 獲取所有鍵值
     * @returns 鍵值對對象
     */
    public GetItems(): object {
      return this.items
    }

    /**
     * 清空字典
     */
    public Clear(): void {
      this.items = {}
    }

    /**
     * 獲取字典大小
     * @returns 
     */
    public Size(): number {
      return Object.keys(this.items).length
    }
  }
}

 

泛型版

namespace demo {
  /**
   * 泛型字典類
   * let dictionary = new Dictionary<string>(); // new一個對象
   * // 設置屬性
   * dictionary.Set('gandalf', '[email protected]');
   * dictionary.Set('john', '[email protected]');
   * dictionary.Set('tyrion', '[email protected]');
   * // 調用
   * console.log(dictionary.Size());
   * console.log(dictionary.Values());
   * console.log(dictionary.Get('tyrion'));
   */
  export class GDictionary<T> {
    /**
     * 字典項目
     */
    private items = {}

    /**
     * 驗證指定鍵是否在字典中
     * @param key 鍵
     * @returns 是否存在
     */
    public Has(key: string): boolean {
      return key in this.items
    }

    /**
     * 設置鍵值
     * @param key 鍵
     * @param value 值
     */
    public Set(key: string, value: T): void {
      this.items[key] = value
    }

    /**
     * 移除指定鍵
     * @param key 鍵
     * @returns 是否移除成功
     */
    public Remove(key: string): boolean {
      if (this.Has(key)) {
        delete this.items[key]
        return true
      }
      return false
    }

    /**
     * 查找特定鍵的值
     * @param key 鍵
     * @returns 值
     */
    public Get(key: string): T {
      return this.Has(key) ? this.items[key] : undefined
    }

    /**
     * 獲取字典所有的鍵
     * @returns 鍵數組
     */
    public Keys(): Array<string> {
      let values = new Array<string>()//存到數組中返回
      for (let k in this.items) {
        if (this.Has(k)) {
          values.push(this.items[k])
        }
      }
      return values
    }

    /**
     * 獲取字典所有的值
     * @returns 值數組
     */
    public Values(): Array<T> {
      // 存到數組中返回
      let values = new Array<T>()
      for (let k in this.items) {
        if (this.Has(k)) {
          values.push(this.items[k])
        }
      }
      return values
    }

    /**
     * 獲取所有鍵值
     * @returns 鍵值對對象
     */
    public GetItems(): object {
      return this.items
    }

    /**
     * 清空字典
     */
    public Clear(): void {
      this.items = {}
    }

    /**
     * 獲取字典大小
     * @returns 
     */
    public Size(): number {
      return Object.keys(this.items).length
    }
  }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章