Skip to content

Instantly share code, notes, and snippets.

View esamattis's full-sized avatar

Esa-Matti Suuronen esamattis

View GitHub Profile
@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": {
@esamattis
esamattis / HTMLEntities.php
Last active February 13, 2021 08:08
Automatically decode HTML entities from wp-graphql content fields
<?php
/**
* WordPress does automatic HTML entity encoding but so does React which
* results in double encoding. This reverts the one from WordPress when
* the content is requested using wp-graphql making your life as a React
* dev easier.
*
*/
class HTMLEntities

Keybase proof

I hereby claim:

  • I am esamattis on github.
  • I am esamatti (https://keybase.io/esamatti) on keybase.
  • I have a public key whose fingerprint is 5049 677F 4375 E9F0 42BD E964 475F 6384 8C75 40AE

To claim this, I am signing this object:

<?php
namespace WPNext;
/**
* WordPress does automatic HTML entity encoding but so does React which
* results in double encoding.
*
* But since Headup is only used with React we can decode the entities using
* PHP and let React do it's encoding thing.
<?php
class TranslationStrings
{
function __construct()
{
if (!\function_exists('pll_translate_string')) {
return;
}
const wait = t => new Promise(resolve => setTimeout(resolve, t));
class Dataloader {
constructor() {
this.pending = {};
}
async load(id) {
return new Promise(resolve => {
this.pending[id] = resolve;
@esamattis
esamattis / uniq-meta-values.php
Last active May 15, 2019 12:57
Get unique meta values for a given post type in WordPress WPGraphQL
<?php
/**
* Add graphql root query for getting unique meta values for a given post type
*/
class UniqMetaValues {
function init() {
add_action( 'graphql_register_types', [ $this, 'register_types' ], 10, 0 );
}
@esamattis
esamattis / classnamed.tsx
Created April 3, 2019 12:58
Class Named Components
import React from "react";
type ClassNamePrimitive = string | false | undefined | null;
interface ClassNamesFunction<Props> {
(props: Props): ClassNamePrimitive | ClassNamePrimitive[];
}
interface ClassNamesDict<Props> {
[className: string]: boolean | ((props: Props) => boolean);
@esamattis
esamattis / AvgCalculator.ts
Created January 28, 2019 16:15
AvgCalculator
type BuildMeasures<T extends string> = {
[K in T]: {count: number; duration: number}
};
export class AvgCalculator<T extends string> {
private measures: BuildMeasures<T>;
constructor(keys: T[]) {
const measures = {} as BuildMeasures<T>;
@esamattis
esamattis / scroll-search.ts
Created January 23, 2019 12:49
Use async iteration to iterate over large search results in Elasticsearch
import {Client, SearchParams} from "elasticsearch";
/**
* Iterate all search results one by one with async iteration without reading
* it all to memory
*/
async function* scrollSearch<Document>(esClient: Client, params: SearchParams) {
let res = await esClient.search<Document>(params);
while (true) {