Skip to content

Instantly share code, notes, and snippets.

View otonielguajardo's full-sized avatar

Otoniel Guajardo otonielguajardo

View GitHub Profile
@otonielguajardo
otonielguajardo / php.plugin.zsh
Last active September 1, 2024 19:12
ohmyzh plugin that creates a phpver_prompt_info function that you can use in your theme to display your current version of php
# ohmyzh plugin that creates a phpver_prompt_info function that you can use in your theme to display your current version of php
# It uses two variables to control how that is shown:
# ZSH_THEME_PHPVER_PREFIX: sets the prefix of the PHPVER. Defaults to [
# ZSH_THEME_PHPVER_SUFFIX: sets the suffix of the PHPVER. Defaults to ]
function phpver_prompt_info(){
if [[ -f "composer.json" ]]; then
if php_version=$(/usr/bin/env php --version 2>/dev/null); then
PHPVER="$(echo "$php_version" | awk 'NR==1 {split($2, version_parts, "."); print version_parts[1] "." version_parts[2]}')"
fi
@otonielguajardo
otonielguajardo / expressRouteGroup.ts
Created May 18, 2023 23:12
Group express routes with this middleware
import { Router } from 'express';
const group = ((callback: (router: Router) => void) => {
const router = Router();
callback(router);
return router;
});
export default group;
@otonielguajardo
otonielguajardo / debounce.decorator.ts
Last active May 3, 2023 20:46
Angular RxJS Debounce Decorator
import { Subject } from 'rxjs';
import { debounceTime } from 'rxjs/operators';
export function Debounce(time: number) {
return function (
target: any,
propertyKey: string,
descriptor: PropertyDescriptor
) {
const originalMethod = descriptor.value;
@otonielguajardo
otonielguajardo / gist:68cd533ed5af7a2b92970732abda1e9a
Created November 24, 2021 17:36
Debian 11 Bullseye dropped libappindicator3-1
dpkg-deb -x <app>.deb temp
dpkg-deb --control <app>.deb
mv DEBIAN/ temp/
sed -ie 's/libappindicator3-1/libayatana-appindicator3-1/' temp/DEBIAN/control
dpkg -b temp <app>-fixed.deb
sudo apt install ./<app>-fixed.deb
rm -rf temp
@otonielguajardo
otonielguajardo / mapNested.ts
Created October 13, 2021 00:13
Iterate and map each object in a nested array
export const mapNested = (nodes: any, mapCallback: any) => {
function recursive(nodes: any) {
return nodes.map((node: any) => {
return recursiveSelector(node, recursive, mapCallback)
});
}
return recursive(nodes);
}
const recursiveSelector = (node: any, recursive: any, map: any) => {
@otonielguajardo
otonielguajardo / formatNested.ts
Created October 13, 2021 00:09
Create a nested array of objects with id and parentId pointers
export const formatNested = function (flatArray: any[], id: any, parentId: any): Array<any> {
const collection: Array<any> = [];
flatArray.forEach((item) => (collection[item[id]] = { ...item, children: [] }));
const result: Array<any> = [];
flatArray.forEach((item) => {
if (item[parentId]) {
if (collection[item[parentId]]) {
collection[item[parentId]].children.push(collection[item[id]]);
}
} else {
@otonielguajardo
otonielguajardo / asyncForEach.ts
Created October 13, 2021 00:07
Use async await with a forEach loop
export const asyncForEach = async function (array: Array<any>, callback: any) {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array);
}
};
// await asyncForEach(collection, async (item) => { await doSomething(item); });
@otonielguajardo
otonielguajardo / api.service.ts
Last active August 3, 2021 16:13
Directus Angular service layer
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs';
import { environment } from '@env/environment';
@Injectable({
providedIn: 'root',
})
export class ApiService {
constructor(private http: HttpClient) {}
@otonielguajardo
otonielguajardo / auth.state.ts
Last active August 3, 2021 16:16
Directus Angular register/login NGXS action flow
...
@Action(Login)
login({ patchState, dispatch, getState }: StateContext<AppStateModel>, { payload }: Login) {
let body = {};
let endpoint = '';
if (payload != null) {
patchState({ currentUserLoading: true });
body = payload;
@otonielguajardo
otonielguajardo / rclone-mount-onedrive.sh
Last active July 25, 2021 19:42
Mount rclone OneDrive to ~/OneDrive on startup
#! /bin/bash
# Checks if rclone exists
if ! command -v rclone &> /dev/null
then
echo "install rclone first"
exit;
fi
# Checks if ~/OneDrive directory exists