Skip to content

Commit c9ba507

Browse files
committed
first functions
1 parent 284be9e commit c9ba507

File tree

11 files changed

+156
-0
lines changed

11 files changed

+156
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules/
2+
yarn.lock

@types/at.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/**
2+
* @param {number} index
3+
*/
4+
export function at(index: number): any;

@types/foreach.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/**
2+
* @param {Function} callback
3+
* @param {any | undefined} thisArg
4+
*/
5+
export function forEach(callback: Function, thisArg?: any | undefined): void;

@types/map.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/**
2+
* @param {Function} callback
3+
* @param {any | undefined} thisArg
4+
*/
5+
export function map(callback: Function, thisArg?: any | undefined): any[];

@types/reduce.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/**
2+
* @param {Function} callback
3+
* @param {any | undefined} initialValue
4+
*/
5+
export function reduce(callback: Function, initialValue?: any | undefined): any;

functions/at.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
Array.prototype.at = at
2+
3+
4+
/**
5+
* @param {number} index
6+
*/
7+
function at(index) {
8+
if (typeof index !== 'number') {
9+
throw new Error(`param need to be a number but receive a ${typeof index }`)
10+
}
11+
12+
const array = this
13+
14+
if (index > 0) {
15+
return array[index]
16+
} else {
17+
let handleArrayPosition = 0;
18+
19+
for (let i = array.length - 1; i > -1; i--) {
20+
handleArrayPosition--
21+
22+
if (handleArrayPosition == index) {
23+
return array[i]
24+
}
25+
}
26+
}
27+
}
28+
29+
module.exports = { at }

functions/foreach.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Array.prototype.forEach = forEach
2+
3+
/**
4+
* @param {Function} callback
5+
* @param {any | undefined} thisArg
6+
*/
7+
function forEach(callback, thisArg = undefined) {
8+
if (typeof callback !== 'function') {
9+
throw new Error(`Second param must be a function but received ${typeof callback}`)
10+
}
11+
12+
const array = this
13+
14+
for (let i = 0; i < array.length; i++) {
15+
callback.apply(thisArg, [array[i], i, array])
16+
}
17+
}
18+
19+
module.exports = { forEach }

functions/map.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
Array.prototype.map = map
2+
3+
/**
4+
* @param {Function} callback
5+
* @param {any | undefined} thisArg
6+
*/
7+
function map(callback, thisArg = undefined) {
8+
if (typeof callback !== 'function') {
9+
throw new Error(`Second param must be a function but received ${typeof callback}`)
10+
}
11+
12+
const array = this
13+
let newArray = []
14+
15+
for (let i = 0; i < array.length; i++) {
16+
const newElement = callback.apply(thisArg, [array[i], i, array])
17+
18+
newArray.push(newElement)
19+
}
20+
21+
return newArray
22+
}
23+
24+
25+
26+
['oi', 'oi', 'oi'].map((element, elementPosition, array) => console.log({
27+
element,
28+
elementPosition,
29+
array
30+
}))
31+
32+
module.exports = { map }

functions/reduce.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Array.prototype.reduce = reduce
2+
3+
/**
4+
* @param {Function} callback
5+
* @param {any | undefined} initialValue
6+
*/
7+
function reduce(callback, initialValue = undefined) {
8+
if (typeof callback !== 'function') {
9+
throw new Error(`Second param must be a function but received ${typeof callback}`)
10+
}
11+
12+
const array = this
13+
let accumulator = initialValue ? initialValue : array[0]
14+
15+
for (let i = 0; i < array.length; i++) {
16+
const newAcc = callback(accumulator, array[i], i, array)
17+
18+
accumulator = newAcc
19+
}
20+
21+
return accumulator
22+
}
23+
24+
25+
module.exports = { reduce }

package.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "JS-Functions",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"license": "MIT",
6+
"typings": "./@types/map.d.ts",
7+
"scripts": {
8+
"compile-declarations": "tsc --declaration",
9+
"clean-declarations": "tsc --declaration --clean"
10+
},
11+
"devDependencies": {
12+
"@types/node": "^16.4.4",
13+
"typescript": "^4.3.5"
14+
},
15+
"dependencies": {
16+
"tsd": "^0.17.0"
17+
}
18+
}

0 commit comments

Comments
 (0)