Skip to content

Instantly share code, notes, and snippets.

View esamattis's full-sized avatar

Esa-Matti Suuronen esamattis

View GitHub Profile
@esamattis
esamattis / fn.ts
Created February 2, 2022 13:00
Type-safe custom globals
function maybeGlobal<T extends object>(): Partial<T> | undefined {
return (typeof window !== "undefined" ? window : undefined) as any;
}
maybeGlobal<{
MY_GLOBAL_VARIABLE: string;
}>()?.MY_GLOBAL_VARIABLE?.toUpperCase();
@esamattis
esamattis / ni.sh
Created January 27, 2022 18:56
Put it to `.zshrc`
ni() {
# Add packages
if [ "${1:-}" != "" ]; then
if [ -f pnpm-lock.yaml ] || [ -f ../../pnpm-lock.yaml ]; then
pnpm install $@
else
npm install $@
fi
return
fi
@esamattis
esamattis / git-embed-to-monorepo.md
Last active May 18, 2021 10:01
How to embed a smaller git to a subdirectory of a monorepo?

Embed Repository to Monorepo with History

Scenario: I have two repositories

  • monorepo
  • tool

and I want to move the tool repository into the monorepo under path packages/tool with rewritten history in a way it looks like it has been there all the time.

@esamattis
esamattis / unwrap.ts
Created March 12, 2021 15:18
Unwraps the type so it's more readable in vscode tool tips
/**
* Unwraps the type so it's more readable in vscode tool tips
* */
export type Unwrap<T> = T extends object
? {
[P in keyof T]: Unwrap<T[P]>;
}
: T;
<?php
register_graphql_connection( [
'fromType' => 'HierarchicalContentNode',
'toType' => 'ContentNode',
'fromFieldName' => 'ancestors',
'description' => __( 'Returns ancestors of the node. Default ordered as lowest (closest to the child) to highest (closest to the root).', 'wp-graphql' ),
'resolve' => function( Post $post, $args, $context, $info ) {
$ancestors = get_ancestors( $post->ID, null, 'post_type' );
if ( empty( $ancestors ) || ! is_array( $ancestors ) ) {
return null;
if input volume of (get volume settings) = 0 then
display notification "MIC ON!"
set level to 100
else
set level to 0
display notification "MUTED MIC!"
end if
set volume input volume level
@esamattis
esamattis / action-logger.php
Created May 11, 2020 07:06
WordPress Action logger
<?php
class ActionLogger {
static $started;
static $prev;
static function start() {
self::$started = hrtime(true);
self::$prev = hrtime(true);
add_action('all', [self::class, '__action_all'], 10000);
@esamattis
esamattis / Anchestors.php
Last active May 7, 2020 07:52
This is broken.
<?php
namespace Valu\Sakke\GraphQL;
use WPGraphQL\AppContext;
use GraphQL\Type\Definition\ResolveInfo;
use GraphQL\Deferred;
/**
* Basing on WPGraphQL v0.7
env=~/.ssh/agent.env
agent_load_env () { test -f "$env" && . "$env" >| /dev/null ; }
agent_start () {
(umask 077; ssh-agent >| "$env")
. "$env" >| /dev/null ; }
agent_load_env
@esamattis
esamattis / karabiner.json
Last active May 23, 2020 17:50
Section (§) to Backtick with Karabiner-Elements on Finnish keyboards
{
"profiles": [
{
"complex_modifications": {
"rules": [
{
"manipulators": [
{
"description": "Section to Backtick (MS keyboard)",
"from": {