Skip to content

Instantly share code, notes, and snippets.

View khaled0fares's full-sized avatar

Khaled Fares khaled0fares

  • Egypt
View GitHub Profile
@khaled0fares
khaled0fares / enumerable.js
Last active July 8, 2016 12:22
Simple implementation for some Enumerable functions in JS
Array.prototype.transform = function(fn){ //map
let newList = [], i = 0, len = this.length;
for(i; i < len; i++){
newList.push(fn(this[i]));
}
return newList;
}
Array.prototype.exclude = function(fn){ //filter
let filteredList = [], i = 0, len = this.length;
@khaled0fares
khaled0fares / type_checking.js
Last active July 18, 2016 10:55
Type checking function in JS
var isIt = function(that, type){
return Object.prototype.toString.call(that) === ('[object '+ type + ']')
}
//Examples
isIt([1,2], "Array"); // true
isIt(undefined, "Undefined"); // true
isIt(true, "String") // false
var Calc = function(init){
this.init = init;
}
Calc.prototype.equals = function(callback){
callback(this.init);
}
Calc.prototype.add = function(val){
this.init += val;
@khaled0fares
khaled0fares / unshift.js
Last active July 21, 2016 21:34
Reimplementation for unshift in JS
let deepCopy = function(copied, copy){
let i = 0, len = copy.length;
for(i; i < len; i++){
copied[i] = copy[i]
}
return copied;
}
@khaled0fares
khaled0fares / every_some.js
Last active July 25, 2016 03:40
implementation of "every" function in JS
Array.prototype.Every = function(callback){
let i = 0, len = this.length;
for(i; i < len; i++){
if(!callback(this[i])){
return false
}
}
return true
}
@khaled0fares
khaled0fares / list.js
Created July 25, 2016 06:37
List data structure in JavaScript
let List = function(){
this.Store = [];
this.size = 0;
this.pos = 0;
}
List.prototype.append = function(ele){
this.Store[this.size++] = ele;
}
@khaled0fares
khaled0fares / single.js
Last active September 23, 2016 22:21
Singleton pattern in JS
let Singleton = (function(name, yearOfBirth, job, relationship){
let instance;
function init(){
return {
name: name,
age: new Date().getFullYear() - yearOfBirth,
job: job,
relationship: relationship
}
@khaled0fares
khaled0fares / stack.js
Last active July 29, 2016 17:06
Stack implementation in JS using class and constructor
'use strict'
let Stack = function(max){
this.Store = [];
this.top = 0;
if(typeof max == 'number' && max != NaN){
this.max = max
}else{
throw new Error(max + ' should be a number');
@khaled0fares
khaled0fares / palindrome.js
Last active July 31, 2016 12:47
Palindrome using stack.
let stack = require("https://gist.github.com/khaled0fares/4ffe7a9db1c4c71b1e361c2c636f0c71");// Link to the stack gist, for calirfication only.
let isPalindrome = function(txt){
let len = txt.length,
stack = new Stack(len),
reversed = "",
i = 0;
while(!stack.full()){
@khaled0fares
khaled0fares / queue.js
Last active July 31, 2016 17:38
Queue implemented in JS
let Queue = function(){
this.Store = [];
}
Queue.prototype.enqueue = function(ele){
this.Store.push(ele);
}
Queue.prototype.dequeue = function(){
if(this.empty()){