Skip to content

Instantly share code, notes, and snippets.

View johnathanesanders's full-sized avatar
🎯
Focusing

Johnathan Sanders johnathanesanders

🎯
Focusing
View GitHub Profile
@johnathanesanders
johnathanesanders / generateArrayOfNumbers.ts
Last active February 23, 2022 14:50
Generate an array of numbers, filled from start to length - allowing values to be skipped in a skipValues array
const generateArray = (start: number, length: number, skipValues: number[] = []): number[] => {
const outputArray: number[] = [];
// Length of array should be length + skipValues length since we will be skipping that many iterations of i
length = length + skipValues.length;
// Loop until proper array length is reached
for (let i=start;i<length-1;i++) {
// If the value of i isn't contained in skipValues, we push it to outputArray;
@johnathanesanders
johnathanesanders / calculatePercentage.ts
Last active August 5, 2020 12:39
An asynchronous utility method for calculating percentages to a given precision
/**
*
* Asynchronously calculate a percentage to a given precision.
* @example
* const denominator: number = 435;
* const numberator: number = 37;
* const precision: number = 2;
* const percentage: number = await calculatePercentage(denominator, numerator, precision);
* console.log(`${numerator} is ${percentage} percent of ${denominator} to a precision of ${precision}`);
@johnathanesanders
johnathanesanders / xmlTools.ts
Created September 6, 2019 10:47
An XML to Object converter in Typescript
export class XmlTools {
private readonly closingPattern: RegExp;
private readonly namePattern: RegExp;
private readonly nestPattern: RegExp;
private preparedStatement: object = {};
private rawStatement: string;
constructor() {
this.closingPattern = /<\/([\w]+)[^>]*\>/g;
this.namePattern = /(?![</])([^ >][A-Za-z0-9]{1,}){1}/g;
@johnathanesanders
johnathanesanders / asyncForEach.js
Created September 6, 2019 10:29
An Asynchronous for-each for JavaScript and Typescript
/**
*
* Asyncronously iterate over an array
* @example
* var array = [1,2,3];
* console.log(`This is just the beginning!`);
* await asyncForEach(array, function (element) {
* console.log(`Iterating ${element}`);
* });
* console.log(`Done!`);