Skip to content

Commit 725704f

Browse files
authored
Merge pull request lgope#1 from lgope/before-marster
added 5 important buil in functions ..
2 parents df2d233 + 024b9e1 commit 725704f

File tree

3 files changed

+215
-1
lines changed

3 files changed

+215
-1
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// catchAwait.js
2+
const catchAwait = promise =>
3+
promise
4+
.then(data => ({ data, error: null }))
5+
.catch(error => ({ error, data: null }));
6+
7+
module.exports = catchAwait;
8+
9+
// working file
10+
const { getItems } = require('./api/items');
11+
const catchAwait = require('./utils/catchAwait');
12+
13+
const allItems = async () => {
14+
const { error, data } = await catchAwait(getItems());
15+
if (!error) {
16+
// code
17+
}
18+
console.error(error);
19+
};
20+
21+
allItems();
22+
23+
/**
24+
* Another way
25+
*/
26+
27+
// catchAsync.js
28+
module.exports = fn => {
29+
return (req, res, next) => {
30+
fn(req, res, next).catch(next);
31+
};
32+
};
33+
34+
// createOne.js
35+
36+
exports.createOne = Model =>
37+
catchAsync(async (req, res, next) => {
38+
const doc = await Model.create(req.body);
39+
40+
res.status(201).json({
41+
status: 'success',
42+
data: {
43+
data: doc,
44+
},
45+
});
46+
});

README.md

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,71 @@
11
# JavaScript
2-
The of this repo is to save my js programs. Basics of JavaScript. Beginner level.
2+
*JavaScript, often abbreviated as JS, is a programming language that conforms to the ECMAScript specification. JavaScript is high-level, often just-in-time compiled, and multi-paradigm. It has curly-bracket syntax, dynamic typing, prototype-based object-orientation, and first-class functions. The of this repo is to save my js programs. Basics of JavaScript. Beginner level.*
3+
4+
## Table of Contents
5+
6+
1. [Important Methods](#methods)
7+
8+
## Methods
9+
> Most important javascript build in methods
10+
11+
<a name="typeof"></a><a name="1.1"></a>
12+
- [1.1](#typeof) **typeof**: Returns the type.
13+
14+
```javascript
15+
console.log(typeof 44); // number
16+
17+
console.log(typeof 'something'); // string
18+
19+
console.log(typeof true); // boolean
20+
21+
let num = 12;
22+
console.log(typeof(num)); // number
23+
24+
```
25+
26+
<a name="toString"></a><a name="1.2"></a>
27+
- [1.2](#toString) **toString**: Returns the string representation of the number's value.
28+
29+
```javascript
30+
let num = 10;
31+
let n = num.toString();
32+
33+
console.log(typeof(num)); // number
34+
35+
console.log(typeof(n)); // string
36+
```
37+
38+
<a name="indexOf"></a><a name="1.3"></a>
39+
- [1.3](#indexOf) **indexOf**: Returns the first index at which a given element can be found in the array, or -1 if it is not present.
40+
41+
```javascript
42+
let str = "Hello world, welcome to the JS Universe.";
43+
console.log(str.indexOf("welcome")); // 13
44+
console.log(str.indexOf("wall")); // -1
45+
46+
const fruits = ['Orange', 'Pineapple', 'Apple', 'Melon'];
47+
console.log(fruits.indexOf('Melon')); // 3
48+
49+
console.log(fruits.indexOf('klkljkh')); // -1
50+
```
51+
52+
<a name="lastIndexOf"></a><a name="1.4"></a>
53+
- [1.4](#lastIndexOf) **lastIndexOf**: Returns the last index at which a given element can be found in the array, or -1 if it is not present.
54+
55+
```javascript
56+
const fruits = ['Orange', 'Pineapple', 'Apple', 'Melon'];
57+
console.log(fruits.lastIndexOf('Melon')); // 3
58+
59+
console.log(fruits.lastIndexOf('klkljkh')); // -1
60+
```
61+
62+
<a name="length"></a><a name="1.5"></a>
63+
- [1.5](#length) **length**: Returns the number of characters or size in a string or array.
64+
65+
```javascript
66+
const fruits = ['Orange', 'Pineapple', 'Apple', 'Melon'];
67+
console.log(fruits.length); // 4
68+
69+
let str = "Hello world, welcome to the JS Universe.";
70+
console.log(str.length); // 40
71+
```

js-coding-technique/loops.js

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
const mil = 1000000;
2+
const arr = Array(mil);
3+
4+
console.time('⏲️');
5+
6+
for (let i = 0; i < mil; i++) {} // 1.6ms
7+
8+
for (const v of arr) {
9+
} // 11.7ms
10+
11+
arr.forEach(v => v); // 2.1ms
12+
13+
for (let i = mil; i > 0; i--) {} // 1.5ms
14+
15+
console.timeEnd('⏲️');
16+
17+
const equine = { unicorn: '🦄', horse: '🐴', zebra: '🦓' };
18+
19+
for (const key in equine) {
20+
// Filters out properties inherited from prototype
21+
if (equine.hasOwnProperty(key)) {
22+
console.log(equine[key]);
23+
}
24+
}
25+
26+
// Unwrap the the Values
27+
28+
for (const val of Object.values(equine)) {
29+
console.log(val);
30+
}
31+
32+
// Create a Map
33+
const equine = new Map(Object.entries(equine));
34+
35+
for (const v of equine.values()) {
36+
console.log(v);
37+
}
38+
39+
const equine = [
40+
['unicorn', '🦄'],
41+
['horse', '🐴'],
42+
['zebra', '🦓'],
43+
];
44+
45+
// 😒 Meh Code
46+
for (const arr of equine) {
47+
const type = arr[0];
48+
const face = arr[1];
49+
console.log(`${type} looks like ${face}`);
50+
}
51+
52+
// 🤯 Destructured Code
53+
for (const [type, face] of equine) {
54+
console.log(`${type} looks like ${face}`);
55+
}
56+
57+
///////////////
58+
arr[Symbol.iterator] = function () {
59+
let i = 0;
60+
let arr = this;
61+
return {
62+
next: function () {
63+
if (i >= arr.length) {
64+
return { done: true };
65+
} else {
66+
const value = arr[i] + '🙉';
67+
i++;
68+
return { value, done: false };
69+
}
70+
},
71+
};
72+
};
73+
74+
////////////////////////////////
75+
76+
const faces = ['😀', '😍', '🤤', '🤯', '💩', '🤠', '🥳'];
77+
78+
// Transform values
79+
const withIndex = faces.map((v, i) => `face ${i} is ${v}`);
80+
81+
// Test at least one value meets a condition
82+
const isPoopy = faces.some(v => v === '💩');
83+
// false
84+
85+
// Test all values meet a condition
86+
const isEmoji = faces.every(v => v > 'ÿ');
87+
// true
88+
89+
// Filter out values
90+
const withoutPoo = faces.filter(v => v !== '💩');
91+
92+
// Reduce values to a single value
93+
const pooCount = faces.reduce((acc, cur) => {
94+
return acc + (cur === '💩' ? 1 : 0);
95+
}, 0);
96+
console.log(pooCount);
97+
98+
// Sort the values
99+
const sorted = faces.sort((a, b) => a < b);

0 commit comments

Comments
 (0)