Skip to content

Instantly share code, notes, and snippets.

View fmartins-andre's full-sized avatar

André Martins fmartins-andre

View GitHub Profile
@fmartins-andre
fmartins-andre / how-to-differ-objects-in-js.md
Created July 26, 2023 14:59
How to differ objects in JavaScript

To check what the real type of and variable, run this command:

  Object.prototype.toString.call(myVar)

It'll return something like [object Object]. The second word is the real type of the variable.

Some examples:

@fmartins-andre
fmartins-andre / create-unique-refs-elements-map.md
Created August 20, 2023 23:09
How to create unique refs for elements being rendered via array.map()

You could define a ref, and then inside the object, make multiple subvalues based on the key of the child:

const ref = useRef({})

{array.map((item)=><div key={item.id} ref={ref.current[item.id] ??= { current: null }}>{item.id}</div>)}

Example: https://playcode.io/1096173

Another approach is callback refs:

@fmartins-andre
fmartins-andre / toggle-dark-mode.sh
Created July 18, 2024 00:49
Toggle between Gnome light and dark modes
#!/bin/bash
lightMode="'prefer-light'"
darkMode="'prefer-dark'"
ligthTheme="'Adwaita'"
darkTheme="'Adwaita-dark'"
function toggleMode {
@fmartins-andre
fmartins-andre / git-remove-local-branches.sh
Last active December 20, 2024 13:19
Remove local branches that were merged
#!/bin/bash
echo 'Removing origins...'
git remote prune origin
BRANCHES=`git branch -vv | grep 'origin/.*: gone]' | awk '{print $1}'`
if [[ `echo -e "$BRANCHES" | wc -w` -gt 0 ]]; then
echo 'Removing branches:'
echo $BRANCHES
@fmartins-andre
fmartins-andre / patch-deep.ts
Created February 6, 2025 16:52
patchDeep: A function to deeply patch an object
declare type DeepPartial<T> = {
[K in keyof T]?: DeepPartial<T[K]> | undefined
}
const isObject = (obj: unknown) => obj && typeof obj === 'object'
/**
* Patch source object with other object.
* Only the source object keys are kept!
* Patch undefined values are ignored
@fmartins-andre
fmartins-andre / utility-types.d.ts
Created February 6, 2025 16:58
Useful types to typescript projects
declare type StringfyKeys<T> =
T extends Record<string, unknown> ? `${keyof T}` : never
declare type NonNullableFields<T> = {
[P in keyof T]: NonNullable<T[P]>
}
declare type NonNullableField<T, K extends keyof T> = T &
NonNullableFields<Pick<T, K>>
@fmartins-andre
fmartins-andre / safe-promise.ts
Created April 17, 2025 20:35
Factory function to create tanstack react lazy query hooks
/**
* execute a callback and return an result/error array
*
* @param {callback} function to be executed
*/
export const safePromise = async <TData, TError>(
callback: () => Promise<TData>
): Promise<[false, TError, undefined] | [true, undefined, TData]> => {
try {
const response: TData = await callback()