Skip to content

Instantly share code, notes, and snippets.

View miguel-leon's full-sized avatar

Miguel Leon miguel-leon

View GitHub Profile
@miguel-leon
miguel-leon / as-path.ts
Last active November 1, 2024 00:32
Path builder for a propety accessor function
interface Accessor<T, U> {
(state: T): U;
}
function property_access(p: string, optionally_chained: boolean): string {
let result = p;
const letter_start = /^[A-Za-z]/.test(p);
if (!letter_start) {
if (!/^\d+$/.test(p)) {
result = `'${ result }'`
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
@media (prefers-color-scheme: dark) {
import { uniques, uniquesByAtt } from './uniques';
describe('uniques function', () => {
it('should filter unique elements by strict equality by default', () => {
const value = [1, 2, 3, 3, 2, 1, 4, 5, 5];
const res = uniques(value);
expect(res).toEqual([1, 2, 3, 4, 5]);
});
@miguel-leon
miguel-leon / deepSet.ts
Created August 14, 2019 16:21
safely deep set nested object properties
function deepSet(value: any, obj: object, ...path: string[]) {
const target = path.slice(0, -1).reduce((p: any, k) => (p[k] = p[k] || {}), obj);
path.slice(-1).forEach(k => target[k] = value);
}
let o;
deepSet(99, o = { a: { x: 88 } }, ...'a.b.c'.split('.'));
console.log(o);
@miguel-leon
miguel-leon / idx.js
Created June 12, 2019 19:16
safe access deep properties
const idx = (o, ...p) => p.reduce((a, i) => a && a[i], o)
export {};
declare global {
interface ArrayConstructor {
range(size, start?): number[];
}
}
Array.range = Array.range || function (size, start = 0) {
return Array.from(Array(size + start).keys()).slice(start);
export function* LazySequence(to, from = 0) {
while (from < to) {
yield from++;
}
}
@miguel-leon
miguel-leon / appendHtml.js
Created November 20, 2018 22:37
parse html and append to element
function appendHtml(element, content) {
const aux = document.createElement(element.tagName);
aux.innerHTML = content;
for (let i = 0; i < aux.children.length; i++) {
element.appendChild(aux.children[i]);
}
}
@miguel-leon
miguel-leon / Action.ts
Last active October 16, 2018 22:34
Auto typed actions to use with stores
export interface Action {
readonly type: string;
}