1.10.0 • Published 7 years ago

my-ts v1.10.0

Weekly downloads
2
License
MIT
Repository
github
Last release
7 years ago

my-ts

fluent syntax for writing TypeScript Apps

travis build npm devDependency Status dependency Status Commitizen friendly semantic-release codecov Known Vulnerabilities Code Climate Issue Count styled with prettier Quality Gate

npm badge

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[];
1.10.0

7 years ago

1.9.0

7 years ago

1.8.0

7 years ago

1.7.0

7 years ago

1.6.0

7 years ago

1.5.0

7 years ago

1.4.0

7 years ago

1.3.0

7 years ago

1.2.0

7 years ago

1.1.0

7 years ago

1.0.8

7 years ago

1.0.7

7 years ago

1.0.6

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago