Quick actions

cmd+k|ctrl+k

Navigation

Languages

Implementing an Array

Snippet info

Language

JavaScript

Visibility

public

Author

laekettavong

Created

2019-02-17T17:28:02Z

Updated

2019-03-05T23:18:02Z

/**
 * @author: K. Lae Kettavong
 * @date: 2/17/19
 * @description: Using an IIFE to encapsulate an array implementation
 */
 
const ArrayImpl = (() => {
    //private members
    let _length = 0
    let _data = {}
    
    /*
    * Removes element at specified index and move trailing elements to the left
    *
    * @param number index - element to remove
    */
    const _shiftLeft = (index) => {
       let counter = 0
        Object.entries(_data).forEach(ele => {
            if(ele[0] != index){
                _data[counter] = ele[1]
                counter++
            }
        })
        delete _data[_length - 1]
        _length--
    }
    
    /*
    * Inserts element at beginning of array and move existing elements to the right
    *
    * @param string item - item to insert
    */
    const _shiftRight = (item) => {
        let data = {0: item}
        let counter = 1
        Object.entries(_data).forEach(ele => {
            data[counter] = ele[1]
            counter++
        })
        _length++
        _data = data
    }
    
    // Anonymous class with public interface
    return class {
        
        /*
        * Returns the element at the specified index
        *
        * @param number index - item index to fetch
        * @return string - element at the given index
        */
        get(index) {
            return _data[index]
        }
        
        /*
        * Adds item to the end of the array
        *
        * @param string item - item to add
        * @return string item - item being inserted
        */
        push(item) {
            _data[_length] = item
            _length++
            return _length
        }
        
        /*
        * Removes the last element in the array
        *
        * @return string item - item being removed
        */
        pop() {
            const item = _data[_length-1]
            delete _data[_length-1]
            _length--
            return item
        }
        
        /*
        * Adds item to the front of the array
        *
        * @param string item - item to add
        * @return string item - item being inserted
        */
        insert(item) {
            _shiftRight(item)
            return item
        }
        
        /*
        * Removes the given element from arry and shifts remaining elements to the left
        *
        * @param number index - index of element to removed
        * @return string item - item being removed
        */
        delete(index) {
            const item = _data[index]
            _shiftLeft(index)
            return item
        }
        
        /*
        * Displays array content
        */
        dump(){
            console.log({ length: _length, data: _data })
        }
    }
})()

const array = new ArrayImpl()
array.push('One')
array.push('2')
array.push('Two')
array.push('Three')
array.push('!')
console.log(array._data) //undefined, cannot access private field
array.dump() // { length: 5, data: { '0': 'One', '1': '2', '2': 'Two', '3': 'Three', '4': '!' } }
console.log(array.delete(1)) // returns 2
array.dump() //{ length: 4, data: { '0': 'One', '1': 'Two', '2': 'Three', '3': '!' } }
console.log(array.pop()) // ! is returned
array.dump() // { length: 3, data: { '0': 'One', '1': 'Two', '2': 'Three' } }
array.insert('Start')
array.dump()  //{ length: 4, data: { '0': 'Start', '1': 'One', '2': 'Two', '3': 'Three' } }

INFO