Skip to content

Instantly share code, notes, and snippets.

@HynekS
HynekS / check-packages.js
Created March 21, 2024 07:56 — forked from simon360/check-packages.js
Compare different package.json files in a monorepo to see if dependency versions differ
const chalk = require("chalk");
const packages = [
require("./package.json"),
require("./packages/cec-scripts/package.json"),
require("./packages/generator-cec/package.json"),
require("./packages/utility-cec-simulator/package.json")
];
const compareDeps = p => {
@HynekS
HynekS / api.ts
Created March 6, 2024 20:37 — forked from DouglasdeMoura/api.ts
Tiny wrapper around fetch
// Extends the return of the HTTPError class
class HTTPError extends Error {
readonly response: any;
readonly status: number;
readonly statusText: string;
constructor(status: number, statusText: string, response: any) {
super(statusText);
this.status = status;
this.statusText = statusText;
@HynekS
HynekS / api.js
Created March 6, 2024 20:07 — forked from giacomocerquone/api.js
Handy thin wrapper around the fetch Api
const _toQueryString = params =>
`?${Object.entries(params)
.map(
([key, value]) =>
`${encodeURIComponent(key)}=${encodeURIComponent(value)}`
)
.join("&")}`;
// EDIT here if you prefer a storage implementation or a store subscription etc.
// you could actually also remove the getToken function and directly call it in the header below
import { useState, useEffect } from "react"
import tw from "twin.macro"
const bearerToken =
process.env.SANITY_STUDIO_TWITTER_API_BEARER_TOKEN
const CustomTweetPreview = ({ value: { id } }) => {
const [tweet, setTweet] = useState(null)
const [error, setError] = useState(null)
import { useEffect, useRef } from "react"
export default function useFocusNextOnEnter() {
const formRef = useRef()
const isForm = node => Boolean(node.nodeName === "FORM")
const handleKeyPress = e => {
if (e.key === "Enter" || e.keyCode === 13) {
const form = e.target.form
const pascalCasePattern = /([A-Z]+[a-z]*)+\b/
@HynekS
HynekS / drawImageScaled.js
Created September 19, 2020 09:51
Helper function to draw bitmap image on html canvas (centered, preserved aspect ratio)
function drawImageScaled({ context, image }) {
const canvas = context.canvas
const hRatio = canvas.width / image.width
const vRatio = canvas.height / image.height
const ratio = Math.min(hRatio, vRatio)
const centerShift_x = (canvas.width - image.width * ratio) / 2
const centerShift_y = (canvas.height - image.height * ratio) / 2
context.clearRect(0, 0, canvas.width, canvas.height)
context.drawImage(
@HynekS
HynekS / console_log.php
Last active March 7, 2020 13:23
Helper function to log php variables to a browser console.
<?php
// Tested on php 7.2.11 and chrome 80
function console_log(...$args)
{
$args_as_json = array_map(function ($item) {
return json_encode($item);
}, $args);
$js_code = "<script>console.log(
@HynekS
HynekS / gatsby-browser.js
Last active April 20, 2020 10:34 — forked from azamatsmith/gatsby-browser.js
Adding Redux to Gatsby - with devtools and redux-thunk
import React from 'react';
import { Provider } from 'react-redux';
import { applyMiddleware, compose, createStore } from 'redux';
import thunk from 'redux-thunk';
import rootReducer from './src/reducers';
const loadDevTools = () =>
process.env.NODE_ENV === 'development' && typeof window !== 'undefined' && window.devToolsExtension
? window.__REDUX_DEVTOOLS_EXTENSION__ &&
window.__REDUX_DEVTOOLS_EXTENSION__()