Skip to content

Instantly share code, notes, and snippets.

Avatar

Jari Pennanen Ciantic

  • Finland, Jyväskylä
View GitHub Profile
@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());
@Ciantic
Ciantic / druid_d2d.rs
Created Feb 19, 2021
set_blend_mode, get_blend_mode
View druid_d2d.rs
//! Convenience wrappers for Direct2D objects.
//!
//! These also function as safety boundaries (though determining the
//! exact safety guarantees is work in progress).
// TODO: get rid of this when we actually do use everything
#![allow(unused)]
use std::ffi::c_void;
use std::fmt::{Debug, Display, Formatter};
View YAML-to-JSON-with-Rust.rs
// Yaml to JSON with Rust
// Dependencies serde_yaml and serde_json
// https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=3ddd4d9a65f03b717eeb1258299e9503
fn main() {
let json_value: serde_json::Value = serde_yaml::from_str(&"
---
test: some value here
another: value here
third:
@Ciantic
Ciantic / parse-url-search-params-to-object.js
Last active Sep 30, 2020
Parse url search parameters to object
View parse-url-search-params-to-object.js
Array.from(new URLSearchParams("foo=1&foo=2&foo=3&blah=a&blah=b").entries()).reduce((a,[k,v]) => ({ ...a, [k]: [...a[k] ?? [], v] }), {});
// {foo: ["1","2","3"], blah: ["a","b"]}
// Or directly to string
Array.from(new URLSearchParams("foo=1&foo=2&foo=3&blah=a&blah=b").entries()).reduce((a,[k,v]) => a.has(k) && (a.set(k, a.get(k) + "," + v) ?? a) || (a.set(k, v) ?? a), new URLSearchParams()).toString();
// "foo=1%2C2%2C3&blah=a%2Cb"
@Ciantic
Ciantic / sql-safe-field-names-with-typescript.ts
Last active Sep 15, 2020
Safeish SQL field names with typescript
View sql-safe-field-names-with-typescript.ts
function fields<T>() {
return new Proxy(
{},
{
get: function (_target, prop, _receiver) {
return prop;
},
}
) as {
[P in keyof T]: P;