Skip to content

Instantly share code, notes, and snippets.

View joepuzzo's full-sized avatar
💭
Coding something

Joe Puzzo joepuzzo

💭
Coding something
View GitHub Profile
const formatDate = (d, config = {}) => {
const date = new Date(d);
return new Intl.DateTimeFormat('en-US', {
timeZone: 'America/Los_Angeles',
month: 'short',
day: 'numeric',
hour: 'numeric',
hour12: true,
...config,
}).format(date);
import { useState, useRef, useEffect } from 'react';
// https://github.com/facebook/react/issues/14543
function useStateWithGetter(initial) {
const ref = useRef();
const mounted = useRef(true);
const [state, setState] = useState(initial);
ref.current = state;
const getFormatter = (formatter, value) => {
// If mask is a string turn it into an array;
if (typeof formatter === 'string') {
return formatter.split('').map((char) => {
if (char === '#') {
return /\d/;
}
@joepuzzo
joepuzzo / parseFunction.js
Last active November 5, 2020 16:01
Parses out functions from pure JSON
let replacer = (key, value) => {
// if we get a function, give us the code for that function
if (typeof value === 'function') {
return value.toString();
}
return value;
}
let reviver = (key, value) => {
if (typeof value === 'string'
@joepuzzo
joepuzzo / getJSPAN.js
Last active November 5, 2020 18:18
Turns json pointer into Java Script Property Access Notation
/**
* Turns a JSON pointer into Java Script Propery Access Notation
*
* /foo/bar/baz/0/taz ---> foo.bar.baz[0].taz
*
* IF trim is set to true
*
* /foo/bar/baz/0/taz ---> foo.bar.baz[0]
*/
export default function getJSPAN($ref, trim) {
import React, { useState, useRef } from 'react';
const useClipboard = () => {
const [copySuccess, setCopySuccess] = useState('Copy');
const textAreaRef = useRef(null);
function copyToClipboard(e: any) {
// @ts-ignore
const copyText = textAreaRef.current.textContent;
const textArea = document.createElement('textarea');
/* -------------------- toPath -------------------- */
const toPath = (path = '') => {
return String.prototype.replace
.call(path, /\['(.+?)'\]/g, '.$1')
.split(/[,[\].]+?/)
.filter(Boolean);
}
/* --------------------- get --------------------- */
const fs = require('fs');
const zlib = require('zlib');
// Read in a file
const pFile = fs.readFileSync(`${__dirname}/bigFile`);
// Log the byte size
console.log('Original Size:', Buffer.byteLength(pFile, 'utf8'));
// Compress it
{
"AE": {
"country": "AE",
"countryCode": "971",
"iddPrefix": "00",
"nddPrefix": "0",
"formatters": [
{
"leadingDigitsPattern": "60|8",
"formatter": [
{
"version": 4,
"countries": {
"AC": {
"phone_code": "247",
"idd_prefix": "00",
"national_number_pattern": "(?:[01589]\\d|[46])\\d{4}",
"types": {
"uan": {
"pattern": "(?:0[1-9]|[1589]\\d)\\d{4}",