0.9.8 • Published 6 years ago
extension-props v0.9.8
extension-props
A utility library delivering some additional utility functions.
Installation
npm install extension-props
# or
yarn add extension-props
Usage
You can use this utilities by two way:
Use utility functions through the provided objects likes ObjectType, ArrayType, ClassType, FunctionType, RegexType, StringType. (Recommend)
const { ObjectType, ArrayType, ClassType, FunctionType, RegexType, StringType } = require('extension-props');
Load additional utility functions into global objects likes String, Function, Object, Array, RegExp.
require('extension-props').extend();
String
forInstance(str)
: checks whether v is a string.- Variations:
StringType.forInstance(str)
String.forInstance(str)
- Variations:
isBlank(str)
: return true, if v string is blank.- Variations:
StringType.isBlank(str)
String.isBlank(str)
- Variations:
isNotBlank(str)
: return true, if v string is not blank.- Variations:
StringType.isBlank(str)
String.isBlank(str)
- Variations:
isEmpty()
: checks a string is empty.- Variations:
StringType.valueOf(str).isEmpty()
String.prototype.isEmpty()
- Variations:
isNotEmpty()
: checks a string is not empty.- Variations:
StringType.valueOf(str).isNotEmpty()
String.prototype.isNotEmpty()
- Variations:
equals(another)
: checks a string is equals with another string.- Variations:
StringType.valueOf(str).equals(another)
String.prototype.equals(another)
- Variations:
equalsIgnoreCase(another)
: checks a string is equals with another string on the basis of content of the string irrespective of case of the string.- Variations:
StringType.valueOf(str).equalsIgnoreCase(another)
String.prototype.equalsIgnoreCase(another)
- Variations:
replaceAll(str, search, replacement)
: returns a new string with all matches of a pattern replaced by a replacement.- Variations:
StringType.replaceAll(str, search, replacement)
String.replaceAll(str, search, replacement)
StringType.valueOf(str).replaceAll(search, replacement)
String.prototype.replaceAll(search, replacement)
- Variations:
replacePlaceholders(str, map)
: returns a new string with all matches of each key in the map replaced by its value.- Variations:
StringType.replacePlaceholders(str, map)
String.replacePlaceholders(str, map)
StringType.valueOf(str).replacePlaceholders(map)
String.prototype.replacePlaceholders(map)
- Variations:
Function
forInstance(func)
: checks whether f is a function.- Variations:
FunctionType.forInstance(func)
Function.functionForInstance(func)
- Variations:
defineFunction(name, prototype)
: define a function with dynamic name.- Variations:
FunctionType.defineFunction(name, prototype)
Function.defineFunction(name, prototype)
FunctionType.valueOf(prototype).defineFunction(name)
Function.prototype.defineFunction(name)
name
: is the name of the function.prototype
(option): is a function that determines the content of the function being created.
- returns a function.
- Variations:
isCallable(func)
: checks whether f function is callable.- Variations:
FunctionType.isCallable(func)
Function.isCallable(func)
- Variations:
isNormalFunction(func)
: checks whether f is a normal function, that means that f is not an async function nor an arrow function.- Variations:
FunctionType.isNormalFunction(func)
Function.isNormalFunction(func)
- Variations:
isAsyncFunction(func)
: checks whether f is an async function.- Variations:
FunctionType.isAsyncFunction(func)
Function.isAsyncFunction(func)
- Variations:
isSyncFunction(func)
: checks whether f is a sync function (negative of isAsyncFunction).- Variations:
FunctionType.isSyncFunction(func)
Function.isSyncFunction(func)
- Variations:
isArrowFunction(func)
: checks whether f is an arrow function.- Variations:
FunctionType.isArrowFunction(func)
Function.isArrowFunction(func)
- Variations:
isNonArrowFunction(func)
: negative of isArrowFunction.- Variations:
FunctionType.isNonArrowFunction(func)
Function.isNonArrowFunction(func)
- Variations:
clone()
: clone a function.- Variations:
FunctionType.valueOf(func).clone()
Function.prototype.clone()
- Variations:
Class
forInstance(cls)
: checks whether c is a class.- Variations:
ClassType.forInstance(cls)
Function.classForInstance(cls)
- Variations:
defineClass(name, superclass, prototype)
: define a class with dynamic name.- Variations:
ClassType.defineClass(name, superclass, prototype)
Function.defineClass(name, superclass, prototype)
ClassType.valueOf(superclass).defineClass(name, prototype)
Function.prototype.defineClass(name, prototype)
name
: is the name of the class.superclass
(option): specify a super class.prototype
(option): is a function that determines the content of constructor of the class being created.- return a class.
- Variations:
isES6Class(cls)
: checks whether c is an es6 class.- Variations:
ClassType.isES6Class(cls)
Function.isES6Class(cls)
- Variations:
preventInheritingClass(obj, classDefinition, except)
: prevent inheriting class.- Variations:
ClassType.preventInheritingClass(obj, classDefinition, except)
Object.preventInheritingClass(obj, classDefinition, except)
ClassType.valueOf(obj).preventInheritingClass(classDefinition, except)
Object.prototype.preventInheritingClass(classDefinition, except)
obj
: is an instance of subclass of classDefinition class.classDefinition
: is a superclass.except
(option): is white list, designate classes that are allowed to inherit.- throw a OverridingError exception, if obj is an instance of a class, and it is not allowed to inherit the classDefinition class.
- return false, if obj is an instance of a class, and it is not subclass of classDefinition class.
- return true, if obj is an instance of a class, and it is allowed to inherit the classDefinition class.
- Recommended: Put this function into the constructor.
- Variations:
preventOverrideFunction(obj, classDefinition, functions)
: prevent overriding of functions.- Variations:
ClassType.preventOverrideFunction(obj, classDefinition, functions)
Object.preventOverrideFunction(obj, classDefinition, functions)
ClassType.valueOf(obj).preventOverrideFunction(classDefinition, functions)
Object.prototype.preventOverrideFunction(classDefinition, functions)
obj
: is an instance of subclass of classDefinition class.classDefinition
: is a superclass.functions
: is an array of functions need to prevent override.- throw a OverridingError exception, if obj contains a function, and it is not allowed to override in the subclass of the classDefinition class.
- return false, if obj is an instance of a class, and it is not subclass of classDefinition class.
- return true, if no function is prevented, it is contained in subclasses.
- Recommended: Put this function into the constructor.
- Variations:
Object
getAllPropertyNames(obj)
: returns an array of all properties of a given object.- Variations:
ObjectType.getAllPropertyNames(obj)
Object.getAllPropertyNames(obj)
- Variations:
getAllPropertyDescriptor(obj, prop)
: returns a property descriptor for a property of a given object.- Variations:
ObjectType.getAllPropertyDescriptor(obj, prop)
Object.getAllPropertyDescriptor(obj, prop)
- Variations:
getAllPropertyDescriptors(obj)
: returns an array of all property descriptors of a given object.- Variations:
ObjectType.getAllPropertyDescriptors(obj)
Object.getAllPropertyDescriptors(obj)
- Variations:
isBlank(obj)
: return true, if obj = undefined or obj = null.- Variations:
ObjectType.isBlank(obj)
Object.isBlank(obj)
- Variations:
isNotBlank(obj)
: return true, if obj != undefined and obj != null.- Variations:
ObjectType.isNotBlank(obj)
Object.isNotBlank(obj)
- Variations:
Regex
forInstance(reg)
: checks whether v is a regex.- Variations:
RegexType.forInstance(reg)
RegExp.forInstance(reg)
- Variations:
escape(str)
: escape regex expression special characters.- Variations:
RegexType.escape(str)
RegExp.escape(str)
- Variations:
matchWords(str)
: create a regex string for checks match with the words in str string.- Variations:
RegexType.matchWords(str)
RegExp.matchWords(str)
- Variations:
Array
forInstance(arr)
: checks whether v is a array.- Variations:
ArrayType.forInstance(arr)
Array.forInstance(arr)
- Variations:
isBlank(arr)
: return true, if v array is blank.- Variations:
ArrayType.isBlank(arr)
Array.isBlank(arr)
- Variations:
isNotBlank(arr)
: return true, if v array is not blank.- Variations:
ArrayType.isNotBlank(arr)
Array.isNotBlank(arr)
- Variations:
isEmpty()
: checks a array is empty.- Variations:
ArrayType.valueOf(str).isEmpty()
Array.prototype.isEmpty()
- Variations:
isNotEmpty()
: checks a array is not empty.- Variations:
ArrayType.valueOf(str).isNotEmpty()
Array.prototype.isNotEmpty()
- Variations:
equals(another)
: checks a array is equals with another array.- Variations:
ArrayType.valueOf(str).equals(another)
Array.prototype.equals(another)
- Variations:
virtualGet(index)
: get value at index. You will still get a value even if the index is out of bounds.- Variations:
ArrayType.valueOf(str).virtualGet(index)
Array.prototype.virtualGet(index)
- Variations:
insert(index, ...elements)
: insert elements into the specified position.- Variations:
ArrayType.valueOf(str).insert(index, ...elements)
Array.prototype.insert(index, ...elements)
- Variations:
lastIndexOf(element)
: returns the last index at which a given element can be found in the array, or -1 if it is not present.- Variations:
ArrayType.valueOf(str).lastIndexOf(element)
Array.prototype.lastIndexOf(element)
- Variations: