Things common in javascript code
npm install javascript-commonThe code in this package is common to be used in some my projects. Although for my projects, it may be useful for you.
Things that can be imported from this package:
#### emptyArray
This is a read-only empty array ([]). Read-only means we cannot add/remove an entry to/from it. It's useful
for a initial value of a "state" variable whose array type. In "React" framework, it can be used as the
parameter of React.useState hook function. If there are many array "state" variable, it's better to keep one
copy in memory. It's safe to share among some "state" variables because an array "state" variable is considered
to change if it's assigned to a different instance of array. So, we may not just call push method and
re-assign it.
#### emptyObject
This is a read-only empty plain object ({}). Read-only means we cannot set/unset a property to/from it. It's
useful for a initial value of a "state" variable whose type of a plain object. In "React" framework, it can be
used as the parameter of React.useState hook function. If there are many object "state" variable, it's better
to keep one copy in memory. It's safe to share among some "state" variables because an object "state" variable
is considered to change if it's assigned to a different instance of object. So, we may not just set/unset
a property and re-assign it.
#### emptyString
This is an empty string (""). An empty string is often used as the default value of a string variable.
#### extendObject(target, extObj)
It extends the object target by adding to it the new members (properties/methods) from the object extObj.
It's like Object.assign(target, extObj), but different from Object.assign, when there are some the same
members among target and extObj, it doesn't remove those members from target and replace them with
the same ones from extObj. The original members still exist in target. This function is simply invokeObject.setPrototypeOf(extObj, target). If it's so simple, why do we need the wrapper function? It's because
we need a static type checking. The type of the returned value should be the type which is combination betweentarget's type and extObj's type.
#### isPlainObject(obj)
To check a variable is a plain object that is an object that is created by using "object literal"
({prop1: val1, prop2: val2, ...}).
Parameters:
- obj
Any variable to check
#### noChange
This function returns the parameter that is passed into it, no change (p => p). Like noop, this function
is often used as the default value for a variable whose function type. Instead of maintaining many copies,
it's better to keep one copy in memory.
#### noop
It's a function that does nothing (() => {}). This function is often used as the default value for
a variable whose function type. Instead of maintaining many copies, it's better to keep one copy in memory.
#### objEquals(obj1, obj2)
To compare two plain objects recursively whether they are equal or not. It's not to check the object reference equality.
Two plain objects are considered equal if they have exactly the same properties (the same property names and values).
If a property value is a plain object then it will also be compared by the same way.
Parameters:
- obj1
First object to compare
- obj2
Second object to compare
#### proxyObject(target, extObj, proxiedIfNotExist)
Similar to extendObject but it doesn't change the prototype of target. It utilizes a Proxy object. It's
useful if target already has a prototype object.
Parameters:
- target
The extended object
- extObj
The object which has the extending members. It may be also a function with format target => extObj.
- proxiedIfNotExist
If true then a member is read from extObj only if the member doesn't exist on target.
By default, it's false.
#### proxyClass(Target, extObj, proxiedIfNotExist)
Similar to proxyObject but the first parameter is not an instance object, it's a class of target object.
This function will create the target instance: const target = new Target(...args) and then call proxyObject.
This function returns a Proxy object of the class, NOT proxy of the Target instance.