Skip to content

Instantly share code, notes, and snippets.

View xiaoyunyang's full-sized avatar
🎯
Focusing

Xiaoyun Yang xiaoyunyang

🎯
Focusing
View GitHub Profile
@xiaoyunyang
xiaoyunyang / testingJest.md
Created July 3, 2019 17:28
Testing With Jest

What to Test

Testing interfaces

  • Child component with the right props
import EditProfileForm from "../feedbackForm";
import Checkbox from "../checkbox";
import EmailInput from "../EmailInput";
@xiaoyunyang
xiaoyunyang / ts-v-js.md
Created July 25, 2019 02:59
TypeScript vs JavaScript example

The example is taken from TypeScript's quick start tutorial

In JavaScript

const greeter = (person) => "hello "+person;
greeter([1,2,3]) // "hello 1,2,3"

In TypeScript

@xiaoyunyang
xiaoyunyang / closure_oo_example.js
Last active August 6, 2019 07:43
Closure Example - Object Oriented Programming
function Person(name) {
var secret = “secret!”
this.name = name
this.setName = function(newName) { this.name = newName }
this.setNameToFoo = function() { this.name = foo }
this.getSecret = function() { return secret }
}
 
var a = new Person(“Max”)
 
function foo() {
let res = ''
let j = 10
for(let i=0; i<j; i++) {
// uncommenting ... the following line causes error
// var j = 5
res += `${j}, `
j -= 1
}
import React, { useState } from "react";
function FriendStatus(props) {
const [isOnline, setIsOnline] = useState(null);
useEffect(() => {
function handleStatusChange(status) {
setIsOnline(status.isOnline);
}
ChatAPI.subscribeToFriendStatus(props.friend.id, handleStatusChange);
import React, { useState } from "react";
const UiState = {
LOADING: "LOADING",
ONLINE: "ONLINE",
OFFLINE: "OFFLINE",
};
const UiStateMessageMap = {
[UiState.LOADING]: "Loading...",
@xiaoyunyang
xiaoyunyang / closure_with_recursion_naive.js
Last active October 2, 2021 15:40
Closure Example - Recursion
var incrementUntil = function(max) { 
if(num >= max) return num 
num++
incrementUntil(max)
}
 
var num = 0
incrementUntil(3)
num //> 3
@xiaoyunyang
xiaoyunyang / TypeScriptMigration.md
Last active April 16, 2022 14:02
A guide for how to migrate your project from Flow to TypeScript
@xiaoyunyang
xiaoyunyang / PostDisplay.js
Last active January 9, 2023 15:16
Use Draft.js to display rich text created using the Draft.js editor.
import React from 'react';
import PropTypes from 'prop-types';
import { CompositeDecorator, convertFromRaw, Editor, EditorState } from 'draft-js';
// Following code based on:
// https://github.com/facebook/draft-js/blob/master/examples/draft-0-10-0/link/link.html
const Link = (props) => {
const {url} = props.contentState.getEntity(props.entityKey).getData();
return (
<a rel="nofollow noreferrer" href={url} target="_blank">
@xiaoyunyang
xiaoyunyang / install-nvm-on-a-mac.md
Last active July 11, 2023 22:25
How to install and use `nvm` on a Mac

What is nvm?

nvm is a version manager for node.js which lets you easily switch between different node versions. You can define an .nvmrc file in your node project which enforces the node version standardization with your codebase collaborators.

Install NVM

Install

wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash