Skip to content

Instantly share code, notes, and snippets.

View zionsg's full-sized avatar

Ng Kitt Horng, Zion zionsg

View GitHub Profile
@zionsg
zionsg / suggested-order-of-module-members.js
Last active September 16, 2022 08:43
Suggested order of module members in Node.js
// Import modules
const crypto = require('crypto');
/**
* Sample module showing suggested order of declaration for module members in Node.js
*
* Suggested order:
* - Constants before Properties before Methods
* - Public before Private
*
@zionsg
zionsg / SuggestedOrderOfClassMembers.php
Last active September 16, 2022 08:52
Suggested order of class members in PHP
<?php
namespace Vendor\Package;
// Class-based imports
use AnotherVendor\AnotherPackage;
use Vendor\Package\FirstTrait;
use Vendor\Package\ParentPackage;
use InvalidArgumentException;
@zionsg
zionsg / get-iso-date-local.js
Last active March 13, 2023 04:50
Output date in local timezone with ISO 8601 format
/**
* Output date in local timezone with ISO 8601 format
*
* @link Adapted from https://stackoverflow.com/a/17415677
* @param {Date} date
* @returns {string}
*/
function getIsoDateLocal(date) {
let tz = 0 - date.getTimezoneOffset();
let sign = (tz >= 0) ? '+' : '-';
@zionsg
zionsg / async-function-to-return-results-of-another-async-function.js
Last active March 13, 2023 04:51
JavaScript: Async function to return results of another async function
/**
* Add async declaration so that caller knows explictly to use await with it.
*
* @link https://bluepnume.medium.com/learn-about-promises-before-you-start-using-async-await-eb148164a9c8
*/
async function sendRequest() {
let promise = new Promise((resolve, reject) => {
// To make it easier for caller, don't use reject() which requires
// caller to add catch block. Use resolve() for both successful
// responses and errors, e.g.
@zionsg
zionsg / API.md
Created March 9, 2019 02:51 — forked from iros/API.md
Documenting your REST API

Title

<Additional information about your API call. Try to use verbs that match both request type (fetching vs modifying) and plurality (one vs multiple).>

  • URL

    <The URL Structure (path only, no root url)>

  • Method:

TIL in Swift, s in let s = optionalString will be unwrapped when used with if and guard.

var possibleString: String?
possibleString = "Hello"

/* if without unwrap */
let s = possibleString
if (s != nil) {
 print(s) // Optional("Hello")
@zionsg
zionsg / NULL-CONCATENATION.md
Last active February 23, 2023 05:09
Null concatenation in different programming languages

Java

Output: s:*null*

class Test {
    public static void main(String[] args) {
        String s = null;
        System.out.println("s:*" + s + "*");
    }
}