fix(types): improve TypeScript strictness compatibility and modernize test exports
This commit is contained in:
@@ -3,6 +3,6 @@
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@push.rocks/smartstring',
|
||||
version: '4.1.0',
|
||||
version: '4.1.1',
|
||||
description: 'A library for handling strings in smart ways, including manipulation and encoding, with TypeScript support.'
|
||||
}
|
||||
|
||||
@@ -89,7 +89,7 @@ const universalBase64 = {
|
||||
*/
|
||||
export class Base64 {
|
||||
private refString: string;
|
||||
constructor(inputStringArg, typeArg: TStringInputType) {
|
||||
constructor(inputStringArg: string, typeArg: TStringInputType) {
|
||||
switch (typeArg) {
|
||||
case 'string': // easiest case
|
||||
this.refString = inputStringArg;
|
||||
|
||||
@@ -26,8 +26,8 @@ const getRandomInt = (min: number, max: number): number => {
|
||||
* ! - Special character
|
||||
* * - Any character (A, a, 0, or !)
|
||||
*/
|
||||
const customRandomatic = (pattern: string, length?: number, options?: any): string => {
|
||||
const charSets = {
|
||||
const customRandomatic = (pattern: string, length?: number, options?: unknown): string => {
|
||||
const charSets: Record<string, string> = {
|
||||
'A': 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
|
||||
'a': 'abcdefghijklmnopqrstuvwxyz',
|
||||
'0': '0123456789',
|
||||
|
||||
@@ -5,13 +5,15 @@ import * as plugins from './smartstring.plugins.js';
|
||||
* @param envArrayArg
|
||||
* @returns {}
|
||||
*/
|
||||
export const makeEnvObject = function (envArrayArg: string[]) {
|
||||
let returnObject = {};
|
||||
let regexString = /(.*)=(.*)/;
|
||||
export const makeEnvObject = function (envArrayArg: string[]): Record<string, string> {
|
||||
const returnObject: Record<string, string> = {};
|
||||
const regexString = /(.*)=(.*)/;
|
||||
if (typeof envArrayArg !== 'undefined') {
|
||||
for (let envKey in envArrayArg) {
|
||||
let regexMatches = regexString.exec(envArrayArg[envKey]);
|
||||
returnObject[regexMatches[1]] = regexMatches[2];
|
||||
for (const envString of envArrayArg) {
|
||||
const regexMatches = regexString.exec(envString);
|
||||
if (regexMatches) {
|
||||
returnObject[regexMatches[1]] = regexMatches[2];
|
||||
}
|
||||
}
|
||||
}
|
||||
return returnObject;
|
||||
|
||||
+25
-27
@@ -1,17 +1,17 @@
|
||||
export class Domain {
|
||||
public fullName: string;
|
||||
public level1: string;
|
||||
public level2: string;
|
||||
public level3: string;
|
||||
public level4: string;
|
||||
public level5: string;
|
||||
public protocol: string;
|
||||
public level1?: string;
|
||||
public level2?: string;
|
||||
public level3?: string;
|
||||
public level4?: string;
|
||||
public level5?: string;
|
||||
public protocol?: string;
|
||||
public zoneName: string;
|
||||
// aliases
|
||||
public topLevel: string;
|
||||
public domainName;
|
||||
public subDomain;
|
||||
public port;
|
||||
public topLevel?: string;
|
||||
public domainName?: string;
|
||||
public subDomain?: string;
|
||||
public port: string;
|
||||
public nodeParsedUrl: URL;
|
||||
constructor(domainStringArg: string) {
|
||||
// lets do the node standard stuff first
|
||||
@@ -26,21 +26,16 @@ export class Domain {
|
||||
const regexMatches = this._domainRegex(
|
||||
domainStringArg.replace(this.nodeParsedUrl.pathname, '')
|
||||
);
|
||||
[this.level1, this.level2, this.level3, this.level4, this.level5] = regexMatches;
|
||||
this.fullName = '';
|
||||
for (let i = 1; i <= 5; i++) {
|
||||
if (regexMatches[i - 1]) {
|
||||
const localMatch = regexMatches[i - 1];
|
||||
this['level' + i.toString()] = localMatch;
|
||||
if (this.fullName === '') {
|
||||
this.fullName = localMatch;
|
||||
} else {
|
||||
this.fullName = localMatch + '.' + this.fullName;
|
||||
}
|
||||
for (const localMatch of regexMatches) {
|
||||
if (this.fullName === '') {
|
||||
this.fullName = localMatch;
|
||||
} else {
|
||||
this['level' + i.toString()] = undefined;
|
||||
this.fullName = localMatch + '.' + this.fullName;
|
||||
}
|
||||
}
|
||||
this.zoneName = this.level2 + '.' + this.level1;
|
||||
this.zoneName = [this.level2, this.level1].filter(Boolean).join('.');
|
||||
|
||||
// aliases
|
||||
this.topLevel = this.level1;
|
||||
@@ -51,19 +46,22 @@ export class Domain {
|
||||
// helper functions
|
||||
|
||||
/** */
|
||||
private _domainRegex(stringArg: string) {
|
||||
private _domainRegex(stringArg: string): string[] {
|
||||
const regexString =
|
||||
/([a-zA-Z0-9\-\_]*)\.{0,1}([a-zA-Z0-9\-\_]*)\.{0,1}([a-zA-Z0-9\-\_]*)\.{0,1}([a-zA-Z0-9\-\_]*)\.{0,1}([a-zA-Z0-9\-\_]*)\.{0,1}$/;
|
||||
const regexMatches = regexString.exec(stringArg);
|
||||
regexMatches.reverse(); //make sure we build the domain from toplevel to subdomain (reversed order)
|
||||
regexMatches.pop(); // pop the last element, which is, since we reversed the Array, the full String of matched elements
|
||||
const regexMatchesFiltered = regexMatches.filter(function (stringArg: string) {
|
||||
if (!regexMatches) {
|
||||
return [];
|
||||
}
|
||||
const reversedMatches = [...regexMatches].reverse(); // make sure we build the domain from top level to subdomain
|
||||
reversedMatches.pop(); // pop the last element, which is, since we reversed the Array, the full String of matched elements
|
||||
const regexMatchesFiltered = reversedMatches.filter(function (stringArg: string) {
|
||||
return stringArg !== '';
|
||||
});
|
||||
return regexMatchesFiltered;
|
||||
}
|
||||
|
||||
private _protocolRegex(stringArg: string) {
|
||||
private _protocolRegex(stringArg: string): string | undefined {
|
||||
const regexString = /^([a-zA-Z0-9]*):\/\//;
|
||||
const regexMatches = regexString.exec(stringArg);
|
||||
if (regexMatches) {
|
||||
@@ -73,7 +71,7 @@ export class Domain {
|
||||
}
|
||||
}
|
||||
|
||||
private _portRegex(stringArg: string) {
|
||||
private _portRegex(stringArg: string): string | undefined {
|
||||
const regexString = /^([a-zA-Z0-9]*):\/\//;
|
||||
const regexMatches = regexString.exec(stringArg);
|
||||
if (regexMatches) {
|
||||
|
||||
+11
-17
@@ -1,5 +1,3 @@
|
||||
import * as plugins from './smartstring.plugins.js';
|
||||
|
||||
/* ---------------------------------------------- *
|
||||
* ------------------ classes ------------------- *
|
||||
* ---------------------------------------------- */
|
||||
@@ -7,11 +5,14 @@ export class GitRepo {
|
||||
host: string;
|
||||
user: string;
|
||||
repo: string;
|
||||
accessToken: string;
|
||||
accessToken?: string;
|
||||
sshUrl: string;
|
||||
httpsUrl: string;
|
||||
constructor(stringArg: string, tokenArg?: string) {
|
||||
let regexMatches = gitRegex(stringArg);
|
||||
const regexMatches = gitRegex(stringArg);
|
||||
if (!regexMatches) {
|
||||
throw new Error(`Invalid git repository URL: ${stringArg}`);
|
||||
}
|
||||
this.host = regexMatches[1];
|
||||
this.user = regexMatches[2];
|
||||
this.repo = regexMatches[3];
|
||||
@@ -24,10 +25,10 @@ export class GitRepo {
|
||||
/* ---------------------------------------------- *
|
||||
* ------------------ helpers ------------------- *
|
||||
* ---------------------------------------------- */
|
||||
const gitRegex = function (stringArg: string) {
|
||||
const gitRegex = function (stringArg: string): RegExpExecArray | null {
|
||||
const regexString =
|
||||
/([a-zA-Z0-9\-_\.]*)(?:\/|\:)([a-zA-Z0-9\-_\.]*)(?:\/)([a-zA-Z0-9\-_\.]*)(?:\.git)/;
|
||||
let regexMatches = regexString.exec(stringArg);
|
||||
const regexMatches = regexString.exec(stringArg);
|
||||
return regexMatches;
|
||||
};
|
||||
|
||||
@@ -35,23 +36,16 @@ const gitLink = function (
|
||||
hostArg: string,
|
||||
userArg: string,
|
||||
repoArg: string,
|
||||
tokenArg: string = '',
|
||||
linkTypeArg
|
||||
tokenArg = '',
|
||||
linkTypeArg: 'https' | 'ssh'
|
||||
): string {
|
||||
let returnString;
|
||||
if (tokenArg !== '') {
|
||||
tokenArg = tokenArg + '@';
|
||||
}
|
||||
switch (linkTypeArg) {
|
||||
case 'https':
|
||||
returnString = 'https://' + tokenArg + hostArg + '/' + userArg + '/' + repoArg + '.git';
|
||||
break;
|
||||
return 'https://' + tokenArg + hostArg + '/' + userArg + '/' + repoArg + '.git';
|
||||
case 'ssh':
|
||||
returnString = 'git@' + hostArg + ':' + userArg + '/' + repoArg + '.git';
|
||||
break;
|
||||
default:
|
||||
console.error('Link Type ' + linkTypeArg + ' not known');
|
||||
break;
|
||||
return 'git@' + hostArg + ':' + userArg + '/' + repoArg + '.git';
|
||||
}
|
||||
return returnString;
|
||||
};
|
||||
|
||||
@@ -69,13 +69,14 @@ export const indentWithPrefix = (stringArg: string, prefixArg: string): string =
|
||||
export const normalize = (stringArg: string): string => {
|
||||
let resultString: string;
|
||||
let splitStringArray: string[] = splitStringAtLineBreak(stringArg);
|
||||
let minCommonLeftOffset: number;
|
||||
let minCommonLeftOffset: number | undefined;
|
||||
|
||||
const deIndentRegex = /^(\s*)/;
|
||||
const emptyLineRegex = /^(\s*)$/;
|
||||
|
||||
for (let stringItem of splitStringArray) {
|
||||
let offsetString = deIndentRegex.exec(stringItem)[1];
|
||||
const regexMatch = deIndentRegex.exec(stringItem);
|
||||
let offsetString = regexMatch ? regexMatch[1] : '';
|
||||
if (
|
||||
(typeof minCommonLeftOffset === 'undefined' || offsetString.length < minCommonLeftOffset) &&
|
||||
!emptyLineRegex.test(stringItem)
|
||||
@@ -84,8 +85,9 @@ export const normalize = (stringArg: string): string => {
|
||||
}
|
||||
}
|
||||
let resultSplitStringArray = [];
|
||||
const commonLeftOffset = minCommonLeftOffset || 0;
|
||||
for (let stringItem of splitStringArray) {
|
||||
resultSplitStringArray.push(stringItem.substr(minCommonLeftOffset));
|
||||
resultSplitStringArray.push(stringItem.substr(commonLeftOffset));
|
||||
}
|
||||
resultString = joinStringWithLineBreaks(resultSplitStringArray);
|
||||
return resultString;
|
||||
|
||||
Reference in New Issue
Block a user