@mikezimm/npmfunctions
Version:
Functions used in my SPFx webparts
304 lines (241 loc) • 16.7 kB
text/typescript
/***
* .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';
*/
import { ISeriesSort } from '../../CSSCharts/ICSSCharts';
import { sortStringArray , sortNumberArray } from '../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';
*/
export function updateNextOpenIndex( targetArray: any[], start: number, value: any ): any[] {
let exit: boolean = false;
for (let index = start; index < targetArray.length; index++) {
if ( !exit && targetArray[ index ] === null ) {
targetArray[ index ] = value ;
exit = true;
}
}
return targetArray;
}
/***
* .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
export function makeid(length: number) {
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;
}
/***
* 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
*
*/
export const 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;
export const 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;
export const 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;
export const 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;
export const 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;
export const 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;
export const 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;
export const 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;
export type IIsGuidTestLocation = 'exact' | 'start' | 'end' | 'contains';
export function isGuid( input: any, compareType : IIsGuidTestLocation = 'exact' ) { //c456c008-5cc7-455e-90dc-ebb67d4496ec
const testMe: string = typeof input === 'string' ? input : input && input.target && input.target.value ? input.target.value : '';
let result : boolean = false;
if ( testMe === undefined || testMe === null || testMe.length < 22 ) {
result = false;
} else {
if ( compareType === 'exact' ) {
result = ValidGuidRegexExact.exec(testMe) ? true : false;
} else if ( compareType === 'start' ) {
result = ValidGuidRegexStart.exec(testMe) ? true : false;
} else if ( compareType === 'end' ) {
result = ValidGuidRegexEnd.exec(testMe) ? true : false;
} else if ( compareType === 'contains' ) {
result = ValidGuidRegexContains.exec(testMe) ? true : false;
}
}
return result;
}
/**
* 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
*/
export function getGuidsFromString( input: any, compareType : IIsGuidTestLocation = 'exact' ) { //c456c008-5cc7-455e-90dc-ebb67d4496ec
const testMe: string = typeof input === 'string' ? input : input && input.target && input.target.value ? input.target.value : '';
let result : string[] | any = null;
if ( testMe === undefined || testMe === null || testMe.length < 22 ) {
result = null;
} else {
if ( compareType === 'exact' ) {
result = ValidGuidRegexExactGlobal.exec(testMe);
} else if ( compareType === 'start' ) {
result = ValidGuidRegexStartGlobal.exec(testMe);
} else if ( compareType === 'end' ) {
result = ValidGuidRegexEndGlobal.exec(testMe);
} else if ( compareType === 'contains' ) {
result = ValidGuidRegexContainsGlobal.exec(testMe);
}
}
return result;
}
/***
* 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
*/
export function getStringArrayFromString ( input : any, delim: string, removeEmpty: boolean, sort: 'asc' | 'dec' | null, trim: boolean = false ) : string[] | null {
let str: string = 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 = ',';
}
let arr : string[] = str.split( delim );
if ( trim === true ) {
arr.map( (s, index) => {
arr[index] = s.trim();
});
}
arr = sortStringArray( arr, sort );
let finalStringArray : string[] = [];
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( (el) => {
return !!el;
});
} else {
finalStringArray = arr;
}
return finalStringArray;
}
export function getNumberArrayFromString( input : string, delim: string, removeEmpty: boolean, removeZeros: boolean, order: ISeriesSort , def: any = null ) : number[] | null {
const stringArray: string[] = typeof input !== 'string' ? [] : input.split( delim );
let numberArray : number[] = [];
if ( stringArray && stringArray.length > 0 ) {
stringArray.map(str => {
if ( removeEmpty === true && str.trim() === '' ) { }
else {
const strNum =Number(str);
if ( removeZeros === true && strNum === 0 ) { }
else {
numberArray.push( strNum );
}
}
});
}
numberArray = sortNumberArray( numberArray, order );
return numberArray;
}
/***
* .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
*/
export function cleanEmptyElementsFromString ( input : any, delim: string, removeEmpty: boolean, sort: 'asc' | 'dec' | null ) : string {
const str: string = typeof input === 'string' ? input : input && input.target && input.target.value ? input.target.value : '';
let stringArray : string[] | any = getStringArrayFromString( str, delim, removeEmpty, sort );
return stringArray.join(';');
}