Skip to content

Instantly share code, notes, and snippets.

View heytulsiprasad's full-sized avatar
⚛️
Overreacting

Tulsi Prasad heytulsiprasad

⚛️
Overreacting
View GitHub Profile
@heytulsiprasad
heytulsiprasad / Post.js
Last active July 26, 2020 08:18
A comprehensive user profile mongoose model
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
// Create Schema
const PostSchema = new Schema({
user: {
type: Schema.Types.ObjectId,
ref: "users",
},
text: {
@heytulsiprasad
heytulsiprasad / monId.md
Last active July 25, 2020 20:02
Validate mongo id from req.param.id

Check string for mongo id

if (id.match(/^[0-9a-fA-F]{24}$/)) {
  // Yes, it's a valid ObjectId, proceed with `findById` call.
}

Why do I need this?

@heytulsiprasad
heytulsiprasad / gist.md
Last active July 25, 2020 19:51
Differentiate vs good and bad code?

Logic

Tip 1

Using else if only when specifically required. Try to see if only else could solve it.

Tip 2

Think of what types the function returns, do they satisfy your needs? Otherwise, coerce them.

@heytulsiprasad
heytulsiprasad / .prettierrc
Created July 25, 2020 22:47
Common prettier config file
{
"useTabs": false, // Indent lines with tabs instead of spaces.
"printWidth": 80, // Specify the length of line that the printer will wrap on.
"tabWidth": 2, // Specify the number of spaces per indentation-level.
"singleQuote": false, // Use single quotes instead of double quotes.
/**
* Print trailing commas wherever possible.
* Valid options:
* - "none" - no trailing commas
* - "es5" - trailing commas where valid in ES5 (objects, arrays, etc)
@heytulsiprasad
heytulsiprasad / Arrow.jsx
Last active August 5, 2020 20:39
A svg turned into react component with added keyframes for continuous animations.
import React from "react";
import styled, { keyframes } from "styled-components";
const slide = keyframes`
from {
transform: translateY(0);
}
to {
transform: translateY(5px);
@heytulsiprasad
heytulsiprasad / photos.jsx
Created August 14, 2020 21:22
Using gatsby-images
import React from "react";
import { graphql } from "gatsby";
import Img from "gatsby-image";
function photos({ data }) {
console.log(data.images.nodes);
return (
<div>
<h1>All My Photos</h1>
@heytulsiprasad
heytulsiprasad / sample.txt
Last active August 23, 2020 13:35
Sample csv file for my Array.reduce blog post
1,Donnamarie,Jemison,djemison0@ifeng.com,Female
2,Herculie,Chadbourne,hchadbourne1@opensource.org,Male
3,Shepperd,Luquet,sluquet2@so-net.ne.jp,Male
4,Sinclare,Nuttey,snuttey3@tmall.com,Male
5,Deane,Crimp,dcrimp4@paginegialle.it,Female
6,Cayla,Solman,csolman5@abc.net.au,Female
7,Mose,Magnar,mmagnar6@360.cn,Male
8,Wilfrid,Robertet,wrobertet7@ted.com,Male
9,Carney,Bantick,cbantick8@techcrunch.com,Male
10,Angelique,Marklund,amarklund9@engadget.com,Female
@heytulsiprasad
heytulsiprasad / tailwind.js
Created September 4, 2020 17:55
Config tailwind to desktop first approach
module.exports = {
theme: {
extend: {},
screens: {
xl: { max: "1279px" },
// => @media (max-width: 1279px) { ... }
lg: { max: "1023px" },
// => @media (max-width: 1023px) { ... }
@heytulsiprasad
heytulsiprasad / .env
Last active September 20, 2020 16:26
Add mongoose to node and express
MONGO_PROD_URI=mongodb+srv://admin:<password>@cluster.mongodb.net/<dbname>?retryWrites=true&w=majority
MONGO_DEV_URI=mongodb://127.0.0.1:27017/<dbname>
@heytulsiprasad
heytulsiprasad / cors.js
Last active September 26, 2020 06:49
Example setup for cors without using cors package
const origin =
process.env.NODE_ENV === "production"
? process.env.FRONTEND_PROD_URL
: process.env.FRONTEND_LOCAL_URL;
app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", origin);
res.header("Access-Control-Allow-Credentials", true);
if (req.method === "OPTIONS") {