npm.io
0.3.0 • Published 5 years ago

object-tool

Licence
MIT
Version
0.3.0
Deps
1
Size
84 kB
Vulns
0
Weekly
0
Stars
1

Modules

ot

Functions

map([ob], [callbackFn])Object

Modifies the input object based on the callback and returns a new object

mapToArray([ob], [callbackFn])Array

Calls back all input object properties and returns the output value as an array

etch([ob], [callbackFn])void

All properties are sent in the input sent to the callback and you can perform your desired operations on the data

keys([ob])Array.<String>

This function is used to capture the array of entity keys inside the object.

len([ob])number

This function is used to get the number of values inside the object

exists([ob], query)boolean

returns true if the key/value pairs in query also exist identically in object. Also supports RegExp values in query. If the query property begins with ! then test is negated.

union(...args)Object

Merge input objects from left to right All properties are replaced

clone([input], [depth])*

This method is for copying functions with specified depth. You can specify how deep to copy objects or arrays

every([ob], iterator)boolean

Returns true if the supplied iterator function returns true for every property in the object

except([ob], [keys], [deleteFromRe])Object

Returns a clone of the object minus the specified properties.

filter([ob], [callbackFn])Object

Sends the input value of the function as key and value to callbackFn, and if the correct value is returned by callbackFn, it creates a new object with the property and sends it to the output.

Typedefs

MapCallbackType : Object

type mapCallback

mapCallbackMapCallbackType

All properties are sent to this callback as etch and after processing the return value, it replaces the previous property.

mapToArrayCallback*

All properties are sent to this callback as etch and after processing the return value.

etchCallbackvoid

All properties are sent to this callback and there is no return value

EveryCallbackboolean

All properties are given to this callback and it can return the boolean value after processing

filterCallbackboolean

Submissions are reviewed by the user and the boolean value is returned

ot

Summary:

This package is for easy use of objects in JavaScript and typescript.

The functions that are repeated in JavaScript are written in typescript for convenience, which is shown in the example below.


Example

const ob = { a : 1 , b : 'example' , c : [1 , 2, 3] };

// javascript

> const keys = Object.keys(ob)

// type return string[] | Array<String>

> ob[keys[0]]

// You will encounter an error using this method in typescript

// object-tool

const keys = ot.keys(ob)

// type return "a" | "b" | "c"

> ob[keys[0]]

// You will not get any error and the value of 1 will be returned

Example

// How to use this package is as follows

> import ot from 'object-tool';

> ot.keys({a:1,b:2})
['a','b']

// or

> import {keys} from 'object-tool';

keys({a:1,b:2})
['a','b']

// or

> const ot = require('object-tool')

ot.keys({a:1,b:2})
['a','b']

// or

> const {keys} = require('object-tool');

keys({a:1,b:2})
['a','b']

// or

map([ob], [callbackFn]) ⇒ Object

Modifies the input object based on the callback and returns a new object

Kind: global function
See: map
Since: 0.1.0

Param Type Description
[ob] Object Object Input object properties are sent to callbackFn
[callbackFn] mapCallback Callback is used to change the values sent . If you want to skip the value sent, return the skip value

Example

function callbackFn(key,value,{skip}){
 if(typeof value === number && value % 2 === 0)
{ return skip; }
 else return {key:key,value:value+'odd'}
}

> ot.map({one:1,tow:2,three:3,four:4},callbackFn)
{one:"1odd",three:"3odd"}

> function callbackFn(key,value){
   if(typeof value === "string")
       {return {key:value,value:key};}
   else return {key:key,value:value};
}

> ot.map({one:1,tow:2,three:"example",four:"javascript")
{one:1,tow:2,example:"three",javascript:"four"}

mapToArray([ob], [callbackFn]) ⇒ Array

Calls back all input object properties and returns the output value as an array

Kind: global function
See: mapToArray
Since: 0.1.0

Param Type Description
[ob] Object Input object for map operation
[callbackFn] mapToArrayCallback Callback is used to change the values sent . If you want to skip the value sent, return the skip value

Example

>function callbackFn(key,value,{skip}){
 if(typeof value === "number" && value%2 !== 0)
 { return skip; }
 else if(type value === "string") return key+'-'+value;
 return value
}

>ot.mapToArray({one:1,tow:2,three:3},callbackFn)
[2]

>ot.mapToArray({one:1,tow:2,three:3,four:'example',five:5,six:'hello'},callbackFn)
[2,"four-example",six:'six-hello']

etch([ob], [callbackFn]) ⇒ void

All properties are sent in the input sent to the callback and you can perform your desired operations on the data

Kind: global function
See: etch
Since: 0.1.0

Param Type Description
[ob] Object Input Object
[callbackFn] etchCallback Callback is used to send object property

Example

>let add = 0;
>const newOb = {}
function callbackFn(key,value,index){
 if(type of value === "number")
{ add += value }

 new Ob[key] = {value:value,index:index}
}

> ot.etch({one:9,tow:2,three:"javascript",four:"example"},callbackFn)

> console.log(add);
11
>console.log(newOb);
{three:{value:"javascript",index:2},four:{value:"example",index:3}

keys([ob]) ⇒ Array.<String>

This function is used to capture the array of entity keys inside the object.

Kind: global function
Since: 0.1.0

Param Type Description
[ob] Object Input object to get keys

Example

>ot.keys({one:1,tow:2,three:3})
["one","tow","three"]

len([ob]) ⇒ number

This function is used to get the number of values inside the object

Kind: global function
Since: 0.1.0

Param Type Description
[ob] Object Input object to count all properties

Example

>ot.len({one:1,tow:2})
2

> ot.len(one:1,tow:2,three:3,four:{one:1})
4

exists([ob], query) ⇒ boolean

returns true if the key/value pairs in query also exist identically in object. Also supports RegExp values in query. If the query property begins with ! then test is negated.

Kind: global function
See: exists
Since: 0.1.0

Param Type Description
[ob] Object the object to examine
query Object the key/value pairs to look for

Example

> ot.exists({ a: 1, b: 2}, {a: 0})
 false
 > ot.exists({ a: 1, b: 2}, {a: 1})
 true
 > ot.exists({ a: 1, b: 2}, {'!a': 1})
 false
 > ot.exists({ name: 'clive hater' }, { name: /clive/ })
 true
 > ot.exists({ name: 'clive hater' }, { '!name': /ian/ })
 true
 > ot.exists({ a: 1}, { a: function(n){ return n > 0; } })
 true
 > ot.exists({ a: 1}, { a: function(n){ return n > 1; } })
 false

union(...args) ⇒ Object

Merge input objects from left to right All properties are replaced

Kind: global function
See: union
Since: 0.1.0

Param Type Description
...args Object a sequence of object instances to be extended

Example

> ot.union({ one: 1, three: 3 }, { one: 'one', two: 2 }, { four: 4 })
{ one: 'one', three: 3, two: 2, four: 4 }

Example

> ot.union({one:[1,2,3],five:5},{tow:{one:1},three:3},{one:1,tow:2}})
{one:1,five:5,tow:2,three:3}

clone([input], [depth]) ⇒ *

This method is for copying functions with specified depth. You can specify how deep to copy objects or arrays

Kind: global function
Note: If the value of the submitted depth is -1, all references will be cloned
See: clone
Since: 0.1.0

Param Type Default Description
[input] * The value to recursively clone.
[depth] number 0 Depth that the function should copy , default value is 0 (just copy first layer)

Example

// Object cloning and object reference comparison

const objects = { a : 1 , b : 2 , c: { a:1 , b:2 }, d: [1 , 2 , 3]}

> deep = ot.clone(objects)

> console.log( deep === objects , deep.c === objects.c , deep.d === objects.c )
false , true , true

> deep = ot.clone( objects , -1 )

> console.log( deep === objects , deep.c === objects.c , deep.d === objects.d )
false , false , false

Example

// Clone arrays and compare array references

> const arrays = [ 1 , 2 , [ 1 , 2 ] , { a : 1 , b : 2 } ]
> let deep = ot.clone(arrays)

> console.log( deep === arrays , deep[2] === arrays[2] , deep[3] === arrays[3] )
false , true , true

> deep = ot.clone(arrays,-1)

> console.log( deep === arrays , deep[2] === arrays[2] , deep[3] === arrays[3] )
false , false , false

every([ob], iterator) ⇒ boolean

Returns true if the supplied iterator function returns true for every property in the object

Kind: global function
See: every
Since: 0.1.0

Param Type Description
[ob] Object the object to inspect
iterator EveryCallback the iterator function to run against each key/value pair, the args are (value, key).

Example

> function aboveTen(input){ return input > 10; }

> ot.every({ eggs: 12, carrots: 30, peas: 100 }, aboveTen)
true

> ot.every({ eggs: 6, carrots: 30, peas: 100 }, aboveTen)
false

except([ob], [keys], [deleteFromRe]) ⇒ Object

Returns a clone of the object minus the specified properties.

Kind: global function
See: except
Since: 0.1.0

Param Type Default Description
[ob] object the input object
[keys] string | Array.<string> a single property, or array of properties to omit
[deleteFromRe] boolean false If the value is correct, the main object comes with the output. default false

Example

> o.except({ a: 1, b: 2, c: 3}, 'b')
{ a: 1, c: 3 }

> o.except({ a: 1, b: 2, c: 3}, ['b', 'a'])
{ c: 3 }

filter([ob], [callbackFn]) ⇒ Object

Sends the input value of the function as key and value to callbackFn, and if the correct value is returned by callbackFn, it creates a new object with the property and sends it to the output.

Kind: global function
See: filter
Since: 0.1.0

Param Type Description
[ob] Object Object Input object properties are sent to callbackFn
[callbackFn] filterCallback Callback to perform operations

Example

> function() callbackFn(key,value,index) { return value%2 === 0 || key.includes('filter') || index === 5}

> ot.filter({one:1,tow:2,three:3,four:4},callbackFn)
{tow:2,four:4}

> ot.filter({map:12,reduce:55,filter:1,etch:11},callbackFn)
{map:12,filter:1}

> ot.filter({one:1,three:3,five:5,seven:7,nine:9,eleven:11},callbackFn)
{eleven:11}

MapCallbackType : Object

type mapCallback

Kind: global typedef
Properties

Name Type
key string
value *

mapCallback ⇒ MapCallbackType

All properties are sent to this callback as etch and after processing the return value, it replaces the previous property.

Kind: global typedef
Note: If you return the skip parameter, the operator passes over that property and it does not move to the output.

Param Type Description
[key] string keyof ob
[value] * valueOf ob
[options] Object callback options
[options.skip] Object If you return this value, the function will pass over this property
[options.index] number indexOf ob

Example

//If the value of the value can be divided into two parts, the value of the property will be deleted.
//In bitumen, the word odd will be added to the end.

>function callbackFn(key,value,{skip}){
 if(typeof value === number && value % 2 === 0)
{ return skip; }
 else return {key:key,value:value+'odd'}
}

mapToArrayCallback ⇒ *

All properties are sent to this callback as etch and after processing the return value.

Kind: global typedef
Note: If you return the skip parameter, the operator passes over that property and it does not move to the output.

Param Type Description
[key] string keyof ob
[value] * valueOf ob
[options] Object callback options
[options.skip] Object If you return this value, the function will pass over this property
[options.index] number

Example

//If the value value is an odd number, it is passed over the property, and if the type is a string, it must be added together with the key and returned, otherwise the value value is returned.

>function callbackFn(key,value,{skip}){
 if(typeof value === "number" && value%2 !== 0)
 { return skip; }
 else if(type value === "string") return key+value;
 return value
}

etchCallback ⇒ void

All properties are sent to this callback and there is no return value

Kind: global typedef

Param Type Description
[key] string keyof ob
[value] * valueOf ob
[index] number indexOf ob

Example

>let add = 0;
>const newOb = {}
>function callbackFn(key,value,index){
 if(type of value === "number")
{ add += value }

 new Ob[key] = {value:value,index:index}
}

EveryCallback ⇒ boolean

All properties are given to this callback and it can return the boolean value after processing

Kind: global typedef

Param Type Description
[value] * valueOf ob
[key] string keyof ob

Example

> function EveryCallback(value,key)
{
 return keyof value === 'number' && value > 3 && key.includes('i')
}

filterCallback ⇒ boolean

Submissions are reviewed by the user and the boolean value is returned

Kind: global typedef

Param Type Description
[key] string keyof ob
[value] * valueOf ob
[index] number indexOf ob

Example

// If the word (of) is inside the key, the value true returns
>function(key){ return key.includes('of')}

//If the type (value) is a number and more than 10, it must return true
>function(key,value){ return typeof value === "number" && value > 10}

//If the index is divisible by 10, it returns true
>function(key,value,index){return index % 10 !== 0}