Skip to content

Instantly share code, notes, and snippets.

View mkrishnan-codes's full-sized avatar
:atom:

Manu Krishnan mkrishnan-codes

:atom:
View GitHub Profile
@mkrishnan-codes
mkrishnan-codes / gitcheatsheet.md
Last active September 16, 2022 09:29
Git cheatsheet

Create a patch from staged changes

git diff --cached > something.patch

Apply patch

git apply /path/to/some-changes.patch
@mkrishnan-codes
mkrishnan-codes / unique-eslint-rule-formatter.js
Last active August 5, 2022 11:56
Get the unique errors reported from ESLint by using custom formatters
module.exports = (results) => {
const byRuleId = results.reduce((map, current) => {
current.messages.forEach(({ ruleId, line, column }) => {
if (!map[ruleId]) {
map[ruleId] = [];
}
const occurrence = `${current.filePath}:${line}:${column}`;
map[ruleId].push(occurrence);
});
@mkrishnan-codes
mkrishnan-codes / log-middleware-sample.js
Created June 7, 2022 01:19
redux custom middleware sample code
const logMiddleWare = store => next => action => {
if (action.type.includes('SET_SOME_DATA')) {
console.log(`%c ${action.type}`, 'background: #222; color: #00ffcc');
}
next(action);
}
@mkrishnan-codes
mkrishnan-codes / truck.ts
Last active June 2, 2020 07:29
Example for generic class in typescript
class Person {
name: string
age: number
constructor(name: string, age: number) {
this.name = name;
this.age = age;
}
}
class Fruit {
ripe: boolean;
@mkrishnan-codes
mkrishnan-codes / BookContext.js
Created February 17, 2020 09:04 — forked from kjmczk/BookContext.js
Fetching API data with React Hooks - Build a React App using Basic Hooks
const BookContextProvider = ({ children }) => {
// ...
// add
const getBooks = async () => {
const res = await fetch(
'https://jsonplaceholder.typicode.com/posts?_start=2&_limit=5'
);
const data = await res.json();
const UserPropsInjectorMiddleware = (store: any) => (next: any) => (action: any) => {
// This will append userProps to all the actions called
// Store used here is an immutable state object, so store.getState()
// is an immutable object and need to use 'get' method to get attributes
// use store.getState().userData if it is a plain object
action.userProps = store.getState().get('userData')
const result = next(action)
return result
}
export default UserPropsInjectorMiddleware;
@mkrishnan-codes
mkrishnan-codes / Device.tsx
Last active October 15, 2019 08:37
Typescript device component - can be used for rendering different react component for different devices based on width
import * as React from 'react'
interface Props {
children: React.ReactNode;
type: 'desktop' | 'mobile' | 'tablet';
}
interface State {
width: number;
}
export const tabWidth = 992;
export const mobWidth = 767;