Skip to content

Instantly share code, notes, and snippets.

Avatar

Jari Pennanen Ciantic

  • Finland, Jyväskylä
View GitHub Profile
View sqlite-worker-query.ts
const worker = new Worker("https://cdn.jsdelivr.net/npm/sql.js@1.7.0/dist/worker.sql-wasm.js");
let rpcid = 0;
async function query(sql: string, params: any, action = "exec") {
const id = rpcid++;
return new Promise<any>((res, rej) => {
const listener = (e: MessageEvent<any>) => {
if (e.data && e.data.id === id) {
worker.removeEventListener("message", listener);
res(e.data);
}
View paytrail-example-without-nonsense.php
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
use Paytrail\SDK\Client;
use Paytrail\SDK\Exception\HmacException;
use Paytrail\SDK\Exception\ValidationException;
use Paytrail\SDK\Model\Address;
use Paytrail\SDK\Model\CallbackUrl;
use Paytrail\SDK\Model\Customer;
@Ciantic
Ciantic / sqlite3_update_hook.cs
Created Feb 11, 2022
This is test code I threw away, it looks like SQLite3 update hook is *per* connection, making it useless for subscribing for events between processes (even if they use same database).
View sqlite3_update_hook.cs
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using SQLitePCL;
static public class Thing {
public static void ListenSqlite(DbContext context)
{
context.Database.OpenConnection();
var c = context.Database.GetDbConnection() as SqliteConnection;
SQLitePCL.raw.sqlite3_update_hook(c.Handle, test_func, new object());
View seo-framework-disable-content-image.php
<?php
// Disable article images that come from the content e.g. first <img /> tag
add_filter('the_seo_framework_image_generation_params', function( $params, $args, $context ) {
if (isset( $params['cbs']['content'])) {
unset($params["cbs"]["content"]);
}
return $params;
}, 11, 3);
@Ciantic
Ciantic / intersectionObserverSteps.js
Last active Oct 30, 2021
Intersection observer steps
View intersectionObserverSteps.js
// steps(10) == [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0] == length = 11
const steps = (r) => Array(r + 1).fill().map((_, i) => i / r); // with fill
const steps = (r) => ([...Array(r + 1)]).map((_, i) => i / r); // with splat
const steps = (r) => Array.apply(null, Array(r + 1)).map((_, i) => i / r); // If no splat
// Benchmark notes: third implementation fails with maximum stack size, first one is the fastest (with fill)
@Ciantic
Ciantic / offsetRelativeTo.js
Created Oct 22, 2021
Get offset relative to selector or window
View offsetRelativeTo.js
/**
* Get offset relative to selector (or window)
*
* @param {HTMLElement} el
* @param {string|undefined} selector if not give, assumes root most (window)
*/
function offsetRelativeTo(el, selector) {
let offsetTop = el.offsetTop;
let offsetLeft = el.offsetLeft;
let offsetParent = el.offsetParent;
@Ciantic
Ciantic / createObservableMap.ts
Last active Sep 19, 2021
ObservableMap (from MobX) to solidjs (not sure if this is a good idea)
View createObservableMap.ts
// LICENSE MIT, Jari Pennanen
// For all purposes, I also consider this public domain work.
import { untrack } from "solid-js";
import { createStore } from "solid-js/store";
/**
* Solid JS
*
* @returns Map like observable
@Ciantic
Ciantic / makeObservable.ts
Last active Sep 18, 2021
makeObservable (from MobX) to solidjs (not sure if this is a good idea)
View makeObservable.ts
import { createSignal, createComputed } from "solid-js";
function createProperty<T, O>(o: O, n: keyof O, def: T) {
const [get, set] = createSignal(def);
Object.defineProperty(o, n, {
get,
set,
});
return def;
}
View tuple_layout.rs
#[repr(C)]
struct Foo(f32, f32, f32);
fn main() {
let a = [Foo(1.0,2.0,3.0), Foo(4.0,5.0,6.0)];
let b = [1.0f32,2.0,3.0,4.0,5.0,6.0];
let c = [(1.0f32,2.0f32,3.0f32), (4.0f32,5.0f32,6.0f32)];
let amem = unsafe { std::slice::from_raw_parts((&a as *const _) as *const u8, std::mem::size_of_val(&a) )};
let bmem = unsafe { std::slice::from_raw_parts((&b as *const _) as *const u8, std::mem::size_of_val(&b) )};
let cmem = unsafe { std::slice::from_raw_parts((&c as *const _) as *const u8, std::mem::size_of_val(&c) )};
@Ciantic
Ciantic / build.rs
Created Feb 25, 2021
Some in-memory stuff I threw away
View build.rs
use std::io::Write;
use std::{env, path::Path};
use std::{fs::OpenOptions, path::PathBuf};
use regex::Regex;
fn main() {
let dir = Path::new(&env::var("CARGO_MANIFEST_DIR").unwrap()).join("lib");
println!("cargo:rustc-link-search=native={}", dir.display());