Skip to content

Instantly share code, notes, and snippets.

Avatar
💥
Keep Buggering On

Gil Barbara gilbarbara

💥
Keep Buggering On
View GitHub Profile
View slugify.ts
export function slugify(input = '') {
return removeAccents(input)
.replace(/[\u0300-\u036f]/g, '')
.replace(/[()]/g, '')
.replace(/ /g, '-')
.replace(/[|^{}%"<>\\`]/g, '')
.toLowerCase();
}
View removeAccents.ts
/**
* Remove accents
*/
export function removeAccents(input: string) {
const removalMap = {
A: /[AⒶAÀÁÂẦẤẪẨÃĀĂẰẮẴẲȦǠÄǞẢÅǺǍȀȂẠẬẶḀĄ]/g,
AA: /[]/g,
AE: /[ÆǼǢ]/g,
AO: /[]/g,
AU: /[]/g,
View machine.js
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
// - XState (all XState exports)
@gilbarbara
gilbarbara / mapReactChildrenRecursively.ts
Last active Sep 23, 2019
Map React Children Recursively
View mapReactChildrenRecursively.ts
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;
}
View validate-spotify-uri.ts
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 Jan 16, 2019
WIP: get type from a React component propTypes
View getTypeFromPropTypes.jsx
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 Feb 15, 2018
compare data properties
View comparator.js
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 Nov 28, 2019
Generate a random alphanumeric string
View randomID.js
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;
}
@gilbarbara
gilbarbara / HtmlWrapper.js
Created Dec 25, 2017
Convert DOM node to React Element
View HtmlWrapper.js
@@ -0,0 +1,21 @@
import React from 'react';
import PropTypes from 'prop-types';
import ReactHtmlParser from 'react-html-parser';
export default class HtmlWrapper extends React.Component {
static propTypes = {
element: PropTypes.oneOfType([
PropTypes.string,
PropTypes.object,
View measurements.js
const measurementsData = require('./measurements.json');
const newData = measurementsData.table.measurements.reduce((acc, val, idx) => {
acc.labels.push(val.name);
val.sizes.forEach((d, i) => {
let row = acc.data.find(r => r[0] === d.name);
if (!row) {
acc.data.push([d.name]);