Skip to content

Commit cacac16

Browse files
committed
first commit javascript coding
0 parents  commit cacac16

File tree

10 files changed

+566
-0
lines changed

10 files changed

+566
-0
lines changed

array-prototype.js

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
function filter(arr, callback) {
2+
const newArray = [];
3+
for (let i = 0; i < arr.length; i++) {
4+
if (callback(arr[i], i, arr)) {
5+
newArray.push(arr[i])
6+
}
7+
}
8+
return newArray;
9+
}
10+
11+
function filterObject(object, callback) {
12+
let newOject = {};
13+
for (const key in object) {
14+
if (callback(object[key], key, object)) {
15+
newOject = { ...newOject, ...{ [key]: object[key] } };
16+
}
17+
18+
}
19+
return newOject;
20+
}
21+
22+
// const objFilter = { a: 2, b: 3, c: 4, f: 10 }
23+
// console.log(filterObject(objFilter, (value => value > 3)));
24+
// const arr = [1, 2, 2, 3, 4, 5, 6]
25+
// console.log(filter(arr, (x => x > 3)))
26+
// filter(arr, ((x, i, a) => {
27+
// console.log(i);
28+
// }))
29+
30+
31+
function map(array, callback) {
32+
const newArray = [];
33+
for (let i = 0; i < array.length; i++) {
34+
newArray.push(callback(array[i], i, array))
35+
}
36+
return newArray;
37+
}
38+
39+
function objectMap(object, callback) {
40+
let newOject = {}
41+
for (const key in object) {
42+
newOject = { ...newOject, ...{ [key]: callback(object[key], key) } }
43+
}
44+
return newOject;
45+
}
46+
// const array = [1, 2, 3, 4]
47+
// const array = [{ x: 2, y: 3 }, { x: 2, y: 3 }]
48+
// console.log(map(array, (x => x.x)));
49+
50+
// const obj = { a: 2, b: 3, x: 4 }
51+
// console.log(Object.keys(obj));
52+
// console.log(
53+
// objectMap(obj, ((val, key) => {
54+
// return val * 2;
55+
// }
56+
// ))
57+
// );
58+
59+
60+
function reduce(array, callback, initialValue) {
61+
let accumulator = initialValue ?? 0;
62+
for (let i = 0; i < array.length; i++) {
63+
accumulator = callback(accumulator, array[i], i, array)
64+
}
65+
return accumulator;
66+
}
67+
68+
function objectReduce(object, callback, initialValue) {
69+
let accumulator = initialValue ?? 0;
70+
for (const key in object) {
71+
accumulator = callback(accumulator, object[key], key, object);
72+
}
73+
return accumulator;
74+
}
75+
76+
// const array = [1, 2, 3, 4]
77+
const object = { a: 2, b: 3, x: 4 }
78+
// console.log(reduce(array, ((acc, item) => {
79+
// acc.push()
80+
// }), [] ));
81+
82+
// console.log(
83+
// objectReduce(object, ((acc, value, key) => {
84+
// acc += value;
85+
86+
// return acc;
87+
// }), 0)
88+
// );
89+
90+
// function flat(array, depth = 1) {
91+
// return array.reduce((acc, item) => {
92+
// if (Array.isArray(item)) {
93+
// if (depth >= 1) {
94+
// acc.push(...flat(item, depth - 1))
95+
// return acc;
96+
// }
97+
// }
98+
// acc.push(item)
99+
// return acc;
100+
// }, [])
101+
// }
102+
103+
function flat(array, depth = 1) {
104+
return array.reduce((acc, item) => {
105+
if (Array.isArray(item) && depth >= 1) {
106+
acc.push(...flat(item, depth - 1))
107+
return acc;
108+
}
109+
acc.push(item);
110+
return acc;
111+
}, [])
112+
}
113+
114+
const arr = [[1, 2], 3, [8, 4, [5, 6]]];
115+
console.log(flat(arr));
116+
117+
118+
119+
120+
121+
122+
123+
124+

chunk.mjs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function chunk(array, size) {
2+
if (!array.length) {
3+
return [];
4+
}
5+
6+
return [array.slice(0, size), ...chunk(array.slice(size), size)];
7+
}
8+
9+
let arr = [1, 2, 3, 4, 5, 6];
10+
console.log(chunk(arr, 0));

clone-deep.mjs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const target = {
2+
field1: 1,
3+
field2: undefined,
4+
field3: {
5+
child: 'child'
6+
},
7+
field4: [2, 4, 8]
8+
};
9+
10+
target.target2 = target;
11+
function cloneDeep(target, map = new WeakMap()) {
12+
if (typeof target === 'object') {
13+
const cloneTarget = Array.isArray(target) ? [] : {}
14+
const currentTarget = map.get(target);
15+
if (currentTarget) {
16+
return currentTarget;
17+
}
18+
map.set(target, cloneTarget)
19+
for (const key in target) {
20+
cloneTarget[key] = cloneDeep(target[key], map);
21+
}
22+
return cloneTarget;
23+
}
24+
return target;
25+
}
26+
27+
28+
const newTarget = cloneDeep(target);
29+
// const newTarget = target;
30+
console.log(target.field4 === newTarget.field4);

convert-object-keys.mjs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
function snake_case(obj) {
2+
return Object.fromEntries
3+
(
4+
Object.entries(obj).map(([key, value]) => {
5+
const newKey = key.replace(/[A-Z]/g, (letter) => '_' + letter.toLowerCase());
6+
return [newKey, value]
7+
})
8+
)
9+
}
10+
11+
function camelCase(obj) {
12+
return Object.fromEntries
13+
(
14+
Object.entries(obj).map(([key, value]) => {
15+
const newKey = key.replace(/[-_][a-z]/g, (letter) => letter.toUpperCase().replace(/-|_/g, ''))
16+
return [newKey, value]
17+
})
18+
)
19+
}
20+
const obj = {
21+
fullName: 'Mahdi Rezazadeh',
22+
birthDate: '1996/12/28'
23+
24+
}
25+
// const snake_case_obj = snake_case(obj);
26+
// console.log('snake_case_obj: ', snake_case_obj);
27+
// const newSample = {
28+
// 'full-name': 'Mahdi Rez',
29+
// 'birth-date': '12313 123123'
30+
// }
31+
// const camelCaseObj = camelCase(newSample);
32+
// console.log('camelCaseObj: ', camelCaseObj);
33+
34+
console.log(mahdi);
35+
36+
37+
var mahdi = function ali() {
38+
return 'mahdi function'
39+
}

debounce.mjs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
function debounce(fn, delay) {
3+
let timer;
4+
return (...args) => {
5+
clearTimeout(timer);
6+
timer = setTimeout(() => {
7+
fn.apply(this, args);
8+
}, delay);
9+
}
10+
}
11+
12+
13+
function save(params) {
14+
console.log('item saved in ' + params);
15+
}
16+
17+
const timer = 1500;
18+
const process = debounce(save, timer);
19+
setInterval(() => {
20+
21+
process(timer)
22+
}, 200);

deep-equal.mjs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const person1 = {
2+
"firstName": "John",
3+
"lastName": "Doe",
4+
"age": 35
5+
};
6+
7+
const person2 = {
8+
"firstName": "John",
9+
"lastName": "Doe",
10+
"age": 35,
11+
};
12+
13+
const isDeepEqual = (object1, object2) => {
14+
15+
const objKeys1 = Object.keys(object1);
16+
const objKeys2 = Object.keys(object2);
17+
18+
if (objKeys1.length !== objKeys2.length) return false;
19+
20+
for (var key of objKeys1) {
21+
const value1 = object1[key];
22+
const value2 = object2[key];
23+
24+
const isObjects = isObject(value1) && isObject(value2);
25+
26+
if ((isObjects && !isDeepEqual(value1, value2)) ||
27+
(!isObjects && value1 !== value2)
28+
) {
29+
return false;
30+
}
31+
}
32+
return true;
33+
};
34+
35+
const isObject = (object) => {
36+
return object != null && typeof object === "object";
37+
};
38+
39+
console.log(isDeepEqual(person1, person2)); //true

factorial.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
var factorial = (function hideCache() {
3+
var cache = [];
4+
return function factorial(num) {
5+
if (num < 2) {
6+
return 1;
7+
}
8+
if (!(num in cache)) {
9+
cache[num] = num * factorial(num - 1);
10+
return cache[num];
11+
}
12+
return cache[num];
13+
}
14+
})()
15+
16+
17+
console.log(factorial(6));

groupBy.mjs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
function groupBy(array, callBack) {
2+
const newArray = [];
3+
const obj = {};
4+
for (let i = 0; i < array.length; i++) {
5+
const key = callBack(array[i], i);
6+
if (!obj[key]) {
7+
obj[key] = [array[i]];
8+
9+
} else {
10+
obj[key].push(array[i])
11+
}
12+
13+
}
14+
newArray.push(obj);
15+
return newArray;
16+
}
17+
18+
const products = [
19+
{ name: 'apples', category: 'fruits' },
20+
{ name: 'oranges', category: 'fruits' },
21+
{ name: 'potatoes', category: 'vegetables' }
22+
];
23+
24+
25+
const groupByCategory = groupBy(products, (res) => res.category)
26+
console.log('groupByCategory: ', groupByCategory);

0 commit comments

Comments
 (0)