This is my attempt at a typescript library aimed at helping make peoples lives a little easier when coding. Here are the components that are currently offered.
- addDays
- append
- Box
- Bubble Sort
- compose
- Conditional
- curry
- date
- formatDate
- formatMoney
- Id
- isValid
- Maybe
- pad
- prop
- PubSub
- QuickSort
- Queue
- Stack
- swap
- trace
- traceMap
This function will let you either pass in a string or a date object and it will add the specified days to it. If you pass in null or undefined, you will get back a new Date; ex:
addDays('1/1/2023', 7)
This function just appends two strings ex:
expect(append('thing')('some')).toEqual('something');
This monad has many uses. It allow you to map over your data to transform it into anything you want. ex:
Box(data)
.map(x => x.filter(y => y.id === 1))
.map(x => x.filter(y => y.date > '1/1/2023'))
.fold(x => x);
A sorting algorithm that loops through all the elements and compares the current element to the next element and uses a swapping technique to handle the sorting
prop | required | date type | description |
---|---|---|---|
items | ✅ | array | an array of items |
asc | [ ] | boolean | use if you want to control the direction |
property | [ ] | keyof T | the property on the array of objects that you want to sort by |
isDate | [ ] | boolean | used to sort by a date value |
ex
const arr = [6, 5, 4, 3, 2, 1];
const result = BubbleSort(arr);
const result = BubbleSort(data, false, 'id');
const result = BubbleSort(dataWithDates, true, 'expires', true);
This function will chain together a number of functions passing the output of each function to the input of the next function ex:
const result = compose(addOne, addTwo, addThree);
A super sweet functional function to handle if/else/then ex:
const obj = {
id: 1,
price: 1.1,
};
const testPrice = Conditional({
if: (x) => typeof x === 'number',
else: () => 'not a number',
then: (x) => x,
});
expect(testPrice(obj.price)).toEqual(1.1);
const obj = {
id: 1,
price: null,
};
const testPrice = Conditional({
if: (x) => typeof x === 'number',
else: () => 'not a number',
then: (x) => x,
});
expect(testPrice(obj.price)).toEqual('not a number');
This is a very powerful function that allows you to curry a function that takes multiple arguments and create functions from that that can take each argument one at a time and return another function until all the arguments have been supplied. ex:
const curriedAdd = curry(Add);
const firstNumberAdded = curriedAdd(4);
const actualFunctionExecutedWhenAllArgumentsArePresent =
firstNumberAdded(5);
expect(actualFunctionExecutedWhenAllArgumentsArePresent).toEqual(9);
This library has functions like get DateDetails, daysInMonth, isSameMonth, IsSameDay, getDayOfDate, and getMonthName ex:
expect(daysInMonth(0, 2023)).toEqual(31);
This function will return back a basic date like 1/1/2023. If you pass in null or undefined, you will get back an empty string. ex:
expect(formatDate(new Date('1/1/2023'))).toBe('1/1/2023');
This function will nicely format money for report purposes ex:
expect(formatMoney('.2')).toEqual('.20');
This is the identity monad left identity
const result1 = Id<number>(x).chain(f).toString();
// @ts-ignore
const result2 = f(x).toString();
expect(result1).toEqual(result2);
right identity
const result2 = Id(x);
expect(result1.toString()).toEqual(result2.toString());s
mapping
const arr = [10, 20, 30];
const result = Id(arr)
.map((x) => x.map((y) => y + 1))
.toString();
expect(result).toEqual('Id(11,21,31)');
This function can be used to validate that a string or a number or date or array or even an object can be used. This is your failsafe function. ex:
expect(isValid(null)).toBeFalsy();
expect(isValid('')).toBeFalsy();
expect(isValid(0)).toBeFalsy();
const obj = {
name: 'mike',
};
expect(isValid(obj)).toBeTruthy();
Another monad, but this one handle null input very well ex:
const appendC = Maybe.chain(prop('b'), prop('c'), append(' is great'));
const goodInput = Maybe.just({
b: {
c: 'fp',
},
});
const badInput = Maybe.just({});
expect(appendC(goodInput).extract()).toEqual('fp is great');
expect(appendC(badInput).extract()).toBeNull();
This function will either left or right pad a string for you ex:
expect(pad('ss', 4, '0')).toEqual('00ss');
A really nice publisher/subscriber algorithm ex:
let subscriberNotification = '';
const response = (msg) => {
subscriberNotification = msg;
};
const pubSub = PubSub<string>();
const unsubscribe = pubSub.subscribe(response);
pubSub.publish('hello');
expect(subscriberNotification).toEqual('hello');
unsubscribe();
pubSub.publish('you should not see this');
expect(subscriberNotification).toEqual('hello');
This function fetches properties of objects ex:
const obj = {
id: 1,
name: 'mike',
};
expect(prop('name')(obj)).toEqual('mike');
This class is the classic stack class. It is LIFO(last in, first out) ex:
const stack = new Stack<string>();
stack.push('a');
expect(stack.size()).toEqual(1);
expect(stack.peek()).toEqual('a');
stack.pop();
expect(stack.size()).toEqual(0);
A Sorting algorithm that uses a pivot to split data and then sort the left and right and then swap out indexes in the array to sort the data. This supports ascending and desending order as well as arrays of objects. ex
const result = QuickSort(dataWithDates, true, 'expires', true);
queue is a data struccture that is FIFO (first in first out) these are the methods
- enqueue
- dequeue
- size
This function will swap two indexes out of an array
This function can be used to debug your monads
This is another useful debugging tool for you monads