Skip to content

Instantly share code, notes, and snippets.

Avatar
💭
working on puppetry 2.1 - new awesome ideas

Dmitry Sheiko dsheiko

💭
working on puppetry 2.1 - new awesome ideas
View GitHub Profile
@dsheiko
dsheiko / post-commit
Last active Jul 1, 2020
Git-hook to create a tag automatically based on lately committed package.json version
View post-commit
#! /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 Mar 6, 2020
Load/execute JavaScript asynchronously
View load.js
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 Jan 31, 2020
Gt cookies by name with JavaScript
View utils.js
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 Jan 19, 2019
Renders React components conditionally e.g. <If exp={ true }></If> to avoid inline short-circuit evaluation/ternary hell
View If.jsx
/*
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 {
View isPrivateMode.js
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;
}
View pre-commit
#!/bin/bash
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep ".*Marketplace.*\.js$")
if [[ "$STAGED_FILES" = "" ]]; then
exit 0
fi
PASS=true
View accept.js
/**
* 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 Mar 8, 2018
Service-worker to prefetch remote images (with expiration) and respond with fallback one when image cannot be fetched
View index.html
<!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 ) {
View Asynchronous Observable
async function* getStocksStream() {
var reader = new AsyncFileReader(“stocks.txt”);
try {
while(!reader.eof) {
var line = await reader.readLine();
await yield JSON.parse(line);
}
}
finally {
await reader.close();
@dsheiko
dsheiko / package.json
Last active Jan 30, 2019
jsdom Element.dataset partial polyfill
View package.json
{
"name": "jsdomdataset",
"version": "1.0.0",
"description": "",
"main": "polyfill-jsdom.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "GPL-3.0",
You can’t perform that action at this time.