Skip to content

Instantly share code, notes, and snippets.

View psenger's full-sized avatar
:octocat:
Makin Bacon

Philip A Senger psenger

:octocat:
Makin Bacon
View GitHub Profile
@psenger
psenger / box.js
Last active September 20, 2022 01:25
[Functional Programming] #JavaScript #FunctionalProgramming
/**
* box - turns a value into an array, is nill returns [].
* @param {*} value - some value.
* @return {*[]}
*/
const box = value => {
// already boxed...
if ( Array.isArray(value) ) {
return value;
}
@psenger
psenger / wrap_childred.jsx
Created March 23, 2021 23:02
[How to Wrap React Children] #ReactJs
const OpaqueWrapper = ({ children }) => (
children && (
<Wrapper>
{React.Children.map(children, child => (
React.cloneElement(child, {style: {...child.props.style, opacity: 0.5}})
))}
</Wrapper>
)
);
@psenger
psenger / How_To_Undo_Mistakes_In_Git.md
Last active March 25, 2021 02:17
[How to Undo Mistakes in Git] #git

How to Undo Mistakes in Git - CLI / Git-Tower

Based on the youtube video How to Undo Mistakes in Git

As a rule of thumb, each commit should have only ONE topic.

How to View Differences

$ git diff 
@psenger
psenger / README.md
Created April 5, 2021 06:09
[How to Add an existing project to a new Git repo] #git
  1. Create your project online
  2. Change directory cd into the project folder locally, and initialize it git init
  3. Add all the files git add *
  4. Commit all the files to the local git git commit -m "My initial commit"
  5. Add the remote git remote add origin git@bitbucket.org:[user]/[my-repo].git
  6. Push the files git push -u origin master
@psenger
psenger / README.md
Last active April 5, 2021 23:13
[Disk Space Calucation Tricks] #MacOS #bash

How to find the total size of certain files within a directory

find ./photos/john_doe -type f -name '*.jpg' -exec du -ch {} + | grep total$

Using find with time resrictions

@psenger
psenger / runme.bash
Last active May 13, 2021 22:15
[Read a .env file into the current shell] #Unix
$ # --------------
$ # How to read a .env file into the current shell, as environment variables.
$ # --------------
$ export $(egrep -v '^#' .env | xargs)
@psenger
psenger / index.js
Last active April 20, 2021 23:53
[Async / Await in a Express Controller - Old School ] #JavaScript #Promise #AsyncAwait
/**
* if you have to support older versions of Express, here is an
* example of working with Async / Await in the controller ( old school ).
**/
const actionController = function actionController(req, res, next) {
// build async IIFE, returning it.
return (async function asyncActionController() {
try {
// await for a promise, etc.
return res.status(201).json({msg:'hello'});
@psenger
psenger / index.js
Last active May 25, 2025 02:00
[Design Pattern: Try-Catch In the Middle of a Chain of Promises] #JavaScript #Promise #try-catch
const Promise = require('bluebird');
// Just for the record, you should avoid this pattern.
// ---------------------------------------------------
// 1.) it is hard to understand
// 2.) It uses nested promises, which is an anti pattern.
//
(async () => {
return Promise.resolve()
.then(()=>{
@psenger
psenger / index.js
Last active December 26, 2024 01:37
[Self Executing NodeJS Script - Template] #bash #shell #NodeJS #Template
#!/usr/bin/env node
/**
* get the CLI arguments
**/
const [ , , ...args] = process.argv;
console.log(`args=${args}`);
require( 'fs' ).writeFileSync( `${path.join(__dirname,path.basename(__filename,'.js'))}.pid`, process.pid.toString(), { encoding: 'utf-8' } )
@psenger
psenger / cleanString.js
Last active August 3, 2021 01:37
[String isEmpty IsBlank isWhiteSpace null or undefined] #JavaScript
/**
* Clean a String of invalid characters - remove all ASCII chars outside of 0-127
**/
const cleanString = (input) => {
let output = "";
for (let i=0; i<input.length; i++) {
if (input.charCodeAt(i) <= 127) {
output += input.charAt(i);
}
}