@mikezimm/npmfunctions
Version:
Functions used in my SPFx webparts
269 lines (267 loc) • 14.6 kB
JavaScript
;
/***
* .d8888. d888888b d8888b. d888888b d8b db d888b .d8888.
* 88' YP `~~88~~' 88 `8D `88' 888o 88 88' Y8b 88' YP
* `8bo. 88 88oobY' 88 88V8o 88 88 `8bo.
* `Y8b. 88 88`8b 88 88 V8o88 88 ooo `Y8b.
* db 8D 88 88 `88. .88. 88 V888 88. ~8~ db 8D
* `8888Y' YP 88 YD Y888888P VP V8P Y888P `8888Y'
*
*
import { cleanSPListURL, cleanURL, encodeDecodeString, } from '@mikezimm/npmfunctions/dist/Services/Strings/urlServices';
import { makeid, isGuidgetStringArrayFromString, cleanEmptyElementsFromString } from '@mikezimm/npmfunctions/dist/Services/Strings/stringServices';
import { camelToSentanceCase, camelize, randomizeCase, upperFirstLetter, lowerFirstLetter } from '@mikezimm/npmfunctions/dist/Services/Strings/stringCase';
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.cleanEmptyElementsFromString = exports.getNumberArrayFromString = exports.getStringArrayFromString = exports.getGuidsFromString = exports.isGuid = exports.ValidGuidRegexContainsGlobal = exports.ValidGuidRegexEndGlobal = exports.ValidGuidRegexStartGlobal = exports.ValidGuidRegexExactGlobal = exports.ValidGuidRegexContains = exports.ValidGuidRegexEnd = exports.ValidGuidRegexStart = exports.ValidGuidRegexExact = exports.makeid = exports.updateNextOpenIndex = void 0;
var sorting_1 = require("../Arrays/sorting");
/***
* db db d8888b. d8888b. .d8b. d888888b d88888b d8b db d88888b db db d888888b .d88b. d8888b. d88888b d8b db d888888b d8b db d8888b. d88888b db db
* 88 88 88 `8D 88 `8D d8' `8b `~~88~~' 88' 888o 88 88' `8b d8' `~~88~~' .8P Y8. 88 `8D 88' 888o 88 `88' 888o 88 88 `8D 88' `8b d8'
* 88 88 88oodD' 88 88 88ooo88 88 88ooooo 88V8o 88 88ooooo `8bd8' 88 88 88 88oodD' 88ooooo 88V8o 88 88 88V8o 88 88 88 88ooooo `8bd8'
* 88 88 88~~~ 88 88 88~~~88 88 88~~~~~ 88 V8o88 88~~~~~ .dPYb. 88 88 88 88~~~ 88~~~~~ 88 V8o88 88 88 V8o88 88 88 88~~~~~ .dPYb.
* 88b d88 88 88 .8D 88 88 88 88. 88 V888 88. .8P Y8. 88 `8b d8' 88 88. 88 V888 .88. 88 V888 88 .8D 88. .8P Y8.
* ~Y8888P' 88 Y8888D' YP YP YP Y88888P VP V8P Y88888P YP YP YP `Y88P' 88 Y88888P VP V8P Y888888P VP V8P Y8888D' Y88888P YP YP
*
* import { updateNextOpenIndex } from '@mikezimm/npmfunctions/dist/Services/Strings/stringServices';
*/
function updateNextOpenIndex(targetArray, start, value) {
var exit = false;
for (var index = start; index < targetArray.length; index++) {
if (!exit && targetArray[index] === null) {
targetArray[index] = value;
exit = true;
}
}
return targetArray;
}
exports.updateNextOpenIndex = updateNextOpenIndex;
/***
* .88b d88. .d8b. db dD d88888b d888888b d8888b.
* 88'YbdP`88 d8' `8b 88 ,8P' 88' `88' 88 `8D
* 88 88 88 88ooo88 88,8P 88ooooo 88 88 88
* 88 88 88 88~~~88 88`8b 88~~~~~ 88 88 88
* 88 88 88 88 88 88 `88. 88. .88. 88 .8D
* YP YP YP YP YP YP YD Y88888P Y888888P Y8888D'
*
*
*/
//https://stackoverflow.com/a/1349426
function makeid(length) {
var result = '';
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var charactersLength = characters.length;
for (var i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
exports.makeid = makeid;
/***
* d888888b .d8888. d888b db db d888888b d8888b.
* `88' 88' YP 88' Y8b 88 88 `88' 88 `8D
* 88 `8bo. 88 88 88 88 88 88
* 88 `Y8b. 88 ooo 88 88 88 88 88
* .88. db 8D 88. ~8~ 88b d88 .88. 88 .8D
* Y888888P `8888Y' Y888P ~Y8888P' Y888888P Y8888D'
*
*
*/
/**
* Regex courtesy of: https://stackoverflow.com/a/13653180/4210807
*
* NOTES:
* ^ at the beginning denotes the test string must START with that: ^ asserts position at start of the string
* ^ at the beginning denotes the test string must END with that: $ asserts position at the end of the string, or before the line terminator right at the end of the string (if any)
* [0-9a-f]{8} - 8 characters between 0-9 and a-f
* [0-9a-f]{4} - 4 characters between 0-9 and a-f
* [1-5][0-9a-f]{3} - one character between 1-5 and 3 between 0-9 and a-f
* [1-5][0-9a-f]{3} - one character between 1-5 and 3 between 0-9 and a-f
* [0-9a-f]{12} - 12 characters between 0-9 and a-f
*
*/
exports.ValidGuidRegexExact = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
exports.ValidGuidRegexStart = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}/i;
exports.ValidGuidRegexEnd = /[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
exports.ValidGuidRegexContains = /[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}/i;
exports.ValidGuidRegexExactGlobal = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/ig;
exports.ValidGuidRegexStartGlobal = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}/ig;
exports.ValidGuidRegexEndGlobal = /[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/ig;
exports.ValidGuidRegexContainsGlobal = /[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}/ig;
function isGuid(input, compareType) {
if (compareType === void 0) { compareType = 'exact'; }
var testMe = typeof input === 'string' ? input : input && input.target && input.target.value ? input.target.value : '';
var result = false;
if (testMe === undefined || testMe === null || testMe.length < 22) {
result = false;
}
else {
if (compareType === 'exact') {
result = exports.ValidGuidRegexExact.exec(testMe) ? true : false;
}
else if (compareType === 'start') {
result = exports.ValidGuidRegexStart.exec(testMe) ? true : false;
}
else if (compareType === 'end') {
result = exports.ValidGuidRegexEnd.exec(testMe) ? true : false;
}
else if (compareType === 'contains') {
result = exports.ValidGuidRegexContains.exec(testMe) ? true : false;
}
}
return result;
}
exports.isGuid = isGuid;
/**
* getGuidsFromString is similar to isGuid except it returns the guid(s) found in the string
*
* https://regex101.com/
*
* Returns null if no guid is found.
* Returns array of string containing the first guid found in the string ( g modifier: global. All matches (don't return after first match) )
*
* @param testMe
* @param compareType
*/
function getGuidsFromString(input, compareType) {
if (compareType === void 0) { compareType = 'exact'; }
var testMe = typeof input === 'string' ? input : input && input.target && input.target.value ? input.target.value : '';
var result = null;
if (testMe === undefined || testMe === null || testMe.length < 22) {
result = null;
}
else {
if (compareType === 'exact') {
result = exports.ValidGuidRegexExactGlobal.exec(testMe);
}
else if (compareType === 'start') {
result = exports.ValidGuidRegexStartGlobal.exec(testMe);
}
else if (compareType === 'end') {
result = exports.ValidGuidRegexEndGlobal.exec(testMe);
}
else if (compareType === 'contains') {
result = exports.ValidGuidRegexContainsGlobal.exec(testMe);
}
}
return result;
}
exports.getGuidsFromString = getGuidsFromString;
/***
* d888b d88888b d888888b .d8888. d888888b d8888b. d888888b d8b db d888b .d8b. d8888b. d8888b. .d8b. db db
* 88' Y8b 88' `~~88~~' 88' YP `~~88~~' 88 `8D `88' 888o 88 88' Y8b d8' `8b 88 `8D 88 `8D d8' `8b `8b d8'
* 88 88ooooo 88 `8bo. 88 88oobY' 88 88V8o 88 88 88ooo88 88oobY' 88oobY' 88ooo88 `8bd8'
* 88 ooo 88~~~~~ 88 `Y8b. 88 88`8b 88 88 V8o88 88 ooo 88~~~88 88`8b 88`8b 88~~~88 88
* 88. ~8~ 88. 88 db 8D 88 88 `88. .88. 88 V888 88. ~8~ 88 88 88 `88. 88 `88. 88 88 88
* Y888P Y88888P YP `8888Y' YP 88 YD Y888888P VP V8P Y888P YP YP 88 YD 88 YD YP YP YP
*
*
* d88888b d8888b. .d88b. .88b d88. .d8888. d888888b d8888b. d888888b d8b db d888b
* 88' 88 `8D .8P Y8. 88'YbdP`88 88' YP `~~88~~' 88 `8D `88' 888o 88 88' Y8b
* 88ooo 88oobY' 88 88 88 88 88 `8bo. 88 88oobY' 88 88V8o 88 88
* 88~~~ 88`8b 88 88 88 88 88 `Y8b. 88 88`8b 88 88 V8o88 88 ooo
* 88 88 `88. `8b d8' 88 88 88 db 8D 88 88 `88. .88. 88 V888 88. ~8~
* YP 88 YD `Y88P' YP YP YP `8888Y' YP 88 YD Y888888P VP V8P Y888P
*
*
*/
/** was: cleanProjEditOptions from TrackMyTime7
* This function takes a string with ;, converts to array of strings and removes empty elements (like if ; is at the end.)
*
* example input: ";test;this;string;;now;"
* example result: ['test','this','string','now']
*
* @param str
*
*
* @param delim - exactly what you pass or
* use delim = ',or;' || ';or,' to clean up string property pane values (uses 2 delimiters)
* @param trim - default = false. true will just remove any leading or trailing spaces from string value
*/
function getStringArrayFromString(input, delim, removeEmpty, sort, trim) {
if (trim === void 0) { trim = false; }
var str = typeof input === 'string' ? input : input && input.target && input.target.value ? input.target.value : '';
if (str == null) {
return null;
}
else if (delim == null || delim == '') {
return [str];
}
if (delim === ',or;' || delim === ';or,') {
str = str.replace(/;/g, ',');
delim = ',';
}
var arr = str.split(delim);
if (trim === true) {
arr.map(function (s, index) {
arr[index] = s.trim();
});
}
arr = (0, sorting_1.sortStringArray)(arr, sort);
var finalStringArray = [];
if (removeEmpty === true) {
//Found here: https://github.com/mikezimm/generic-solution/issues/156
//Was returning array with empty strings "" before. This also filters those out.
//https://www.samanthaming.com/pictorials/how-to-remove-all-falsy-values-from-an-array/#_3-filter
finalStringArray = arr.filter(function (el) {
return !!el;
});
}
else {
finalStringArray = arr;
}
return finalStringArray;
}
exports.getStringArrayFromString = getStringArrayFromString;
function getNumberArrayFromString(input, delim, removeEmpty, removeZeros, order, def) {
if (def === void 0) { def = null; }
var stringArray = typeof input !== 'string' ? [] : input.split(delim);
var numberArray = [];
if (stringArray && stringArray.length > 0) {
stringArray.map(function (str) {
if (removeEmpty === true && str.trim() === '') { }
else {
var strNum = Number(str);
if (removeZeros === true && strNum === 0) { }
else {
numberArray.push(strNum);
}
}
});
}
numberArray = (0, sorting_1.sortNumberArray)(numberArray, order);
return numberArray;
}
exports.getNumberArrayFromString = getNumberArrayFromString;
/***
* .o88b. db d88888b .d8b. d8b db d88888b .88b d88. d8888b. d888888b db db d88888b db d88888b .88b d88. d88888b d8b db d888888b .d8888.
* d8P Y8 88 88' d8' `8b 888o 88 88' 88'YbdP`88 88 `8D `~~88~~' `8b d8' 88' 88 88' 88'YbdP`88 88' 888o 88 `~~88~~' 88' YP
* 8P 88 88ooooo 88ooo88 88V8o 88 88ooooo 88 88 88 88oodD' 88 `8bd8' 88ooooo 88 88ooooo 88 88 88 88ooooo 88V8o 88 88 `8bo.
* 8b 88 88~~~~~ 88~~~88 88 V8o88 88~~~~~ 88 88 88 88~~~ 88 88 88~~~~~ 88 88~~~~~ 88 88 88 88~~~~~ 88 V8o88 88 `Y8b.
* Y8b d8 88booo. 88. 88 88 88 V888 88. 88 88 88 88 88 88 88. 88booo. 88. 88 88 88 88. 88 V888 88 db 8D
* `Y88P' Y88888P Y88888P YP YP VP V8P Y88888P YP YP YP 88 YP YP Y88888P Y88888P Y88888P YP YP YP Y88888P VP V8P YP `8888Y'
*
*
* d88888b d8888b. .d88b. .88b d88. .d8888. d888888b d8888b. d888888b d8b db d888b
* 88' 88 `8D .8P Y8. 88'YbdP`88 88' YP `~~88~~' 88 `8D `88' 888o 88 88' Y8b
* 88ooo 88oobY' 88 88 88 88 88 `8bo. 88 88oobY' 88 88V8o 88 88
* 88~~~ 88`8b 88 88 88 88 88 `Y8b. 88 88`8b 88 88 V8o88 88 ooo
* 88 88 `88. `8b d8' 88 88 88 db 8D 88 88 `88. .88. 88 V888 88. ~8~
* YP 88 YD `Y88P' YP YP YP `8888Y' YP 88 YD Y888888P VP V8P Y888P
*
*
*/
/** was originally copied from cleanProjEditOptions from TrackMyTime7
* This function takes a string with ;, converts to array of strings and removes empty elements (like if ; is at the end.)
*
* example input: ";test;this;string;;now;"
* example result: "test;this;string;now"
* @param str
*/
function cleanEmptyElementsFromString(input, delim, removeEmpty, sort) {
var str = typeof input === 'string' ? input : input && input.target && input.target.value ? input.target.value : '';
var stringArray = getStringArrayFromString(str, delim, removeEmpty, sort);
return stringArray.join(';');
}
exports.cleanEmptyElementsFromString = cleanEmptyElementsFromString;
//# sourceMappingURL=stringServices.js.map