Created
March 15, 2015 12:21
-
-
Save maxgherman/3d5901240f15e791116e to your computer and use it in GitHub Desktop.
Typescript immutable array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ImmutableArray<T> { | |
private _data : Array<T>; | |
public get value() : Array<T>{ | |
return this._data.slice(0); | |
} | |
constructor(data : Array<T>) { | |
if(Array.isArray(data) !== true) | |
throw new Error('data should represent an array'); | |
this._data = this.initialize(data); | |
} | |
private initialize(data : Array<T>) : Array<T> { | |
return data.map(item => { | |
var type = typeof (item); | |
return type === 'object' || | |
type === 'function' ? Object.freeze(item) : item; | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment