@f5devcentral/atg-shared-utilities
Advanced tools
Comparing version 0.7.0 to 0.8.0
{ | ||
"name": "@f5devcentral/atg-shared-utilities", | ||
"version": "0.7.0", | ||
"version": "0.8.0", | ||
"scripts": { | ||
@@ -5,0 +5,0 @@ "lint": "eslint .", |
@@ -71,8 +71,31 @@ # atg-shared-utilities | ||
This function will take a variable, convert it to an array (if it is not an array), and return it. If undefined, it will return `[]`. | ||
Takes a variable, converts it to an array (if it is not an array), and returns it. If undefined, it will return `[]`. | ||
### doesArrayContainAnyOf(array1, array2) | ||
Checks if any member of tArray is present in array. | ||
### doesArrayContain(array, target) | ||
Checks if any member of target is present in array. | ||
### insertAfterOrAtBeginning(array, target, item, comparison) | ||
Inserts an item after another item in an array or at the beginning of the array. | ||
### insertBeforeOrAtBeginning(array, target, item, comparison) | ||
Inserts an item before another item in an array or at the beginning of the array. | ||
### insertAfterOrAtEnd(array, target, item, comparison) | ||
Inserts an item after another item in an array or at the end of the array. | ||
### insertBeforeOrAtEnd(array, target, item, comparison) | ||
Inserts an item before another item in an array or at the end of the array. | ||
## requestUtils | ||
This collection of utils is for sending http requests | ||
This collection of utils is for sending http requests. | ||
@@ -79,0 +102,0 @@ ### send(options, body) |
/** | ||
* Copyright 2021 F5 Networks, Inc. | ||
* Copyright 2023 F5 Networks, Inc. | ||
* | ||
@@ -19,11 +19,158 @@ * Licensed under the Apache License, Version 2.0 (the "License"); | ||
class arrayUtils { | ||
static ensureArray(input) { | ||
if (typeof input === 'undefined') { | ||
return []; | ||
/** | ||
* Takes a variable, converts it to an array (if it is not an array), and returns it. | ||
* | ||
* @param {any} input - The item to be converted to an array | ||
* | ||
* @returns {any[]} - The input inside an array (or just the input if it was already an array). | ||
*/ | ||
const ensureArray = function (input) { | ||
if (typeof input === 'undefined') { | ||
return []; | ||
} | ||
return (Array.isArray(input)) ? input : [input]; | ||
}; | ||
/** | ||
* Checks if any member of tArray is present in array | ||
* | ||
* @public | ||
* @param {array} arr1 - check base array | ||
* @param {array} arr2 - the target array to compare with | ||
* @returns {boolean} - true any of arr2 is in arr1, else false | ||
*/ | ||
const doesArrayContainAnyOf = function (arr1, arr2) { | ||
if (typeof arr1 === 'undefined' || typeof arr2 === 'undefined') { | ||
return false; | ||
} | ||
return arr1.some((r) => arr2.indexOf(r) >= 0); | ||
}; | ||
/** | ||
* Checks if any member of tArray is present in array | ||
* | ||
* @public | ||
* @param {array} array - check base array | ||
* @param {value} target - the target value of any type | ||
* @returns {boolean} - true if target is in the array, else false | ||
*/ | ||
const doesArrayContain = function (array, target) { | ||
if (typeof array === 'undefined') { | ||
return false; | ||
} | ||
return array.indexOf(target) !== -1; | ||
}; | ||
const getIndex = function (array, target, comparison) { | ||
let targetIndex; | ||
if (comparison === 'eq') { | ||
targetIndex = array.findIndex((element) => element === target); | ||
} else { | ||
targetIndex = array.findIndex((element) => element.indexOf(target) > -1); | ||
} | ||
return targetIndex; | ||
}; | ||
const getLastIndex = function (array, target, comparison) { | ||
let targetIndex; | ||
for (targetIndex = array.length - 1; targetIndex >= 0; targetIndex -= 1) { | ||
if (comparison === 'eq') { | ||
if (array[targetIndex] === target) { | ||
break; | ||
} | ||
} else if (array[targetIndex].indexOf(target) > -1) { | ||
break; | ||
} | ||
return (Array.isArray(input)) ? input : [input]; | ||
} | ||
} | ||
return targetIndex; | ||
}; | ||
module.exports = arrayUtils; | ||
/** | ||
* Inserts an item after another item in an array or at the beginning of the array. | ||
* | ||
* @param {array} array - The array into which to insert | ||
* @param {string} target - The string to search for | ||
* @param {string} item - The item to insert | ||
* @param {string} comparison - The type of comparison to perform. | ||
* 'eq' to do an '===' comparison | ||
* 'inc' to see if the item is contained in an element of the array | ||
*/ | ||
const insertAfterOrAtBeginning = function (array, target, item, comparison) { | ||
const targetIndex = getLastIndex(array, target, comparison); | ||
if (targetIndex > -1) { | ||
array.splice(targetIndex + 1, 0, item); | ||
} else { | ||
array.unshift(item); | ||
} | ||
}; | ||
/** | ||
* Inserts an item before another item in an array or at the beginning of the array. | ||
* | ||
* @param {array} array - The array into which to insert | ||
* @param {string} target - The string to search for | ||
* @param {string} item - The item to insert | ||
* @param {string} comparison - The type of comparison to perform. | ||
* 'eq' to do an '===' comparison | ||
* 'inc' to see if the item is contained in an element of the array | ||
*/ | ||
const insertBeforeOrAtBeginning = function (array, target, item, comparison) { | ||
const targetIndex = getIndex(array, target, comparison); | ||
if (targetIndex > -1) { | ||
array.splice(targetIndex, 0, item); | ||
} else { | ||
array.unshift(item); | ||
} | ||
}; | ||
/** | ||
* Inserts an item after another item in an array or at the end of the array. | ||
* | ||
* @param {array} array - The array into which to insert | ||
* @param {string} target - The string to search for | ||
* @param {string} item - The item to insert | ||
* @param {string} comparison - The type of comparison to perform. | ||
* 'eq' to do an '===' comparison | ||
* 'inc' to see if the item is contained in an element of the array | ||
*/ | ||
const insertAfterOrAtEnd = function (array, target, item, comparison) { | ||
const targetIndex = getLastIndex(array, target, comparison); | ||
if (targetIndex > -1) { | ||
array.splice(targetIndex + 1, 0, item); | ||
} else { | ||
array.push(item); | ||
} | ||
}; | ||
/** | ||
* Inserts an item before another item in an array or at the end of the array. | ||
* | ||
* @param {array} array - The array into which to insert | ||
* @param {string} target - The string to search for | ||
* @param {string} item - The item to insert | ||
* @param {string} comparison - The type of comparison to perform. | ||
* 'eq' to do an '===' comparison | ||
* 'inc' to see if the item is contained in an element of the array | ||
*/ | ||
const insertBeforeOrAtEnd = function (array, target, item, comparison) { | ||
const targetIndex = getIndex(array, target, comparison); | ||
if (targetIndex > -1) { | ||
array.splice(targetIndex, 0, item); | ||
} else { | ||
array.push(item); | ||
} | ||
}; | ||
module.exports = { | ||
ensureArray, | ||
doesArrayContainAnyOf, | ||
doesArrayContain, | ||
insertAfterOrAtBeginning, | ||
insertAfterOrAtEnd, | ||
insertBeforeOrAtBeginning, | ||
insertBeforeOrAtEnd | ||
}; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
78713
12
1507
178