Skip to content

Instantly share code, notes, and snippets.

@sam-ngu
sam-ngu / array_merge_preserve.php
Last active October 28, 2019 08:10
PHP: array merge while preserving numeric keys
<?php
/*
Eg. $array1:
array:1 [
3 => array:1 [
"subscription_id" => 27
]
]
@sam-ngu
sam-ngu / arrow_function_comparison1.js
Created November 11, 2019 00:03
JS arrow function demonstration
let clock = {
timerIntervalId: null,
timer: 60, // 60 sec
startTimer: function(){
//
function decrement(){
if(this.timer < 0)
this.stopTimer()
else
this.timer -= 1
@sam-ngu
sam-ngu / webpack.mix.js
Last active November 19, 2019 04:04
Laravel Mix + Vuejs Hot reload config
mix.options({
hmrOptions: {
host: 'laravel.dev.local', // site's host name
port: 8080,
}
});
// // fix css files 404 issue
mix.webpackConfig({
// add any webpack dev server config here
@sam-ngu
sam-ngu / promisedemo.js
Last active April 3, 2020 02:57
Demonstration on how Promises work in Javascript
// creating a new promise by instantiating the Promise Class
let somePromise = new Promise(function(resolve, reject){
// the resolve and reject arguments are callback functions to be called.
// resolve: to be called when the promise is successful. We can pass data to it for the subsequent the 'then' method
// reject: to be called when the promise is unsuccessful. Again we can pass data to it for the 'catch' method
try{
// to mimick asynchoronous behaviour,
// trigger the timeout callback in 1 sec
setTimeout(function(){
console.log('hey there 1 sec later.')
@sam-ngu
sam-ngu / asyncpromise.js
Last active April 3, 2020 09:57
Async Demo
// adding the 'async' keyword in front of a function will make it asynchronous.
// JS will automatically turn the output into a Promise
async function sayHello(){
return 'Hello"
}
sayHello().then((response) => {
console.log(response) // expected output: 'Hello'
})
@sam-ngu
sam-ngu / parse_url_parameter_name.php
Created August 24, 2020 13:02
Parsing URL parameter name using Symfony Route.
<?php
$uri = 'https://test.com/api/users/{user}?page=4&size=5';
$route = new SymfonyRoute(
preg_replace('/\{(\w+?)\?\}/', '{$1}', $uri), [], [], ['utf8' => true, 'action' => []],
'', [], []
);
$compiled = $route->compile();
@sam-ngu
sam-ngu / docker-compose.yml
Last active October 10, 2020 05:26
Docker compose example
# we are using the v3.7 syntax to write this docker compose file
version: "3.7"
# define a list
networks:
backend:
# the services section tells docker compose how to run the docker images
# a service can have multiple containers, all based on the same image
services:
@sam-ngu
sam-ngu / composer-install.sh
Created December 7, 2020 01:59
Install composer globally
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === '756890a4488ce9024fc62c56153228907f1545c228516cbf63f885e036d37e9a59d27d63f46af1d4d07ee0f76181c7d3') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
sudo php composer-setup.php --install-dir=/usr/bin --filename=composer
php -r "unlink('composer-setup.php');"
@sam-ngu
sam-ngu / laravel_7_mail_markdown_component.blade.php
Last active January 19, 2021 21:48
A quick reference to all Laravel 7 mail markdown components. Source code to https://sam-ngu.medium.com/laravel-7-mail-markdown-components-a1afc18f6efd
@component('mail::message')
# Introduction
The body of your message.
@component('mail::button', ['url' => ''])
Button Text
@endcomponent
@component('mail::panel')
@sam-ngu
sam-ngu / formdata.js
Last active April 14, 2021 00:16
Form Data demo
// payload to send in the API request
const payload = {
email: "example@example.com",
recipients: [
'test1@example.com',
'test2@example.com',
],
attachment: fileObject,
}