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 / 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(':')
@srph
srph / Helper.php
Created May 15, 2019 21:13
PHP: Generate invitation codes/licenses strings
<?php
namespace App\Support;
use Str;
class Helper {
/**
* e.g., X4345-ZXCA4-S3XYB-3AST1
*/
@srph
srph / index.tsx
Last active May 15, 2019 20:08
react-gateway: Support fallbacks for GatewayDest (alternative for https://github.com/cloudflare/react-gateway/pull/39)
/**
* Supercharges GatewayDest so that it allows default children, because this does not work:
* <GatewayDest><div>Default content if nobody has passed anything to me yet.</GatewayDest>
* @see https://github.com/cloudflare/react-gateway/pull/39
*
* @usage <GatewayDestWithFallback name={constants.gateway.backUrl} fallback={<UiNavigation.Action />} />
*/
import * as React from 'react'
import { GatewayDest } from 'react-gateway'
@srph
srph / getStreak.ts
Created April 18, 2019 02:50
JS: Example code to get number of consecutive dates
import { parse, subDays } from 'date-fns';
import getEntryTodayDateString from './getEntryTodayDateString'
import getEntryDateString from './getEntryDateString'
/**
* Get the streak between today and the last date.
* Assumes that there is an entry for today.
*/
export default function getStreak(tracker: AppDataTracker): number {
@srph
srph / Helper.php
Created April 1, 2019 13:03
Converts an array of objects into an object, keys based on its key
<?php
namespace App\Support;
class Helper {
/**
* Converts an array of objects into an object, keys based on its key
* [{ "entry_date": "" }, { "entry_date": "" }] -> { "2019-08-20": {}, ... }
*/
static public function toPropertyKeys($array, $property) {
@srph
srph / isNumericKeyCode.ts
Created March 28, 2019 11:20
JS: Check if the pressed key (evt.keyCode) is a numeric key
const NUMERIC_0 = 48
const NUMERIC_9 = 57
/**
* Check if the pressed key (evt.keyCode) is a numeric key
*/
export default function isNumericKeyCode(key: number): boolean {
return key >= NUMERIC_0 && key <= NUMERIC_9
}
@srph
srph / index.ts
Last active March 26, 2019 18:45
React Router 4: Allow nested routes
import * as React from 'react'
import { Route } from 'react-router-dom'
import { RouteProps } from 'react-router'
class RouterRoute extends React.Component<RouteProps> {
render() {
const { component, children, ...rest } = this.props
const Component = this.props.component
return (