Skip to main content

getURLParameters

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

More JavaScript Posts

Redux Slice

CHS
0 likes • Mar 11, 2021 • 0 views
JavaScript
import { createSlice } from "@reduxjs/toolkit";
const alert = createSlice({
name: "alert",
initialState: {
msg: "",
status: "",
},
reducers: {
set_alert: (state, action) => {
return {
...state,
msg: action.payload.msg,
status: action.payload.status,
};
},
clear_alert: (state, action) => {
return {
...state,
msg: "",
status: "",
};
},
},
});
export default alert.reducer;
const { set_alert, clear_alert } = alert.actions;
export const setAlert = (msg, status) => dispatch => {
dispatch(set_alert({ msg, status }));
setTimeout(() => dispatch(clear_alert()), 4000);
};
export const clearAlert = () => dispatch => {
dispatch(clear_alert());
};

Temperature Converter

0 likes • Nov 19, 2022 • 0 views
JavaScript
const celsiusToFahrenheit = (celsius) => celsius * 9/5 + 32;
const fahrenheitToCelsius = (fahrenheit) => (fahrenheit - 32) * 5/9;
// Examples
celsiusToFahrenheit(15); // 59
celsiusToFahrenheit(0); // 32
celsiusToFahrenheit(-20); // -4
fahrenheitToCelsius(59); // 15
fahrenheitToCelsius(32); // 0

Timer

0 likes • Feb 5, 2021 • 2 views
JavaScript
class Timer{
start(){
this.startTime = Date.now();
}
stop(){
const dt = Date.now() - this.startTime;
console.log(dt);
return dt;
}
getExecTime(fun, asynchronous){
return asynchronous ? this.getAsyncExecTime(fun) : this.getSyncExecTime(fun);
}
getSyncExecTime(fun){
this.start();
fun();
return this.stop();
}
async getAsyncExecTime(fun){
this.start();
await fun();
return this.stop();
}
static average(fun, numTimes, asynchronous = false){
const times = [];
const promises = [];
let timer = new Timer();
if(asynchronous){
for(let i = 0; i < numTimes; i++){
timer.getAsyncExecTime(fun);
}
}else{
for(let i = 0; i < numTimes; i++){
timer.getSyncExecTime(fun);
}
}
return (times => times.reduce(((sum, acc) => sume + acc), 0) / times.length);
}
static async timeAsync(fun, numTimes){
if(numTimes <= 1) return new Timer().getAsyncExecTime(fun);
const promises = [];
let timer = new Timer(true);
for(let i = 0; i < numTimes; i++){
promises.push(fun().catch((e)=>{}));
}
try{
await Promise.all(promises).catch((e)=>{});
}catch(e){
//do absolutely nothing
}
return timer.stop();
}
constructor(start = false){
if(start) this.start();
}
}

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;

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;
}, []);

Untitled

CAS
0 likes • Aug 13, 2023 • 2 views
JavaScript
function sortNumbers(arr) {
return arr.slice().sort((a, b) => a - b);
}
const unsortedArray = [4, 1, 9, 6, 3, 5];
const sortedArray = sortNumbers(unsortedArray);
console.log("Unsorted Numbers:", unsortedArray);
console.log("\n");
console.log("Sorted Numbers:", sortedArray);