cu-common-utils

1.0.9 • Public • Published

Common Utils

Common utils is a library for JS projects having all the common functionalities while developing an angular/ node/ react project.

Installation

Use npm to install the package

npm install cu-common-utils

Usage

For import syntax

import * as cu from 'cu-common-utils';
 
let x = {};
console.log(cu.isEmptyObject(x));

For require syntax

var cu = require('cu-common-utils');
let x = {};
console.log(cu.isEmptyObject(x));

Functions

Function Parameters Description Return Value
isEmpty(value) value: The value that is to be checked Checks if the value is null/ undefined or empty true/false
isEmptyObject(object) object: The object that is to be checked Checks if object is null/ undefined or empty({}) true/false
isArray(object) object: Object to be checked if it is an array or not Checks if the passed object is array or not true/false
getValidArray(object, path, defaultValue) object: The object to be traversed
path: Hierarchy of the key whose value is to be checked
defaultValue(Optional): The default value to be returned if value is not found.
Checks the path of the array and returns the value if there are no errors - path value (if there is no error)
- returns empty array [] in case of error by default
getValidObject(object, path, defaultValue) object: The object to be traversed
path: Hierarchy of the key whose value is to be checked
defaultValue(Optional): The default value to be returned if value is not found.
Checks the path of the object and returns the value if there are no errors - path value (if there is no error)
- returns empty object {} in case of error by default
getValidString(object, path, defaultValue) object: The object to be traversed
path: Hierarchy of the key whose value is to be checked
defaultValue(Optional): The default value to be returned if value is not found.
Checks the path of the string and returns the value if there are no errors - path value (if there is no error)
- returns empty string "" in case of error by default
getValidNumber(object, path, defaultValue) object: The object to be traversed
path: Hierarchy of the key whose value is to be checked
defaultValue(Optional): The default value to be returned if value is not found.
Checks the path of the number and returns the value if there are no errors - path value (if there is no error)
- returns 0 in case of error by default
mergeArrays(list1, list2) list1: first list
list2: second list
Merge two lists and return a new list. - Returns merged list
- returns an empty list [] in case of an error
cloneObject(object, defaultValue) object: The object/value to be cloned
defaultValue(Optional): Value to be returned if object is null or an error occurs
Creates a new object that is the clone of passed object. - return new object without passing the reference
- return empty object {} in case of error
stringifyJSON(object, defaultValue) object: The object/value to be stringified
defaultValue(Optional): Value to be returned if object is null or an error occurs
Convert JSON object to string. - returns stringified JSON
- returns empty string "" in case of an error as default value
parseJSON(objectString, defaultValue) objectString: The string whose value will be parsed
defaultValue(Optional): Value to be returned if object is null or an error occurs
Converts string value to JSON. - returns parsed JSON object from string
- returns empty object {} in case of an error as default value
findObjectInList(list, compareObject, keyInList, defaultValue, keyForCompareObject) list: The list from which object will be searched
compareObject: The object which is to be compared
keyInList: Object key value in list that will be compared
defaultValue(Optional): Default value that will be returned in case of an error
keyForCompareObject(Optional): key of the compareObject that will be compared with list objects key value
Find an object from the list. - returns the object if object is found
- returns undefined as default value
findElementInList(list, value, defaultValue) list: The list from which object will be searched
value: The value to be compared
defaultValue(Optional): Default value that will be returned in case of an error
Find value in the list - returns the object if object is found
- returns undefined as default value
replaceAll(text, regex, replacement, defaultValue) text: The string in which regex will be searched
regex: The regular expression to which this string is to be matched
replacement (Optional): The string to be substituted for each element. By default it is ""
defaultValue (Optional): The default value to be returned in case of error/ exception
Replaces each substring of this string that matches the given regular expression with the given replacement - Returns the replaced regex string in case of no error/ exception
- Returns empty string ("") as default value in case of error/ exception
indexOf(text, regex, startIndex, defaultValue) text: The string in which regex will be searched
regex: The regular expression to which this string is to be matched
startIndex (Optional): starting index from where regex will be matched. O by default
defaultValue (Optional): The default value to be returned in case of error/ exception. By default returns -1.
Searches starting index of a given string from the text. - Returns starting index of the matched string
- Returns -1 if value is not found or in case of an error

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT

Package Sidebar

Install

npm i cu-common-utils

Weekly Downloads

1

Version

1.0.9

License

MIT

Unpacked Size

18.4 kB

Total Files

4

Last publish

Collaborators

  • ahmermh