diff --git a/index.html b/index.html index 04b833e41..3d36e0aec 100644 --- a/index.html +++ b/index.html @@ -6,11 +6,11 @@ fi JS Functional Library - + - + diff --git a/index.js b/index.js index e69de29bb..51d36a97f 100644 --- a/index.js +++ b/index.js @@ -0,0 +1,94 @@ +function makeArrayInput(collection) { + if (Array.isArray(collection) === true) { + return collection.slice() + } else { + return (Object.values(collection)) + } +} + +function myEach(collection, anyCallback) { + const newData = makeArrayInput(collection) + for (let i = 0; i < newData.length; i++) { + anyCallback(newData[i]) + } + return collection +} + +function myMap(collection, transformCallback) { + const newData = makeArrayInput(collection) + let newArray = [] + for (let value in newData) { + newArray.push(transformCallback(newData[value])) + } + return newArray +} + +function myReduce(collection, totalCallback, acc) { + const newData = makeArrayInput(collection) + if(!acc){ + acc = newData[0] + newData.shift() + } + newData.forEach((element) => { + acc = totalCallback(acc, element, collection) + }) + return acc +} + +function myFind(collection, predicate) { + const newData = makeArrayInput(collection) + for (let element of newData) { + if (predicate(element)) { + return element + } + } + return undefined +} + +function myFilter(collection, predicate) { + const newData = makeArrayInput(collection) + const newCollection = [] + for (let element of newData) { + if (predicate(element)) { + newCollection.push(element) + } + } + return newCollection +} + +function mySize(collection) { + const newData = makeArrayInput(collection) + return newData.length +} + +function myFirst(array, n) { + if(!n){ + return array[0] + } else { + return array.slice(0, n) + } +} + +function myLast(array, n) { + if(!n) { + return array[array.length -1] + } else { + return array.slice(-n) + } +} + +function myKeys(object) { + const keys = [] + for (let key in object) { + keys.push(key) + } + return keys +} + +function myValues(object) { + const values = [] + for (let key in object) { + values.push(object[key]) + } + return values +}