Skip to content

Instantly share code, notes, and snippets.

@TheJarX
TheJarX / destructureMIME.js
Created March 25, 2021 20:19
Simple but can help
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types
// you can return an array instead of an object
function getDataFromMIME(mime) {
const [type, subtype, parameter] = mime.split(/[\/\;]/);
const [paramName, paramValue] = parameter.split('=');
return {
type,
subtype,
[paramName]: paramValue,
parameter: [paramName, paramValue]
@TheJarX
TheJarX / handleFileForm.js
Last active December 13, 2021 12:55
Sending files from React to an Apollo api
import { useMutation } from '@apollo/client';
import SOME_MUTATION from 'path/to/your/mutation.graphql';
export default function SomeForm() {
const [mutate] = useMutation(SOME_MUTATION);
const handleChange = ({
target: {
validity,
files: [file],
@TheJarX
TheJarX / toSnakeCase.js
Last active March 23, 2021 21:40
Function to convert camel case hash to snake case
const keyToSnake = word => word.replace(
/[A-Z]+/,
match => `_${match.toLowerCase().split('').join('_')}`,
);
const toSnakeCase = (obj) => {
const entries = Object.entries(obj).map(
entry => [keyToSnake(entry[0]), entry[1]],
);