Skip to content

Instantly share code, notes, and snippets.

View dsheiko's full-sized avatar
💭
working on puppetry 3.2.5

Dmitry Sheiko dsheiko

💭
working on puppetry 3.2.5
View GitHub Profile
@dsheiko
dsheiko / AntDesignUploadExpressServer.js
Last active October 20, 2022 08:51
Ant Design Upload server side for Express.js example
import React from "react";
import http from "http";
import fs from "fs";
import { join } from "path";
import express from "express";
import bodyParser from "body-parser";
import { default as Busboy } from "busboy";
const app = express(),
@dsheiko
dsheiko / onAllImagesLoaded.js
Created August 20, 2021 13:11
Function runs callback when all the images in the document fully loaded
/**
* @returns Image[]
*/
function getIncompleteImages() {
return Array.from( document.querySelectorAll( "img" ) )
.filter( img => !img.complete || img.naturalWidth === 0 );
}
/**
* Execute the given callback when all the images in the document loaded
* We call this function when markup with images is rendered but images are still loading
@dsheiko
dsheiko / post-commit
Last active March 24, 2023 21:42
Git-hook to create a tag automatically based on lately committed package.json version
#! /bin/bash
version=`git diff HEAD^..HEAD -- "$(git rev-parse --show-toplevel)"/package.json | grep -m 1 '^\+.*version' | sed -s 's/[^A-Z0-9\.\-]//g'`
if [[ ! $version =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)(\-[A-Z]+\.[0-9]+)?$ ]]; then
echo -e "Skip tag: invalid version '$version'"
exit 1
fi
git tag -a "v$version" -m "`git log -1 --format=%s`"
echo "Created a new tag, v$version"
@dsheiko
dsheiko / load.js
Created March 6, 2020 12:52
Load/execute JavaScript asynchronously
function loadJs(url){
return new Promise( (resolve, reject) => {
if (document.querySelector(`head > script[src="${src}"]`) !== null) return resolve()
const script = document.createElement("script")
script.src = url
script.onload = resolve
script.onerror = reject
document.head.appendChild(script)
});
}
@dsheiko
dsheiko / utils.js
Created January 31, 2020 15:54
Gt cookies by name with JavaScript
function getCookie( name ) {
const value = "; " + document.cookie,
parts = value.split( "; " + name + "=" );
if ( parts.length === 2 ) {
return parts.pop().split( ";" ).shift();
}
return null;
}
@dsheiko
dsheiko / If.jsx
Last active January 19, 2019 22:47
Renders React components conditionally e.g. <If exp={ true }></If> to avoid inline short-circuit evaluation/ternary hell
/*
Usage:
<If exp={ true }> components to render </If>
<If exp={ false }> components not to render </If>
*/
import React from "react";
import PropTypes from "prop-types";
export default class If extends React.Component {
function isPrivateMode() {
return new Promise((resolve) => {
const on = function(){ resolve( true ); }
const off = function(){ resolve(false); }
const testLocalStorage = function(){
try {
if ( localStorage.length ) {
off();
return;
}
#!/bin/bash
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep ".*Marketplace.*\.js$")
if [[ "$STAGED_FILES" = "" ]]; then
exit 0
fi
PASS=true
/**
* Get round nested ternary operator
*
* <header>{
* accept()
* .when( a == 1, <h1>Case 1</h1>)
* .when( a == 2, <h1>Case 2</h1>)
* .otherwise( <h1>Case 3</h1>)
* .render()
* }</header>
@dsheiko
dsheiko / index.html
Last active October 27, 2023 19:59
Service-worker to prefetch remote images (with expiration) and respond with fallback one when image cannot be fetched
<!DOCTYPE html>
<html>
<head>
<title>Service-worker demo</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script>
if ( "serviceWorker" in navigator ) {