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 / ignored_files.ts
Created July 16, 2023 22:23
A basic implementation of how files to be ignored in 'ignore' files are found and ignored
import * as path from "path"
import * as fs from "fs"
// find all files that match the corresponding splitted directory component
// find all files and folders that match the regular expression of the corresponding path
// filter out all unmatched files/folders from the list based on splitted path
function getIgnoredFiles(ignorePath: string) :string[]{
const allFiles = walk(process.cwd(), [], {})
const regex = buildRegex(ignorePath)
@ChukwuEmekaAjah
ChukwuEmekaAjah / timestampdb.js
Created May 30, 2023 22:55
This is an implementation of timestamp key-value store that allows historical record of values inserted for a key in a db. This implementation uses a sorted set of timestamps to make sure that the db meets its constraints.
class TimeStampDB{
constructor (){
this.db = {}
}
set(key, timestamp, value){
this.db[key] = this.db[key] || {}
this.db[key][timestamp] = value
this.db[key]['timestamps'] = this.db[key]['timestamps'] || []
// find insert position for sorted set of timestamps array
@ChukwuEmekaAjah
ChukwuEmekaAjah / ago.js
Created August 28, 2022 20:01
Return how long ago an event occurred with respect to current time.
const pluralize = function (value){
return Math.floor(value) > 1 ? 's' : '';
}
function ago(date){
const now = Math.floor(Date.now()/1000)
const time = new Date(date)
if(isNaN(time.getTime())){
throw new Error(`Passed in date ${date} is not a valid date`)
}
let timeNow = new Date();
let currentHour = timeNow.getHours();
let count = 1;
let idk = null
function greet(){
if (count == 10){
clearInterval(idk);
}
if ((5 <= currentHour) && (currentHour <= 10)){
@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++){
@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 / 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 / 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 / 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 / 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