Skip to content

Instantly share code, notes, and snippets.

💭
working on puppetry 2.1 - new awesome ideas

Dmitry Sheiko dsheiko

💭
working on puppetry 2.1 - new awesome ideas
Block or report user

Report or block dsheiko

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@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",
@dsheiko
dsheiko / placeholder.js
Created Aug 15, 2012
Emulates H5Forms placeholders on legacy browsers
View placeholder.js
/*
* Emulates H5Forms placeholders on legacy browsers
*
* <input placeholder="Search" />
*
* @author $Author: sheiko $
* @license MIT
* @copyright (c) Dmitry Sheiko http://www.dsheiko.com
*/
You can’t perform that action at this time.