Skip to content

Commit

Permalink
include add and count in relationship class
Browse files Browse the repository at this point in the history
  • Loading branch information
MyMediaMagnet committed Mar 27, 2018
1 parent ffd5aab commit 7ae532c
Show file tree
Hide file tree
Showing 15 changed files with 365 additions and 65 deletions.
142 changes: 116 additions & 26 deletions dist/classes/collection/Collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ Object.defineProperty(exports, "__esModule", {
value: true
});

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); // A useful wrapper for arrays with a variety of helpers

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _Where = require('./Where');

Expand All @@ -15,9 +14,16 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

/**
* A useful wrapper for arrays with a variety of helpers
*/
var Collection = function () {

// Setup a new collection from an array
/**
* Setup a new collection from an array
*
* @param {*} items
*/
function Collection(items) {
_classCallCheck(this, Collection);

Expand All @@ -28,7 +34,11 @@ var Collection = function () {
this.wheres = [];
}

// Set the items in the collection
/**
* Set the items in the collection
*
* @param {*} items
*/


_createClass(Collection, [{
Expand All @@ -39,7 +49,12 @@ var Collection = function () {
return this;
}

// Find an item in the collection based on 'id'
/**
* Find an item in the collection based on the primary key
*
* @param {*} id
* @param {*} key
*/

}, {
key: 'find',
Expand All @@ -55,31 +70,43 @@ var Collection = function () {
return null;
}

// Grab the first item in the collection
/**
* Return the first item in the collection
*/

}, {
key: 'first',
value: function first() {
return this.get().items[0];
}

// Grab the last item in the collection
/**
* Return the last item in the collection
*/

}, {
key: 'last',
value: function last() {
return this.items[this.items.length - 1];
}

// Grab nth item in the collection
/**
* Grab nth item in the collection
*
* @param {*} nth
*/

}, {
key: 'nth',
value: function nth(_nth) {
return this.items[_nth];
}

// Add an item to the collection
/**
* Add an item to the collection
*
* @param {*} item
*/

}, {
key: 'add',
Expand All @@ -89,7 +116,11 @@ var Collection = function () {
return this;
}

// Remove an item from the collection
/**
* Remove an item from the collection
*
* @param {*} item
*/

}, {
key: 'remove',
Expand All @@ -102,7 +133,13 @@ var Collection = function () {
return this;
}

// Start a query on the collection
/**
* Start a query on the collection
*
* @param {*} field_name
* @param {*} operand
* @param {*} value
*/

}, {
key: 'where',
Expand All @@ -112,7 +149,10 @@ var Collection = function () {
return this;
}

// Based on the given query, do any items exist in the collection
/**
* Based on the given query, do any items exist in the collection
* @todo fix issue related to this method
*/

}, {
key: 'exists',
Expand All @@ -128,7 +168,9 @@ var Collection = function () {
return false;
}

// Return an array of all the items in the collection
/**
* Return an array of all the items in the collection
*/

}, {
key: 'get',
Expand All @@ -147,12 +189,16 @@ var Collection = function () {
return collection;
}

// Loop through each item in the collection
/**
* Assuming the parameter sent in is a function
* loop through each item in the collection
*
* @param {*} callback
*/

}, {
key: 'each',
value: function each(callback) {
// Make sure the callback is a function​
if (typeof callback !== "function") return false;

this.items.forEach(function (item, key) {
Expand All @@ -162,15 +208,22 @@ var Collection = function () {
return true;
}

// Given any queries, get a count of all the items in the collection
/**
* Given any wheres, get a count of all the items in the collection
*/

}, {
key: 'count',
value: function count() {
return this.get().items.length;
}

// Sort the items by integers or strings
/**
* Sort the items by integers or strings
*
* @param {*} key
* @param {*} direction
*/

}, {
key: 'sort',
Expand Down Expand Up @@ -205,7 +258,12 @@ var Collection = function () {
return this;
}

// Sort the items using the date function
/**
* Sort the items using the date function
*
* @param {*} key
* @param {*} direction
*/

}, {
key: 'sortByDate',
Expand All @@ -225,7 +283,11 @@ var Collection = function () {
return this;
}

// Get the sum of all values, or all values of a given column
/**
* Get the sum of all values, or all values of a given column
*
* @param {*} key
*/

}, {
key: 'sum',
Expand All @@ -242,7 +304,11 @@ var Collection = function () {
return total;
}

// Get the min of all values, or all values of a given column
/**
* Get the min of all values, or all values of a given column
*
* @param {*} key
*/

}, {
key: 'min',
Expand All @@ -260,7 +326,11 @@ var Collection = function () {
return min;
}

// Get the max of all values, or all values of a given column
/**
* Get the max of all values, or all values of a given column
*
* @param {*} key
*/

}, {
key: 'max',
Expand All @@ -278,15 +348,24 @@ var Collection = function () {
return max;
}

// Get the average of all values, or all values of a given column
/**
* Get the average of all values, or all values of a given column
*
* @param {*} key
*/

}, {
key: 'average',
value: function average(key) {
return this.sum(key) / this.items.length;
}

// Get the average of all values, or all values of a given column
/**
* If a collection is sent in, merge that collection into this collection
* If an array is sent in, merge the array into this collection
*
* @param {*} items
*/

}, {
key: 'merge',
Expand All @@ -306,8 +385,17 @@ var Collection = function () {
return this;
}

// PRIVATE METHODS
// Private: used to determine if an item should be added to list based on where query
/**
***********************************
* PRIVATE METHODS
***********************************
*/

/**
* Private: used to determine if an item should be added to list based on where query
*
* @param {*} item
*/

}, {
key: '_passesWhereQuery',
Expand All @@ -321,7 +409,9 @@ var Collection = function () {
return true;
}

// Private: Clear any existing where statements
/**
* Private: Clear any existing where statements
*/

}, {
key: '_clearWheres',
Expand Down
18 changes: 15 additions & 3 deletions dist/classes/collection/Where.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,13 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var Where = function () {
// Construct a where statement
/**
* Construct a where statement
*
* @param {*} field_name
* @param {*} operand
* @param {*} value
*/
function Where(field_name, operand, value) {
_classCallCheck(this, Where);

Expand All @@ -46,7 +52,11 @@ var Where = function () {
this.operandClass = this.setupOperand();
}

// Confirm that the given values pass the check based on the given operand
/**
* Confirm that the given values pass the check based on the given operand
*
* @param {*} item
*/


_createClass(Where, [{
Expand All @@ -55,7 +65,9 @@ var Where = function () {
return this.operandClass.check(item[this.field_name], this.value);
}

// Set the class to handle the check based on the operand
/**
* Set the class to handle the check based on the operand
*/

}, {
key: 'setupOperand',
Expand Down
7 changes: 6 additions & 1 deletion dist/classes/collection/operands/DoesNotEqual.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ var DoesNotEqual = function (_Operand) {
key: 'check',


// Confirm the actual value does not equal the expected value
/**
* Confirm the actual value does not equal the expected value
*
* @param {*} actual
* @param {*} expected
*/
value: function check(actual, expected) {
return actual !== expected;
}
Expand Down
7 changes: 6 additions & 1 deletion dist/classes/collection/operands/Equals.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ var Equals = function (_Operand) {
key: 'check',


// Confirm the actual value equals the expected value
/**
* Confirm the actual value equals the expected value
*
* @param {*} actual
* @param {*} expected
*/
value: function check(actual, expected) {
return actual === expected;
}
Expand Down
7 changes: 6 additions & 1 deletion dist/classes/collection/operands/GreaterThan.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ var GreaterThan = function (_Operand) {
key: 'check',


// Confirm the actual value is greater than the expected value
/**
* Confirm the actual value is greater than the expected value
*
* @param {*} actual
* @param {*} expected
*/
value: function check(actual, expected) {
return actual > expected;
}
Expand Down
Loading

0 comments on commit 7ae532c

Please sign in to comment.