arrow-utils
arrow-utils library its a collection of 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
Utilities
- HTTP Methods here
🔗 - Pipeline here
🔗 - Set Environment Configuration here
🔗 - Otp Generator here
🔗 - Get Date Difference here
🔗 - Encryption and Decryption here
🔗 - Password Strength here
🔗 - Generate Token here
🔗
Array Methods
- head, tail here
🔗 - sort, sortBy here
🔗 - sum, sumBy here
🔗 - avg, avgBy here
🔗 - max, maxBy here
🔗 - min, minBy here
🔗 - groupBy, groupByPick here
🔗 - unique, uniqueBy, uniqueObject here
🔗 - pick here
🔗 - findOne, filter, filerPick here
🔗 - updateOne, updateMany here
🔗 - removeOne, removeMany here
🔗 - check here
🔗 - validate here
🔗 - intersect, union, concat here
🔗 - toObject here
🔗 - copy here
🔗 - flat here
🔗
Object Methods
- forEach here
🔗 - isEmpty here
🔗 - checkValues here
🔗 - merge here
🔗 - isEqual here
🔗 - copy here
🔗 - pick here
🔗 - remove here
🔗
Math Methods
- getPercentage, getValue here
🔗
HTTP Methods
const arrow = require("arrow-utils");
var header = {};
arrow.get("http://domain-name.com/api", header).then((response) => {
console.log(response);
});
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)
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
loads environment variables to process.env from .env file
steps:
create .env file inside our root directory
-> touch .env
add environment variables in .env file
.env file looks like
PORT = 8080
DB_NAME = arrow
const arrow = require("arrow-utils");
arrow.setEnvConfig();
console.log("PORT : ", process.env.PORT);
console.log("DB_NAME : ", process.env.DB_NAME);
Output:
PORT : 8080
DB_NAME : arrow
Generate OTP
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
Password Strength
const arrow = require("arrow-utils");
console.log(arrow.passwordStrength("password123"));
output:
Average
Generate Token
const arrow = require("arrow-utils");
console.log(arrow.generateToken());
output:
f243ccfd44978501a70620b4ad994cfc306e17d9
Differences of two dates
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
head and 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
const { arrayMethods } = require("arrow-utils");
let array = [1, 5, 3, 6, 11, 7];
console.log(arrayMethods.sort(array));
output:
[ 1, 3, 5, 6, 7, 11 ]
Array SortBy
const { arrayMethods } = 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(arrayMethods.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
const { arrayMethods } = require("arrow-utils");
let array = [1, 5, 3, 6, 11, 7];
console.log(arrayMethods.sum(array));
output:
33
Array SumBy
const { arrayMethods } = 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(arrayMethods.sumBy(products, "price inventory.no"));
// pass last parameter as false to get output key as string
console.log(arrayMethods.sumBy(products, "price inventory.no", false));
output:
{ price: 100, inventory: { no: 6 } }
{ price: 100, 'inventory.no': 6 }
Array Avg
const { arrayMethods } = require("arrow-utils");
let array = [4, 3, 4];
console.log(arrayMethods.avg(array));
output:
3.6666666666666665
Array AvgBy
const { arrayMethods } = 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(arrayMethods.avgBy(products, "price inventory.no"));
output:
{ price: 50, inventory: { no: 3 } }
Array Max, Min, MaxBy, MinBy
const { arrayMethods } = 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: 6, angularjs: 8 } },
];
let array = [1, 5, 3, 6, 11, 7];
console.log(arrayMethods.min(array));
console.log(arrayMethods.max(array));
console.log(arrayMethods.minBy(sort_by, "skils_percentage.nodejs"));
console.log(arrayMethods.maxBy(sort_by, "skils_percentage.nodejs"));
output:
1
11
{ name: 'lokesh', skils_percentage: { nodejs: 6, angularjs: 8 } }
{ name: 'ganesh', skils_percentage: { nodejs: 9, reactjs: 9 } }
Array GroupBy, GroupByPick
const { arrayMethods } = require("arrow-utils");
let array_of_obj = [
{
name: "siva",
designation: "Nodejs developer",
project: {
name: "PR12",
code: "CC01",
},
},
{
name: "ganesh",
designation: "Mean Stack developer",
project: {
name: "PR11",
code: "CC02",
},
},
];
const groupByResult = arrayMethods.groupBy(array_of_obj, "project.name");
console.log(JSON.stringify(groupByResult, null, 2));
const groupByPickResult = arrayMethods.groupBy(
array_of_obj,
"project.name",
"name project.name"
);
console.log(JSON.stringify(groupByPickResult, null, 2));
output:
// groupBy
[
{
"groupBy": "PR12",
"values": [
{
"name": "siva",
"designation": "Nodejs developer",
"project": {
"name": "PR12",
"code": "CC01"
}
}
]
},
{
"groupBy": "PR11",
"values": [
{
"name": "ganesh",
"designation": "Mean Stack developer",
"project": {
"name": "PR11",
"code": "CC02"
}
}
]
}
]
//groupBy with pick
[
{
"groupBy": "PR12",
"values": [
{
"name": "siva",
"project": {
"name": "PR12"
}
}
]
},
{
"groupBy": "PR11",
"values": [
{
"name": "ganesh",
"project": {
"name": "PR11"
}
}
]
}
]
Array Unique
const { arrayMethods } = require("arrow-utils");
const array = [3, 4, 2, 3, 4, 1, 3];
console.log(arrayMethods.unique(array));
output:
[ 3, 4, 2, 1 ]
Array Unique By
const { arrayMethods } = 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 = arrayMethods.uniqueBy(array, "detail.name");
console.log(result);
output:
[
{ id: 1, detail: { name: 'Nandha' } },
{ id: 2, detail: { name: 'kumar' } },
{ id: 1, name: { value: 'Siva' } }
]
Array Of Object Unique
const { arrayMethods } = require("arrow-utils");
let arr = [
{ name: "siva", age: 22 },
{ name: "siva", age: 22 },
{ name: "kumar", age: 22 },
{ name: "kumar", age: 23 },
];
console.log(arrayMethods.uniqueObject(arr));
output:
[
{ name: 'siva', age: 22 },
{ name: 'kumar', age: 22 },
{ name: 'kumar', age: 23 }
]
Array Pick
const { arrayMethods } = require("arrow-utils");
let array_of_obj = [
{
name: "siva",
project: { name: "CM11", code: "C011" },
skil: "nodejs developer",
},
{
name: "gopi",
project: { name: "CM11", code: "C011" },
skil: "nodejs developer",
},
];
console.log(arrayMethods.pick(array_of_obj, "name project.name"));
output:
[
{ name: 'siva', project: { name: 'CM11' } },
{ name: 'gopi', project: { name: 'CM11' } }
]
Array Filter, FindOne
const { arrayMethods } = require("arrow-utils");
let array_of_obj = [
{
name: "siva",
project: { name: "CM13", period: "6 Month" },
},
{
name: "gopi",
project: { name: "CM12", period: "8 Month" },
},
{
name: "ganesh",
project: { name: "CM13", period: "6 Month" },
},
];
console.log(
arrayMethods.findOne(
array_of_obj,
{
name: "gopi",
"project.name": "CM12",
"project.period": "8 Month",
},
"name"
)
);
console.log(arrayMethods.filter(array_of_obj, { "project.name": "CM12" }));
console.log(
arrayMethods.filter(
array_of_obj,
{ name: "gopi", "project.name": "CM12" },
"name project.period project1.period"
)
);
output:
// FindOne { name: 'gopi' }
// Filter[ { name: 'gopi', project: { name: 'CM12', period: '8 Month' } } ]
// Filter with pick [ { name: 'gopi', project: { period: '8 Month' } } ]
Array UpdateOne and UpdateMany
const { arrayMethods } = 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" },
},
];
console.log(
arrayMethods.updateOne(
array_of_obj1,
{ "project.name": "CM12" },
{ "project.status": "completed", isCompleted: true }
)
);
console.log(
arrayMethods.updateMany(
array_of_obj1,
{ "project.name": "CM12" },
{ "project.status": "completed", isCompleted: true }
)
);
output:
// UpdateOne
[
{ name: 'siva', project: { name: 'CM13', period: '6 Month' } },
{
name: 'gopi',
project: { name: 'CM12', period: '7 Month', status: 'completed' },
isCompleted: true
},
{ 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' },
isCompleted: true
},
{
name: 'ajay',
project: { name: 'CM12', period: '8 Month', status: 'completed' },
isCompleted: true
},
{ name: 'ganesh', project: { name: 'CM13', period: '6 Month' } }
]
Array Remove and RemoveMany
const { arrayMethods } = require("arrow-utils");
let array = [
{ name: "siva" },
{ name: "gopi" },
{ name: "kumar" },
{ name: "ganesh" },
{ name: "kumar" },
];
console.log(arrayMethods.removeOne(array, { name: "gopi" }));
console.log(arrayMethods.removeMany(array, { name: "kumar" }));
output:
//removeOne
[
{ name: 'siva' },
{ name: 'kumar' },
{ name: 'ganesh' },
{ name: 'kumar' }
]
//removeMany
[ { name: 'siva' }, { name: 'ganesh' } ]
Array Some / Every
const { arrayMethods } = 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(arrayMethods.some(array_for_check, "sensor.status", "active"));
console.log(arrayMethods.every(array_for_check, "sensor.status", "active"));
output:
true
true
Array Validate
const { arrayMethods } = require("arrow-utils");
console.log(arrayMethods.validate([undefined, 3, null, false]));
output:
[3]
Array Intersect, Union, Concat
const { arrayMethods } = require("arrow-utils");
let arr = [2, 3, 4, 5, 7];
let arr2 = [3, 7, 5];
console.log(arrayMethods.intersect(arr, arr2));
console.log(arrayMethods.union(arr, arr2));
console.log(arrayMethods.concat(arr, arr2));
output:
//Intersect [ 3, 5, 7 ]
//Union [ 2, 3, 4, 5, 7, 3, 7, 5 ]
//Concat [ 2, 3, 4, 5, 7, 3, 7, 5 ]
Array toObject
const { arrayMethods } = require("arrow-utils");
let array = [
["siva", "ganesh", "gopi"],
["E11", "E12", "E13"],
["PR11", "PR11", "PR12"],
];
let obj_keys = ["name", "EmpID", "ProjectID"];
console.log(arrayMethods.toObject(array, obj_keys));
output:
[
{ name: 'siva', EmpID: 'E11', ProjectID: 'PR11' },
{ name: 'ganesh', EmpID: 'E12', ProjectID: 'PR11' },
{ name: 'gopi', EmpID: 'E13', ProjectID: 'PR12' }
]
Array Copy
const { arrayMethods } = 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 = arrayMethods.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 Flat
const { arrayMethods } = require("arrow-utils");
let array = [1, 5, 3, 6, 11, 7, [102, [232, [[[344]]]]]];
console.log(arrayMethods.flat(array));
output:
[
'1', '5', '3',
'6', '11', '7',
'102', '232', '344'
]
Object forEach
const { objectMethods } = require("arrow-utils");
var products = { name: "product_one", cost: 100, dealer: "A.M.S" };
objectMethods.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 isEmpty
const { objectMethods } = require("arrow-utils");
let obj = {};
console.log(objectMethods.isEmpty(obj));
output:
true
Object Validate
const { objectMethods } = require("arrow-utils");
let obj = {};
obj.name = "siva";
obj.password = null;
obj.age = undefined;
console.log(objectMethods.checkValues(obj, [null, undefined, NaN]));
output:
[ { password: null }, { age: undefined } ]
Object Merge
const { objectMethods } = require("arrow-utils");
var sourceObject = { dealer: "A.M.S", stock_details: { available: 5 } };
var product_one = { name: "product_one", cost: 100 };
var product_two = { name: "product_two", cost: 200 };
console.log(
objectMethods.merge("dealer stock_details.available", sourceObject, [
product_one,
product_two,
])
);
output:
[
{
name: 'product_one',
cost: 100,
dealer: 'A.M.S',
stock_details: { available: 5 }
},
{
name: 'product_two',
cost: 200,
dealer: 'A.M.S',
stock_details: { available: 5 }
}
]
Object Equality
const { objectMethods } = require("arrow-utils");
let objOne = { name: "sensorOne", working_status: { temp: "23c" } };
let objTwo = { name: "sensorOne", working_status: { temp: "23c" } };
console.log(objectMethods.isEqual(objOne, objTwo));
output:
true
Object Copy
const { objectMethods } = require("arrow-utils");
let objOne = { name: "sensorOne", working_status: { temp: "23c" } };
console.log(objectMethods.copy(objOne));
output:
{ name: 'sensorOne', working_status: { temp: '23c' } }
Object Pick
const { objectMethods } = require("arrow-utils");
var products = {
name: "product_one",
stock_details: {
available: 5,
sold: 5,
total: 10,
},
cost: 100,
dealer: "A.M.S",
};
console.log(
"result1: ",
JSON.stringify(
objectMethods.pick(products, "stock_details cost dealer"),
null,
2
)
);
console.log(
"result2: ",
JSON.stringify(
objectMethods.pick(products, "stock_details.total cost dealer"),
null,
2
)
);
output:
result1: {
"stock_details": {
"available": 5,
"sold": 5,
"total": 10
},
"cost": 100,
"dealer": "A.M.S"
}
result2: {
"cost": 100,
"dealer": "A.M.S",
"stock_details": {
"total": 10
}
}
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 and value from percentage
const arrow = require("arrow-utils");
var percentage = arrow.getPercentage(14380, 100000);
var value = arrow.getValue(percentage, 100000);
console.log("percentage : ", percentage);
console.log("value : ", value);
output:
percentage : 14.38
value : 14380