Skip to content

JavaScript Functional Library Project Submission #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 141 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
//Collection Functions (Arrays or Objects)

const myEach = (collection, callback) => {
if (Array.isArray(collection)) {
for (let i = 0; i < collection.length; i++) {
callback(collection[i])
}
} else {
for (let key in collection) {
callback(collection[key])
}
}
return collection
}

const myMap = (collection, callback) => {
const mapArray = []

myEach(collection, (item, index, collection) => {
mapArray.push(callback(item, index, collection))
})

return mapArray
}

const myReduce = (collection, callback, acc) => {

const objectValues = []

if (!Array.isArray(collection)) {
for (let key in collection) {
objectValues.push(collection[key])
}
collection = objectValues
}

if (!acc) {
acc = collection[0]

myEach(collection.slice(1), (value) => {
acc = callback(acc, value)
})
return acc
}
myEach(collection, (value) => {
acc = callback(acc, value)
})

return acc

}

const myFind = (collection, predicate) => {
for (let i = 0; i < collection.length; i++) {
if (predicate(collection[i])) {
return collection[i];
}
}
return undefined
}

const myFilter = (collection, predicate) => {
const filteredArray = []

for (let i = 0; i < collection.length; i++) {
if (predicate(collection[i])) {
filteredArray.push(collection[i])
}
}

return filteredArray
}

const mySize = collection => {

const objectKeys = []

if (!Array.isArray(collection)) {
for (let key in collection) {
objectKeys.push(key)
}
collection = objectKeys
}

return collection.length
}


//Array Functions

const myFirst = (array, n) => {
if (n) {
return array.slice(0, n)
}
return array[0]
}

const myLast = (array, n) => {

if (n) {
return array.slice(-n)
}
return array[mySize(array)-1]
}

const mySortBy = (array, callback) => {
return [...array].sort((a, b) => {
const valA = callback(a);
const valB = callback(b);

if (typeof valA === 'string' && typeof valB === 'string') {
return valA.localeCompare(valB);
}

return valA - valB;
})
}

const myFlatten = (array, [shallow], newArr=[]) => {

}

//Object Functions

const myKeys = object => {
const keys = []

for (let key in object) {
keys.push(key)
}
return keys
}

const myValues = function (object) {
const values = []

for (let key in object) {
values.push(object[key])
}
return values
}