@mikezimm/npmfunctions
Version:
Functions used in my SPFx webparts
359 lines (352 loc) • 18.3 kB
JavaScript
;
/***
* .d8b. d8888b. d8888b. .d8b. db db .d8888.
* d8' `8b 88 `8D 88 `8D d8' `8b `8b d8' 88' YP
* 88ooo88 88oobY' 88oobY' 88ooo88 `8bd8' `8bo.
* 88~~~88 88`8b 88`8b 88~~~88 88 `Y8b.
* 88 88 88 `88. 88 `88. 88 88 88 db 8D
* YP YP 88 YD 88 YD YP YP YP `8888Y'
*
*
//Checks
import { doesObjectExistInArrayInt, doesObjectExistInArray, compareArrays, getMaxPropOfKeyInObjectArray, getNextElementInArray
getKeySummary, getKeyChanges
} from '@mikezimm/npmfunctions/dist/Services/Arrays/checks';
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.getKeyChanges = exports.KeyChangeDelimiter = exports.DoesNotExistLabel = exports.BaseTypeKeys = exports.BaseViewKeys = exports.BaseFieldKeys = exports.getKeySummary = exports.compareArrays = exports.doesObjectExistInArray = exports.doesObjectExistInArrayInt = exports.indexOfAnyCase = void 0;
/**
* export interface ICompareResult
* export interface
* export interface
*
* export function doesObjectExistInArrayInt
* export function doesObjectExistInArray
* export function compareArrays
* export function getMaxPropOfKeyInObjectArray
* export function getNextElementInArray
*
* export function getKeySummary
* export function getKeyChanges
*/
var services_1 = require("./services");
var ErrorHandler_1 = require("../Logging/ErrorHandler");
/***
* d888888b d8b db d8888b. d88888b db db .d88b. d88888b .d8b. d8b db db db .o88b. .d8b. .d8888. d88888b
* `88' 888o 88 88 `8D 88' `8b d8' .8P Y8. 88' d8' `8b 888o 88 `8b d8' d8P Y8 d8' `8b 88' YP 88'
* 88 88V8o 88 88 88 88ooooo `8bd8' 88 88 88ooo 88ooo88 88V8o 88 `8bd8' 8P 88ooo88 `8bo. 88ooooo
* 88 88 V8o88 88 88 88~~~~~ .dPYb. 88 88 88~~~ 88~~~88 88 V8o88 88 8b 88~~~88 `Y8b. 88~~~~~
* .88. 88 V888 88 .8D 88. .8P Y8. `8b d8' 88 88 88 88 V888 88 Y8b d8 88 88 db 8D 88.
* Y888888P VP V8P Y8888D' Y88888P YP YP `Y88P' YP YP YP VP V8P YP `Y88P' YP YP `8888Y' Y88888P
*
*
*/
/**
* Based on: https://bobbyhadz.com/blog/javascript-array-contains-string-case-insensitive
* Any non-string checks will be considered 'not a match'
* @param checkMe
* @param inArray
*/
function indexOfAnyCase(checkMe, inArray, consoleLog, alertMe) {
var result = -1;
//result = inArray.findIndex( element => { //NOTE: findIndex does not work in my code (typescript error)
inArray.map(function (element, index) {
try {
if (element.toLowerCase() === checkMe.toLowerCase()) {
result = index;
}
}
catch (e) {
//Sending false, false to getHelpfullErrorV2 because I'm handling that here
var errMessage = (0, ErrorHandler_1.getHelpfullErrorV2)(e, alertMe, consoleLog, 'indexOfAnyCase ~ 65');
}
});
return result;
}
exports.indexOfAnyCase = indexOfAnyCase;
/***
* d8888b. .d88b. d88888b .d8888. .d88b. d8888b. d88b d88888b .o88b. d888888b d88888b db db d888888b .d8888. d888888b d888888b d8b db .d8b. d8888b. d8888b. .d8b. db db
* 88 `8D .8P Y8. 88' 88' YP .8P Y8. 88 `8D `8P' 88' d8P Y8 `~~88~~' 88' `8b d8' `88' 88' YP `~~88~~' `88' 888o 88 d8' `8b 88 `8D 88 `8D d8' `8b `8b d8'
* 88 88 88 88 88ooooo `8bo. 88 88 88oooY' 88 88ooooo 8P 88 88ooooo `8bd8' 88 `8bo. 88 88 88V8o 88 88ooo88 88oobY' 88oobY' 88ooo88 `8bd8'
* 88 88 88 88 88~~~~~ `Y8b. 88 88 88~~~b. 88 88~~~~~ 8b 88 88~~~~~ .dPYb. 88 `Y8b. 88 88 88 V8o88 88~~~88 88`8b 88`8b 88~~~88 88
* 88 .8D `8b d8' 88. db 8D `8b d8' 88 8D db. 88 88. Y8b d8 88 88. .8P Y8. .88. db 8D 88 .88. 88 V888 88 88 88 `88. 88 `88. 88 88 88
* Y8888D' `Y88P' Y88888P `8888Y' `Y88P' Y8888P' Y8888P Y88888P `Y88P' YP Y88888P YP YP Y888888P `8888Y' YP Y888888P VP V8P YP YP 88 YD 88 YD YP YP YP
*
*
*/
/**
* This function checks to see if an element of an array (object) contains a specific property/value pair.
*
* example call: if ( doesObjectExistInArray(currentFields, 'StaticName', checkField ) ) {
* This takes an array of field objects (currentFields), and looks to see if any of the objects has a key of StaticName which has a value of checkField variable.
*
* @param sourceArray
* @param objectProperty
* @param propValue
*/
function doesObjectExistInArrayInt(sourceArray, objectProperty, propValue, exact) {
if (exact === void 0) { exact = true; }
var result = doesObjectExistInArray(sourceArray, objectProperty, propValue, exact);
if (result === false) {
return -1;
}
else {
return parseInt(result);
}
}
exports.doesObjectExistInArrayInt = doesObjectExistInArrayInt;
function doesObjectExistInArray(sourceArray, objectProperty, propValue, exact) {
if (exact === void 0) { exact = true; }
var result = false;
for (var i in sourceArray) {
var test = false;
if (exact === true) { //2020-10-07: Added this to allow for Id string to number checks
test = sourceArray[i][objectProperty] === propValue ? true : false;
}
else {
test = sourceArray[i][objectProperty] == propValue ? true : false;
}
if (test) {
result = i;
break;
}
}
return result;
}
exports.doesObjectExistInArray = doesObjectExistInArray;
/***
* .o88b. .d88b. .88b d88. d8888b. .d8b. d8888b. d88888b .d8b. d8888b. d8888b. .d8b. db db .d8888.
* d8P Y8 .8P Y8. 88'YbdP`88 88 `8D d8' `8b 88 `8D 88' d8' `8b 88 `8D 88 `8D d8' `8b `8b d8' 88' YP
* 8P 88 88 88 88 88 88oodD' 88ooo88 88oobY' 88ooooo 88ooo88 88oobY' 88oobY' 88ooo88 `8bd8' `8bo.
* 8b 88 88 88 88 88 88~~~ 88~~~88 88`8b 88~~~~~ 88~~~88 88`8b 88`8b 88~~~88 88 `Y8b.
* Y8b d8 `8b d8' 88 88 88 88 88 88 88 `88. 88. 88 88 88 `88. 88 `88. 88 88 88 db 8D
* `Y88P' `Y88P' YP YP YP 88 YP YP 88 YD Y88888P YP YP 88 YD 88 YD YP YP YP `8888Y'
*
*
* The original goal of this function, would be to remove objects from one array if it were in another array.
* As an example, I have an array of items I want to add to a list (addItemsArray)
* Then I run a process which creates another 'result' array of what things were actually added - minus any errors
* The function will remove the items in the 'result' array from the 'addItemsArray.
* Only the items that were not added (ie the ones that errored out) will be left... or maybe it would add a key with the result.
*
*
* @param checkForTheseItems - this is the array of items you want to check for in the sourceArray ('inThisArray')
* @param inThisArray - this is the array where you are looking for items in
* @param method - this tells what to do... either flage items in 'inThisArray' with found/not found, or remove the found ones
* @param keyToCheck - checkForTheseItems must have a key which has this syntax: checkValue: "Title===Training"
* keyToCheck would === 'checkValue' and the value for that key must have the syntax: PropertyKey===ValueOfProperty;
* In the example above, it will split Title===Training into ['Title','Training']
* Then look for all items in 'inThisArray' which have the value 'Training' in the key 'Title', and apply the method you want to apply.
*/
function compareArrays(checkForTheseItems, inThisArray, method, keyToCheck, checkDelimiter, messsages) {
var compareKey = 'compareArrays';
var foundTag = 'Found';
var notFoundTag = 'Not' + foundTag;
var result = {
checkForTheseItems: checkForTheseItems,
inThisArray: inThisArray,
found: [],
notFound: [],
result: [],
message: '',
};
var foundCount = 0;
var notFoundCount = 0;
var notFoundItems = '';
//Loop through all the objects you want to check for
for (var c in checkForTheseItems) {
var foundThisCheck = false;
//Expecting syntax "Title===Email triage"
var splitStr = checkForTheseItems[c][keyToCheck];
if (splitStr) { //Only check if this has a value for keyToCheck
var splitArr = splitStr.split(checkDelimiter);
var testKey = splitArr[0];
var testVal = splitArr[1];
if (splitArr.length !== 2) {
//There was a problem with the test value... needs to be syntax like this: "Title===Email triage"
notFoundItems += '\n???: ' + splitStr;
}
else {
//Loop through all the objects in the 'inThisArray' and process them
for (var i in inThisArray) {
var objectToUpdate = inThisArray[i];
if (inThisArray[i][testKey] === testVal) {
//Value was found.... do whatever needs to be done.
objectToUpdate[compareKey] = foundTag;
/*
if ( method === 'AddTag') { //Add item to result and then add keyTag to it
objectToUpdate[compareKey] = foundTag;
} else if ( method === 'ReturnNOTFound') { //Do not add this one to the result array
} else if ( method === 'ReturnFound') { //Not sure about this loop yet
}
*/
foundThisCheck = true;
break;
}
}
}
}
if (foundThisCheck === false) {
notFoundItems += '\nNotFound: ' + splitStr;
checkForTheseItems[c][compareKey] = notFoundTag;
}
}
/** this is where we need to do some other things for other options
*
*/
for (var i in inThisArray) {
var objectToUpdate = inThisArray[i];
//Value was found.... do whatever needs to be done.
if (objectToUpdate[compareKey]) {
objectToUpdate[compareKey] = 'Found';
result.found.push(objectToUpdate);
foundCount++;
}
else {
objectToUpdate[compareKey] = 'NOTFound';
result.notFound.push(objectToUpdate);
notFoundCount++;
}
}
result.message = result.notFound.map(function (thisOne) {
return 'NF: ' + (0, services_1.stringifyKeyValue)(thisOne, 0, '===') + '\n';
}).join('');
if (method === 'ReturnFound') {
result.result = result.found;
}
else if (method === 'ReturnNOTFound') {
result.result = result.notFound;
}
else if (method === 'AddTag') {
result.result = result.inThisArray;
}
if (messsages !== 'None') {
console.log('compareArrays - result: ' + method, result);
}
if (messsages === 'Alert' || messsages === 'Both') {
//alert('compareArrays - completed! Check Console for results');
var alertMessage = "Found (".concat(foundCount, ") matches in both arrays");
if (notFoundCount > 0) {
alertMessage += '\nCheck Console.log for details';
alertMessage += "\nDid NOT find these (".concat(notFoundCount, ") items!");
alertMessage += '\n' + result.message;
}
alert(alertMessage);
}
return result;
}
exports.compareArrays = compareArrays;
/***
* d888b d88888b d888888b db dD d88888b db db .d8888. db db .88b d88. .88b d88. .d8b. d8888b. db db
* 88' Y8b 88' `~~88~~' 88 ,8P' 88' `8b d8' 88' YP 88 88 88'YbdP`88 88'YbdP`88 d8' `8b 88 `8D `8b d8'
* 88 88ooooo 88 88,8P 88ooooo `8bd8' `8bo. 88 88 88 88 88 88 88 88 88ooo88 88oobY' `8bd8'
* 88 ooo 88~~~~~ 88 88`8b 88~~~~~ 88 `Y8b. 88 88 88 88 88 88 88 88 88~~~88 88`8b 88
* 88. ~8~ 88. 88 88 `88. 88. 88 db 8D 88b d88 88 88 88 88 88 88 88 88 88 `88. 88
* Y888P Y88888P YP YP YD Y88888P YP `8888Y' ~Y8888P' YP YP YP YP YP YP YP YP 88 YD YP
*
*
*
* Copied from FoamControl.tsx
* @param thisDataObjec
* @param compareTypes Tested values = ['string','number','boolean'];
* @param ignoreKeys Tested values = ['element'];
* @returns
*/
function getKeySummary(baselineObject, compareTypes, ignoreKeys, parseMe) {
var keySummary = {};
Object.keys(baselineObject).map(function (key) {
var keyType = typeof baselineObject[key];
if (compareTypes.indexOf(keyType) > -1 && ignoreKeys.indexOf(key) < 0) {
keySummary[key] = baselineObject[key];
}
});
if (parseMe !== false) {
keySummary = JSON.parse(JSON.stringify(keySummary));
}
return keySummary;
}
exports.getKeySummary = getKeySummary;
exports.BaseFieldKeys = ['Title', 'StaticName', 'TypeAsString', 'FieldTypeKind', 'TypeDisplayName', 'Formula', 'InternalName', 'Required', 'Sealed', 'CanBeDeleted'];
exports.BaseViewKeys = ['Title', 'Id'];
exports.BaseTypeKeys = ['Title', 'Id'];
exports.DoesNotExistLabel = '- >> Does NOT Exist << -';
exports.KeyChangeDelimiter = ' >>> ';
/***
* d888b d88888b d888888b db dD d88888b db db .o88b. db db .d8b. d8b db d888b d88888b .d8888.
* 88' Y8b 88' `~~88~~' 88 ,8P' 88' `8b d8' d8P Y8 88 88 d8' `8b 888o 88 88' Y8b 88' 88' YP
* 88 88ooooo 88 88,8P 88ooooo `8bd8' 8P 88ooo88 88ooo88 88V8o 88 88 88ooooo `8bo.
* 88 ooo 88~~~~~ 88 88`8b 88~~~~~ 88 8b 88~~~88 88~~~88 88 V8o88 88 ooo 88~~~~~ `Y8b.
* 88. ~8~ 88. 88 88 `88. 88. 88 Y8b d8 88 88 88 88 88 V888 88. ~8~ 88. db 8D
* Y888P Y88888P YP YP YD Y88888P YP `Y88P' YP YP YP YP VP V8P Y888P Y88888P `8888Y'
*
*
*/
/**
* getKeyChanges function compares two objects which are similar... and returns the differences.
* An example would be versions of a list item.
* the keyChanges would compare both arrays to each other and only return the keys which have values that are not equal
* This is more for converting changes into single label per key notation that is more easily readable
*
* baselineObject = { a: 1, b: 2} compareObject = { a: 1, b: 3} => result would be newObject = { b: '2 >>> 3' }
*
*
* POSSIBLE BREAKING CHANGE VVVVV
*
* 2021-05-18: REVISED TO START USING 2nd Paramter (now called specificKeys )
*
* TO AVOID ISSUES (this paramter was received but not used in the past) - pass in null or []
*
* @param baselineObject
* @param specificKeys ( was keySummary ) - Pass array of specific keys to check, empty or null to use keys in baselineObject
* @param compareObject
* @param parseMe
*/
function getKeyChanges(baselineObject, specificKeys, compareObject, parseMe) {
var keyChanges = {};
var TestTheseKeys = [];
if (baselineObject === null || compareObject === null) {
TestTheseKeys = exports.BaseFieldKeys;
// TestTheseKeys = BaseFieldKeys;
}
else if (specificKeys !== null && specificKeys !== undefined && specificKeys.length > 0) {
TestTheseKeys = specificKeys;
}
else {
TestTheseKeys = baselineObject ? Object.keys(baselineObject) : Object.keys(compareObject);
}
TestTheseKeys.map(function (key) {
var baselineObjectVal = baselineObject ? baselineObject[key] : key === 'Title' ? exports.DoesNotExistLabel : undefined;
var compareObjectVal = compareObject ? compareObject[key] : key === 'Title' ? exports.DoesNotExistLabel : undefined;
//Can't directly compare arrays or objects so you have to stringify them first
if (typeof baselineObjectVal === 'object') {
baselineObjectVal = JSON.stringify(baselineObject[key]);
}
//Can't directly compare arrays or objects so you have to stringify them first
if (typeof compareObjectVal === 'object') {
compareObjectVal = JSON.stringify(compareObject[key]);
}
if (baselineObjectVal !== compareObjectVal) {
var keyChange = baselineObjectVal + exports.KeyChangeDelimiter + compareObjectVal;
//Do not make comparisons for these...
var ignoreCompares = [
"undefined".concat(exports.KeyChangeDelimiter, "null"),
"undefined".concat(exports.KeyChangeDelimiter, "function(){}"),
"undefined".concat(exports.KeyChangeDelimiter, "[object HTMLDivElement]"),
"undefined".concat(exports.KeyChangeDelimiter, "[object Object]"),
"undefined".concat(exports.KeyChangeDelimiter)
];
if (ignoreCompares.indexOf(keyChange) < 0 && keyChange.indexOf("undefined".concat(exports.KeyChangeDelimiter, "function")) < 0) {
keyChanges[key] = keyChange;
}
if (keyChange === "undefined".concat(exports.KeyChangeDelimiter, "[object Object]")) {
console.log('OBJECT NOT COMPARED', baselineObjectVal, compareObjectVal);
}
}
});
if (parseMe !== false) {
keyChanges = JSON.parse(JSON.stringify(keyChanges));
}
return keyChanges;
}
exports.getKeyChanges = getKeyChanges;
//# sourceMappingURL=checks.js.map