UNPKG

staffordsmith

Version:

An npm functional programming library project

1 lines 13.5 kB
{"changed":false,"filter":false,"title":"index.js","tooltip":"/index.js","value":"'use strict';\n\n// YOU KNOW WHAT TO DO //\n\n/**\n * each: Designed to loop over a collection, Array or Object, and applies the \n * action Function to each value in the collection.\n * \n * @param {Array or Object} collection: The collection over which to iterate.\n * @param {Function} action: The Function to be applied to each value in the \n * collection\n */\nfunction each(collection, action) {\n if(Array.isArray(collection)) {\n for(var i = 0; i < collection.length; i++) {\n action(collection[i], i, collection);\n }\n } else {\n for (var key in collection) {\n action(collection[key], key, collection);\n }\n }\n}\nmodule.exports.each = each;\n\n/**\n * identity: Designed to return whatever value that is inserted.\n * \n * @param {Any Value}: This can be any one Value.\n * \n * @return The same exact value that was set as a param.\n * \n * Usage:\n * \n */ \n \n function identity(value){\n return value;\n }\n module.exports.identity = identity;\n \n/**\n* typeOf: Designed to return the type of value in a string. \n* Types are one of:\n* - \"string\"\n* - \"array\"\n* - \"object\"\n* - \"undefined\"\n* - \"number\"\n* - \"boolean\"\n* - \"null\"\n* - \"function\"\n * \n * @param {value}: Can be any given value.\n * \n * @return {'String'}: Will be a string containing the type of value.\n * \n * Usage:\n * \n * function typeOf(value){\n * if(Array.isArray(value)){\n* return 'Array';\n* }else if(value === null){\n return 'null';\n }\n return typeof value;\n* }\n* module.exports.typeOf = typeOf;\n* \n*/\n\n/** \n* first: Designed to take in two args(array, number). If array is not given it returns a \n* a []. Next it checks to see if a number is given || is not a number at all. If any of these\n* are true it will return the first element of the array. Then if the number is \n* more than array.length it will return the entire array. Lastly it will return\n* the first (whatever number is) elements of the array\n* \n* @param {array, number}\n* \n* @return See first Above.\n* \n* Usage:\n*/ \n function first(array, number){\n let list = [];\n if(!Array.isArray(array)){\n return list;\n }\n if(!number || isNaN(number)){\n return array[0];\n }else if(number > array.length){\n return array;\n }else{\n for(var i = 0; i < number; i++){\n list.push(array[i]);\n }\n }\n return list\n }\n module.exports.first = first;\n \n/**\n * last: Designed to return the last element in a array. It takes two args\n * (array, number) so if no array is past in || number is negative it will \n * retern a []. Then if no number is past in || number is NaN it will return the \n * last element in the array. Then if number is larger than array.length return\n * entire array. Otherwise it will loop over the array and return the last \n * (Whatever number is) elements\n * in the array.\n * \n * @param {array, number}\n * \n * Usage:\n * \n */ \n function last(array, number){\n \n if(!Array.isArray(array) || number < 0){\n return [];\n }else{\n if(!number || isNaN(number)){\n return array.length - 1;\n }else if(number > array.length){\n return array\n }else{\n return array.slice(array.length - number, array.length)\n }\n }\n }\n module.exports.last = last;\n \n/**\n * indexOf: Designed to take two args(array, value). It will loop thru the array\n * and return the first occurrance of the values Index. Next if the value is not \n * found in the array it will return -1.\n * \n * @param {array, value}\n * \n * Usage:\n */\n function indexOf(array, value){\n for(var i = 0 array.length; i++){\n if(array[i] === value){\n return i;\n }else{\n \n }\n }\n return -1;\n }\n module.exports.indexOf = indexOf;\n/**\n * filter: Designed to take two args(array, function) first it will loop thru each\n * element in the array sending it thru the function(element,index,array) as params.\n * It will return a new array with all the elements that return true from the \n * function.\n * \n * @param {array, function}\n * \n * Usage:\n */ function filter(array, sort){\n let page = [];\n for(let i = 0; i < array.length; i++){\n const result = sort(array[i], i, array);\n if(result === true){\n page.push(array[i]);\n }\n } \n return page;\n};\nmodule.exports.filter = filter;\n\n/**\n * reject: Designed to take two args(array, function), it will loop over each element in \n * the array applying the function(element,index,array) to each element. It will \n * return a new array with the elements that returnes a false value from the function.\n * \n * @args {array, function}\n * Usage:\n */\n function reject(array, function(element,i, array){\n let box = []\n if(.filter(array, function(element, i, array){\n return !element;\n }).length > 0){\n box.push(element)\n }\n });\n module.exports.reject = reject;\n \n/**\n * partition: Designed to take two args(array, function), it will loop throught\n * array passing the function on each element in the array. It will return a \n * array that contains two sub arrays. One sub should contain truthy and one should \n * contain falsy.\n * \n * @args {array, function}\n * \n * Usage:\n */\n function partition(array, function){\n let result = [];\n let box = [];\n let bigBox = [];\n for(let i = 0; i < array.length; i++){\nif(bunch(array[i], i, array) === true){\n result.push(array[i]);\n }else{\n box.push(array[i]);\n }\n }\n bigBox.push(result);\n bigBox.push(box);\n return bigBox;\n } \n module.exports.partition = partition;\n \n /**\n * Unique: Designed to take one arg(array), it will return a new array of all \n * elements with duplicates removed. Use indexOf from above .\n * \n * @arg {array}\n * \n * Usage:\n */\n function unique(array){\n var arr = [];\n for(var i = 0;i < array.length;i++){\n if(_.indexOf(arr,array[i]) < 0){\n arr.push(array[i]);\n }\n }\n return arr;\n }; \n module.exports.unique = unique;\n/**\n * map: Designed to take two args(collection, function), it past the function\n * over every element in the given collection. If collection is array \n * function(element,i,collection). If collection is Object function(value,key,collection)\n * Whatever the return value is will be saved in a new array. then it will return the array.\n * \n * Usage: \n */\n _.map = function(collection, action){\n let arr = [];\n if(Array.isArray(collection)){\n for(var i = 0; i < collection.length; i++){\n arr.push(action(collection[i], i, collection));\n }\n }else {\n for(var key in collection){\n arr.push(action(collection[key], key, collection));\n }\n }\n return arr;\n};\nmodule.exports.map = map;\n\n/**\n * pluck: Designed to take two args(array of objects, a property), it will an \n * array containing the value of property foe every elememt.\n * \n * @arg {array of objects, property}\n * \n * Usage: \n */\n function pluck(array, prop){\n return _.map(array, function(obj, i, arr) {\n return obj[prop];\n });\n};\nmodule.exports.pluck = pluck;\n\n/**\n * contains: Designed to take two args(arrray, value), will return true if \n * value is found in array. Else it wiil return false.\n * \n * @arg {array, value}\n * \n * Usage:\n */\n function contains(array, value){\n return _.indexOf(array,value) >= 0 ? true : false ;\n};\n module.exports.contains = contains;\n \n/**\n * every: Designed to take two args(collection,function), it will call the function\n * on every element of the collection. If collection is array function(element,index,collection)\n * If collection is object function(value, key, collection). So it will return true \n * if every element returns true from the function. If one returns false from the function\n * return false.\n * \n * @arg(collection,function)\n * \n * Usage: \n */\n function every(collection, action){\n if(!action){\n if(_.filter(collection, function(element, i, collection){\n return !element;\n }).length > 0){\n return false;\n }\n \n }else if(action){\n if(Array.isArray(collection)){\n for(var e = 0; e < collection.length; e++){\n if(action(collection[e], e, collection) === true){\n \n }else {\n return false;\n }\n \n } \n }\n \n \n }else if(collection instanceof Object){\n for(let key in collection){\n if(action(collection[key], key, collection) === true){\n \n }else{\n return false;\n }\n }\n }\n return true;\n};\nmodule.exports.every = every;\n \n/**\n *some: Designed to take args(collection,function), it will take the function and\n * pass over every element in the array. If collection is array function(element,i,collection)\n * , and if object function(value,key,collection). Next it will check if anyone of\n * the return are true if so it will return true. If all elements return false the the \n * return value is false. but if no function is given rtuen true if on element returns true.\n * \n * @arg(collection,function)\n * \n * Usage\n */\n_.some = function(collection, action){\n if(!action){\n if( _.filter(collection, function(element){\n return element;\n }).length > 0){\n return true;\n }else {\n return false;\n }\n }else if(action){\n if(Array.isArray(collection)){\n for(var i = 0; i < collection.length; i++){\n if(action(collection[i], i, collection) === false){\n \n }else{\n return true;\n }\n }\n \n }else{\n for(var key in collection){\n if(_.indexOf(Object.keys(collection), [key]) < 0){\n return true;\n }\n }\n }\n return false;\n }\nmodule.exports.every = every;\n \n/**\n *reduce: Designed to take two arg(array,function,seed), it will call the function\n * over every element in array--function(previous result,element, array). the return value \n * of function will be used as previous result for next turn thru array. On the first\n * turn seed will be used as previous result. If no seed use array[0]/ first value as seed.\n * After final turn thur array return the value on the final function call.\n * \n *@arg{array,function,seed}\n * \n * Usage:\n */\n_.reduce = function(array, action, seed){\n let value;\n _.each(array, function(element, i, array){\n if(seed === undefined && i === 0){\n value = array[0];\n }else if(i === 0){\n value = action(seed, element, i);\n }else{\n value = action(value, element, i);\n }\n });\n return value;\n};\nmodule.exports.reduce = reduce;\n\n/**\n *extend: Designed to have two arg(object1,object2) and could possibly have more objects.\n *First it will copy object2 properties to object2. If more propperties are passed in\n * copy them to object1 in the order they are passed. lastly return updated object1.\n * \n * @arg{Object1,object2}\n * \n * Usaage:\n */\n _.extend = function(target, obj2){\n var sources = [].slice.call(arguments, 1);\n sources.forEach(function (source) {\n Object.getOwnPropertyNames(source).forEach(function(propName) {\n Object.defineProperty(target, propName,\n Object.getOwnPropertyDescriptor(source, propName));\n });\n });\n return target;\n };\n module.exports.extend = extend;\n\n","undoManager":{"mark":-1,"position":-1,"stack":[]},"ace":{"folds":[],"scrolltop":0,"scrollleft":0,"selection":{"start":{"row":418,"column":25},"end":{"row":418,"column":25},"isBackwards":false},"options":{"guessTabSize":true,"useWrapMode":false,"wrapToView":true},"firstLineState":0},"timestamp":1541824136953}