diff --git a/Asynchronous-JavaScript/byeTryCatchErrorHandling.js b/Asynchronous-JavaScript/byeTryCatchErrorHandling.js new file mode 100644 index 0000000..7a37ab2 --- /dev/null +++ b/Asynchronous-JavaScript/byeTryCatchErrorHandling.js @@ -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, + }, + }); + }); diff --git a/README.md b/README.md index f129988..bb0c9bf 100644 --- a/README.md +++ b/README.md @@ -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 + + +- [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 + + ``` + + +- [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 + ``` + + +- [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 + ``` + + +- [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 + ``` + + +- [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 + ``` \ No newline at end of file diff --git a/js-coding-technique/loops.js b/js-coding-technique/loops.js new file mode 100644 index 0000000..db195f5 --- /dev/null +++ b/js-coding-technique/loops.js @@ -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);