Skip to content

Instantly share code, notes, and snippets.

View chewhx's full-sized avatar
👋

Chew Han Xiang chewhx

👋
View GitHub Profile
@chewhx
chewhx / pad.txt
Last active January 25, 2021 09:40
// Pad string paragraph to a number of characters, and appends ellipsis.
// ---------- example------------
//
// const String = "Curabitur gravida nisi at nibh. In hac habitasse platea dictumst. Aliquam augue quam, sollicitudin vitae, consectetuer eget, rutrum at, lorem. Integer tincidunt ante vel ipsum. Praesent blandit lacinia erat. Vestibulum sed magna at nunc commodo placerat. nunc purus"
//
// pad(String, 100)
//
// returns "'Curabitur gravida nisi at nibh. In hac habitasse platea dictumst. Aliquam augue quam, sollicitudin v...'
//
@chewhx
chewhx / expenseCategories.txt
Created January 30, 2021 06:51
Categories for expenses
expenseCategories = [
'auto & parking',
'bills & utilities',
'business services',
'cash & cheque',
'education',
'entertainment',
'family',
'fees & charges',
'finance & investments',
["Acerola – West Indian Cherry","Apple","Apricots","Avocado","Banana","Blackberries","Blackcurrant","Blueberries","Breadfruit","Cantaloupe","Carambola","Cherimoya","Cherries","Clementine","Coconut Meat","Cranberries","Custard-Apple","Date Fruit","Durian","Elderberries","Feijoa","Figs","Gooseberries","Grapefruit","Grapes","Guava","Honeydew Melon","Jackfruit","Java-Plum","Jujube Fruit","Kiwifruit","Kumquat","Lemon","Lime","Longan","Loquat","Lychee","Mandarin","Mango","Mangosteen","Mulberries","Nectarine","Olives","Orange","Papaya","Passion Fruit","Peaches","Pear","Persimmon – Japanese","Pitaya (Dragonfruit)","Pineapple","Pitanga","Plantain","Plums","Pomegranate","Prickly Pear","Prunes","Pummelo","Quince","Raspberries","Rhubarb","Rose-Apple","Sapodilla","Sapote, Mamey","Soursop","Strawberries","Sugar-Apple","Tamarind","Tangerine","Watermelon"]
// config script to connect to MongoDB with MongooseJS
const mongoose = require("mongoose");
const connectDB = async () => {
const conn = await mongoose.connect(process.env.MONGODB_URL, {
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false,
useUnifiedTopology: true,
});
// create a class object to be passed to middleware for Express error handling
class errorResponse extends Error {
constructor(message, statusCode) {
super(message);
this.statusCode = statusCode;
Error.captureStackTrace(this, this.constructor);
}
}
// middleware script to handle error for all routes consistently
const errorHandler = async (err, req, res, next) => {
let error = { ...err };
error.message = err.message;
// Log to conosle for dev
console.log(err);
const asyncHandler = (fn) => (req, res, next) => {
Promise.resolve(fn(req, res, next)).catch(next);
};
module.exports = asyncHandler;
function pickOne(arr: any[]) {
if (!arr.length) {
throw new Error('Param cannot be empty');
}
return arr[Math.floor(Math.random() * arr.length)];
}
@chewhx
chewhx / romanNumerals.js
Created June 6, 2021 02:04
roman numerals object
const romanNumerals = {
0: "",
1: "I",
2: "II",
3: "III",
4: "IV",
5: "V",
6: "VI",
7: "VII",
8: "VIII",
@chewhx
chewhx / _font-weights.scss
Last active June 14, 2022 14:41
Scss mapping for font-weight .fw- classes
$font-weights: (100, 200, 300, 400, 500, 600, 700, 800, 900);
@each $weight in $font-weights {
.fw-#{$weight} {
font-weight: $weight;
}
}