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 / stringTruncation.md
Last active February 20, 2024 04:57
String Truncation Algorithm

Naive

const TRUNCATE_WORDS_CUTOFF = 12;

export const truncateName = (name: string, truncateCutoff: number = TRUNCATE_WORDS_CUTOFF) =>
    name.length > truncateCutoff
        ? `${name.substring(0, truncateCutoff)}...`
        : name;
@xiaoyunyang
xiaoyunyang / dev-secret-best-practice.md
Last active July 18, 2023 20:25
How to keep dev secrets

Instead of always running source .env before running yarn, you can add your dev secrets like NPM_TOKEN or OPEN_AI_API_KEY to a global dev environmental variable file on your machine (e.g. dev.env) then add this to your .bashrc to source it on startup:

# dev.env
export NPM_TOKEN=<create one in npm settings>
# bashrc
source ~/dev.env
@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
@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 / TypeScriptMigration.md
Last active April 16, 2022 14:02
A guide for how to migrate your project from Flow to TypeScript
@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
import React, { useState } from "react";
const UiState = {
LOADING: "LOADING",
ONLINE: "ONLINE",
OFFLINE: "OFFLINE",
};
const UiStateMessageMap = {
[UiState.LOADING]: "Loading...",
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);