Skip to content

Instantly share code, notes, and snippets.

View vella-nicholas's full-sized avatar

Nicholas Vella vella-nicholas

View GitHub Profile
@vella-nicholas
vella-nicholas / expiring_session_cache.php
Created May 11, 2022 10:34
Handy code to use session as cache
<?php
if (isset($_SESSION['LAST_ACTIVITY']) && (time() - $_SESSION['LAST_ACTIVITY'] > 1800)) {
// last request was more than 30 minutes ago
session_unset(); // unset $_SESSION variable for the run-time
session_destroy(); // destroy session data in storage
}
$_SESSION['LAST_ACTIVITY'] = time(); // update last activity time stamp
@vella-nicholas
vella-nicholas / numberToWords.js
Created April 28, 2022 06:04
Converts and integer to its string equivalent (EN)
const numberToWords = function(num) {
let result = toHundreds(num % 1000);
const bigNumbers = ["Thousand", "Million", "Billion"];
for (let i = 0; i < 3; ++i) {
num = Math.trunc(num / 1000);
result = num % 1000 !== 0 ? [toHundreds(num % 1000), bigNumbers[i], result].filter(Boolean).join(" ") : result;
}
return result.length === 0 ? "Zero" : result;
}
function toHundreds(num) {
#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""This module's docstring summary line.
This is a multi-line docstring. Paragraphs are separated with blank lines.
Lines conform to 79-column limit.
Module and packages names should be short, lower_case_with_underscores.
Notice that this in not PEP8-cheatsheet.py
@vella-nicholas
vella-nicholas / MockHttpClient.cs
Created April 15, 2019 10:05 — forked from GeorgDangl/MockHttpClient.cs
Mock an Asp.Net Core HttpClient with a custom HttpMessageHandler using Moq
using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Moq;
using Moq.Protected;
namespace Tests
{
public class MockHttpClient
/**
* How to check if a date is valid with vanilla JavaScript
* @link https://gomakethings.com/how-to-check-if-a-date-is-valid-with-vanilla-javascript/
*/
/**
* Get the number of days in any particular month
* @link https://stackoverflow.com/a/1433119/1293256
* @param {integer} m The month (valid: 0-11)
* @param {integer} y The year
@vella-nicholas
vella-nicholas / ObserverPatternC#.cs
Created March 15, 2019 16:51 — forked from kashifmunir/ObserverPatternC#.cs
Observer Pattern Example C#
/// <summary>
/// Observer class
/// </summary>
abstract class Observer
{
public abstract void notify(string eventName, object eventValue);
}
/// <summary>
/// Observable class
@vella-nicholas
vella-nicholas / ballot.sol
Created November 22, 2018 19:08
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.4.25+commit.59dbf8f1.js&optimize=false&gist=
pragma solidity ^0.4.0;
contract Ballot {
struct Voter {
uint weight;
bool voted;
uint8 vote;
address delegate;
}
struct Proposal {
const maxAge = 30; // maximum cache age (min)
/**
* Gets the cached data for the specified request.
* @param url The request URL.
* @return The cached data or null if no cached data exists for this request.
*/
getCacheData(url: string): HttpResponse<any> | null {
const cacheEntry = this.cachedData[url];
@vella-nicholas
vella-nicholas / ballot.sol
Created November 16, 2018 08:33
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.4.25+commit.59dbf8f1.js&optimize=true&gist=
pragma solidity ^0.4.0;
contract Ballot {
struct Voter {
uint weight;
bool voted;
uint8 vote;
address delegate;
}
struct Proposal {