easy-object (eo)
Description
Small library which goal is to make object operations in JavaScript easy and faultless. Currently in active development. You can check the newest version at develop branch.
Written in vanilla JavaScript using ES6 syntax.
Now working (I hope):
- returning type of data passed
- checking if sth is an object,
- checking if sth is of a given type,
- shallow cloning,
- deep cloning arrays and objects,
- checking if two variables have equal values.
And will be more
Installation
Just download the file eo.js and import it to your project. This library has no dependencies needed to work. There are two ways to import:
- by default
import eo from './eo';
- by name
// for example if you want to import only function "isEqual"
import { isEqual } from './eo'
Of course you need babel or sth like this because of import and ES6 syntax.
it`s not available via npm at this moment, but will be ;)
Tests
There are available tests in this projects. They use "ava" package. After you clone this repo first you must install required dependencies and then you can run tests.
For example:
// in bash
npm i
// after it finish
npm t
Documentation
Objects
-
eo :
object
-
Main library object which share all available functions
Constants
-
dataType ⇒
string
-
Return type of data passed to function.
-
is ⇒
boolean
-
Check if passed value is of selected type and returns true if is.
-
isObject ⇒
boolean
-
Check if passed value is an object and returns true if is. Use is function.
-
clone ⇒
*
-
Returns new instance of data passed. Only shallow copy. Clone only objects, arrays and dates.
-
cloneDeep ⇒
*
-
Returns new instance of data passed. Do deep cloninng. Use clone function. Deep clone only objects and arrays.
-
isEqual ⇒
boolean
-
Check if two variables have equal values. It can compare primitives, arrays, objects and dates. Don`t compare functions.
object
eo : Main library object which share all available functions
Kind: global namespace
-
eo :
object
-
.dataType(data, [lowerCase]) ⇒
string
-
.isObject(val, [...config]) ⇒
boolean
-
.is(val, type) ⇒
boolean
-
.clone(val) ⇒
*
-
.cloneDeep(val) ⇒
*
-
.isEqual(val1, val2) ⇒
boolean
-
.dataType(data, [lowerCase]) ⇒
string
eo.dataType(data, [lowerCase]) ⇒ Return type of data passed to function.
Kind: static method of eo
Returns: string
- Data type.
Throws:
-
TypeError
When you passed other than boolean value as a second argument.
Param | Type | Default | Description |
---|---|---|---|
data | * |
Data passed to function. | |
[lowerCase] | boolean |
true |
Option to configure if function return lower cased string or not. |
Example
//without options
eo.dataType([1,2,3]); //'array'
//with option
eo.dataType([1,2,3], false); //'Array'
boolean
eo.isObject(val, [...config]) ⇒ Check if passed value is an object and returns true if is. Use is function.
Kind: static method of eo
Returns: boolean
- Is value an object
Param | Type | Description |
---|---|---|
val | * |
Value passed to a function |
[...config] | string |
Argument(s) which allow to exclude selected object types |
Example
Example usage with no config
// true
let func = () => {};
eo.isObject(func);
Example
Example usage with config
// false
let func = () => {};
eo.isObject(func, 'function');
boolean
eo.is(val, type) ⇒ Check if passed value is of selected type and returns true if is.
Kind: static method of eo
Returns: boolean
- Is value a selected type
Throws:
-
Error
When you didn`t passed second argument or you passed wrong second argument.
Param | Type | Description |
---|---|---|
val | * |
Value passed to a function |
type | string |
Type you want to check |
Example
Example usage
// true
let obj = {a:1,b:2};
eo.is(obj, 'object');
*
eo.clone(val) ⇒ Returns new instance of data passed. Only shallow copy. Clone only objects, arrays and dates.
Kind: static method of eo
Returns: *
- New instance of data.
Throws:
-
Error
When no value is passed to function.
Todo
- [ ] Add option to clone functions and other objects.
Param | Type | Description |
---|---|---|
val | * |
Value to copy. |
Example
Example usage
let foo = {a:1,b:2},
bar = eo.clone(foo);
console.log(foo === bar); //false
*
eo.cloneDeep(val) ⇒ Returns new instance of data passed. Do deep cloninng. Use clone function. Deep clone only objects and arrays.
Kind: static method of eo
Returns: *
- Deeply cloned new instance of data.
Throws:
-
Error
When no value is passed to function.
Param | Type | Description |
---|---|---|
val | * |
Data to clone. |
Example
Example usage
let foo = {a:[1,2,3], b:2},
bar = eo.cloneDeep(foo);
console.log(foo === bar) //false
boolean
eo.isEqual(val1, val2) ⇒ Check if two variables have equal values. It can compare primitives, arrays, objects and dates. Don`t compare functions.
Kind: static method of eo
Returns: boolean
- Do variables have equal values.
Throws:
-
SyntaxError
When passed less than two arguments. -
TypeError
When variables passed are different types. -
TypeError
When passed a function.
Param | Type | Description |
---|---|---|
val1 | * |
First variable to compare. |
val2 | * |
Second variable to compare. |
Example
Example usage
let foo = [1,2,3],
bar = [1,2,3];
eo.isEqual(foo, bar); //true
string
dataType ⇒ Return type of data passed to function.
Kind: global constant
Returns: string
- Data type.
Throws:
-
TypeError
When you passed other than boolean value as a second argument.
Param | Type | Default | Description |
---|---|---|---|
data | * |
Data passed to function. | |
[lowerCase] | boolean |
true |
Option to configure if function return lower cased string or not. |
Example
//without options
dataType({a:1,b:2}); //'object'
//with option
dataType({a:1,b:2}, false); //'Object'
boolean
is ⇒ Check if passed value is of selected type and returns true if is.
Kind: global constant
Returns: boolean
- Is value a selected type
Throws:
-
Error
When you didn`t pass second argument or you passed wrong second argument.
Param | Type | Description |
---|---|---|
val | * |
Value passed to a function |
type | string |
Type you want to check |
Example
Example usage
//false
let obj = {a:1.b:2};
is(obj, 'array');
Array.<string>
is~optList : List of all available types to check by function is
Kind: inner constant of is
boolean
isObject ⇒ Check if passed value is an object and returns true if is. Use is function.
Kind: global constant
Returns: boolean
- Is value an object
Param | Type | Description |
---|---|---|
val | * |
Value passed to a function |
[...config] | string |
Argument(s) which allow to exclude selected object types |
Example
Example usage with no config
// true
let func = () => {};
isObject(func);
Example
Example usage with config
// false
let func = () => {};
isObject(func, 'function');
*
clone ⇒ Returns new instance of data passed. Only shallow copy. Clone only objects, arrays and dates.
Kind: global constant
Returns: *
- New instance of data.
Throws:
-
Error
When no value is passed to function.
Todo
- [ ] Add option to clone functions and other objects.
Param | Type | Description |
---|---|---|
val | * |
Value to copy. |
Example
Example usage
let foo = [1,2,3],
bar = clone(foo);
console.log(foo === bar); //false
*
cloneDeep ⇒ Returns new instance of data passed. Do deep cloninng. Use clone function. Deep clone only objects and arrays.
Kind: global constant
Returns: *
- Deeply cloned new instance of data.
Throws:
-
Error
When no value is passed to function.
Param | Type | Description |
---|---|---|
val | * |
Data to clone. |
Example
Example usage
let foo = [1,2,{a:1,b:2}],
bar = cloneDeep(foo);
console.log(foo === bar) //false
boolean
isEqual ⇒ Check if two variables have equal values. It can compare primitives, arrays, objects and dates. Don`t compare functions.
Kind: global constant
Returns: boolean
- Do variables have equal values.
Throws:
-
SyntaxError
When passed less than two arguments. -
TypeError
When variables passed are different types. -
TypeError
When passed a function.
Param | Type | Description |
---|---|---|
val1 | * |
First variable to compare. |
val2 | * |
Second variable to compare. |
Example
Example usage
let foo = {a:1,b:2},
bar = {a:1,b:2,c:3};
isEqual(foo, bar); //false