0.3.0 • Published 4 years ago

object-tool v0.3.0

Weekly downloads
1
License
MIT
Repository
github
Last release
4 years ago

Modules

Functions

Typedefs

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.

// javascript

const keys = Object.keys(ob)

// type return string[] | Array

ob[keys0]

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

// object-tool

const keys = ot.keys(ob)

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

ob[keys0]

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

**Example**  
```js
// 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

ParamTypeDescription
obObjectObject Input object properties are sent to callbackFn
callbackFnmapCallbackCallback 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

ParamTypeDescription
obObjectInput object for map operation
callbackFnmapToArrayCallbackCallback 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

ParamTypeDescription
obObjectInput Object
callbackFnetchCallbackCallback 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

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

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

ParamTypeDescription
obObjectthe object to examine
queryObjectthe 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

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

ParamTypeDefaultDescription
input*The value to recursively clone.
depthnumber0Depth 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

ParamTypeDescription
obObjectthe object to inspect
iteratorEveryCallbackthe 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

ParamTypeDefaultDescription
obobjectthe input object
keysstring | Array.<string>a single property, or array of properties to omit
deleteFromRebooleanfalseIf 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

ParamTypeDescription
obObjectObject Input object properties are sent to callbackFn
callbackFnfilterCallbackCallback 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

NameType
keystring
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.

ParamTypeDescription
keystringkeyof ob
value*valueOf ob
optionsObjectcallback options
options.skipObjectIf you return this value, the function will pass over this property
options.indexnumberindexOf 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.

ParamTypeDescription
keystringkeyof ob
value*valueOf ob
optionsObjectcallback options
options.skipObjectIf you return this value, the function will pass over this property
options.indexnumber

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

ParamTypeDescription
keystringkeyof ob
value*valueOf ob
indexnumberindexOf 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

ParamTypeDescription
value*valueOf ob
keystringkeyof 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

ParamTypeDescription
keystringkeyof ob
value*valueOf ob
indexnumberindexOf 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}
0.3.0

4 years ago

0.2.0

4 years ago

0.1.0

4 years ago

1.0.4

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.3

4 years ago

1.0.0

4 years ago