Skip to content

Instantly share code, notes, and snippets.

Avatar

Erko Bridee erkobridee

View GitHub Profile
View useTimeout.ts
import * as React from 'react';
type TFunction = (...args: any[]) => any;
/**
* safe way to use window.setTimeout using a hook to handle it
*
* @param {TFunction} callback to be executed by the timeout
* @param {number} delay time to the execution
* @param {boolean} autorun flag that says if should start running right after call the hook - default true
View useInterval.ts
import * as React from 'react';
export type TFunction = (...args: any[]) => any;
/**
* safe way to use window.setInterval using a hook to handle it
*
* @param {TFunction} callback to be executed on each interval
* @param {number} delay time between the executions
* @param {boolean} autorun flag that says if should start running right after call the hook - default true
View ClickOutside.tsx
import * as React from 'react';
export interface IClickOutsideProps {
onClick: () => void;
/** element to add the click event listener, default selector is body */
parentTargetQuerySelector?: string;
}
/**
* Function Component to re-use the behavior of click outside
View useRefState.ts
import * as React from 'react';
/**
* extend the React.useState to have the state referente, so it will be possible to use its value
* inside of other callbacks
*
* @param {T} initialValue
*
* @return {[T, React.MutableRefObject<T>, React.Dispatch<React.SetStateAction<T>>]} array
*/
View array_helpers.ts
type TJSObject = { [key: string]: any };
type TJSValue = TJSObject | any;
type TArrayFilter<T> = (x: T) => boolean;
const TO_STRING = {}.toString;
const isObjectBasicCheck = <T extends object>(value: any): value is T => value !== null && typeof value === 'object';
@erkobridee
erkobridee / utils_values.js
Created Jan 25, 2019
nodejs script to manipulate and generate random data (uses the lodash and the moment libs)
View utils_values.js
'use strict';
var _ = require('lodash'),
hash = require('./utils_hash')
;
//----------------------------------------------------------------------------//
var SERVER_DATE_FORMAT = 'YYYY-MM-DD';
module.exports.SERVER_DATE_FORMAT = SERVER_DATE_FORMAT;
@erkobridee
erkobridee / utils_hash.js
Last active Jan 25, 2019
node.js script to generate a hash value using the hash.js and the lodash
View utils_hash.js
'use strict';
var _ = require('lodash'),
toHash = {}
;
//============================================================================//
// hash ids with mininum length = 8
var hashids = (function(){
@erkobridee
erkobridee / requestImageAsString.ts
Created Jan 23, 2019
example of how read an image from the server as a binary one and parse it to its base64 string
View requestImageAsString.ts
const readImageAsDataURL = async (imageBlob: Blob): Promise<string> => {
// https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readAsDataURL
const imageReader = new FileReader();
return new Promise<string>(resolve => {
const load = () => {
imageReader.removeEventListener('load', load);
resolve(imageReader.result as string);
};
imageReader.addEventListener('load', load, false);
imageReader.readAsDataURL(imageBlob);
View react-markdown.md
// https://github.com/rexxars/react-markdown
// https://github.com/GregRos/typed-react-markdown
import * as ReactMarkdown from 'react-markdown';
<ReactMarkdown linkTarget="_blank">
  {intl.formatMessage(translations.message_with_links)}
</ReactMarkdown>
View htmlentities.ts
/*
useful references:
String.prototype.replace()
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace
https://alligator.io/js/string-replace/
Encode and Decode HTML entities using pure Javascript
https://ourcodeworld.com/articles/read/188/encode-and-decode-html-entities-using-pure-javascript
You can’t perform that action at this time.