0.1.2 • Published 6 years ago

easy-object-js v0.1.2

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

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

Constants

eo : object

Main library object which share all available functions

Kind: global namespace

eo.dataType(data, lowerCase) ⇒ string

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.
ParamTypeDefaultDescription
data*Data passed to function.
lowerCasebooleantrueOption 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'

eo.isObject(val, ...config) ⇒ boolean

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

ParamTypeDescription
val*Value passed to a function
...configstringArgument(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');

eo.is(val, type) ⇒ boolean

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.
ParamTypeDescription
val*Value passed to a function
typestringType 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.
ParamTypeDescription
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.
ParamTypeDescription
val*Data to clone.

Example

Example usage
let foo = {a:[1,2,3], b:2},
 bar = eo.cloneDeep(foo);
console.log(foo === bar) //false

eo.isEqual(val1, val2) ⇒ boolean

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.
ParamTypeDescription
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

dataType ⇒ string

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.
ParamTypeDefaultDescription
data*Data passed to function.
lowerCasebooleantrueOption 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'

is ⇒ boolean

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.
ParamTypeDescription
val*Value passed to a function
typestringType you want to check

Example

Example usage
//false
let obj = {a:1.b:2};
is(obj, 'array');

is~optList : Array.<string>

List of all available types to check by function is

Kind: inner constant of is

isObject ⇒ boolean

Check if passed value is an object and returns true if is. Use is function.

Kind: global constant
Returns: boolean - Is value an object

ParamTypeDescription
val*Value passed to a function
...configstringArgument(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.
ParamTypeDescription
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.
ParamTypeDescription
val*Data to clone.

Example

Example usage
let foo = [1,2,{a:1,b:2}],
 bar = cloneDeep(foo);
console.log(foo === bar) //false

isEqual ⇒ boolean

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.
ParamTypeDescription
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