Skip to content

Instantly share code, notes, and snippets.

@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 / rtlsdr-osx.md
Last active September 22, 2023 07:30
Instructions on how to build RTL-SDR using MacPorts based dependencies
@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 += '.';
}
@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 / pre-push
Last active April 29, 2019 21:06
Git 'pre-push' script to prevent pushing to a given branch on a remote
#!/bin/bash
# Simple 'pre-push' script to prevent an accidental push to a
# specific branch on a specific remote. Mainly needed when a
# remote branch is not protected.
#
# Place this in the .git/hooks folder of your project and then
# ensure you make it executable. Modify according to your needs.
restricted_branch=master
@ajmas
ajmas / no-content-type.js
Created January 2, 2019 05:03
Allow express body-parser to deal with missing content-type headers
// I was dealing with such an issue, where there was no content-type
// curl --upload-file '/Users/myuser/Movies/VID-20171025.mp4' -H http://localhost:3000/api/upload/path/to/myfile.xyz
const bodyParser = require('body-parser');
function req(req, res, next) {
const fileName = req.params.name;
if (Buffer.isBuffer(req.body)) {
fs.writeFileAsync("/tmp/" + fileName, req.body)
@ajmas
ajmas / sequelieze-field-encrypt.js
Last active February 20, 2022 11:07
Code to encrypt a Sequelize fields
// Code to encrypt data in sequelize fields
// We are using ascii85 as a way save on storage. Also, note that
// the space delimiter we are using is a bit of an abuse since in
// normal cases ascii85 will skip over it, but since we are using
// after encoding and before encoding, it shouldn't be an issue.
//
// Fields are broken down to facilitate unit testing.
//
// based on code here: http://vancelucas.com/blog/stronger-encryption-and-decryption-in-node-js/
@ajmas
ajmas / CircularProgress.vue
Created February 8, 2018 05:40
Vue Component for drawing a circular progress bar
<!--
Vue Component for drawing a circular progress bar
-->
<template>
<div class="circular-progress" ref="circularProgress">
<svg :height="height" :width="width">
<path class="channel" :d="channel"/>
<path class="progress" :d="progress" />
<text x="55%" y="55%" :font-size="fontSize">
{{percent}}%
@ajmas
ajmas / vt100-to-html.js
Last active January 5, 2018 04:38
Quick attempt to convert ansi/vt100 escape sequence to HTML
let text = "\u001b[H\u001b(B\u001b[mtop - 02:11:23 up 36 days, 18:31, 0 users, load average: 4.25, 5.34, 5.98\u001b(B\u001b[m\u001b[39;49m\u001b(B\u001b[m\u001b[39;49m\u001b[K\r\n\r\n%Cpu(s):\u001b(B\u001b[m\u001b[39;49m\u001b[1m 21.7 \u001b(B\u001b[m\u001b[39;49;35;43mus,\u001b(B\u001b[m\u001b[39;49m\u001b[1m 8.7 \u001b(B\u001b[m\u001b[39;49msy,\u001b(B\u001b[m\u001b[39;49m\u001b[1m 0.0 \u001b(B\u001b[m\u001b[39;49mni,\u001b(B\u001b[m\u001b[39;49m\u001b[1m 68.3 \u001b(B\u001b[m\u001b[39;49mid,\u001b(B\u001b[m\u001b[39;49m\u001b[1m 0.0 \u001b(B\u001b[m\u001b[39;49mwa,\u001b(B\u001b[m\u001b[39;49m\u001b[1m 0.0 \u001b(B\u001b[m\u001b[39;49mhi,\u001b(B\u001b[m\u001b[39;49m\u001b[1m 1.1 \u001b(B\u001b[m\u001b[39;49msi,\u001b(B\u001b[m\u001b[39;49m\u001b[1m 0.3 \u001b(B\u001b[m\u001b[39;49mst\u001b(B\u001b[m\u001b[39;49m\u001b(B\u001b[m\u001b[39;49m\u001b[K\r\nKiB Mem :\u001b(B\u001b[m\u001b[39;49m\u001b[1m 62916344 \u001b(B\u001b[m\u001b[39;49mtotal,\u001b(B\u001b[m\u001b[39;49m\u001b[1m 3671244 \u001b(B
@ajmas
ajmas / Demo.vue
Last active December 26, 2017 00:37
dabeng/OrgChart adapted to work with Vue Single File Component
<template>
<div>
<div id="orgchart" class="orgchart">
<node :model="nodeData" v-on:node-click="nodeClick"></node>
</div>
<p><button id="add">Add</button><button id="remove">Remove</button></p>
<p>(You can double click on an item to turn it into a folder.)</p>
<ul id="demo">