Skip to content

Instantly share code, notes, and snippets.

@dkarmalita
dkarmalita / app.js
Last active March 2, 2021 07:30
React in-browser setup 2021
/* global React, ReactDOM, document, SubComponent */
/*
eslint-disable
react/react-in-jsx-scope,
react/jsx-filename-extension,
react/jsx-one-expression-per-line,
no-useless-constructor,
react/prefer-stateless-function,
react/jsx-no-undef
@dkarmalita
dkarmalita / app.js
Last active November 3, 2020 09:04
Babel * in-browser setup with only loader from CDN (only index.html and your app.js neccesary)
/**
* You can use require here
* @example
* const sub = require('./sub')
* console.log('SUB >>',sub.default())
*/
ReactDOM.render((<h1>It Works!</h1>), document.getElementById('root'));
@dkarmalita
dkarmalita / app.js
Created October 30, 2020 08:09
React | ContextStore
/* Some conponent which uses contextState and has be connected (subscribed) to the store data */
const MyContextUser = props => {
// console.log('render', props.id)
return (
<div>
<div>{ props.myContextState.count }</div>
{ props.title
&& (
<button
onClick={() => props.myContextState.setState({ count: props.myContextState.count + 1 })}
@dkarmalita
dkarmalita / api.js
Last active October 24, 2020 19:37
React • in-browser setup
const exampleApi = {
async getMovies(){
return fetch('https://jsonplaceholder.typicode.com/todos/1')
.then((response) => {
return response.json();
})
}
}
exampleApi.getMovies().then(console.log)
@dkarmalita
dkarmalita / caff.bash
Last active November 1, 2020 10:23
Caff * OSX caffeinate advanced
#! /usr/bin/env bash
# kard, 20201020, https://gist.github.com/dkarmalita/c2bdec0181ca7dc81902138d004f34ad
# Run the caffeinate prefaced by date and time stamp. After the script
# finished (use "q" key), it cleans all of the caffeinate processes.
echo Caff starts at $(date "+%H:%M:%S %d/%m/%y")
caffeinate -di &
n="_"
@dkarmalita
dkarmalita / pinglog.bash
Last active November 1, 2020 10:24
Ping Log * ping logger with date/time stamp and caffeinate, OSX
#! /usr/bin/env bash
# kard 20201020, https://gist.github.com/dkarmalita/25225177299bcf7f80584b8d13fc5a0c
# OSX,
# Run pins with date/time stamps in each line. It stores the log in the ping.log
# file within the current working directory and opens the Console application within
# the log opened. Also, it uses caffeinate to prevent the computer from fall to sleep.
# After the script finishing, all of the processes are automatically closed.
echo Ping logger starts at $(date "+%H:%M:%S %d/%m/%y")
/**
@example
const { getNodeOptionValue } = require('nodeOptions');
const maxOldSpaceSize = getNodeOptionValue('max_old_space_size');
*/
const parseNodeOptions = () => (!process.env.NODE_OPTIONS ? [] : process.env.NODE_OPTIONS.split(/[=:]/));
const _optionsArr = parseNodeOptions();
const _getOptionsCount = () => (_optionsArr.length ? _optionsArr.length / 2 : 0);
const _getOptionName = i => _optionsArr[i * 2];
// v20200514 + extendVal
/* eslint-disable no-restricted-syntax, guard-for-in, */
module.exports = function cloneObject(
aObject,
extendVal = x => x,
) {
if (
!aObject
|| (typeof aObject !== 'object')
) {
const net = require('net');
const isPortOpen = async (port, { timeout = 1000, host } = {}) => {
const promise = new Promise(((resolve, reject) => {
const socket = new net.Socket();
const onError = () => {
socket.destroy();
reject();
};
export default function getSearchValueFromUrl(searchKey, removeFromUrl) {
const { origin, pathname, search, hash } = window.location;
const params = new URLSearchParams(search);
const value = params.get(searchKey);
if (removeFromUrl) {
params.delete(searchKey);
const nextsearch = params.toString();
const newHref = `${origin}${pathname}${nextsearch ? `?${nextsearch}` : ''}${hash}`;
window.history.replaceState(null, '', newHref);
}