Skip to content

Instantly share code, notes, and snippets.

View gilbarbara's full-sized avatar
💥
Keep Buggering On

Gil Barbara gilbarbara

💥
Keep Buggering On
View GitHub Profile
@gilbarbara
gilbarbara / config.ts
Last active September 28, 2023 16:49
Playwright coverage
import { join } from 'path';
export const TEMP_DIRECTORY = join(__dirname, '..', '.temp');
@gilbarbara
gilbarbara / vercel-cors.ts
Created August 16, 2022 00:07
Vercel cors helper
export function cors(fn: NextApiHandler, methods = ['GET']) {
return async (request: NextApiRequest, response: NextApiResponse) => {
const allowedMethods = [...methods, 'OPTIONS'].join(',');
response.setHeader('Access-Control-Allow-Credentials', 'true');
response.setHeader('Access-Control-Allow-Origin', '*');
response.setHeader('Access-Control-Allow-Methods', allowedMethods);
response.setHeader(
'Access-Control-Allow-Headers',
'X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Authorization, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version',
export function slugify(input = '') {
return removeAccents(input)
.replace(/[\u0300-\u036f]/g, '')
.replace(/[()]/g, '')
.replace(/ /g, '-')
.replace(/[|^{}%"<>\\`]/g, '')
.toLowerCase();
}
/**
* Remove accents
*/
export function removeAccents(input: string) {
const removalMap = {
A: /[AⒶAÀÁÂẦẤẪẨÃĀĂẰẮẴẲȦǠÄǞẢÅǺǍȀȂẠẬẶḀĄ]/g,
AA: /[Ꜳ]/g,
AE: /[ÆǼǢ]/g,
AO: /[Ꜵ]/g,
AU: /[Ꜷ]/g,
@gilbarbara
gilbarbara / machine.js
Last active January 2, 2020 16:48
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
// - XState (all XState exports)
@gilbarbara
gilbarbara / mapReactChildrenRecursively.ts
Last active September 23, 2019 19:36
Map React Children Recursively
interface IOptions {
predicate: (child: React.ReactChild) => boolean;
props: { [key: string]: any };
}
function mapReactChildrenRecursively(children: React.ReactNode, options: IOptions): React.ReactNode {
if (!options.predicate || !options.props) {
return children;
}
@gilbarbara
gilbarbara / validate-spotify-uri.ts
Created April 27, 2019 19:57
Validate Spotify URIs
function validateURI(input: string): boolean {
let isValid = false;
if (input && input.indexOf(':') > -1) {
const [key, type, id] = input.split(':');
if (key && type && id && id.length === 22) {
isValid = true;
}
}
@gilbarbara
gilbarbara / getTypeFromPropTypes.jsx
Created January 16, 2019 00:44
WIP: get type from a React component propTypes
const React = require('react');
const PropTypes = require('prop-types');
class Component extends React.Component {
render() {
return React.createElement('h1', {}, 'Component!');
}
}
Component.propTypes = {
@gilbarbara
gilbarbara / comparator.js
Created February 15, 2018 02:49
compare data properties
function comparator(data: Object, nextData: Object): Object {
return {
changedFrom(key: string, actual: 'string', prev: string): boolean {
return data[key] === prev && nextData[key] === actual;
},
changedTo(key: string, actual: 'string'): boolean {
return data[key] !== actual && nextData[key] === actual;
},
changed(key: string): boolean {
return data[key] !== nextData[key];
@gilbarbara
gilbarbara / randomID.js
Last active November 28, 2019 21:16
Generate a random alphanumeric string
function randomID(size = 6) {
const possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
let text = '';
for (let i = 0; i < size; i++) {
text += possible.charAt(Math.floor(Math.random() * possible.length));
}
return text;
}