@f5devcentral/atg-shared-utilities-dev
Advanced tools
Comparing version 0.2.16 to 0.2.17
@@ -15,2 +15,6 @@ # Changelog | ||
## [0.2.17] = 2024-01-19 | ||
### Changed | ||
- checkAndDelete now accepts and returns objects as well as arrays | ||
## [0.2.16] = 2023-11-14 | ||
@@ -17,0 +21,0 @@ ### Changed |
/** | ||
* Copyright 2023 F5, Inc. | ||
* Copyright 2024 F5, Inc. | ||
* | ||
@@ -4,0 +4,0 @@ * Licensed under the Apache License, Version 2.0 (the "License"); |
{ | ||
"name": "@f5devcentral/atg-shared-utilities-dev", | ||
"version": "0.2.16", | ||
"version": "0.2.17", | ||
"scripts": { | ||
@@ -10,4 +10,4 @@ "lint": "eslint .", | ||
"devDependencies": { | ||
"@f5devcentral/eslint-config-f5-atg": "latest", | ||
"eslint": "^8.53.0", | ||
"@f5devcentral/eslint-config-f5-atg": "^0.1.8", | ||
"eslint": "^8.56.0", | ||
"mocha": "^10.2.0", | ||
@@ -14,0 +14,0 @@ "nyc": "^15.1.0" |
@@ -15,8 +15,21 @@ # atg-shared-utilities-dev | ||
### property(objectArray, propertyToCheck, expectedPropertyType, options) | ||
### checkAndDeleteProperty(objectToCheck, propertyToCheck, expectedPropertyType, options) | ||
This takes in an array of objects, checks each object for the property, checks the property's type against the expected value, and then deletes the property from the object. | ||
This function is primarily useful in integration and unit testing. Where some of the values returned are randomized. This allows for the randomness to happen then it validates the return value is both present and of the expected type. Then by removing it, testing can then do a deep comparison of the expected values. | ||
Note: propertyToCheck is handled as a literal, and so paths do not work. | ||
Example properties: automatically generated ids, runtimes, and dates. | ||
This takes in an object or an array of objects, checks each object for the property, checks the property's type against the expected value, and then deletes the property from the object. | ||
This does not check nested objects for the "propertyToCheck". | ||
This throws an error if the propertyToCheck is not found, or the typeof the property does not match the expectedPropertyType. | ||
- objectToCheck <Object>: The object that is being checked. | ||
- propertyToCheck <String>: The name of the property to be checked. | ||
- expectedPropertyType <String>: The type the property is expected to equal. | ||
- options <Object>: An object which holds options that changes how this function behaves (see below). | ||
WARNING: At the moment, due to the copying process in this function, objects with "undefined" property values will not appear in the returned object. | ||
#### options | ||
@@ -26,2 +39,4 @@ | ||
- skipUndefinedProperties <String>: Options to loosen the error checking if a property is not always available in every object in the array. | ||
#### options.skipUndefinedProperties | ||
@@ -31,5 +46,9 @@ | ||
- "MIN0": Skips check if an object[propertyToCheck] is undefined. Formerly the `isMissingProp`. | ||
- "MIN1": Skips check if an object[propertyToCheck] is undefined, but Errors if ALL object[propertyToCheck] are undefined. | ||
- "MIN0": Skips check if objectToCheck[propertyToCheck] is undefined. Formerly the `isMissingProp`. | ||
- "MIN1": Skips check if objectToCheck[propertyToCheck] is undefined, but Errors if ALL object[propertyToCheck] are undefined. | ||
#### examples | ||
const result = checkAndDeleteProperty({ foo: 'bar' }, 'foo', 'string'); // removes foo from the object | ||
const resultArray = checkAndDeleteProperty([{ foo: 'bar' }, { foo: 'bar' }], 'foo', 'string'); // removes foo from both objects | ||
## SCRIPTS | ||
@@ -36,0 +55,0 @@ |
/** | ||
* Copyright 2023 F5, Inc. | ||
* Copyright 2024 F5, Inc. | ||
* | ||
@@ -4,0 +4,0 @@ * Licensed under the Apache License, Version 2.0 (the "License"); |
/** | ||
* Copyright 2023 F5, Inc. | ||
* Copyright 2024 F5, Inc. | ||
* | ||
@@ -24,17 +24,25 @@ * Licensed under the Apache License, Version 2.0 (the "License"); | ||
* | ||
* @param {array <object>} objectArray Array of JSON objects with properties to test | ||
* @param {object or array <object>} objectOrArray Object or Array of Objects with properties to test | ||
* @param {string} propertyToCheck The name of the property to be tested and deleted | ||
* @param {string} expectedPropertyType The expected typeof value for the property to test | ||
* @param {object} options OPTIONAL: An object of possible options | ||
* @param {string} options.skipUndefinedProperty This option accepts enumerated strings to handle | ||
* @param {string} options.skipUndefinedProperties This option accepts enumerated strings to handle | ||
* possible undefined properties in the array. | ||
* Accepts "MIN0" and "MIN1" see README.md | ||
* | ||
* @returns {array <object>} This function returns a DEEP copy of the | ||
* objectArray or an empty array, if objectArray is | ||
* @returns {object array <object>} This function returns a DEEP copy of the | ||
* objectOrArray or an empty array, if objectOrArray is | ||
* undefined. | ||
*/ | ||
const checkAndDeleteProperty = function (objectArray, propertyToCheck, expectedPropertyType, options) { | ||
const objArrCopy = JSON.parse(JSON.stringify(objectArray || [])); | ||
const checkAndDeleteProperty = function (objectOrArray, propertyToCheck, expectedPropertyType, options) { | ||
let objArrCopy; | ||
let isArray = true; | ||
if (objectOrArray && !Array.isArray(objectOrArray)) { | ||
objArrCopy = [JSON.parse(JSON.stringify(objectOrArray))]; | ||
isArray = false; | ||
} else { | ||
objArrCopy = JSON.parse(JSON.stringify(objectOrArray || [])); | ||
} | ||
options = options || {}; | ||
@@ -66,3 +74,6 @@ let deletes = 0; | ||
} | ||
return objArrCopy; | ||
if (isArray) { | ||
return objArrCopy; | ||
} | ||
return objArrCopy[0]; | ||
}; | ||
@@ -69,0 +80,0 @@ |
/** | ||
* Copyright 2023 F5, Inc. | ||
* Copyright 2024 F5, Inc. | ||
* | ||
@@ -25,2 +25,3 @@ * Licensed under the Apache License, Version 2.0 (the "License"); | ||
let testArray; | ||
let testObject; | ||
@@ -47,3 +48,15 @@ beforeEach(() => { | ||
]; | ||
testObject = { | ||
funky: 'monkey', | ||
chunky: { | ||
hunky: 'bunky' | ||
}, | ||
apples: 10, | ||
klunky: { | ||
trunky: 13 | ||
} | ||
}; | ||
}); | ||
it('should an empty array should return an empty array', () => { | ||
@@ -59,2 +72,51 @@ const result = checkAndDeleteProperty([]); | ||
it('should accept and return an object if one is provided', () => { | ||
let result = checkAndDeleteProperty(testObject, 'funky', 'string'); | ||
assert.deepStrictEqual( | ||
result, | ||
{ | ||
chunky: { | ||
hunky: 'bunky' | ||
}, | ||
apples: 10, | ||
klunky: { | ||
trunky: 13 | ||
} | ||
} | ||
); | ||
result = checkAndDeleteProperty(result, 'klunky', 'object'); | ||
assert.deepStrictEqual( | ||
result, | ||
{ | ||
chunky: { | ||
hunky: 'bunky' | ||
}, | ||
apples: 10 | ||
} | ||
); | ||
result = checkAndDeleteProperty(result, 'apples', 'number'); | ||
assert.deepStrictEqual( | ||
result, | ||
{ | ||
chunky: { | ||
hunky: 'bunky' | ||
} | ||
} | ||
); | ||
// Verify the original was not changed | ||
assert.deepStrictEqual( | ||
testObject, | ||
{ | ||
funky: 'monkey', | ||
chunky: { | ||
hunky: 'bunky' | ||
}, | ||
apples: 10, | ||
klunky: { | ||
trunky: 13 | ||
} | ||
} | ||
); | ||
}); | ||
it('should check and delete the array if the property and expected type is valid', () => { | ||
@@ -106,2 +168,31 @@ const result = checkAndDeleteProperty(testArray, 'foo', 'string'); | ||
it('should error if the expected property is a nested property', () => { | ||
assert.throws(() => checkAndDeleteProperty(testArray, 'object', 'object'), | ||
/An object in array lacked: object and options.skipUndefinedProperties was NOT set/); | ||
// Original array should not have been modified | ||
assert.deepStrictEqual( | ||
testArray, | ||
[ | ||
{ | ||
foo: 'bar', | ||
funky: 'monkey' | ||
}, | ||
{ | ||
foo: 'bar', | ||
power: 'full', | ||
powerLevel: 9001 | ||
}, | ||
{ | ||
foo: 'bar', | ||
deep: { | ||
object: { | ||
array: [1, 2, 3, 4] | ||
} | ||
} | ||
} | ||
] | ||
); | ||
}); | ||
it('should error if the expected type does not match the property type', () => { | ||
@@ -221,2 +312,39 @@ assert.throws(() => checkAndDeleteProperty(testArray, 'foo', 'number'), | ||
const results = checkAndDeleteProperty(testObject, 'foo', 'string', options); | ||
// Verify the results were not changed | ||
assert.deepStrictEqual( | ||
results, | ||
{ | ||
funky: 'monkey', | ||
chunky: { | ||
hunky: 'bunky' | ||
}, | ||
apples: 10, | ||
klunky: { | ||
trunky: 13 | ||
} | ||
} | ||
); | ||
// Verify the original was not changed | ||
assert.deepStrictEqual( | ||
testObject, | ||
{ | ||
funky: 'monkey', | ||
chunky: { | ||
hunky: 'bunky' | ||
}, | ||
apples: 10, | ||
klunky: { | ||
trunky: 13 | ||
} | ||
} | ||
); | ||
}); | ||
it('should NOT error if the propertyToCheck is undefined in all array objects and options.skipUndefinedProperties === MIN0', () => { | ||
const options = { | ||
skipUndefinedProperties: 'MIN0' | ||
}; | ||
const results = checkAndDeleteProperty(testArray, 'funky', 'string', options); | ||
@@ -325,2 +453,26 @@ | ||
it('should error if the propertyToCheck is NOT defined in the object provided and options.skipUndefinedProperties === MIN1', () => { | ||
const options = { | ||
skipUndefinedProperties: 'MIN1' | ||
}; | ||
assert.throws(() => checkAndDeleteProperty(testObject, 'notAProperty', 'string', options), | ||
/None of the supplied objects had: notAProperty. The MIN1 option enforces at least 1 expected deletion/); | ||
// Verify the original was not changed | ||
assert.deepStrictEqual( | ||
testObject, | ||
{ | ||
funky: 'monkey', | ||
chunky: { | ||
hunky: 'bunky' | ||
}, | ||
apples: 10, | ||
klunky: { | ||
trunky: 13 | ||
} | ||
} | ||
); | ||
}); | ||
it('should error if the propertyToCheck is NOT defined in at least 1 object and options.skipUndefinedProperties === MIN1', () => { | ||
@@ -327,0 +479,0 @@ const options = { |
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
49191
14
776
81