A Collection of helpful tools for Typescript projects.
NPM:
npm install @lukebechtel/lab-ts-utils
YARN:
yarn add @lukebechtel/lab-ts-utils
This function detects the minimum indentation level of each line and removes it. It reads over all lines, identifies the minimum indentation level, and strips all indentation before this level.
import { fixIndent } from '@lukebechtel/lab-ts-utils';
const str = `
Hello,
World!
Have a good day.`;
console.log(fixIndent(str));
// Expected output:
// Hello,
// World!
// Have a good day.
Prepends "Bearer" to a JWT token if it doesn't already start with "Bearer".
import { jwtBearerify } from '@lukebechtel/lab-ts-utils';
const token = 'abc.xyz.123';
console.log(jwtBearerify(token));
// Expected output:
// Bearer abc.xyz.123
Uses lodash mergeWith
to produce a deeply merged object from provided objects.
import { mergeDeep } from '@lukebechtel/lab-ts-utils';
const obj1 = { a: [1], b: 2 };
const obj2 = { a: [3], c: 4 };
console.log(mergeDeep({toMerge: [obj1, obj2]}));
// Expected output:
// { a: [1, 3], b: 2, c: 4 }
This function checks if an item has some value. It narrows down the type from TValue | null | undefined to TValue, allowing TypeScript to know that the value is not null or undefined.
import { notEmpty } from '@lukebechtel/lab-ts-utils';
const value1 = undefined;
const value2 = 'Hello, World!';
console.log(notEmpty(value1)); // Expected output: false
console.log(notEmpty(value2)); // Expected output: true
This function adds a given prefix to the start of each line in a given string.
import { prefixAllLines } from '@lukebechtel/lab-ts-utils';
const str = `Hello,
World!
Have a good day.`;
const prefix = '>> ';
console.log(prefixAllLines(prefix, str));
// Expected output:
// >> Hello,
// >> World!
// >> Have a good day.
You can replace '@lukebechtel/lab-ts-utils'
with the actual name of your library. Also, replace the dummy variables and strings with actual examples that you wish to use.
This function creates a simple logger with customizable prefixing and logging functions. You may use rawString
, simpleString
, or a function
as a prefix to your log statements. Logging functions are also customizable and will default to appropriate console functions if no alternatives are provided.
let logger = createSimpleLogger({}); // creates a logger with no prefix and default console logging
logger.log("log this"); // console output: "log this"
let logger = createSimpleLogger({
prefix: {
type: 'rawString',
rawString: 'raw'
}
});
logger.log("log this"); // console output: "raw log this"
let logger = createSimpleLogger({
prefix: {
type: 'simpleString',
simpleString: 'simple'
}
});
logger.log("log this"); // console output: "[simple]: log this"
let logger = createSimpleLogger({
prefix: {
type: 'function',
func: ({logType, logArgs}) => `[${logType.toUpperCase()}|${logArgs.length} arguments]: `
}
});
logger.log("log this", "and this"); // console output: "[LOG|2 arguments]: log this and this"
This function receives a multi-line string and trims the whitespace at the start and end of each line.
let str = " Line 1 \n Line 2 ";
let trimmedStr = trimAllLines(str);
console.log(trimmedStr); // console output: "Line 1\nLine 2"
This function adjusts the indentation of a block string and optionally removes leading and trailing blank line blocks.
let str = " Line 1\n Line 2\n \n";
let trimmedStr = trimLines(str, { trimVerticalEnd: true });
console.log(trimmedStr); // console output: "Line 1\nLine 2"
This function repeatedly executes a promise-returning function until it resolves or reaches a stopping condition.
let attempts = 0;
let maxAttempts = 3;
let result = await tryUntilAsync({
func: async () => {
attempts++;
if (attempts === maxAttempts) {
return "Success"
}
else {
throw new Error("Fail");
}
},
tryLimits: {
maxAttempts
}
});
console.log(result); // console output: "Success"
let attempts = 0;
let maxAttempts = 3;
let result = await tryUntilAsync({
func: async () => {
attempts++;
return attempts;
},
stopCondition: (result: number) => result === maxAttempts,
tryLimits: {
maxAttempts
}
});
console.log(result); // console output: 3
let start = Date.now();
let delayInSec = 2;
let result = await tryUntilAsync({
func: async () => "Success",
delay: {
delayFunction: () => {
return new Promise(resolve => setTimeout(resolve, delayInSec * 1000));
}
}
});
let end = Date.now();
console.log(result); // console output: "Success"
console.log(`Time elapsed: ${(end-start)/1000} seconds`); // console output: "Time elapsed: 2 seconds"
Please note that it's possible to encounter a TryUntilTimeoutError
if the maximum number of attempts is reached or if the maximum time is exceeded without a successful result.