Skip to content

Instantly share code, notes, and snippets.

Find Regex:
import(?:["'\s]*([\w*{}\n\r\t, ]+)\s)from\s*?["'](\w.*)["'\s].*
Replace Regex:
import $1 from '$2.ts'
or
@dobernike
dobernike / flattenObj.ts
Last active October 30, 2021 20:55
flatten object typescript and ramda
import { chain, fromPairs, map, toPairs, type } from 'ramda'
export const flattenObj = (obj: Record<string | number, string>) => {
const go = (obj_: Record<string | number, string>): any[] =>
chain(([k, v]: [k: string, v: Record<string | number, string>]) => {
if (type(v) === 'Object' || type(v) === 'Array') {
return map(([k_, v_]: [k_: string, v_: string]) => [`${k}.${k_}`, v_], go(v))
}
return [[k, v]]
}, toPairs(obj_))
@dobernike
dobernike / babel-plugin-to-transform-createSelector.js
Last active September 16, 2021 15:30
Visualizing react-redux performance bottlenecks with babel
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
exports.default = function babelPluginTransformCreateSelector({ types: t }) {
// Add the variable name as the first argument to the createSelector function
function addNameArgument(body, name) {
body.arguments.unshift(t.stringLiteral(name));
array.push(name);
}
// Find and add the variable name as the first argument to the createSelector function in Arrow Function Expression
@dobernike
dobernike / Encoding UTF8 ⇢ base64 and Decoding base64 ⇢ UTF8
Created June 23, 2021 20:02
Encoding UTF8 ⇢ base64 and Decoding base64 ⇢ UTF8
// btoa() only support characters from String.fromCodePoint(0) up to String.fromCodePoint(255).
// For Base64 characters with a code point 256 or higher you need to encode/decode these before and after.
// “convert string to Base64” usually means “encode string as UTF-8 and encode the bytes as Base64”, and that's exactly what btoa(unescape(encodeURIComponent(str))) does.
// Without unescape you don't get a Base64 representation of a UTF-8 encoded string.
// https://stackoverflow.com/questions/30631927/converting-to-base64-in-javascript-without-deprecated-escape-call
// The MDN article originally suggested using unescape and escape to solve the Character Out Of Range exception problem, but they have since been deprecated.
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/unescape
// solutions from https://stackoverflow.com/questions/30106476/using-javascripts-atob-to-decode-base64-doesnt-properly-decode-utf-8-strings
// Encoding UTF8 ⇢ base64
import { DependencyList, useEffect } from 'react'
type Effect<T> = (isMounted: () => boolean) => T | PromiseLike<T>
type Destroy<T> = DependencyList | ((result: T) => void)
function hasDestroy<T>(destroy?: Destroy<T>): destroy is (result: T) => void {
return typeof destroy === 'function'
}
export function useAsyncEffect<T = unknown>(
.main-nav {
position: relative;
width: auto;
// width: 320px;
min-height: 51px;
// margin: 0 auto;
background-color: #826550;
}
body {
margin: 0;
padding: 0;
}
a {
text-decoration: none;
}
img {
a.arrow {
display: inline-block;
margin: 0 10px;
font-size: 0;
}
.arrow::before {
content: "";
display: inline-block;
border: 20px solid #0074d9;