Skip to content

Instantly share code, notes, and snippets.

View ChukwuEmekaAjah's full-sized avatar
💭
Building

Chukwuemeka Ajah ChukwuEmekaAjah

💭
Building
View GitHub Profile
@ChukwuEmekaAjah
ChukwuEmekaAjah / init.js
Created October 31, 2018 12:03
Initialization and use of vhost expressjs middleware library.
const express = require('express');
const app = express();
const vhost = require('vhost');
// after registering all middleware, then you register your hostname as
app.use(vhost('admin.localhost',app));
@ChukwuEmekaAjah
ChukwuEmekaAjah / global_variables.js
Created October 31, 2018 12:08
Global variables in vhost index.js file.
var asteriskRegExp = /\*/g
var asteriskReplace = '([^\.]+)'
var endAnchoredRegExp = /(?:^|[^\\])(?:\\\\)*\$$/
var escapeRegExp = /([.+?^=!:${}()|\[\]\/\\])/g
var escapeReplace = '\\$1'
@ChukwuEmekaAjah
ChukwuEmekaAjah / is_reg_exp.js
Created October 31, 2018 12:26
Regular expression tester.
function isregexp(val) {
return Object.prototype.toString.call(val) === '[object RegExp]'
}
@ChukwuEmekaAjah
ChukwuEmekaAjah / vhost_of.js
Created October 31, 2018 12:35
Creating a vhost object.
function vhostof(req, regexp) {
var host = req.headers.host
var hostname = hostnameof(req)
if (!hostname) {
return
}
var match = regexp.exec(hostname)
@ChukwuEmekaAjah
ChukwuEmekaAjah / hostnameof.js
Created October 31, 2018 13:14
Extracting the hostname from a host string in a NodeJS request header.
function hostnameof(req) {
var host = req.headers.host
if (!host) {
return
}
var offset = host[0] === '['
? host.indexOf(']') + 1
: 0
@ChukwuEmekaAjah
ChukwuEmekaAjah / hostregexp.js
Created October 31, 2018 14:12
Creating a dynamic code regular expression object to match hosts.
function hostregexp(val) {
var source = !isregexp(val)
? String(val).replace(escapeRegExp, escapeReplace).replace(asteriskRegExp, asteriskReplace)
: val.source
// force leading anchor matching
if (source[0] !== '^') {
source = '^' + source
}
@ChukwuEmekaAjah
ChukwuEmekaAjah / vhost.js
Created October 31, 2018 15:05
vhost main function
/**
* Create a vhost middleware.
*
* @param {string|RegExp} hostname
* @param {function} handle
* @return {Function}
* @public
*/
function vhost(hostname, handle) {
@ChukwuEmekaAjah
ChukwuEmekaAjah / commonRegexpPatterns.js
Last active July 29, 2020 21:08
A list of common regular expression patterns implemented in every day programming work.
let dayRegex = /^(([0-2]?[1-9])|([3][0-1]))$/; //01-31
let monthRegex = /^(([0]?[1-9])|([1][0-2]))$/; //01-12
let emailRegex = /^\S*?@\S*?\.[a-z]{2,3}^/;
let lessThanTen = /^[0]?[1-9]?$/ // 1-9, 01-09
let percentRegex = /^([1-9][0-9]?)$|(^100$)/ // 1-100
@ChukwuEmekaAjah
ChukwuEmekaAjah / calendar.js
Created August 9, 2020 00:42
A utility function that prints out an ordered calendar based on the passed in date or present day date. It was inspired by the Facebook page publishing tools date picker functionality.
function isLeapYear(year){
return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
}
function calendar(inputDate){
const presentDate = inputDate ? (new Date(inputDate)) : (new Date());
console.log("date is ", presentDate)
if(Boolean(presentDate) === false){
@ChukwuEmekaAjah
ChukwuEmekaAjah / linearDataDuplicateChecker.js
Created August 23, 2020 23:07
Linear time data duplicate check temporary storage comparison between JavaScript arrays and objects. It appears using arrays as temporary storage would give you 2x faster performance when compared to using Object literals.
let numbers = [];
for(let i = 0; i < 100000; i++){
numbers.push(Math.round(Math.random()*100000))
}
function hashDuplicate(value){
console.time("hash duplicate")
let hashValues = {}
for(let i = 0; i < numbers.length; i++){