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 / 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)
@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 / 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");
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 / 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
@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 / DeepLinkService.js
Created September 5, 2019 11:39
Service for cross platform handling of deep links.
import { Logger } from 'core/helpers'
class DeepLinkService {
init(navigation) {
this.navigation = navigation
}
handleUrl = (event, navigation) => {
try {
if (!event?.url) {
@capJavert
capJavert / dep-parser.js
Created July 19, 2019 12:43
WIP: Dep parser
const {
dependencies: dependenciesNew,
devDependencies: devDependenciesNew
} = require('../cazmatrans.hr-native-0.59/package.json')
const { dependencies, devDependencies } = require('./package.json')
// const newDeps = Object.keys(devDependenciesNew)
// console.log(newDeps.filter(dep => !devDependencies[dep]).join(' '))
const newDeps = Object.keys(dependenciesNew)
@capJavert
capJavert / useFetchWithCleanup.js
Created May 2, 2019 09:10
WIP: Uses signal option of fetch method to cleanup request handlers after component unmount
import { useEffect } from 'react'
const noop = () => {}
export default (
url,
options = {},
complete = noop,
error = noop,
final = noop
@capJavert
capJavert / withRemoteConfig.js
Created April 2, 2019 20:16
HOC for injecting config props after firebase remote config async call is completed
import React from 'react'
import firebase from 'react-native-firebase'
const withRemoteConfig = (configValues = []) => Component => (
class EnhancedComponent extends React.Component {
state = {
config: {}
}
componentDidMount = async () => {