1.0.9 • Published 4 years ago

cu-common-utils v1.0.9

Weekly downloads
3
License
MIT
Repository
github
Last release
4 years ago

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

FunctionParametersDescriptionReturn Value
isEmpty(value)value: The value that is to be checkedChecks if the value is null/ undefined or emptytrue/false
isEmptyObject(object)object: The object that is to be checkedChecks if object is null/ undefined or empty({})true/false
isArray(object)object: Object to be checked if it is an array or notChecks if the passed object is array or nottrue/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 listMerge 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 occursCreates 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 occursConvert 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 occursConverts 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 valueFind 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 errorFind 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 searchedregex: The regular expression to which this string is to be matchedreplacement (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/ exceptionReplaces 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 searchedregex: The regular expression to which this string is to be matchedstartIndex (Optional): starting index from where regex will be matched. O by defaultdefaultValue (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

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago