@zionappsupport/core
v0.3.3
Published
Helper Methods for Angular, Ionic and NgRx projects.
Downloads
33
Readme
@zionappsupport/core
Helper Methods for Angular, Ionic, NgRx or any TypeScript projects. This library is designed to prevent bugs from null
and undefined
variables and properties. Key areas of use include, but are not limited to:
- NgRx selectors
- Utility functions
npm install @zionappsupport/core --save
Methods
Item
1. Strings
a. Case-Insensitive Equals
Do two strings match with case-insensitivity?
caseInsensitiveEquals: (s1: string, s2: string) => boolean;
b. Case-Insensitive Includes
Find if a substring of a string value exists with case insensitive matching
caseInsensitiveIncludes: (value: string, query: string) => boolean;
2. Objects
a. getPropertyValue
Get a property of an object specifying a default value.
getPropertyValue: (item: any, key: string, defaultValue: any) => any;
b. getBooleanPropertyValue, getNumericPropertyValue, getStringPropertyValue
Get a property of an object expecting a specific type
getBooleanPropertyValue: (item: any, key: string) => boolean;
/ Default is false
getGuidPropertyValue: (item: any, key: string) => string;
// Default is null
getIdPropertyValue: (item: any, key: string) => number;
// Default is null
getNumericPropertyValue: (item: any, key: string) => number;
// Default is 0
getStringPropertyValue: (item: any, key: string) => string;
// Default is ''
c. isPropertyDefined
Does an object exist and is a specific property truthy?
isPropertyDefined: (item: any, key: string) => boolean;
3. Any Type
a. isItemDefined
Is a variable is falsy?
isItemDefined: (item: any) => boolean;
b. isItemNotDefined
Is a variable truthy?
isItemNotDefined: (item: any) => boolean;
List
1. hasAnyItems
Is your list truthy and does it have any items?
hasAnyItems: (list: any[]) => boolean;
2. hasNoItems
Is your list falsy or does it have no items?
hasNoItems: (list: any[]) => boolean;
3. hasAllItemsInList
Do all ids match the ids of items a list?
hasAllItemsInList: (list: string[], idProperty: string, idValues: any[]) => boolean;
Example
const list = [{ id: 1, name: 'Apples'}, { id: 2, name: 'Oranges')];
console.log(hasAllItemsInList(list, 'id', [1, 2])); // Outputs 'true'
console.log(hasAllItemsInList(list, 'id', [1, 3])); // Outputs 'false'
4. hasAllQueryValuesInList
Do all query strings match as a substring of at least 1 string in a list?
hasAllQueryValuesInList: (list: string[], queries: string[]) => boolean;
Example
const list = ['Apples', 'Oranges'];
console.log(hasAllQueryValuesInList(list, ['Apples'])); // Outputs 'true'
console.log(hasAllQueryValuesInList(list, ['Apples', 'Oranges'])); // Outputs 'false'
5. hasItemInList
Does an id match the ids of at least 1 item a list?
hasItemInList: (list: any[], idProperty: string, idValue: any) => boolean;
Example
const list = [{ id: 1, name: 'Apples'}, { id: 2, name: 'Oranges')];
const list = [{ id: 1, name: 'Apples'}, { id: 2, name: 'Oranges')];
console.log(hasItemInList(list, 'id', 1)); // Outputs 'true'
console.log(hasItemInList(list, 'id', 3)); // Outputs 'false'
6. hasQueryValueInList
Does a query match as a substring of at least 1 string in a list?
hasQueryValueInList: (list: string[], query: string) => boolean;
Example
const list = ['Apples', 'Oranges'];
console.log(hasQueryValueInList(list, 'Apples')); // Outputs 'true'
console.log(hasQueryValueInList(list, 'Pears')); // Outputs 'false'
- compareKeyFunctionAsc: Sort ascending helper function.
- compareKeyFunctionDesc: Sort descending helper function.
- getSortedItemsByKeyAsc: Get a new sorted ascending list by any property.
- getSortedItemsByKeyDesc: Get a new sorted descending list by any property.
- sanitizeList: Get a list ensuring if it is null or undefined, it will return an empty array.
Time
- getISOTimestamp: Get an ISO timestamp string. Format: YYYY-MM-DDTHH:MM:SS.sssZ