Skip to main content

Bitwise XOR operator

0 likes • Nov 19, 2022 • 0 views
JavaScript
Loading...

More JavaScript Posts

Mongoose DB Connection

CHS
0 likes • Mar 11, 2021 • 0 views
JavaScript
require("dotenv").config();
const mongoose = require("mongoose");
const db = process.env.MONGO_URI;
const connectDB = async () => {
try {
await mongoose.connect(db, {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
useCreateIndex: true
});
console.log("MongoDB Connected");
} catch (err) {
console.error(err.message);
}
};
module.exports = connectDB;

Generate a random HEX color

0 likes • Nov 18, 2022 • 0 views
JavaScript
// Generate a random HEX color
randomColor = () => `#${Math.random().toString(16).slice(2, 8).padStart(6, '0')}`;
// Or
const randomColor = () => `#${(~~(Math.random()*(1<<24))).toString(16)}`;

Destructuring Assignment

0 likes • Nov 19, 2022 • 0 views
JavaScript
//JavaScript program to swap two variables
//take input from the users
let a = prompt('Enter the first variable: ');
let b = prompt('Enter the second variable: ');
//using destructuring assignment
[a, b] = [b, a];
console.log(`The value of a after swapping: ${a}`);
console.log(`The value of b after swapping: ${b}`);

Express Login Endpoint

CHS
0 likes • Mar 11, 2021 • 4 views
JavaScript
router.post("/", async (req, res) => {
const { email, password } = req.body;
try {
if (!email) return res.status(400).json({ msg: "An email is required" });
if (!password)
return res.status(400).json({ msg: "A password is required" });
const user = await User.findOne({ email }).select("_id password");
if (!user) return res.status(400).json({ msg: "Invalid credentials" });
const match = await bcrypt.compare(password, user.password);
if (!match) return res.status(400).json({ msg: "Invalid credentials" });
const accessToken = genAccessToken({ id: user._id });
const refreshToken = genRefreshToken({ id: user._id });
res.cookie("token", refreshToken, {
expires: new Date(Date.now() + 604800),
httpOnly: true,
});
res.json({ accessToken });
} catch (err) {
console.log(err.message);
res.status(500).json({ msg: "Error logging in user" });
}
});

stripe api json iteration

0 likes • Nov 18, 2022 • 0 views
JavaScript
var arr = [{
"success": true,
"data": [
{
"id": "ipi_1KrrbvDOiB2klwsKhuqUWqt1",
"object": "issuing.transaction",
"amount": -2743,
"amount_details": {
"atm_fee": null
},
"authorization": "iauth_1KrrbuDOiB2klwsKoFjQZhhd",
"balance_transaction": "txn_1KrrbwDOiB2klwsK1YkjJJRi",
"card": "ic_1Krqe5DOiB2klwsK44a35eiE",
"cardholder": "ich_1KrqL8DOiB2klwsKtBnZhzYr",
"created": 1650753567,
"currency": "usd",
"dispute": null,
"livemode": false,
"merchant_amount": -2743,
"merchant_currency": "usd",
"merchant_data": {
"category": "advertising_services",
"category_code": "7311",
"city": "San Francisco",
"country": "US",
"name": "Aeros Marketing, LLC",
"network_id": "1234567890",
"postal_code": "94103",
"state": "CA"
},
"metadata": {},
"type": "capture",
"wallet": null
},
{
"id": "ipi_1Krrbvc62B2klwsKhuqUWqt1",
"object": "issuing.transaction",
"amount": -9999,
"amount_details": {
"atm_fee": null
},
"authorization": "iauth_1KrrbuDOiB2klwsKoFjQZhhd",
"balance_transaction": "txn_1KrrbwDOiB2klwsK1YkjJJRi",
"card": "ic_1Krqe5DOiB2klwsK44a35eiE",
"cardholder": "ich_1KrqL8DOiB2klwsKtBnZhzYr",
"created": 1650753567,
"currency": "USD",
"dispute": null,
"livemode": false,
"merchant_amount": -9999,
"merchant_currency": "usd",
"merchant_data": {
"category": "fast_food",
"category_code": "7311",
"city": "San Francisco",
"country": "US",
"name": "Aeros Marketing, LLC",
"network_id": "1234567890",
"postal_code": "94103",
"state": "CA"
},
"metadata": {},
"type": "capture",
"wallet": null
}
]
}];
const reduced = arr.reduce((prev, curr) => {
prev.push({
amount: curr.data[0].merchant_amount,
id: curr.data[0].id,
currency: curr.data[0].currency,
category: curr.data[0].merchant_data.category
});
console.log(prev)
return prev;
}, []);

deep merge objects

0 likes • Nov 19, 2022 • 2 views
JavaScript
const deepMerge = (a, b, fn) =>
[...new Set([...Object.keys(a), ...Object.keys(b)])].reduce(
(acc, key) => ({ ...acc, [key]: fn(key, a[key], b[key]) }),
{}
);
deepMerge(
{ a: true, b: { c: [1, 2, 3] } },
{ a: false, b: { d: [1, 2, 3] } },
(key, a, b) => (key === 'a' ? a && b : Object.assign({}, a, b))
);
// { a: false, b: { c: [ 1, 2, 3 ], d: [ 1, 2, 3 ] } }