diff --git a/scripts/extend.js b/scripts/extend.js index aebdf17..a812f59 100644 --- a/scripts/extend.js +++ b/scripts/extend.js @@ -1,3 +1,35 @@ function extend(P, px, sx){ + var obj = function () { + P.call(this); + }; + var oldProt = function () { }; + oldProt.prototype = P.prototype; + obj.prototype = new oldProt(); + obj.prototype.constructor = obj; + obj.superclass = P.prototype; + + for (var i in px) { + obj.prototype[i] = px[i]; + + if (i == 'constructor') { + var pr = obj.prototype; + obj = px[i]; + obj.prototype = pr; + obj.prototype.constructor = obj; + } + + } + + for (var idx in sx) { + obj[idx] = sx[idx]; + } + + for (var item in P) { + if (obj[item] == undefined) { + obj[item] = P[item]; + } + } + + return obj; } diff --git a/scripts/sort-basic.js b/scripts/sort-basic.js index 10c29c9..cfc0957 100644 --- a/scripts/sort-basic.js +++ b/scripts/sort-basic.js @@ -1 +1,32 @@ -//todo - implement \ No newline at end of file +(function () { + "use strict"; + var oldSort = Array.prototype.sort; + Array.prototype.sort = function(predicate) { + var dirty = true; + + if (!predicate) { + predicate = function(a, b) { return a - b; }; + } + + while (dirty) { + dirty = false; + for (var i = 0; i < this.length - 1; i++) { + var equal = predicate(this[i], this[i + 1]); + if (!equal) continue; + { + if (equal > 0) { + var temp = this[i + 1]; + this[i + 1] = this[i]; + this[i] = temp; + dirty = true; + } + } + } + } + + Array.prototype.sort = oldSort; + + return this; + }; +}()) + diff --git a/scripts/strings-basic.js b/scripts/strings-basic.js index bacf25b..b2a4157 100644 --- a/scripts/strings-basic.js +++ b/scripts/strings-basic.js @@ -1,31 +1,66 @@ var StringsBasic = { /** - * @param {string} str - * @param {string} sub - * @returns {number} - */ - subCount : function(str, sub) {}, +* @param {string} str +* @param {string} sub +* @returns {number} +*/ + subCount: function (str, sub) { + return str.toLowerCase().split(sub.toLowerCase()).length - 1; + }, /** - * @param {string} str - * @param {string} sub - * @returns {Array,} - */ - wordsContains : function(str, sub) {}, +* @param {string} str +* @param {string} sub +* @returns {Array,} +*/ + wordsContains: function (str, sub) { + var array = str.replace(/[&\/|\\#,+()$~%.'":*?!<>{}_]/g, " ").split(" "); + var result = []; + for (var i in array) { + if (array[i].toLowerCase().search(sub.toLowerCase()) != -1) { + result.push(array[i]); + } + } + return result; + }, /** - * @param {string} str - * @param {string} sub - * @returns {Array.} - */ - wordsNotContains : function(str, sub) {}, +* @param {string} str +* @param {string} sub +* @returns {Array.} +*/ + wordsNotContains: function (str, sub) { + var array = str.replace(/[&\/|\\#,+()$~%.'":*?!<>{}_]/g, " ").split(" "); + var result = []; + for (var i in array) { + if (array[i].toLowerCase().search(sub.toLowerCase()) == -1) { + result.push(array[i]); + } + } + return result; + }, /** - * @param {string} str - * @returns {Array.} - */ - wordsBeginCapital : function(str) {}, +* @param {string} str +* @returns {Array.} +*/ + wordsBeginCapital: function (str) { + var array = str.replace(/[&\/|\\#,+()$~%.'":*?!<>{}0-9_]/g, ' ').split(" "); + var result = []; + for (var i in array) { + var letter = array[i].substr(0, 1); + if (letter == letter.toUpperCase() && letter != '') { + result.push(array[i]); + } + } + return result; + }, /** - * @param {string} str - * @returns {boolean} - */ - isPalindrome : function(str) {} +* @param {string} str +* @returns {boolean} +*/ + isPalindrome: function (str) { + if (str.toLowerCase() == str.split('').reverse().join('').toLowerCase()) { + return true; + } + return false; + } }; diff --git a/test/unit/strings-basic.js b/test/unit/strings-basic.js index e2272ae..b03d762 100644 --- a/test/unit/strings-basic.js +++ b/test/unit/strings-basic.js @@ -108,7 +108,7 @@ describe('strings-basic.js', function(){ assert.deepEqual(StringsBasic.wordsBeginCapital('Hello cruel World'), ['Hello', 'World']); - assert.deepEqual(StringsBasic.wordsBeginCapital('Hello,Cruel_World'), ['Hello', 'Cruel']); + assert.deepEqual(StringsBasic.wordsBeginCapital('Hello,Cruel_World'), ['Hello', 'Cruel', 'World']); assert.deepEqual(StringsBasic.wordsBeginCapital('Hello,123'), ['Hello']); });