Created
March 16, 2016 20:24
-
-
Save asduser/7ea33de3bb6ba9e94f97 to your computer and use it in GitHub Desktop.
TypeScript sample.
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
var __extends = (this && this.__extends) || function (d, b) { | |
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; | |
function __() { this.constructor = d; } | |
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | |
}; | |
// Enumeration to define a Person gender. | |
var Gender; | |
(function (Gender) { | |
Gender[Gender["Male"] = 0] = "Male"; | |
Gender[Gender["Female"] = 1] = "Female"; | |
})(Gender || (Gender = {})); | |
// Enumeration to specify Nationality type. | |
var Citizenship; | |
(function (Citizenship) { | |
Citizenship[Citizenship["Asian"] = 0] = "Asian"; | |
Citizenship[Citizenship["Indian"] = 1] = "Indian"; | |
})(Citizenship || (Citizenship = {})); | |
// This class can't have any instance. | |
var Nationality = (function () { | |
function Nationality() { | |
} | |
Nationality.prototype.get = function () { return this.WhoAmI; }; | |
; | |
return Nationality; | |
}()); | |
// Declare extended Nationality class. | |
var Asian = (function (_super) { | |
__extends(Asian, _super); | |
function Asian() { | |
_super.call(this); | |
this.WhoAmI = Citizenship.Asian; | |
} | |
return Asian; | |
}(Nationality)); | |
// Interface implementation. | |
var Person = (function () { | |
function Person(age, name, gender) { | |
if (age === void 0) { age = 20; } | |
if (name === void 0) { name = "Unnamed"; } | |
if (gender === void 0) { gender = Gender.Female; } | |
this.Nationality = new Asian(); | |
this.Name = name || "Unnamed"; | |
this.Age = age || 20; | |
this.Gender = gender; | |
this.Id = ++Person.increment; | |
} | |
Person.prototype.getInfo = function () { | |
return "My name is " + this.Name + ", age is " + this.Age + ", gender is " + (this.Gender ? 'Female' : 'Male') + ", I am " + (this.Nationality.get() ? 'Indian' : 'Asian') + ", Unique ID: " + this.Id; | |
}; | |
Person.increment = 0; | |
return Person; | |
}()); | |
// Interface implementation. | |
var PersonManager = (function () { | |
function PersonManager() { | |
this.List = new Array(); | |
} | |
PersonManager.prototype.add = function (p) { | |
this.List.push(p); | |
}; | |
PersonManager.prototype.getById = function (n) { | |
return this.List.find(function (p) { | |
return p.Id == n; | |
}); | |
}; | |
PersonManager.prototype.getAll = function () { | |
return this.List; | |
}; | |
PersonManager.prototype.removeById = function (n) { | |
var isFound = false; | |
this.List.forEach(function (p, index, coll) { | |
if (p.Id == n) { | |
coll.splice(index, 1); | |
isFound = true; | |
} | |
}); | |
return isFound; | |
}; | |
PersonManager.prototype.removeAll = function () { | |
this.List = []; | |
return true; | |
}; | |
return PersonManager; | |
}()); | |
// Fill the data. | |
var _PM = new PersonManager(); | |
_PM.add(new Person()); | |
_PM.add(new Person(20, "Bob", 0)); | |
_PM.add(new Person(null, null, 0)); | |
// Work with existing data. | |
var persons = _PM.getAll(); | |
persons.forEach(function (p) { return console.log(p.getInfo()); }); | |
alert(_PM.getById(5)); | |
alert(_PM.getById(3).getInfo()); | |
console.log(_PM.removeById(2)); | |
console.log(persons.length); | |
if (!Array.prototype.find) { | |
Array.prototype.find = function (predicate) { | |
if (this == null) { | |
throw new TypeError('Array.prototype.find called on null or undefined'); | |
} | |
if (typeof predicate !== 'function') { | |
throw new TypeError('predicate must be a function'); | |
} | |
var list = Object(this); | |
var length = list.length >>> 0; | |
var thisArg = arguments[1]; | |
var value; | |
for (var i = 0; i < length; i++) { | |
value = list[i]; | |
if (predicate.call(thisArg, value, i, list)) { | |
return value; | |
} | |
} | |
return undefined; | |
}; | |
} |
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
interface IPerson { | |
getInfo(): string; | |
} | |
interface IPersonManager { | |
add(p: Person): void; | |
getById(n: number): Person; | |
getAll(): Array<Person>; | |
removeById(n: number): boolean; | |
removeAll(): boolean; | |
} | |
// Enumeration to define a Person gender. | |
enum Gender { | |
Male, Female | |
} | |
// Enumeration to specify Nationality type. | |
enum Citizenship { | |
Asian, Indian | |
} | |
// This class can't have any instance. | |
abstract class Nationality { | |
public get(): Citizenship { return this.WhoAmI }; | |
protected WhoAmI: Citizenship; | |
} | |
// Declare extended Nationality class. | |
class Asian extends Nationality { | |
constructor() { | |
super(); | |
this.WhoAmI = Citizenship.Asian; | |
} | |
} | |
// Interface implementation. | |
class Person implements IPerson { | |
constructor(age: number = 20, name: string = "Unnamed", gender: Gender = Gender.Female) { | |
this.Name = name || "Unnamed"; | |
this.Age = age || 20; | |
this.Gender = gender; | |
this.Id = ++Person.increment; | |
} | |
private static increment: number = 0; | |
public Id: number; | |
private Name: string; | |
private Age: number; | |
private Gender: Gender; | |
private Nationality: Asian = new Asian(); | |
public getInfo(): string { | |
return `My name is ${this.Name}, age is ${this.Age}, gender is ${this.Gender ? 'Female' : 'Male'}, I am ${this.Nationality.get() ? 'Indian' : 'Asian'}, Unique ID: ${this.Id}`; | |
} | |
} | |
// Interface implementation. | |
class PersonManager implements IPersonManager { | |
constructor() { | |
this.List = new Array<Person>(); | |
} | |
private List: Array<Person>; | |
public add(p: Person): void { | |
this.List.push(p); | |
} | |
public getById(n: number): Person { | |
return this.List.find((p) => { | |
return p.Id == n; | |
}); | |
} | |
public getAll(): Array<Person> { | |
return this.List; | |
} | |
public removeById(n: number): boolean { | |
let isFound: boolean = false; | |
this.List.forEach((p, index, coll) => { | |
if (p.Id == n) { | |
coll.splice(index, 1); | |
isFound = true; | |
} | |
}); | |
return isFound; | |
} | |
public removeAll(): boolean { | |
this.List = []; | |
return true; | |
} | |
} | |
// Fill the data. | |
let _PM = new PersonManager(); | |
_PM.add(new Person()); | |
_PM.add(new Person(20, "Bob", 0)); | |
_PM.add(new Person(null, null, 0)); | |
// Work with existing data. | |
let persons = _PM.getAll(); | |
persons.forEach((p) => console.log(p.getInfo())); | |
alert( _PM.getById(5) ); | |
alert( _PM.getById(3).getInfo() ); | |
console.log(_PM.removeById(2)); | |
console.log( persons.length ); | |
// ---------------------------------------------------------------------- | |
// A special JS-polyfill which allows to use a native Array 'find' method. | |
// ---------------------------------------------------------------------- | |
interface Array<T> { | |
find(predicate: (search: T) => boolean): T; | |
} | |
if (!Array.prototype.find) { | |
Array.prototype.find = function (predicate) { | |
if (this == null) { | |
throw new TypeError('Array.prototype.find called on null or undefined'); | |
} | |
if (typeof predicate !== 'function') { | |
throw new TypeError('predicate must be a function'); | |
} | |
var list = Object(this); | |
var length = list.length >>> 0; | |
var thisArg = arguments[1]; | |
var value; | |
for (var i = 0; i < length; i++) { | |
value = list[i]; | |
if (predicate.call(thisArg, value, i, list)) { | |
return value; | |
} | |
} | |
return undefined; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment