Blog Spot!


The Decorator Pattern - JavaScript

diagram

Really simple and bare-bone example:

function MacBook() {
  this.cost = function () {
    return 997
  };
  this.screenSize = function () {
    return 11.6
  };

}

// Decorator 1
function Memory(macbook) {
  var v = macbook.cost();

  macbook.cost = function() {
    return v + 75;
  };
}

// Decorator 2
function Engraving(macbook) {
  var v = macbook.cost();

  macbook.cost = function() {
    return  v + 200;
  };

}

// Decorator 3
function Insurance(macbook) {
  var v = macbook.cost();

  macbook.cost = function() {
    return  v + 250;
  };

}

var mb = new MacBook();
Memory(mb);
Engraving(mb);
Insurance(mb);

// Outputs: 1522
console.log(mb.cost());

// Outputs: 11.6
console.log(mb.screenSize());

My Pastebin example...

Added on 23.Feb.2019
Tags: patterns oop js decorator

Can I zip an entire folder using gzip?

stackexchange

Unlike zip, gzip functions as a compression algorithm only.

Because of various reasons some of which hearken back to the era of tape drives, Unix uses a program named tar to archive data, which can then be compressed with a compression program like gzip, bzip2, 7zip, etc.

In order to "zip" a directory, the correct command would be:

tar -zcvf archive.tar.gz directory/ 

This will tell tar to:

  • compress it using the z (gzip) algorithm
  • c (create) an archive from the files in directory (tar is recursive by default)
  • v (verbosely) list (on /dev/stderr so it doesn't affect piped commands) all the files it adds to the archive.
  • and store the output as a f (file) named archive.tar.gz

The tar command offers gzip support (via the -z flag) purely for your convenience. The gzip command/lib is completely separate. The command above is effectively the same as:

tar -cv directory | gzip > archive.tar.gz

To decompress and unpack the archive into the current directory you would use:

tar -zxvf archive.tar.gz

That command is effectively the same as:

gunzip < archive.tar.gz | tar -xv

UPDATE: 2020-02-04

Using the -a prefix will auto-detect the compression type from the extension type:

  • ex. tar -acf [file.gz|bz...] [directory] to compress -c
  • ex. tar -axf [file.gz|bz...] to extract -x

Added on 15.Feb.2019
Tags: linux tar gzip archive terminal

Apache2 301 redirect

301 redirect .htaccess in apache2

Redirect 301 /path/to/the-url /new-path/to/the-new-url

Added on 29.Jan.2019
Tags: apache redirect 301 moved-permanently

How can I view live MySQL queries?

stackoverflow

$ mysqladmin -u my_pass -p -i 1 processlist

This will print the current queries on your screen every second.

  • -u The mysql user you want to execute the command as
  • -p Prompt for your password (so you don't have to save it in a file or have the command appear in your command history)
  • -i The interval in seconds.

Use the --verbose flag to show the full process list, displaying the entire query for each process.

There is a possible downside: fast queries might not show up if they run between the interval that you set up. IE: My interval is set at one second and if there is a query that takes .02 seconds to run and is ran between intervals, you won't see it.

Use this option preferably when you quickly want to check on running queries without having to set up a listener or anything else.

Added on 26.Jan.2019
Tags: mysql sql live queries view process

Simple PHP script showing how to send an Android push notification.

gist-5675017

<?php
// API access key from Google API's Console
define( 'API_ACCESS_KEY', 'YOUR-API-ACCESS-KEY-GOES-HERE' );
$registrationIds = array( $_GET['id'] );
// prep the bundle
$msg = array
(
    'message'   => 'here is a message. message',
    'title'     => 'This is a title. title',
    'subtitle'  => 'This is a subtitle. subtitle',
    'tickerText'    => 'Ticker text here...Ticker text here...Ticker text here',
    'vibrate'   => 1,
    'sound'     => 1,
    'largeIcon' => 'large_icon',
    'smallIcon' => 'small_icon'
);
$fields = array
(
    'registration_ids'  => $registrationIds,
    'data'          => $msg
);

$headers = array
(
    'Authorization: key=' . API_ACCESS_KEY,
    'Content-Type: application/json'
);

$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send' );
curl_setopt( $ch,CURLOPT_POST, true );
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
$result = curl_exec($ch );
curl_close( $ch );
echo $result;

Added on 23.Jan.2019
Tags: php push notification send google fcm

Search


PHP Libraries


Carbon lib / docs
Idiorm lib / docs
Image Workshop lib / docs
lorenzos/Minixed lib / docs
Parsedown lib / docs
PHP Paginator lib / docs
PHP Redis lib / docs
QrCode lib / docs
Requests lib / docs
Slim lib / docs
Spyc lib / docs
TWIG lib / docs
Upload lib / docs
Validation lib / docs
Zebra Image lib / docs

JS Libraries


AJV lib / docs
BackboneJS lib / docs
Bootstrap Notify lib / docs
C3.js lib / docs
ChartJS lib / docs
FastMD5 lib / docs
HighlightJS lib / docs
jQuery-Storage lib / docs
JS-Cookie lib / docs
Leaflet JS lib / docs
LowDB lib / docs
Marked lib / docs
NeedlyJS lib / docs
ParcelJS lib / docs
RequireJS lib / docs
Swig lib / docs
Toastr lib / docs
Underscore lib / docs
ValidateJS lib / docs
top