Skip to content

Instantly share code, notes, and snippets.

View capJavert's full-sized avatar
🤘
Kicking ass!

Ante Barić capJavert

🤘
Kicking ass!
View GitHub Profile
@capJavert
capJavert / randomString.js
Created September 9, 2019 13:00
Generate random string of specified length.
const randomString = (length = 16) => {
let string = ''
for (let i = 0; i < length; i += 1) {
const random = (Math.random() * 16) | 0 // eslint-disable-line
string += random.toString(16)
}
return string
@capJavert
capJavert / fetchRetry.polyfill.js
Last active October 4, 2019 05:57
Simple fetch retry polyfill.
/**
* Simple polyfill to support retry of fetch requests
*/
;(() => {
global.fetchOriginal = global.fetch
/**
* Create retry fetch with last request arguments
*
* @param {string} resource request URL or resource to fetch
const fetch = require('isomorphic-unfetch')
const { performance } = require('perf_hooks')
//Replace char function
function setCharAt(str,index,chr) {
if(index > str.length-1) return str;
return str.substr(0,index) + chr + str.substr(index+1);
}
//Check if they are the same
@capJavert
capJavert / clearHtml.js
Created February 4, 2020 11:15
Clear HTML markup from string (the Gmail way)
const clearHtml = (data) => {
let html = data
html = html.replace(/<style([\s\S]*?)<\/style>/gi, '');
html = html.replace(/<script([\s\S]*?)<\/script>/gi, '');
html = html.replace(/<\/div>/ig, '\n');
html = html.replace(/<\/li>/ig, '\n');
html = html.replace(/<li>/ig, ' * ');
html = html.replace(/<\/ul>/ig, '\n');
html = html.replace(/<\/p>/ig, '\n');
html = html.replace(/<br\s*[\/]?>/gi, "\n");
@capJavert
capJavert / pascalCase.js
Last active April 28, 2020 14:02
Convert delimiter based string like hyphen-case to pascalCase string
const capitalize = text => text.replace(/^\w/, c => c.toUpperCase())
/**
* Convert delimiter based string like hyphen-case to
* pascalCase string
*
* @param {*} text
* @param {string} [delimiter='-'] delimiter regex, for / you need to pass \/
* @returns {string} pascal cased string
*/
@capJavert
capJavert / requireOneOf.js
Created October 8, 2020 20:22
Require at least one of provided props
import PropTypes from 'prop-types'
/**
* Require at least one of provided props
*
* @example
*
* const requireOneOfProps = requireOneOf({
* entity: PropTypes.string,
* entities: PropTypes.arrayOf(PropTypes.string)