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 / index.js
Created June 30, 2020 12:49
JS: snake_case and pascalCase to "Title Case"
const toTanginaMoCase = (str: string): string => {
// Convert snake case to title case
return str.split('_')
.map(str => str.charAt(0).toUpperCase() + str.substr(1))
.join('')
.replace(/([A-Z])/g, ' $1')
.trim()
}
@srph
srph / index.js
Last active April 23, 2020 14:15
js: Get cities of a region in the PH
var provinces = require('philippines/provinces')
var cities = require('philippines/cities')
function getCitiesByRegion(region) {
// Provinces under that specific region
var provinces = provinces.filter(province => province.region === region)
return provinces
// Append cities to the provinces under that specific region
// [{ name: 'Batanes}, region: 'II', key: 'BTN' }] -> [{ name: 'Batanes}, region: 'II', key: 'BTN', cities: ['Basco', ...] }]
@srph
srph / index.tsx
Last active April 15, 2020 14:23
React: useLocalStorageState hook
import { useState } from 'react'
const useLocalStorageState = <T>(key: string, defaultValue?: T): [T, (value: T) => void] => {
const [state, internalSetState] = useState<T>(() => {
try {
const value = localStorage.getItem(key)
if (value) return JSON.parse(value)
} catch (e) {
console.error(e)
}
@srph
srph / index.tsx
Created March 23, 2020 13:27
React: Unfinished 1-level deep navigation stack
import React, { useContext, useState, useEffect } from 'react'
interface NavigationItem {
back: string
title: string
}
interface ContextType {
active: NavigationItem | null
setActive: (active: NavigationItem | null) => void
@srph
srph / main.go
Last active December 27, 2019 01:29
Go: Merge sort - programming exercise
package main
import (
"fmt"
)
func main() {
list := []int{38, 27, 24, 56, -16, 12, 32, -3, 16, 11, 15, 25, 29}
fmt.Printf("Original: %v\n", mergeSort(list))
fmt.Printf("Post-Merge Sort: %v\n", merged)
@srph
srph / index.js
Created December 27, 2019 00:42
JS: Merge sort - programming exercise
const array = [38, 27, 24, 56, 12, 32, 16, 11, 15, 25, 29];
console.log(mergeSort(array));
@srph
srph / Page.js
Last active March 5, 2020 06:04
React: example global state with context
import React, { useContext } from 'react'
import { useUser } from 'contexts/User'
export default function Page() {
const user = useUser(context)
return (
<div>Your name is {user.name}</div>
)
}
@srph
srph / index.d.ts
Created September 29, 2019 12:02
react-input-slider: TypeScript definition
declare module 'react-input-slider' {
export interface SliderValue {
x: number
y: number
}
export interface SliderProps {
axis?: 'x' | 'y' | 'xy'
x?: number
y?: number
@srph
srph / UiInputSlider.tsx
Last active September 29, 2019 12:03
react-input-slider: Lazily update value only when the drag ends.
import * as React from 'react'
import { useState, useRef } from 'react'
import Slider, { SliderValue } from 'react-input-slider'
type Style = { [key: string]: any }
interface Props {
value: number
min: number
max: number
@srph
srph / index.js
Created September 19, 2019 12:19
JS: convenient logger for debugging
const log = (label, value) => {
console.log(label, value)
return value
}