Skip to content

Instantly share code, notes, and snippets.

View FanchenBao's full-sized avatar

Fanchen Bao FanchenBao

View GitHub Profile
@FanchenBao
FanchenBao / resize_screenshots.sh
Last active February 9, 2021 01:08
Source code 02 used in medium article "Make Fastlane Snapshot Work with React Native"
#!/bin/bash
# This script is used to resize the screenshot produced by fastlane screenshot
# to its appropriate size acceptable by App Store Connect
# DEFAULTS
IPHONE_11="1284x2778"
IPHONE_8_PLUS="1242x2208"
IPAD_PRO_3RD="2048x2732"
@FanchenBao
FanchenBao / dummyButton.js
Last active September 24, 2021 16:52
Source code 01 used in medium article "Make Fastlane Snapshot Work with React Native"
import * as React from 'react';
import {View, TouchableHighlight, Text} from 'react-native';
import {env} from 'config.js';
export const DummyButton = (props: PropsT) => {
const {reduxCallback} = props;
if (env === 'prod') { // do not display dummy buttons in production release
return null;
}
@FanchenBao
FanchenBao / updated_mycomponent.test.js
Last active January 6, 2021 00:31
This gist accompanies the UPDATED version of the medium article "Test hooks on 'keyboardDidShow' in React Native Functional Component"
import * as React from 'react';
import {Keyboard} from 'react-native';
import {render, act, cleanup} from '@testing-library/react-native'; // a better testing framework for event triggering
import {MyComponent} from 'mycomponent.js';
describe('Test MyComponent', () => {
afterAll(cleanup);
test('Text shown by default', () => {
const {queryByText} = render(<MyComponent />);
@FanchenBao
FanchenBao / mycomponent.test.js
Created January 5, 2021 03:38
This gist accompanies the medium article "Test hooks on 'keyboardDidShow' in React Native Functional Component"
import * as React from 'react';
import {Keyboard} from 'react-native';
import {mount} from 'enzyme';
import {MyComponent} from 'mycomponent.js';
describe('Test MyComponent', () => {
const mockSetShowText = jest.fn();
const mockUseState = jest.spyOn(React, 'useState');
const mockKeyboardListener = jest.spyOn(Keyboard, 'addListener');
const keyboardCallbackMap = {};
@FanchenBao
FanchenBao / mycomponent.js
Last active January 5, 2021 03:37
This gist accompanies the medium article "Test hooks on 'keyboardDidShow' in React Native Functional Component"
import * as React from 'react';
import {View, Text, Keyboard} from 'react-native';
const MyComponent = () => {
const [showText, setShowText] = React.useState(true);
// Use keyboard event (https://reactnative.dev/docs/keyboard)
React.useEffect(() => {
Keyboard.addListener('keyboardDidShow', _keyboardDidShow);
Keyboard.addListener('keyboardDidHide', _keyboardDidHide);
@FanchenBao
FanchenBao / .zshrc
Last active March 8, 2024 18:16
iTerm2 and Oh My Zsh Setup
# If you come from bash you might have to change your $PATH.
export PATH=/usr/local/bin:/usr/local/sbin:$PATH
# Path to your oh-my-zsh installation.
export ZSH="/Users/fanchenbao/.oh-my-zsh"
# Set default editor
export EDITOR='nano'
# Set name of the theme to load --- if set to "random", it will
@FanchenBao
FanchenBao / create_binary_tree.py
Last active November 23, 2020 20:55
Utility functions for use in LeetCode
from typing import List
from collections import deque
# Definition for a binary tree node.
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
@FanchenBao
FanchenBao / levenshtein.py
Created March 26, 2020 17:27
Levenshtein word distance algorithm
def levenshtein(token1, token2):
mtx = [[0] * (len(token2) + 1) for _ in range(len(token1) + 1)]
# fill the first column and row
for j in range(len(token2) + 1):
mtx[0][j] = j
for i in range(len(token1) + 1):
mtx[i][0] = i
# DP
for i, t1 in enumerate(token1):
for j, t2 in enumerate(token2):
@FanchenBao
FanchenBao / thing_shadow_via_AWSIoTPythonSDK.py
Last active January 20, 2020 02:19
Sample code for updating and getting aws iot thing shadow using AWSIoTPythonSDK or boto3.
"""
This is heavily borrowed from AWSIoTPythonSDK's sample code here:
https://github.com/aws/aws-iot-device-sdk-python/blob/master/samples/basicShadow/basicShadowUpdater.py
"""
from time import sleep
from AWSIoTPythonSDK.MQTTLib import AWSIoTMQTTShadowClient
import json
import logging
logger = logging.getLogger("AWSIoTPythonSDK.core")
@FanchenBao
FanchenBao / re-rendering_demo.js
Last active January 20, 2020 01:54
Demo code to show that fat arrow function and binding causes re-rendering in react native
import React, {PureComponent} from 'react';
import {
View,
Text,
TouchableHighlight,
TouchableOpacity,
} from 'react-native';
class Demo extends PureComponent {
constructor(props) {