Skip to content

Instantly share code, notes, and snippets.

@GingerBear
GingerBear / create-context-provider.tsx
Created September 27, 2019 17:57
Util function to create type safe state management store with useContext and useReducer
/**
*
* Util function to create type safe state management store with useContext and useReducer.
* The mechanism is described by https://kentcdodds.com/blog/how-to-use-react-context-effectively/
*
*/
import React, { useReducer, createContext, useContext } from 'react';
export type ContextProviderProps = { children: React.ReactNode };
@GingerBear
GingerBear / arr-filter-type-guard.ts
Created September 17, 2019 15:11
TS arr filter type guard
function notNullOrUndefined<T>(x: T | null | undefined): x is T {
return x !== null && x !== undefined;
}
const arr = [1, 2, null, undefined]; // (number | null | undefined)[]
const arrFiltered = arr.filter(notNullOrUndefined); // number[]
@GingerBear
GingerBear / return-type-overloads.ts
Created May 16, 2019 18:42
return different type based parameter with overloads
function foo(): null;
function foo(a: number): string;
function foo(a?) {
if (typeof a === 'number') {
return '123';
} else {
return null;
}
}
@GingerBear
GingerBear / sampleArray.js
Created April 2, 2019 19:13
sample Array
function sampleArray(arr, num) {
if (arr.length <= num) {
return arr;
} else {
const every = Math.floor(arr.length / num);
const sampleIndex = Array(num)
.fill(0)
.map((_, i) => i * every);
// keep last element
@GingerBear
GingerBear / Dockerfile
Last active April 1, 2019 20:38
alpine node3 with python 3 and puppeteer ready
FROM node:10-alpine
# Installs Python 3
RUN apk add --no-cache python3 && \
python3 -m ensurepip && \
rm -r /usr/lib/python*/ensurepip && \
pip3 install --upgrade pip setuptools && \
if [ ! -e /usr/bin/pip ]; then ln -s pip3 /usr/bin/pip ; fi && \
if [[ ! -e /usr/bin/python ]]; then ln -sf /usr/bin/python3 /usr/bin/python; fi && \
rm -r /root/.cache
@GingerBear
GingerBear / proxy_dev_server.js
Created March 7, 2019 21:15
proxy dev server
const express = require('express');
const path = require('path');
const fs = require('fs');
const proxy = require('http-proxy-middleware');
const app = express();
const proxyConfig = require('./proxy-remote.conf.json');
const port = 4200;
const ngBuildPath = path.join(__dirname, 'dist/projectAbc');
const ngBuildIndex = fs
@GingerBear
GingerBear / proxy_dev_server.js
Created March 7, 2019 21:15
proxy dev server
const express = require('express');
const path = require('path');
const fs = require('fs');
const proxy = require('http-proxy-middleware');
const app = express();
const proxyConfig = require('./proxy-remote.conf.json');
const port = 4200;
const ngBuildPath = path.join(__dirname, 'dist/projectAbc');
const ngBuildIndex = fs
@GingerBear
GingerBear / codepush_release.md
Created April 24, 2018 03:42
codepush release
  • install appcenter cli https://github.com/Microsoft/appcenter-cli
  • appcenter login
  • update "name": "GNCUAT", in package.json
  • add "react-native": "0.0.0", to dependencies in package.json
  • appcenter codepush release-react -a Branding-Brand/GNCIOS -d Latest
@GingerBear
GingerBear / union_type_check.ts
Last active April 19, 2018 20:08
union type check
type typeA = { a: string };
type typeB = { b: string };
type typeAorB = typeA | typeB;
// type check for union type
function isA(foo: typeA | typeB): foo is typeA {
return (<typeA>foo).a !== undefined;
}
// usage