Skip to content

Commit

Permalink
UPD: add mini underscore comments
Browse files Browse the repository at this point in the history
  • Loading branch information
alexpods committed Jan 19, 2014
1 parent 0373708 commit e2cd28e
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 5 deletions.
73 changes: 71 additions & 2 deletions dist/ClazzJS.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,50 @@
}((new Function('return this'))(), 'ClazzJS', [], function(undefined) {
/**
* Mini underscore
* Add one non underscore method: isSimpleObject.
* Add two non underscore method: isSimpleObject() and construct()
*/
var _ = (function() {
var _ = {};

var toString = Object.prototype.toString;
var slice = Array.prototype.slice;

/**
* Is a given variable undefined?
*
* @param {*} obj Some object
* @returns {boolean} true if given variable is undefined
*/
_.isUndefined = function(obj) {
return obj === void 0;
};

/**
* Is a given variable object?
*
* @param {*} obj Some object
* @returns {boolean} true if given variable is object
*/
_.isObject = function(obj) {
return obj === Object(obj);
};

/**
* Is a given variable a simple object?
*
* @param {*} obj Some object
* @returns {boolean} true if given variable is a simple object
*/
_.isSimpleObject = function(obj) {
return obj && ({}).constructor === obj.constructor;
};

/**
* Is a given variable is null?
*
* @param {*} obj Some object
* @returns {boolean} true if given variable is undefined
*/
_.isNull = function(obj) {
return obj === null;
};
Expand All @@ -70,10 +94,22 @@
})(isFunctions[i]);
}

/**
* Converts array like object to real array
*
* @param {object} obj Array like object
* @returns {array} Array
*/
_.toArray = function(obj) {
return slice.call(obj);
};

/**
* Extend a given object with all the properties in passed-in object(s)
*
* @param {object} obj Some object
* @returns {object} Extended object
*/
_.extend = function(obj) {
var sources = slice.call(arguments, 1);

Expand All @@ -90,15 +126,33 @@
return obj;
};

/**
* Create a (shallow-cloned) duplicate of an object
*
* @param {object} obj Some object
* @returns {object} Cloned object
*/
_.clone = function(obj) {
if (!_.isObject(obj)) return obj;
return _.isArray(obj) ? obj.slice() : _.extend({}, obj);
};

/**
* Gets last element of array
*
* @param {array} arr Some array
* @returns {*} Last element of array
*/
_.last = function(arr) {
return arr[arr.length - 1];
};

/**
* Checks whether object is empty
*
* @param {*} obj Some object
* @returns {boolean} true if object is empty
*/
_.isEmpty = function(obj) {
if (obj == null) return true;
if (_.isArray(obj) || _.isString(obj)) return obj.length === 0;
Expand All @@ -107,6 +161,14 @@
return true;
};

/**
* forEach loop realization
*
* @param {object} obj Some object
* @param {function) iterator Iterator
* @param {object} context Iteration context
* @returns {boolean} true if object is empty
*/
_.each = function(obj, iterator, context) {
if (obj == null) return;

Expand All @@ -127,6 +189,13 @@
}
};

/**
* Create class instance
*
* @param {function} klass Some constructor
* @param {array} params Constructor parameters
* @returns {object) Created object
*/
_.construct = function(klass, params) {
var K = function() {
return klass.apply(this, params);
Expand Down Expand Up @@ -1267,7 +1336,7 @@
}
}

clazz.prototype = _.extend(Object.create(parent ? parent.prototype : {}), clazz.prototype);
clazz.prototype = _.extend(this.objectCreate(parent ? parent.prototype : {}), clazz.prototype);

clazz.__parent = parent || null;
clazz.prototype.constructor = clazz;
Expand Down
2 changes: 1 addition & 1 deletion dist/ClazzJS.min.js

Large diffs are not rendered by default.

71 changes: 70 additions & 1 deletion src/_.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,49 @@
/**
* Mini underscore
* Add one non underscore method: isSimpleObject.
* Add two non underscore method: isSimpleObject() and construct()
*/
var _ = (function() {
var _ = {};

var toString = Object.prototype.toString;
var slice = Array.prototype.slice;

/**
* Is a given variable undefined?
*
* @param {*} obj Some object
* @returns {boolean} true if given variable is undefined
*/
_.isUndefined = function(obj) {
return obj === void 0;
};

/**
* Is a given variable object?
*
* @param {*} obj Some object
* @returns {boolean} true if given variable is object
*/
_.isObject = function(obj) {
return obj === Object(obj);
};

/**
* Is a given variable a simple object?
*
* @param {*} obj Some object
* @returns {boolean} true if given variable is a simple object
*/
_.isSimpleObject = function(obj) {
return obj && ({}).constructor === obj.constructor;
};

/**
* Is a given variable is null?
*
* @param {*} obj Some object
* @returns {boolean} true if given variable is undefined
*/
_.isNull = function(obj) {
return obj === null;
};
Expand All @@ -33,10 +57,22 @@ var _ = (function() {
})(isFunctions[i]);
}

/**
* Converts array like object to real array
*
* @param {object} obj Array like object
* @returns {array} Array
*/
_.toArray = function(obj) {
return slice.call(obj);
};

/**
* Extend a given object with all the properties in passed-in object(s)
*
* @param {object} obj Some object
* @returns {object} Extended object
*/
_.extend = function(obj) {
var sources = slice.call(arguments, 1);

Expand All @@ -53,22 +89,48 @@ var _ = (function() {
return obj;
};

/**
* Create a (shallow-cloned) duplicate of an object
*
* @param {object} obj Some object
* @returns {object} Cloned object
*/
_.clone = function(obj) {
if (!_.isObject(obj)) return obj;
return _.isArray(obj) ? obj.slice() : _.extend({}, obj);
};

/**
* Gets last element of array
*
* @param {array} arr Some array
* @returns {*} Last element of array
*/
_.last = function(arr) {
return arr[arr.length - 1];
};

/**
* Checks whether object is empty
*
* @param {*} obj Some object
* @returns {boolean} true if object is empty
*/
_.isEmpty = function(obj) {
if (obj == null) return true;
if (_.isArray(obj) || _.isString(obj)) return obj.length === 0;
for (var key in obj) if (obj.hasOwnProperty(key)) return false;
return true;
};

/**
* forEach loop realization
*
* @param {object} obj Some object
* @param {function) iterator Iterator
* @param {object} context Iteration context
* @returns {boolean} true if object is empty
*/
_.each = function(obj, iterator, context) {
if (obj == null) return;

Expand All @@ -90,6 +152,13 @@ var _ = (function() {
}
};

/**
* Create class instance
*
* @param {function} klass Some constructor
* @param {array} params Constructor parameters
* @returns {object) Created object
*/
_.construct = function (klass, params) {
var K = function() {
return klass.apply(this, params);
Expand Down
2 changes: 1 addition & 1 deletion src/core/Clazz/Factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ _.extend(Factory.prototype, {
}
}

clazz.prototype = _.extend(Object.create(parent ? parent.prototype : {}), clazz.prototype);
clazz.prototype = _.extend(this.objectCreate(parent ? parent.prototype : {}), clazz.prototype);

clazz.__parent = parent || null;
clazz.prototype.constructor = clazz;
Expand Down

0 comments on commit e2cd28e

Please sign in to comment.