Skip to content

Instantly share code, notes, and snippets.

View srph's full-sized avatar
🛠️
Building @Stride-Labs Frontend

Kier Borromeo srph

🛠️
Building @Stride-Labs Frontend
View GitHub Profile
@srph
srph / getStandardFormattedDateTime.ts
Created June 7, 2019 03:24
JS: mysql-formatted string with date-fns
import { format } from 'date-fns'
/**
* Gives you a mysql standard formatted datetime
* e.g., 2018-08-08 23:00:00
*/
function getStandardFormattedDateTime(date: Date = new Date()) {
return format(date, 'YYYY-MM-DD HH-mm-ss')
}
@srph
srph / index.ts
Created June 6, 2019 08:56
JS: Group chat messages based on criteria (initial code for Care.tv)
interface AppPartyLog {
id: number
party_id: number
type: 'activity' | 'message'
activity?: {
id: string
text: string
user: AppUser
}
message?: {
@srph
srph / index.tsx
Created June 4, 2019 22:48
React: Force route to remount (e.g., refetch data) when params change
/**
* Little trick to force our component to remount entirely if the user
* creates a tracker all while viewing another tracker.
*/
function WrappedDashboardTracker(props: Props) {
return (
<DashboardTracker key={props.match.params.trackerId} {...props} />
)
}
@srph
srph / PeriodicNow.ts
Created June 2, 2019 09:55
React: Get the current date periodically
import * as React from 'react'
interface Props {
interval: number
children: (now: Date) => React.ReactNode
}
interface State {
now: Date
}
@srph
srph / compose-refs.ts
Created May 25, 2019 21:30
React: Function to assign multiple refs to an element (compose refs)
function composeRefs<T>(refs: React.Ref<T>[]): React.Ref<T> {
return function(component: T) {
refs.forEach((ref) => {
if (typeof ref === 'function') {
ref(component)
} else if (ref && 'current' in ref) {
ref.current = component
}
})
}
@srph
srph / index.ts
Created May 21, 2019 12:39
JS: Get remaining time from seconds
interface RemainingTimeValue {
hours: number
minutes: number
seconds: number
}
/**
* Get remaining time from seconds
*
* Used by `toReadableTime` and `distanceInWordsAbbreivated`
@srph
srph / readme.md
Last active June 2, 2019 12:30
React Hook: Get an updated current date instance every second

Usage

import { useNow } from './useNow'

function MyApp() {
  const now = useNow({ interval: 1000 })
  
  return (
    <div>The current date is {now}</div>
 )
@srph
srph / toSearchIndexObject.ts
Last active May 19, 2019 10:22
JS: Performantly get the original index of an item.
import get from 'lodash.get'
interface SearchMap {
[key: string]: number
}
/**
* Performantly get the original index of an item.
* Use-case: Get index of item in array A in array B
* e.g., Check if user (inside users array) is inside the invitations array
@srph
srph / toSearchObject.ts
Last active May 19, 2019 06:58
JS: Make it easy to search for an item inside an array
interface SearchMap {
[key: string]: boolean
}
/**
* Make it easy to search for an item inside an array
*
* @input
* [{ id: 5 }, { id: 6 }, { id: 7 }, { id: 32 }]
* @output
@srph
srph / index.ts
Created May 17, 2019 22:37
JS: Convert seconds to readable time
/**
* Converts seconds to readable time (e.g., 03:45:24, 45:34, 34)
*/
export default function toReadableTime(seconds: number): string {
const hh = Math.floor(seconds / 3600)
const mm = Math.floor((seconds % 3600 / 60))
const ss = Math.floor(seconds % 60)
if (hh >= 1) {
return [hh, mm, ss].map(t => String(t).padStart(2, '0')).join(':')