Skip to content

Instantly share code, notes, and snippets.

import { useEffect, useRef } from 'react'
export const useOutsideClick = (callback: () => void) => {
const ref = useRef<HTMLDivElement>(null)
useEffect(() => {
const handleClickOutside = (event: MouseEvent) => {
if (ref.current && !ref.current.contains(event.target as Node)) {
callback()
}
@SubZane
SubZane / genericSort.tsx
Created December 13, 2022 19:45
genericSort
export interface InterfaceGenericSort<T> {
property: Extract<keyof T, string | number | Date>
isDescending: boolean
}
export function genericSort<T>(objectA: T, objectB: T, sorter: InterfaceGenericSort<T>) {
const result = () => {
if (objectA[sorter.property] > objectB[sorter.property]) {
return 1
} else if (objectA[sorter.property] < objectB[sorter.property]) {
@SubZane
SubZane / genericFilterSearch.tsx
Created December 13, 2022 19:43
genericFilterSearch
function genericFilterSearch<T>(object: T, properties: Array<keyof T>, query: string): boolean {
if (query === '') {
return true
}
return properties.some((property) => {
const value = object[property]
if (typeof value === 'string' || typeof value === 'number') {
return value.toString().toLowerCase().includes(query.toLowerCase())
}
@SubZane
SubZane / react.validate-email.tsx
Created March 17, 2021 09:49
Validates Email address against regular expression and a list of "allowed" email domains.
export function isValidEmail(email: string, acceptedDomains?: string[]) {
const emailformat = /^(([^<>()[\]\.,;:\s@\"]+(\.[^<>()[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i
let isValid = false
if (email.match(emailformat)) {
isValid = true
if (acceptedDomains !== undefined) {
isValid = acceptedDomains.some(function(domain) {
return email.endsWith(domain)
})
@SubZane
SubZane / snippets.json
Created March 24, 2020 07:41
Visual Code Snippets for the one armed coder
{
"Curly Braces": {
"prefix": "cb",
"body": [
"{$1}"
],
"description": "curly braces"
},
"Brackets": {
"prefix": "bb",
@SubZane
SubZane / findNode.js
Last active March 3, 2020 12:16
find key in nested json object
function findNode(object, id) {
const foundObj = object.filter(p => p.id === id)
if (foundObj.length > 0) {
return {...foundObj};
} else {
const oc = object.filter(p => p.hasOwnProperty('subnodes'))
if (oc && typeof oc === 'object' && oc !== null) {
for (var i=0; i < oc.length; i++) {
const retObj = findNode(oc[i].subnodes, id)
if (retObj != null) {
class AddSetViewController: UIViewController {
@IBOutlet weak var RepsPickerView: UIPickerView!
var rotationAngle: CGFloat!
override func viewDidLoad() {
super.viewDidLoad()
rotationAngle = -90 * (.pi / 180)
let y = RepsPickerView.frame.origin.y
for i in *.png; do sips -s format jpeg -s formatOptions 100 "${i}" --out "${i%png}jpg"; done
@SubZane
SubZane / config.json
Last active August 13, 2017 08:46
Homebridge temp config
{
"bridge": {
"name": "Homebridge",
"username": "CC:22:3D:E3:CE:30",
"port": 51826,
"pin": "031-45-154"
},
"platforms": [{
"platform": "Telldus",
@SubZane
SubZane / replaceinfile.txt
Created December 22, 2016 23:25
Replace string in file
find . -name '*.txt' -print0 | xargs -0 sed -i "" "s/this/that/g"