my-ts
v1.10.0
Published
fluent API for TypeScript
Downloads
5
Maintainers
Readme
my-ts
fluent syntax for writing TypeScript Apps
Installation
npm install my-ts --save
Examples
const object: any = ...
if (my(object).isNullOrUndefinedOrEmpty) {
//code omitted for brevity
}
const validator = (element: any): IValidationResult => { ... }
const {isValid, validationErrors} = my(object).validateWith(validator);
if (isValid ) {
//code omitted for brevity
}
const notNull = (element: any): boolean => { ... }
if (my(object).is(notNull)) {
//code omitted for brevity
}
const elements: any[] = ...
const elementWithSpecificPattern = (element: any): boolean => { ... }
if (my(elements).hasAtLeastOne(elementWithSpecificPattern)) {
//code omitted for brevity
}
const object: any = ...
const process = (element: any): any => { ... }
const {hasFailed, hasSucceeded, result, error} = my(object).tryTo(process);
if (hasSucceeded) {
//code omitted for brevity
}
const elements: any[] = ...
const closed = (element: any): boolean => { ... }
if (my(elements).areAll(closed)) {
//code omitted for brevity
}
const textFileContent: string = ...
const lines = my(textFileContent).splitToLines();
API
/**
* Check if input object is null
*/
isNull: boolean;
/**
* Check if input object is undefined
*/
isUndefined: boolean;
/**
* Check if input object is null or undefined
*/
isNullOrUndefined: boolean;
/**
* Check if input object is empty
*/
isEmpty: boolean;
/**
* Check if input object has owned properties
*/
hasOwnProperties: boolean;
/**
* Check if input object is null or undefined or empty
*/
isNullOrUndefinedOrEmpty: boolean;
/**
* Check if input array contains a value
*/
contains<T>(value: T): boolean;
/**
* Get in input array the first element that matches the predicate
* @param {function} - Predicate used to find the element
* @returns {T | undefined}
*/
firstOrDefault<T>(predicate: (element: T, index: number) => boolean): T | undefined;
/**
* Select, in a new array, all items of the input array that satisfies the predicate.
* @param {function} predicate - Predicate used to select the elements in input array
* @returns {T[]} - returns the selected items or an empty array if no item is selected
*/
where<T>(predicate: (element: T, index: number) => boolean): T[];
/**
* Validate input object with specified validator.
* @param {function} validator - function that takes input object and returns an IValidationResult object
* @returns {IValidationResult} - returns the IValidationResult object returned by the validator
*/
validateWith<T>(validator: (element: T) => IValidationResult): IValidationResult;
/**
* Check if input object matches the predicate
* @param {function} predicate - Predicate used to verify that input object satisfies a specific condition
* @returns {boolean | undefined}
*/
is<T>(predicate: (element: T) => boolean): boolean | undefined;
/**
* Check if input object does not match the predicate
* @param {function} predicate - Predicate used to verify that input object does not satisfy a specific condition
* @returns {boolean | undefined}
*/
isNot<T>(predicate: (element: T) => boolean): boolean | undefined;
/**
* Check if input array contains at least one element that satisfies the predicate
* @param {function} predicate - Predicate used to find an element
* @returns {boolean | undefined} - returns true if one element has been found.
* returns undefined if the predicate throws an exception
*/
hasAtLeastOne<T>(predicate: (element: T) => boolean): boolean | undefined;
/**
* Try to execute an action on input element
* @param {function} action - Action used to process the element
* @returns {IActionResult<U>} - returns an IActionResult object that wraps the result returned by the action.
*/
tryTo<T, U>(action: (element: T) => U): IActionResult<U>;
/**
* Check if all items of the input array satisfies the predicate.
* @param {function} predicate - Predicate used to check each item in the input array
* @returns {boolean | undefined} - returns true when the predicate returns true for all items.
* returns undefined if the predicate throws an error
*/
areAll<T>(predicate: (element: T) => boolean): boolean | undefined;
/**
* Extract all lines from the input string.
* @returns {string[]} - by default every line in the result is trimmed and empty lines are removed}
*/
splitToLines(): string[];