README
arrow-utils
arrow-utils library binds array and object methods.
Complex Array and Object operations are made simple.
install
With npm do:
to install arrow-utils
npm install arrow-utils
to install arrow-utils globally
npm install -g arrow-utils
For Docs ,see:arrow-utils
Utilities
- HTTP Methods (GET-POST)
- Pipeline
- Set Environment Configuration
- Otp Generator
- Get Date Difference
- Encryption and Decryption
- PasswordStrength
- GenerateToken
- Get Possible Routes & find shortest path
Array Methods
- Array_Unique
- Array_Object_Unique
- Array_FindNearestValue
- Array_FindNearestObject
- Array_Sort
- Array_SortBy
- Array_Sum
- Array_SumBy
- Array_Avg
- Array_AvgBy
- Array_Max
- Array_Min
- Array_MaxBy
- Array_MinBy
- Array_GroupBy
- Array_GroupBy_With_Pick
- Array_FindOne
- Array_Filters
- Array_UpdateOne
- Array_UpdateMany
- Array_RemoveOne
- Array_RemoveMany
- Array_Filters_With_Pick
- Array_Pick
- Array_Check
- Array_Intersect
- Array_Union
- Array_Copy
- Array_Validate
- Array_Object_Creation
Object Methods
- Object_forEach
- Object_IsEmpty
- Object_Validate
- Object_Merge
- Object_isEqual
- Object_Copy
- Object_Pick
- Object_Remove
Math Methods
- getPercentage
- getValue
HTTP-Get
HTTP client for the browser and node.js
const arrow=require('arrow-utils')
var header={}
arrow.get('http://domain-name.com/api',header).then((response)=>{
console.log(response)
})
HTTP-Post
HTTP client for the browser and node.js
const arrow=require('arrow-utils')
var body={},header={}
arrow.post('http://domain-name.com/api',body,header).then((response)=>{
console.log(response)
})
Pipeline (Function Chaining)
Which executes function as synchronous and return the result as input to next function
const arrow=require('arrow-utils')
let input="malayalam";
console.log(input == arrow.Pipeline(input,[one,two,three]) ? "is palindrome" : "not palindrome" )
function one(data){
return data.split("")
}
function two(data){
return data.reverse()
}
function three(data){
return data.join("")
}
output:
is palindrome
Set Nodejs Environment variable
Set environment variable for Nodejs project loads environment variables from a .env file into process.env
steps:
create .env file inside our root directory
-> touch .env
add environment variable in .env file
.env file looks like
PORT = 8080
DB_NAME = arrow
const arrow=require('arrow-utils')
arrow.setEnvConfig() now loads environment variables from a .env file into process.env
console.log("PORT : ",process.env.PORT)
console.log("DB_NAME : ",process.env.DB_NAME)
Output:
PORT : 8080
DB_NAME : arrow
Generate OTP
Generate One Time Password with n-integers
const arrow=require('arrow-utils')
console.log(arrow.otpGen(10))
console.log(arrow.otpGen(6))
console.log(arrow.otpGen(4))
output:
8312746263
772736
0134
Encryption and Decryption
const arrow=require('arrow-utils')
let key="password23"
let encryptedKey = arrow.encrypt(key)
console.log("encryptedKey : ",encryptedKey)
let decryptedKey = arrow.decrypt(encryptedKey)
console.log("decryptedKey : ",decryptedKey)
output:
encryptedKey : GdJoFdxZjzNmxViuuAdR}`JPydNTyo\ZgiiCvcND
decryptedKey : password23
PasswordStrength
const arrow=require('arrow-utils')
console.log(arrow.PasswordStrength("password123"))
output:
Average
GenerateToken
const arrow=require('arrow-utils')
console.log(arrow.generateToken())
output:
f243ccfd44978501a70620b4ad994cfc306e17d9
differences of two dates
Track the difference in milliseconds to
const arrow=require('arrow-utils')
let date1=new Date("2018-08-19T11:54:29.083Z");
let date2=new Date("2018-08-19T12:55:26.083Z");
console.log(arrow.get_date_difference(date1,date2,'ms'))
console.log(arrow.get_date_difference(date1,date2,'sec'))
console.log(arrow.get_date_difference(date1,date2,'min'))
console.log(arrow.get_date_difference(date1,date2,'hour'))
output:
ms 3657000
sec 3657
min 61
hour 1
Get Possible Routes
Get Possible Routes with find shortest path
const arrow=require('arrow-utils')
let distanceMatrix=[
[0,2,5,1],
[10,0,2,30],
[4,6,0,8],
[2,10,4,0],
]
let city=["a","b","c","d"];
console.log(arrow.getOptimizedRoutes(distanceMatrix,city))
output:
{ all_possible_routes:
[ { route: [ 'a', 'b', 'c', 'd', 'a' ], distance: 14 },
{ route: [ 'a', 'd', 'b', 'c', 'a' ], distance: 17 },
{ route: [ 'a', 'd', 'c', 'b', 'a' ], distance: 21 },
{ route: [ 'a', 'c', 'd', 'b', 'a' ], distance: 33 },
{ route: [ 'a', 'b', 'd', 'c', 'a' ], distance: 40 },
{ route: [ 'a', 'c', 'b', 'd', 'a' ], distance: 43 } ],
shortest_path: [ 'a', 'b', 'c', 'd', 'a' ],
shortest_distance: 14,
longest_path: [ 'a', 'c', 'b', 'd', 'a' ],
longest_distance: 43 }
head & tail
const arrow=require('arrow-utils')
let array=[1,5,3,6,11,7]
console.log(arrow.head(array))
console.log(arrow.tail(array))
output:
//head 1
//tail [5,3,6,11,7]
Array Sort
Sorts the input array
const arrow=require('arrow-utils')
let array=[1,5,3,6,11,7]
console.log(arrow.Array_Sort(array))
output:
[1,3,5,6,7,11]
Array SortBy
Sort By specific fields
const arrow=require('arrow-utils')
let sort_by=[
{name:"siva",skils_percentage:{nodejs:8,reactjs:7}},
{name:"ganesh",skils_percentage:{nodejs:9,reactjs:9}},
{name:"gopi",skils_percentage:{nodejs:7,reactjs:8}},
]
console.log(arrow.Array_SortBy(sort_by,'skils_percentage.nodejs'))
output:
[ { name: 'gopi', skils_percentage: { nodejs: 7, reactjs: 8 } },
{ name: 'siva', skils_percentage: { nodejs: 8, reactjs: 7 } },
{ name: 'ganesh', skils_percentage: { nodejs: 9, reactjs: 9 } }
]
Array Sum
calculate total of array
const arrow=require('arrow-utils')
let array=[1,5,3,6,11,7]
console.log(arrow.Array_Sum(array))
output:
33
Array SumBy
Sort By specific fields
const arrow=require('arrow-utils')
let products = [
{
"id": "1",
"Name": "Name 1",
"Price": 60,
"inventory": {"no":1},
},
{
"id": "2",
"Name": "Name 2",
"Price": 40,
"inventory": {"no":5},
}
]
console.log(arrow.Array_SumBy(products,"Price inventory.no"))
output:
{ Price: 100, 'inventory.no': 6 }
Array Avg
find average of array
const arrow=require('arrow-utils')
let array=[1,5,3,6,11,7]
console.log(arrow.Array_Avg(array))
output:
2.857142857142857
Array AvgBy
find average By specific fields
const arrow=require('arrow-utils')
let products = [
{
"id": "1",
"Name": "Name 1",
"Price": 60,
"inventory": {"no":1},
},
{
"id": "2",
"Name": "Name 2",
"Price": 40,
"inventory": {"no":5},
}
]
console.log(arrow.Array_AvgBy(products,"Price inventory.no"))
output:
{ Price: 50, 'inventory.no': 3 }
Array Max, Min , MaxBy , MinBy
Finds Maximum and Minimum values in the array
const arrow=require('arrow-utils')
let sort_by=[
{name:"siva",skils_percentage:{nodejs:8,reactjs:7}},
{name:"ganesh",skils_percentage:{nodejs:9,reactjs:9}},
{name:"gopi",skils_percentage:{nodejs:7,reactjs:8}},
{name:"lokesh",skils_percentage:{nodejs:7,angularjs:8}},
]
let array=[1,5,3,6,11,7]
console.log(arrow.Array_Min(array))
console.log(arrow.Array_Max(array))
console.log(arrow.Array_MaxBy(sort_by,'skils_percentage.nodejs'))
console.log(arrow.Array_MinBy(sort_by,'skils_percentage.nodejs'))
output:
1
11
{ name: 'ganesh'skils_percentage: { nodejs: 9, reactjs: 9 } }
{ name: 'gopi', skils_percentage: { nodejs: 7, reactjs: 8 } }
Array GroupBy ,Array_GroupBy_With_Pick
Even Groups the nested object in an array
const arrow=require('arrow-utils')
let array_of_obj=[
{
name:"siva",
designation:"Nodejs developer",
project:{
name:"PR12"
}
},
{
name:"ganesh",
designation:"Mean Stack developer",
project:{
name:"PR11"
}
},
]
console.log(arrow.Array_GroupBy(array_of_obj,'project.name'))
console.log(arrow.Array_GroupBy_With_Pick(array_of_obj,'project.name','name project.name'))
output:
// Array_GroupBy
[
{
groupBy: 'PR12',
values: [
{
name: 'siva',
designation: 'Nodejsdeveloper',
project: { name: 'PR12' }
}
]
},{
groupBy: 'PR11',
values: [
{
name: 'ganesh',
designation: 'MeanStackdeveloper',
project: { name: 'PR11'}
}
]
}
]
//Array_GroupBy_With_Pick
[
{
groupBy: 'PR12',
values: [
{
name: 'siva',
'project.name': 'PR12',
'project.code': 'cc12'
}
]
},
{
groupBy: 'PR11',
values: [
{
name: 'ganesh',
'project.name': 'PR11',
'project.code': 'cc11'
}
]
}
]
Array Unique
restricts the repeated data
const arrow=require('arrow-utils')
let unique_array=[3,4,2,3,4,1,3]
console.log(arrow.Array_Unique(unique_array))
output:
[ 3, 4, 2, 1 ]
Array Unique By
restricts the repeated data
const arrow=require('arrow-utils')
let array=[ {id: 1,detail: {name: "Nandha" }},{id: 4,detail: {name: "Nandha" }},{id: 2,detail: {name: "kumar"} }, {id: 1,name: {value: "Siva" } }]
var result = arrowLocal.Array_UniqueBy(array, "detail.name")
output:
[ { id: 1, detail: { name: 'Nandha' } },
{ id: 2, detail: { name: 'kumar' } },
{ id: 1, name: { value: 'Siva' } } ]
Array Of Object Unique
const arrow=require('arrow-utils')
let arr=[
{name:"siva",age:22}
,{name:"siva",age:22}
,{name:"kumar",age:22}
,{name:"kumar",age:23}
]
console.log(arrow.Array_Object_Unique(arr))
output:
[ { name: 'siva', age: 22 },
{ name: 'kumar', age: 22 },
{ name: 'kumar', age: 23 } ]
Find Nearest Value
const arrow=require('arrow-utils')
let numArray = [3,22,4,5,7,112,452,2,8,9,33,11]
let findValue = 6
let type = "MAX" // MIN or MAX
console.log("near value ",arrow.Array_FindNearestValue(numArray, findValue, type) )
output:
near value 7
Find Nearest Object
const arrow=require('arrow-utils')
let studentArray = [
{name: "Nandha",age: 23},
{name: "Gopi",age: 21},
{name: "Ganesh",age: 19},
{name: "Ajay",age: 28},
{name: "Mohan",age: 26},
]
let age = 22
let type = "MAX" // MIN or MAX
console.log( "near obj ", arrow.Array_FindNearestObject(studentArray, "age", age, type) )
output:
near obj { name: 'Gopi', age: 21 }
Array Pick
Choose the exact value
const arrow=require('arrow-utils')
let array_of_obj=[
{
name:"siva",
project:{name:"CM11"},
skil:"nodejs developer"
},
{
name:"gopi",
project:{name:"CM11"},
skil:"nodejs developer"
},
{
name:"ganesh",
project:{name:"CM12"},
skil:"react developer"
},
{
name:"ajay",
project:{name:"CM12"},
skil:"react developer"
},
{
name:"lokesh",
project:{name:"CM12"},
skil:"angular developer"
},
]
console.log(arrow.Array_Pick(array_of_obj,"name project.name"))
output:
[ { name: 'siva', 'project.name': 'CM11' },
{ name: 'gopi', 'project.name': 'CM11' },
{ name: 'ganesh', 'project.name': 'CM12' },
{ name: 'ajay', 'project.name': 'CM12' },
{ name: 'lokesh', 'project.name': 'CM12' }
]
Array Filters ,FindOne,Pick values
apply filter to array and findone object from array
const arrow=require('arrow-utils')
let array_of_obj=[
{
name:"siva",
project:{name:"CM13",period:"6 Month"},
},
{
name:"gopi",
project:{name:"CM12",period:"7 Month"},
},
{
name:"gopi",
project:{name:"CM12",period:"8 Month"},
},
{
name:"ganesh",
project:{name:"CM13",period:"6 Month"},
},
{
name:"ajay",
project:{name:"CM13",period:"7 Month"},
},
]
console.log(arrow.Array_Filters(array_of_obj,{"project.name":"CM12"}))
console.log(arrow.Array_FindOne(array_of_obj,{name:"gopi", "project.name":"CM12","project.period":"6 Month"}))
console.log(arrow.Array_Filters_With_Pick(array_of_obj,{name:"gopi", "project.name":"CM12"}'skils_percentage.nodejs'))
output:
Array Filters [ { name: 'gopi', project: { name: 'CM12', period: '7 Month' } },
{ name: 'gopi', project: { name: 'CM12', period: '8 Month' } } ]
Array FindOne { name: 'gopi', project: { name: 'CM12', period: '7 Month' } }
Array Filters with pick [ { 'skils_percentage.nodejs': 7 },
{ 'skils_percentage.nodejs': 7 } ]
Array_UpdateOne And Array_UpdateMany
const arrow=require('arrow-utils')
let array_of_obj1=[
{
name:"siva",
project:{name:"CM13",period:"6 Month"},
},
{
name:"gopi",
project:{name:"CM12",period:"7 Month"},
},
{
name:"ajay",
project:{name:"CM12",period:"8 Month"},
},
{
name:"ganesh",
project:{name:"CM13",period:"6 Month"},
}
]
console.log(arrow.Array_UpdateOne(array_of_obj1,{"project.name":"CM12"},{"status":"completed"}))
console.log(arrow.Array_UpdateMany(array_of_obj1,{"project.name":"CM12"},{"status":"completed"}))
output:
// UpdateOne [ { name: 'siva', project: { name: 'CM13', period: '6 Month' } },
{ name: 'gopi',
project: { name: 'CM12', period: '7 Month' },
status: 'completed' },
{ name: 'ajay', project: { name: 'CM12', period: '8 Month' } },
{ name: 'ganesh', project: { name: 'CM13', period: '6 Month' } }]
//UpdateMany [ { name: 'siva', project: { name: 'CM13', period: '6 Month' } },
{ name: 'gopi',
project: { name: 'CM12', period: '7 Month' },
status: 'completed' },
{ name: 'ajay',
project: { name: 'CM12', period: '8 Month' },
status: 'completed' },
{ name: 'ganesh', project: { name: 'CM13', period: '6 Month' } },
]
Array_Remove
const arrow=require('arrow-utils')
let array=[{name:"siva"},{name:"gopi"},{name:"kumar"},{name:"ganesh"},{name:"kumar"}]
console.log(arrow.Array_Remove(array5,{name:"kumar"}))
output:
[ { name: 'siva' },
{ name: 'gopi' },
{ name: 'ganesh' },
{ name: 'kumar' } ]
Array_Remove_Many
const arrow=require('arrow-utils')
let array=[{name:"siva"},{name:"gopi"},{name:"kumar"},{name:"ganesh"},{name:"kumar"}]
console.log(arrow.Array_Remove_Many(array5,{name:"kumar"}))
output:
[ { name: 'siva' }, { name: 'gopi' }, { name: 'ganesh' } ]
Array_Check
Check each object value with specific condition return true if all the object value satisfy condition
const arrow=require('arrow-utils')
let array_for_check=[
{sensor:{status:"active",version:1.0}},
{sensor:{status:"active",version:2.0}},
{sensor:{status:"active",version:3.0}},
{sensor:{status:"active",version:4.0}}
]
console.log(arrow.Array_Check(array_for_check,'sensor.status',"active"))
output:
true
Array_Copy
const arrow=require('arrow-utils')
let array_for_check=[
{sensor:{status:"active",version:1.0}},
{sensor:{status:"active",version:2.0}},
{sensor:{status:"active",version:3.0}},
{sensor:{status:"active",version:4.0}}
]
let new_array_for_check=arrow.Array_Copy(array_for_check)
console.log(new_array_for_check)
output:
[
{sensor:{status:"active",version:1.0}},
{sensor:{status:"active",version:2.0}},
{sensor:{status:"active",version:3.0}},
{sensor:{status:"active",version:4.0}}
]
Array_Validate
const arrow=require('arrow-utils')
console.log(arrow.Array_Validate([undefined,3,null,false]))
output:
[3]
Array_Intersect , Array_Union
const arrow=require('arrow-utils')
let arr=[2,3,4,5,7]
let arr2=[3,7,5]
console.log(arrow.Array_Intersect(arr,arr2))
console.log(arrow.Array_Union(arr,arr2))
output:
[ 3, 5, 7 ] //Intersect
[ 2, 3, 4, 5, 7, 3, 7, 5 ] //Union
Array_Object_Creation
const arrow=require('arrow-utils')
let array=[["siva","ganesh","gopi"],["E11","E12","E13"],["PR11","PR11","PR12"]]
let obj_keys=["name","EmpID", "ProjectID"]
console.log(arrow.Array_Object_Creation(array,obj_keys))
output:
[ { name: 'siva', EmpID: 'E11', ProjectID: 'PR11' },
{ name: 'ganesh', EmpID: 'E12', ProjectID: 'PR11' },
{ name: 'gopi', EmpID: 'E13', ProjectID: 'PR12' } ]
Object forEach
const arrow=require('arrow-utils')
var products={ name:"product_one", cost:100, dealer:"A.M.S"}
arrow.Object_forEach(products).forEach((each)=>{
console.log("key ",each.key)
console.log("value ",each.value)
})
output:
key name
value product_one
key cost
value 100
key dealer
value A.M.S
Object is Empty
Checks whether the object is Empty or not
const arrow=require('arrow-utils')
let obj={}
console.log(arrow.Object_IsEmpty(obj))
output:
true
Object Validate
Validate the Object keys are undefined,null,NaN..
const arrow=require('arrow-utils')
let obj={}
obj.name="siva";
obj.password=null;
obj.age=undefined;
console.log(arrow.Object_Check_Values(obj,[null,undefined,NaN]))
output:
[ { password: null }, { age: undefined } ]
Object Merge
const arrow=require('arrow-utils')
var sourceObject = {dealer:"A.M.S" ,stock_details:{available:5}}
var product_one = {name:"product_one",cost:100}
var product_one = {name:"product_two",cost:200}
console.log(arrow.Object_Merge("dealer stock_details.available",sourceObject,[product_one,product_one]))
output:
[ { name: 'product_two',
cost: 200,
dealer: 'A.M.S',
'stock_details.available': 5 },
{ name: 'product_two',
cost: 200,
dealer: 'A.M.S',
'stock_details.available': 5 } ]
Object Equality
Compare Two Objects
const arrow=require('arrow-utils')
let objOne={name:"sensorOne",working_status:{temp:"23c"}}
let objTwo={name:"sensorOne",working_status:{temp:"23c"}}
console.log(arrow.Object_isEqual(objOne,objTwo))
output:
true
Object Copy
const arrow=require('arrow-utils')
let objOne={name:"sensorOne",working_status:{temp:"23c"}}
console.log(arrow.Object_Copy(objOne))
output:
{ name: 'sensorOne', working_status: { temp: '23c' } }
Object Pick
var products={
name:"product_one",
stock_details:{
available:5,
sold:5,
total:10,
},
cost:100,
dealer:"A.M.S"
}
console.log(arrow.Object_Pick(products,'stock_details.sold cost dealer'))
output:
{ 'stock_details.sold': '5', cost: '100', dealer: 'A.M.S' }
Object Remove
const arrow=require('arrow-utils')
let objOne={name:"sensorOne",working_status:{temp:"23c"}}
console.log(arrow.Object_Remove(objOne,"name"))
output:
{ working_status: { temp: '23c' } }
Get Percentage && value from percentage
const arrow=require('arrow-utils')
var percentage = arrow.getPercentage(14380,100000);
console.log("percentage : ",percentage)
var value = arrow.getValue(percentage,100000)
console.log("value : ",value)
output:
percentage : 14.38
value : 14380