Skip to content

Instantly share code, notes, and snippets.

@ajmas
ajmas / numberformat.js
Created September 2, 2020 17:01
International Number Formatting in JS
const suffixes = {
en: {
one: 'st',
two: 'nd',
few: 'rd',
other: 'th'
},
fr: {
one: 'er',
two: 'e',
@ajmas
ajmas / date-utils.js
Last active August 25, 2020 00:24
JS Date Utilities
/**
* Given an array of days of week, return in how many days the
* next candidate day is. If no candidate is found, then -1 is
* returned, since delta is always positive and non-zero.
*
* The following chart expresses the logic, where 'X' represents
* the `dayOfWeek` and `daysOfWeek` represent the days of next
* possible occurrence.
*
* ```
@ajmas
ajmas / status-check
Last active August 4, 2020 23:32
Simple server health check, that sends failure to a discord or slack channel
#!/bin/sh
## simple server health checker. when the service does not respond it calls a
## Discord or Slack webhook
## adjust values below according to you needs:
server=www.google.com
service=discord
webhook_url="https://discordapp.com/api/webhooks/...."
@ajmas
ajmas / post-checkout
Created July 22, 2020 18:40
Git hook for NodeJS development
#!/bin/sh
## Checks to see if the package.json changed following a checkout or merge and then automatically
## runs `npm install`. If you use yarn or pnpm, then change the line with `npm install` as needed.
##
## add this code to both .git/hooks/post-checkout and .git/hooks/post-merge
file="package.json"
if [[ $(git diff HEAD@{1}..HEAD@{0} -- "${file}" | wc -l) -gt 0 ]]
then
@ajmas
ajmas / asyncArrayFunction.js
Created May 4, 2020 17:13
Async Array functions in NodeJS
/**
* This module provides a set of async friendly functions that
* mirror the non-async versions in array.
*
* They are not necessarily optimised, but they do work when we
* need to deal with async or promise based operations in the
* handler.
*/
/**
@ajmas
ajmas / 502.html
Created April 15, 2020 20:56
502 Error Page for NodeJS hosting
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>502 Bad Gateway</title>
<!-- Browser icons from https://github.com/alrra/browser-logos -->
@ajmas
ajmas / deploy-myapp.sh
Last active April 15, 2020 18:17
Deployment Script
#!/bin/sh
project=myapp
repo_path=~/repositories/${project}
dist_path=~/repositories/${project}
deploy_dir=/usr/local/${project}
export env=prod
cd "${repo_path}"
@ajmas
ajmas / HttpUtils.ts
Last active March 25, 2020 17:07
Various HTTP related utilities in node js, specifically in Typescript
import { Request } from 'express';
function isTrue(value) {
return ['1', 1, 'yes', 'true', 'on'].indexOf(value) > -1;
}
function booleanValue(value) {
if (value) {
return ['1', 1, 'yes', 'true', 'on'].indexOf(value) > -1;
}
@ajmas
ajmas / autobind.js
Created May 29, 2019 04:51
Method to autobind methods in NodeJS
// Based logic on example for discovering properties here: https://stackoverflow.com/questions/31054910/get-functions-methods-of-a-class
// This takes a class instance and returns an object with the objects bound to the instance
// Written, mainly to see if it could be done to avoid having to do individual explcit binds when
// registering with express router
function autoBind(instance) {
let obj = instance;
let props = []
do {
@ajmas
ajmas / localised-calendar.js
Last active July 27, 2019 21:44
Display date in calendar locales
// see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat
// see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#Locale_identification_and_negotiation
function formatField(text, length = 30) {
const textLen = text.length;
const padCount = length - textLen;
let newText = text + ' ';
for (let i=0; i<padCount; i++) {
newText += '.';
}