Functions to create, process and test objects.
Methods
-
<static> change(obj, action, settings) → {Object}
-
Change all or filtered fields of object, applying specified action/transformation.
Parameters:
Name Type Argument Description obj
Object Object whose fields should be changed.
action
function | String An action/operation that should be applied to get new field value. See description of
transform
setting of copy.settings
Object <optional>
Operation settings. Keys are settings names, values are corresponding settings values. The following settings are supported:
filter
- a filter that should be used to select fields for modification (see checkField); fields conforming to the filter will be changedfilterConnect
:String
- a boolean connector that should be used when array of filters is specified infilter
setting (see checkField)
- See:
Returns:
Modified object (value of
obj
parameter).- Type
- Object
-
<static> checkField(obj, field, filter, settings) → {Boolean}
-
Check whether the field of given object corresponds to specified condition(s) or filter(s).
Parameters:
Name Type Argument Description obj
Object Object to be processed.
field
String Name of the field that should be checked.
filter
Any A filter or array of filters specifying conditions that should be checked. A filter can be:
- a function; if the function returns a true value it means that the field corresponds to this filter; the following parameters will be passed into the function: field value, field name and reference to the object
- a regular expression; if the field value (converted to string) matches the regular expression it means that the field corresponds to this filter
- a string; value can be one of the following:
own
- if the field is own property of the object it means that the field corresponds to this filter!own
- if the field is not own property of the object it means that the field corresponds to this filter- any other value - is used as the check when calling isKindOf;
if isKindOf returns
true
for the given field value and the filter it means that the field corresponds to this filter
- an object;
- if the object has
and
oror
field, its value is used as subfilter and will be passed in recursive call ofcheckField
as value offilter
parameter; the field name (and
oror
) will be used as value offilterConnect
setting (see below); if the result of the recursive call istrue
it means that the field corresponds to this filter - if the object has
field
field, its value is used as filter; if the filter is a regular expression and the field name matches the regular expression it means that the field corresponds to this filter; otherwise the field corresponds to this filter when the filed name strictly equals to the filter (converted to string) - if the object has
value
field, its value is used as filter; if the field value strictly equals to the filter value it means that the field corresponds to this filter - in any other case if the field value strictly equals to the object it means that the field corresponds to this filter
- if the object has
- any other value; if the field value strictly equals to the filter value it means that the field corresponds to this filter
settings
Object <optional>
Operation settings. Keys are settings names, values are corresponding settings values. The following settings are supported (setting's default value is specified in parentheses):
filterConnect
:String
(and
) - a boolean connector that should be used when array of filters is specified infilter
parameter; valid values are the following:and
,or
(case-insensitive); any other value is treated asand
value
:Any
- a value that should be used for check instead of field's value
Returns:
true
if the field corresponds to specified filter(s), otherwisefalse
.- Type
- Boolean
-
<static> copy(source, target, settings) → {Object}
-
Copy all or filtered fields from source object into target object, applying specified transformation if necessary.
Parameters:
Name Type Argument Description source
Object Object whose fields will be copied.
target
Object Object into which fields should be copied.
settings
Object <optional>
Operation settings. Keys are settings names, values are corresponding settings values. The following settings are supported:
filter
- a filter that should be used to select fields for copying (see checkField); fields conforming to the filter will be copiedfilterConnect
:String
- a boolean connector that should be used when array of filters is specified infilter
setting (see checkField)transform
- an action/operation that should be applied to get field's value that will be copied instead of value from source object; can be a string specifying transformation (see transform) or a function whose result will be used as field's value; object with the following fields will be passed into the function:field
- field namevalue
field value from source objectsource
- reference to the source objecttarget
- reference to the target object
- See:
Returns:
Reference to the target object (value of
target
parameter).- Type
- Object
-
<static> empty(value) → {Any}
-
Empty the given value according to the following rules:
- for array: removes all elements from the value
- for object: removes all own fields from the value
- for string: returns empty string
- for number: returns
0
- otherwise: returns
undefined
Parameters:
Name Type Description value
Any Value to be processed.
Returns:
Processed value (for array or object) or empty value corresponding to the given value.
- Type
- Any
-
<static> fromArray(list, keyField, settings) → {Object}
-
Create object (map) from list of objects. Fields of the created object are values of specified field of objects, values of the created object are corresponding items of the list.
Parameters:
Name Type Argument Description list
Array List of objects/values to be processed.
keyField
function | String <optional>
Specifies names of fields of the created object. Can be name of field or method whose value is used as field name of the created object, or function that returns the field name. In the latter case the following parameters will be passed in the function: the source object (an item of the list), the created object, the index of the source object. When the parameter is not set, items of the list are used as field names.
settings
Object <optional>
Operation settings. Keys are settings names, values are corresponding settings values. The following settings are supported (setting's default value is specified in parentheses):
deleteKeyField
:Boolean
(false
) - specifies whether key field (whose value is field name of the created object) should be deleted from the source object (an item of the list)filter
- a filter specifying objects that should be included into result (see checkField); an item of the list will be used as value for checkfilterConnect
:String
- a boolean connector that should be used when array of filters is specified infilter
setting (see checkField)
- See:
Returns:
Object created from the given list.
- Type
- Object
-
<static> getClass(value) → {String}
-
Return class of given value (namely value of internal property
[[Class]]
).Parameters:
Name Type Description value
Any Value whose class should be determined.
Returns:
String indicating value class.
- Type
- String
-
<static> getFields(obj, settings) → {Array}
-
Return list of all or filtered fields of specified object.
Parameters:
Name Type Argument Description obj
Object Object to be processed.
settings
Object <optional>
Operation settings. Keys are settings names, values are corresponding settings values. The following settings are supported:
filter
- a filter specifying fields that should be selected (see checkField)filterConnect
:String
- a boolean connector that should be used when array of filters is specified infilter
setting (see checkField)
- See:
Returns:
List of all or filtered fields of specified object.
- Type
- Array
-
<static> getFreeField(obj, settings) → {String}
-
Return name of first free (absent) field of specified object, that conforms to the following pattern: <prefix><number>
Parameters:
Name Type Argument Description obj
Object Object in which a free field should be found. If
null
(or any false value) is set, the first value that conforms to the pattern will be returned.settings
Object <optional>
Operation settings. Keys are settings names, values are corresponding settings values. The following settings are supported (setting's default value is specified in parentheses):
checkPrefix
:Boolean
(false
) - specifies whether pattern consisting only from prefix (without number) should be checkedprefix
:String
(f
) - prefix of sought fieldstartNum
:Integer
(0
) - starting number which is used as part of pattern by search/check
Returns:
Name of field which is absent in the specified object and conforms to the pattern.
- Type
- String
-
<static> getSize(obj, settings) → {Integer}
-
Return number of all or filtered fields of specified object.
Parameters:
Name Type Argument Description obj
Object Object to be processed.
settings
Object <optional>
Operation settings. Keys are settings names, values are corresponding settings values. The following settings are supported:
filter
- a filter specifying fields that should be counted (see checkField)filterConnect
:String
- a boolean connector that should be used when array of filters is specified infilter
setting (see checkField)
- See:
Returns:
Number of all or filtered fields of specified object.
- Type
- Integer
-
<static> getType(value) → {String}
-
Return type of given value.
Parameters:
Name Type Description value
Any Value whose type should be determined.
Returns:
For
NaN
-'nan'
, fornull
-'null'
, otherwise - result oftypeof
operator.- Type
- String
-
<static> getValueKey(obj, value, bAll) → {Array|String|null}
-
Return the name of field (or list of names) having the specified value in the given object.
Parameters:
Name Type Argument Description obj
Object Object to be checked.
value
Any Value that should be searched for.
bAll
Boolean <optional>
Whether names of all found fields having the specified value should be returned. Default value is
false
.Returns:
Names of fields (when
bAll
istrue
) or a field name having the specified value, ornull
when the object do not contain the specified value.- Type
- Array | String | null
-
<static> getValues(obj, settings) → {Array}
-
Return list of all or filtered field values of specified object.
Parameters:
Name Type Argument Description obj
Object Object to be processed.
settings
Object <optional>
Operation settings. Keys are settings names, values are corresponding settings values. The following settings are supported:
filter
- a filter specifying fields that should be selected (see checkField)filterConnect
:String
- a boolean connector that should be used when array of filters is specified infilter
setting (see checkField)
- See:
Returns:
List of all or filtered field values of specified object.
- Type
- Array
-
<static> isEmpty(value) → {Boolean}
-
Check whether given value is an empty value i.e.
null
,undefined
,0
, empty object, empty array or empty string.Parameters:
Name Type Description value
Any Value to be checked.
- See:
Returns:
true
if the value is an empty value, otherwisefalse
.- Type
- Boolean
-
<static> isKindOf(value, sKind) → {Boolean}
-
Check whether given value has (or does not have) specified kind (type or class).
Parameters:
Name Type Description value
Any Value to be checked.
sKind
String Type or class for check. Can be any value that is returned by getType and getClass, or one of the following:
empty
- check whether the value is empty (see isEmpty)even
- check whether the value is an even integerfalse
- check whether the value is a false valueinfinity
- check whether the value is a number representing positive or negative infinityinteger
- check whether the value is an integer numbernegative
- check whether the value is a negative numberodd
- check whether the value is an odd integerpositive
- check whether the value is a positive numberreal
- check whether the value is a real numbertrue
- check whether the value is a true valuezero
- check whether the value is0
If exclamation mark (
!
) is set before the kind, it means that the check should be negated i.e. check whether given value does not have the specified kind. For example,!real
means: check whether the value is not a real number.
Returns:
true
if value has the specified kind (or does not have when the check is negated), otherwisefalse
.- Type
- Boolean
-
<static> isSizeMore(obj, nValue, settings) → {Boolean}
-
Check whether number of all or filtered fields of specified object is more than the given value.
Parameters:
Name Type Argument Description obj
Object Object to be checked.
nValue
Number Value that should be used for comparison with number of fields.
settings
Object <optional>
Operation settings. Keys are settings names, values are corresponding settings values. The following settings are supported:
filter
- a filter specifying fields that should be counted (see checkField)filterConnect
:String
- a boolean connector that should be used when array of filters is specified infilter
setting (see checkField)
- See:
Returns:
true
, when number of all or filtered fields is more than the given value, otherwisefalse
.- Type
- Boolean
-
<static> map(obj, action, settings) → {Object}
-
Create new object containing all or filtered fields of the source object/array, applying specified action/transformation for field values.
Parameters:
Name Type Argument Description obj
Array | Object Array/object whose fields should be copied.
action
function | String An action/operation that should be applied to get field value that will be saved in created object. See description of
transform
setting of copy.settings
Object <optional>
Operation settings. Keys are settings names, values are corresponding settings values. The following settings are supported:
filter
- a filter that should be used to select fields for copying (see checkField); fields conforming to the filter will be copied in created objectfilterConnect
:String
- a boolean connector that should be used when array of filters is specified infilter
setting (see checkField)
- See:
Returns:
Created object containing processed fields.
- Type
- Object
-
<static> remove(obj, filter, settings) → {Array|Object}
-
Remove filtered fields/elements from specified object/array.
Parameters:
Name Type Argument Description obj
Array | Object Array or object to be processed.
filter
Any A filter or array of filters specifying fields or elements that should be removed (see checkField).
settings
Object <optional>
Operation settings. Keys are settings names, values are corresponding settings values. The following settings are supported:
filterConnect
:String
- a boolean connector that should be used when array of filters is specified infilter
parameter (see checkField)
- See:
Returns:
Processed array or object.
- Type
- Array | Object
-
<static> reverse(value) → {Any}
-
Reverse or negate the given value according to the following rules:
- for array: reverses order of its elements
- for object: swaps fields with their values (i.e. for
{a: "b", c: "d"}
returns{b: "a", d: "c"}
) - for string: reverses order of its characters
- for number: returns negated value (i.e.
- value
) - for boolean: returns negated value (i.e.
! value
) - otherwise: returns source value without modification
Parameters:
Name Type Description value
Any Value to be processed.
Returns:
Processed value.
- Type
- Any
-
<static> split(obj, firstObjFields, settings) → {Array}
-
Divide given object into 2 parts: the first part includes specified fields, the second part includes all other fields.
Parameters:
Name Type Argument Description obj
Object Object to be divided.
firstObjFields
Array | Object | null List of names of fields that should be included in the first part, or an object defining those fields. If value is not specified (
null
orundefined
),filter
setting should be used to divide fields into parts.settings
Object <optional>
Operation settings. Keys are settings names, values are corresponding settings values. The following settings are supported:
filter
- a filter that should be used to divide fields into parts (see checkField); fields conforming to the filter will be included in the first part, fields that do not conform to the filter will be included in the second partfilterConnect
:String
- a boolean connector that should be used when array of filters is specified infilter
setting (see checkField)
- See:
Returns:
Created parts: item with index 0 is the first part, item with index 1 is the second part.
- Type
- Array
-
<static> transform(value, sAction) → {Any}
-
Transform the given value applying the specified operation.
Parameters:
Name Type Description value
Any Value to be transformed.
sAction
String Operation that should be applied to transform the value. Can be one of the following:
array
- convert the value to array (usingArray(value)
)boolean
- convert the value to boolean value (usingBoolean(value)
)empty
- empty the value (see empty)function
- convert the value to function (usingFunction(value)
)integer
- try to convert the value to an integer number (usingMath.round(Number(value)
)number
- try to convert the value to number (usingNumber(value)
)object
- convert the value to object (usingObject(value)
)reverse
- reverse the value (see reverse)string
- convert the value to string (usingString(value)
)- otherwise - source value
Returns:
Transformed value.
- Type
- Any