Created
November 10, 2015 09:30
-
-
Save azam/4513fd3e0058dac7c929 to your computer and use it in GitHub Desktop.
Simple Javascript stack library
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
/** | |
* stack.js | |
* (c) 2015 Azamshul Azizy | |
* | |
* [description] | |
* @param {[type]} factory [description] | |
* @return {[type]} [description] | |
*/ | |
(function(stackjs) { | |
// if (typeof(define) === "function" && typeof(define.amd) !== "undefined") { | |
// define(["jquery"], factory); | |
// } else if (typeof(jQuery) !== "undefined") { | |
// factory(jQuery); | |
// } else if (typeof(jquery) !== "undefined") { | |
// factory(jquery); | |
// } else if (typeof($) !== "undefined") { | |
// factory($); | |
// } | |
// })(function($jquery) {;; | |
console.log("1"); | |
if (typeof(define) === "function" && typeof(define.amd) !== "undefined") { | |
console.log("require"); | |
define([], stackjs); | |
} else { | |
console.log("others"); | |
stackjs(); | |
} | |
})(function() {;; | |
console.log("2"); | |
var __stacks = []; | |
var __occupiedExecuted = false; | |
var __occupied = function() {}; | |
var __cleared = function() {}; | |
var __increased = function() {}; | |
var __decreased = function() {}; | |
var __execOccupied = function() { | |
console.log("__execOccupied"); | |
__occupiedExecuted = true; | |
__occupied(); | |
}; | |
var __execCleared = function() { | |
console.log("__execCleared"); | |
__cleared(); | |
__execOccupied = false; | |
}; | |
var __execIncreased = function() { | |
console.log("__execIncreased"); | |
if (!__occupiedExecuted) { | |
__execOccupied(); | |
} | |
__increased(); | |
}; | |
var __execDecreased = function() { | |
console.log("__execDecreased"); | |
__decreased(); | |
if (__stacks.length <= 0) { | |
__execCleared(); | |
} | |
}; | |
var o = {}; | |
o.occupied = function(cb) { | |
if (typeof(cb) === "function") { | |
__occupied = cb; | |
} | |
return o; | |
}; | |
o.cleared = function(cb) { | |
if (typeof(cb) === "function") { | |
__cleared = cb; | |
} | |
return o; | |
}; | |
o.increased = function(cb) { | |
if (typeof(cb) === "function") { | |
__increased = cb; | |
} | |
return o; | |
}; | |
o.decreased = function(cb) { | |
if (typeof(cb) === "function") { | |
__decreased = cb; | |
} | |
return o; | |
}; | |
o.push = function(s) { | |
if (s !== undefined) { | |
__stacks.push(s); | |
__execIncreased(); | |
} | |
return o; | |
}; | |
o.pop = function() { | |
__stacks.pop(); | |
if (__stacks.length > 0) { | |
__execDecreased(); | |
} | |
return o; | |
}; | |
o.put = function(s) { | |
if (s !== undefined) { | |
var moved = false; | |
for (var i = __stacks.length; i >= 0; i--) { | |
if (__stacks[i] === s) { | |
__stacks.splice(i, 1); | |
moved = true; | |
} | |
} | |
__stacks.push(s); | |
if (!moved) { | |
__execIncreased(); | |
} | |
} | |
return o; | |
}; | |
o.clear = function() { | |
var spliced = false; | |
for (var i = __stacks.length; i >= 0; i--) { | |
__stacks.splice(i, 1); | |
spliced = true; | |
} | |
if (spliced) { | |
__execDecreased(); | |
} | |
return o; | |
}; | |
o.remove = function(s) { | |
if (s !== undefined) { | |
for (var i = __stacks.length; i >= 0; i--) { | |
if (__stacks[i] === s) { | |
__stacks.splice(i, 1); | |
__execDecreased(); | |
} | |
} | |
} | |
return o; | |
}; | |
o.removeFirst = function(s) { | |
if (s !== undefined) { | |
var idx = -1; | |
for (var i = __stacks.length; i >= 0; i--) { | |
if (__stacks[i] === s) { | |
idx = i; | |
} | |
} | |
if (idx >= 0) { | |
__stacks.splice(i, 1); | |
__execDecreased(); | |
} | |
} | |
return o; | |
}; | |
o.removeLast = function(s) { | |
if (s !== undefined) { | |
for (var i = __stacks.length; i >= 0; i--) { | |
if (__stacks[i] === s) { | |
__stacks.splice(i, 1); | |
__execDecreased(); | |
return o; | |
} | |
} | |
} | |
return o; | |
}; | |
console.log(o); | |
return o; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment