Skip to content

Instantly share code, notes, and snippets.

View martinhj's full-sized avatar
🏴‍☠️

Martin Holt Juliussen martinhj

🏴‍☠️
View GitHub Profile
@martinhj
martinhj / Button.test.js
Created November 29, 2019 12:35
Jest / @testing-library/react-native test examples
import React from 'react';
import Button from '.';
import { AlertButton, SubmitButton } from '.';
import { fireEvent, render, wait } from '@testing-library/react-native';
import Icon from '../Icon';
const caption = 'Test button';
const color = '#f00f0f';
const accessibilityLabel = 'This is a test button';
@martinhj
martinhj / github-clean-layout-for-code-print.js
Last active December 3, 2019 08:18
Clean up github code for printing (including main view for readme print)
(() => {
const flattenArray = (acc, cur) => acc = [...acc, ...cur]
const queryElement = selector => document.querySelector(selector)
const getAllChildren = elem => elem.children
const removeElem = elem => elem && elem.parentElement.removeChild(elem);
const removeClassFromElem = (elem, styleclass) => elem && elem.classList.remove(styleclass);
const removeClassesFromElem = ([elem, styleclasses]) => styleclasses.forEach(styleclass => removeClassFromElem(elem, styleclass));
const findAllChildrensExcept = (elemToRemoveExcept) => elemToRemoveExcept
.map(([elem, keepChildrenQueries]) => [queryElement(elem), keepChildrenQueries.map(queryElement)])
@martinhj
martinhj / overwrite_file_before_deletion.sh
Created October 16, 2019 11:44
safe deletion: overwrite file before deletion
dd if=/dev/zero of=filewithsecret bs=1 count=12342 conv=notrunc
# ^^^^^- filesize (ls -l filewithsecret)
# ^^^^^- notrunc: do not truncate the file if count is < filesize (do not leave secret bits and bytes hanging around on your disk....)
# Could combine this with a for loop to do it a couple of times if feeling extra
@martinhj
martinhj / mount_disk_images.sh
Last active October 16, 2019 11:44
mount disk images: scan image for partition and set up loop independent devices for each
# Linux (when in need for read/write on ext*
losetup -Pf --show imageFilePath.img
mount /dev/loop0p1 /mnt/
# Mac
hdiutil attach -nomount imageFilePath.img
sudo diskutil mountDisk /dev/disk2
# don't need this anymore... sudo mount -t msdos /dev/disk2s1 /Volumes/somethingsomething
@martinhj
martinhj / greed.md
Last active November 18, 2020 08:53
Regex greed quantifiers
Greedy quantifier Lazy quantifier Description
* *? Star Quantifier: 0 or more
+ +? Plus Quantifier: 1 or more
? ?? Optional Quantifier: 0 or 1
{n} {n}? Quantifier: exactly n
{n,} {n,}? Quantifier: n or more
{n,m} {n,m}? Quantifier: between n and m
@martinhj
martinhj / shell-special-parameters.zsh
Last active June 3, 2019 11:30
Shell special parameters
# from https://stackoverflow.com/questions/5163144/what-are-the-special-dollar-sign-shell-variables
# Aslo see https://www.gnu.org/software/bash/manual/html_node/Special-Parameters.html
$1# (, $2, $3 , ...) are the positional parameters.
"$@" # is an array-like construct of all positional parameters, {$1, $2, $3 ...}.
"$*" # is the IFS expansion of all positional parameters, $1 $2 $3 ....
$# # is the number of positional parameters.
$- # current options set for the shell.
$$ # pid of the current shell (not subshell).
$_ # most recent parameter (or the abs path of the command to start the current shell immediately after startup).
@martinhj
martinhj / remove-quarantine.sh
Created May 29, 2019 11:05
Remove file quarantine in macos x
xattr -d com.apple.quarantine file-downloaded-with-Mail-app-fx.txt
@martinhj
martinhj / generateSearchQueryWithTemplateLiteralsWithFallback.js
Last active May 16, 2019 12:43
Generate Search Query With Tagged Template Literals
const linkPath = '/workingMode/distracted/aBit'
const isDarkTheme = true
const generateSearchQuery = (strings, linkExp, searchArgument = 'gaveUp') => {
let searchQuery = '?search='
const [,searchQueryArg ,] = strings
const themeString = isDarkTheme ? 'dark' : 'light'
if (searchQueryArg !== '') searchQuery = searchQueryArg
return `/en-us${linkExp}${searchQuery}${searchArgument}?themeType=${themeString}`
@martinhj
martinhj / throwOnMissingArgument.js
Created April 29, 2019 09:24
Elegant throw error on missing argument
// From https://davidwalsh.name/javascript-tricks
/**
Those three dots made the task so much easier!
Require Function Parameters
Being able to set default values for function arguments was an awesome addition to JavaScript, but check out this trick for requiring values be passed for a given argument:
*/
const isRequired = () => { throw new Error('param is required'); };
@martinhj
martinhj / get-property-from-json.sh
Last active April 11, 2019 13:55
Get object property from json in shell
jq .repository package.json