UNPKG

arrow-utils

Version:

arrow-utils library binds array and object methods.Complex Array and Object operations are made simple

1,062 lines (854 loc) 21 kB
# arrow-utils ``` arrow-utils library its a collection of array and object methods. Complex array and object operations are made simple. ``` # install With [npm](https://npmjs.org) do: ``` to install arrow-utils npm install arrow-utils to install arrow-utils globally npm install -g arrow-utils ``` ##### Utilities - HTTP Methods [here 🔗](#http-methods) - Pipeline [here 🔗](#pipeline-function-chaining) - Set Environment Configuration [here 🔗](#set-nodejs-environment-variable) - Otp Generator [here 🔗](#generate-otp) - Get Date Difference [here 🔗](#differences-of-two-dates) - Encryption and Decryption [here 🔗](#encryption-and-decryption) - Password Strength [here 🔗](#password-strength) - Generate Token [here 🔗](#generate-token) ##### Array Methods - head, tail [here 🔗](#head-and-tail) - sort, sortBy [here 🔗](#array-sort) - sum, sumBy [here 🔗](#array-sum) - avg, avgBy [here 🔗](#array-avg) - max, maxBy [here 🔗](#array-max-min-maxby-minby) - min, minBy [here 🔗](#array-max-min-maxby-minby) - groupBy, groupByPick [here 🔗](#array-groupby-groupbypick) - unique, uniqueBy, uniqueObject [here 🔗](#array-unique) - pick [here 🔗](#array-pick) - findOne, filter, filerPick [here 🔗](#array-filters-findone) - updateOne, updateMany [here 🔗](#array-updateone-and-updatemany) - removeOne, removeMany [here 🔗](#array-remove-and-removemany) - check [here 🔗](#array-check) - validate [here 🔗](#array-validate) - intersect, union, concat [here 🔗](#array-intersect-union-concat) - toObject [here 🔗](#array-toobject) - copy [here 🔗](#array-copy) - flat [here 🔗](#head-tail) ##### Object Methods - forEach [here 🔗](#object-foreach) - isEmpty [here 🔗](#object-isempty) - checkValues [here 🔗](#object-validate) - merge [here 🔗](#object-merge) - isEqual [here 🔗](#object-equality) - copy [here 🔗](#object-copy) - pick [here 🔗](#object-pick) - remove [here 🔗](#object-remove) ##### Math Methods - getPercentage, getValue [here 🔗](#get-percentage-and-value-from-percentage) ##### HTTP Methods ```js const arrow = require("arrow-utils"); var header = {}; arrow.get("http://domain-name.com/api", header).then((response) => { console.log(response); }); ``` ```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) ```js 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 ``` ```js 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 ```js 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 ```js 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 ```js const arrow = require("arrow-utils"); console.log(arrow.passwordStrength("password123")); ``` output: ``` Average ``` ##### Generate Token ```js const arrow = require("arrow-utils"); console.log(arrow.generateToken()); ``` output: ``` f243ccfd44978501a70620b4ad994cfc306e17d9 ``` ##### Differences of two dates ```js 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 ```js 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 ```js 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 ```js 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 ```js const { arrayMethods } = require("arrow-utils"); let array = [1, 5, 3, 6, 11, 7]; console.log(arrayMethods.sum(array)); ``` output: ``` 33 ``` ##### Array SumBy ```js 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 ```js const { arrayMethods } = require("arrow-utils"); let array = [4, 3, 4]; console.log(arrayMethods.avg(array)); ``` output: ``` 3.6666666666666665 ``` ##### Array AvgBy ```js 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 ```js 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 ```js 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 ```js 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 ```js 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 ```js 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 ```js 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 ```js 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 ```js 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 ```js 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 ```js 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 ```js const { arrayMethods } = require("arrow-utils"); console.log(arrayMethods.validate([undefined, 3, null, false])); ``` output: ``` [3] ``` ##### Array Intersect, Union, Concat ```js 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 ```js 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 ```js 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 ```js 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 ```js 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 ```js const { objectMethods } = require("arrow-utils"); let obj = {}; console.log(objectMethods.isEmpty(obj)); ``` output: ``` true ``` ##### Object Validate ```js 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 ```js 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 ```js 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 ```js 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 ```js 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 ```js 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 ```js 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 ```