Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

added 5 important buil in functions .. #1

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

Merged
lgope merged 5 commits into master from before-marster
May 8, 2020
Merged
Show file tree
Hide file tree
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
46 changes: 46 additions & 0 deletions Asynchronous-JavaScript/byeTryCatchErrorHandling.js
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// catchAwait.js
const catchAwait = promise =>
promise
.then(data => ({ data, error: null }))
.catch(error => ({ error, data: null }));

module.exports = catchAwait;

// working file
const { getItems } = require('./api/items');
const catchAwait = require('./utils/catchAwait');

const allItems = async () => {
const { error, data } = await catchAwait(getItems());
if (!error) {
// code
}
console.error(error);
};

allItems();

/**
* Another way
*/

// catchAsync.js
module.exports = fn => {
return (req, res, next) => {
fn(req, res, next).catch(next);
};
};

// createOne.js

exports.createOne = Model =>
catchAsync(async (req, res, next) => {
const doc = await Model.create(req.body);

res.status(201).json({
status: 'success',
data: {
data: doc,
},
});
});
71 changes: 70 additions & 1 deletion README.md
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,2 +1,71 @@
# JavaScript
The of this repo is to save my js programs. Basics of JavaScript. Beginner level.
*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.*

## Table of Contents

1. [Important Methods](#methods)

## Methods
> Most important javascript build in methods

<a name="typeof"></a><a name="1.1"></a>
- [1.1](#typeof) **typeof**: Returns the type.

```javascript
console.log(typeof 44); // number

console.log(typeof 'something'); // string

console.log(typeof true); // boolean

let num = 12;
console.log(typeof(num)); // number

```

<a name="toString"></a><a name="1.2"></a>
- [1.2](#toString) **toString**: Returns the string representation of the number's value.

```javascript
let num = 10;
let n = num.toString();

console.log(typeof(num)); // number

console.log(typeof(n)); // string
```

<a name="indexOf"></a><a name="1.3"></a>
- [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.

```javascript
let str = "Hello world, welcome to the JS Universe.";
console.log(str.indexOf("welcome")); // 13
console.log(str.indexOf("wall")); // -1

const fruits = ['Orange', 'Pineapple', 'Apple', 'Melon'];
console.log(fruits.indexOf('Melon')); // 3

console.log(fruits.indexOf('klkljkh')); // -1
```

<a name="lastIndexOf"></a><a name="1.4"></a>
- [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.

```javascript
const fruits = ['Orange', 'Pineapple', 'Apple', 'Melon'];
console.log(fruits.lastIndexOf('Melon')); // 3

console.log(fruits.lastIndexOf('klkljkh')); // -1
```

<a name="length"></a><a name="1.5"></a>
- [1.5](#length) **length**: Returns the number of characters or size in a string or array.

```javascript
const fruits = ['Orange', 'Pineapple', 'Apple', 'Melon'];
console.log(fruits.length); // 4

let str = "Hello world, welcome to the JS Universe.";
console.log(str.length); // 40
```
99 changes: 99 additions & 0 deletions js-coding-technique/loops.js
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
const mil = 1000000;
const arr = Array(mil);

console.time('⏲️');

for (let i = 0; i < mil; i++) {} // 1.6ms

for (const v of arr) {
} // 11.7ms

arr.forEach(v => v); // 2.1ms

for (let i = mil; i > 0; i--) {} // 1.5ms

console.timeEnd('⏲️');

const equine = { unicorn: '🦄', horse: '🐴', zebra: '🦓' };

for (const key in equine) {
// Filters out properties inherited from prototype
if (equine.hasOwnProperty(key)) {
console.log(equine[key]);
}
}

// Unwrap the the Values

for (const val of Object.values(equine)) {
console.log(val);
}

// Create a Map
const equine = new Map(Object.entries(equine));

for (const v of equine.values()) {
console.log(v);
}

const equine = [
['unicorn', '🦄'],
['horse', '🐴'],
['zebra', '🦓'],
];

// 😒 Meh Code
for (const arr of equine) {
const type = arr[0];
const face = arr[1];
console.log(`${type} looks like ${face}`);
}

// 🤯 Destructured Code
for (const [type, face] of equine) {
console.log(`${type} looks like ${face}`);
}

///////////////
arr[Symbol.iterator] = function () {
let i = 0;
let arr = this;
return {
next: function () {
if (i >= arr.length) {
return { done: true };
} else {
const value = arr[i] + '🙉';
i++;
return { value, done: false };
}
},
};
};

////////////////////////////////

const faces = ['😀', '😍', '🤤', '🤯', '💩', '🤠', '🥳'];

// Transform values
const withIndex = faces.map((v, i) => `face ${i} is ${v}`);

// Test at least one value meets a condition
const isPoopy = faces.some(v => v === '💩');
// false

// Test all values meet a condition
const isEmoji = faces.every(v => v > 'ÿ');
// true

// Filter out values
const withoutPoo = faces.filter(v => v !== '💩');

// Reduce values to a single value
const pooCount = faces.reduce((acc, cur) => {
return acc + (cur === '💩' ? 1 : 0);
}, 0);
console.log(pooCount);

// Sort the values
const sorted = faces.sort((a, b) => a < b);

AltStyle によって変換されたページ (->オリジナル) /