Skip to content

Instantly share code, notes, and snippets.

@chrisribe
chrisribe / -design-patterns-cheat-sheet.md
Last active June 23, 2023 18:11
A cheat sheet outlining some popular design patterns, their pros and cons, and guidance on when to use them

Here's a cheat sheet outlining some popular design patterns, their pros and cons, and guidance on when to use them:

  1. Singleton Pattern:

    • Pros: Ensures only one instance of a class exists, provides a global point of access.
    • Cons: Can introduce tight coupling and make unit testing difficult.
    • Use when: You need to limit the number of instances of a class, such as for managing shared resources or global configurations.
  2. Factory Pattern:

    • Pros: Provides a centralized place to create objects, encapsulates object creation logic.
  • Cons: Can become complex as the number of product types increases.
//Events to break on.
var aEvents = [
'click',
'touchstart'
];
function breakNow(e) {
console.log(`break now 4 event: ${e.type}`, e);
console.log("STACK is:", stackTrace());
@chrisribe
chrisribe / arrayContains.js
Last active August 12, 2022 19:52
Array contains partial and exact match
var allowedURLs = [
"https://www.google.com",
"https://www.youtube.com/watch?v=mp5WJwT7HW0",
"https://www.youtube.com"
];
function arrayContains(targetArray, stringTofind, exactMatch) {
var matches = targetArray.filter(function (aItem) {
if (exactMatch)
return (stringTofind === aItem);
@chrisribe
chrisribe / SwipeListener.js
Last active January 1, 2022 15:55
Simple Swipe Event Listener
// Swipe listener for mobile touch events
// run in chrome console in mobile mode and swipe at the current page body
function SwipeListener(targetId, fn){
this.startX = 0;
this.startY = 0;
this.swipeEventCb = fn;
this.gestureZone = document.querySelector(targetId);
this.start();
}
@chrisribe
chrisribe / php-tools.php
Last active January 28, 2020 19:58
PHP kill / terminate process by exe name
/**
* Kill windows process if found and older that given time (defaults to now)
* Ref: https://stackoverflow.com/questions/14385122/how-to-kill-a-windows-process-running-longer-than-30-minutes
*/
function killProcessByName($processName, $ifOlderThan = "now"){
//Build datetime formated for "wmic process CreationDate"
//Format: year-month-day-hour-minute-second-microsecond-timezone (in minutes!!!)
$dt = new DateTime($ifOlderThan);
$t = $dt->format('YmdHis.u') . ($dt->format('Z') / 60);