I recently implemented a dictionary class in TypeScript. I'm still new to JavaScript so I'm curious what people think about it.

class Dictionary {
    public constructor ();
    public constructor (object: Object);
    public constructor (object?) {
        if (object != null) {
            for (let property in object) {
                if (object.hasOwnProperty(property)) {
                    this[property] = object[property];
                }
            }
        }
    }

    public clone(): Dictionary {
        let result = new Dictionary(this);
        return result;
    }

    public getKeys(): string[] {
        let result = [];
        for (let item in this) {
            if (this.hasOwnProperty(item)) {
                result.push(item);
            }
        }
        return result;
    }

    public getValues(): any[] {
        let result = [];
        for (let item in this) {
            if (this.hasOwnProperty(item)) {
                result.push(this[item]);
            }
        }
        return result;
    }

    public tryAddItem(key: string, value: any): boolean {
        let isAddItem = !this.hasOwnProperty(key) && typeof(value) !== 'undefined';
        if (isAddItem) {
            this[key] = value;
        }
        return isAddItem;
    }

    public tryUpdateItem(key: string, value: any): boolean {
        let isUpdateItem = this.hasOwnProperty(key) && typeof(value) !== 'undefined';
        if (isUpdateItem) {
            this[key] = value;
        }
        return isUpdateItem;
    }

    public tryDeleteItem(key: string): boolean {
        let isDeleteItem = this.hasOwnProperty(key);
        if (isDeleteItem) {
            delete this[key];
        }
        return isDeleteItem;
    }
}

I realized that objects in JavaScript act a lot like dictionaries so I'm basically just adding functionality to the object. I'm not sure how I would want to handle sorting. I think I would prefer to create a method that returned a sorted array or object based on some function. Similar to what a JavaScript Linq library would do.

share|improve this question
1  
Using the ES6 Map could help with some stuff. – gcampbell Jul 6 '16 at 17:08

From a short review:

  • I would follow the method names provided at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map they are both shorter and more intuitive. Your method names are too long

  • More particularly, I would change the name of 'getValuestoclone`

  • I would have the set/get/delete method return the Dictionary object for chained calls, if set/get is called on a ownProperty, I would throw an exception.

share|improve this answer
    
Thank you for the review! I intentionally left out the get and set because Dictionary[key]; gets the value and Dictionary[key] = vlaue; sets the value. I'm also not a fan of failing javascript. This is why I have the tryAdd, tryUpdate, and tryDelete. Although, I like the idea of chaining but I think this would go against the Single Responsibility rule in SOLID and the Command-Query separation principle. I think a type of linq library that could be used with Dictionaries should support chaining. Although, that's definitely me speaking from a C# bias. :) – christo8989 Jul 8 '16 at 6:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.