update normalize of smartstring
This commit is contained in:
@ -1,11 +1,13 @@
|
||||
import * as docker from './smartstring.docker'
|
||||
import * as indent from './smartstring.indent'
|
||||
import * as normalize from './smartstring.normalize'
|
||||
import * as typescript from './smartstring.typescript'
|
||||
|
||||
export {
|
||||
docker,
|
||||
typescript,
|
||||
indent
|
||||
docker,
|
||||
typescript,
|
||||
normalize,
|
||||
indent
|
||||
}
|
||||
|
||||
export { Base64, base64 } from './smartstring.base64'
|
||||
|
@ -9,61 +9,61 @@ export type TBase64Input = 'string' | 'base64' | 'base64uri'
|
||||
* handle base64 strings
|
||||
*/
|
||||
export class Base64 {
|
||||
private refString: string
|
||||
constructor(inputStringArg, typeArg: TBase64Input) {
|
||||
switch (typeArg) {
|
||||
case 'string': // easiest case
|
||||
this.refString = inputStringArg
|
||||
break
|
||||
case 'base64':
|
||||
this.refString = base64.decode(inputStringArg)
|
||||
break
|
||||
case 'base64uri':
|
||||
this.refString = base64.decode(inputStringArg)
|
||||
}
|
||||
private refString: string
|
||||
constructor(inputStringArg, typeArg: TBase64Input) {
|
||||
switch (typeArg) {
|
||||
case 'string': // easiest case
|
||||
this.refString = inputStringArg
|
||||
break
|
||||
case 'base64':
|
||||
this.refString = base64.decode(inputStringArg)
|
||||
break
|
||||
case 'base64uri':
|
||||
this.refString = base64.decode(inputStringArg)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* the simple string (unencoded)
|
||||
*/
|
||||
get simpleString() {
|
||||
return this.refString
|
||||
}
|
||||
/**
|
||||
* the simple string (unencoded)
|
||||
*/
|
||||
get simpleString () {
|
||||
return this.refString
|
||||
}
|
||||
|
||||
/**
|
||||
* the base64 encoded version of the original string
|
||||
*/
|
||||
get base64String() {
|
||||
return base64.encode(this.refString)
|
||||
}
|
||||
/**
|
||||
* the base64 encoded version of the original string
|
||||
*/
|
||||
get base64String () {
|
||||
return base64.encode(this.refString)
|
||||
}
|
||||
|
||||
/**
|
||||
* the base64uri encoded version of the original string
|
||||
*/
|
||||
get base64UriString() {
|
||||
return base64.encodeUri(this.refString)
|
||||
}
|
||||
/**
|
||||
* the base64uri encoded version of the original string
|
||||
*/
|
||||
get base64UriString () {
|
||||
return base64.encodeUri(this.refString)
|
||||
}
|
||||
}
|
||||
|
||||
export let base64 = {
|
||||
/**
|
||||
* encodes the string
|
||||
*/
|
||||
encode: (stringArg: string) => {
|
||||
return plugins.jsBase64.encode(stringArg)
|
||||
},
|
||||
/**
|
||||
* encodes the string
|
||||
*/
|
||||
encode: (stringArg: string) => {
|
||||
return plugins.jsBase64.encode(stringArg)
|
||||
},
|
||||
|
||||
/**
|
||||
* encodes a stringArg to base64 uri style
|
||||
*/
|
||||
encodeUri: (stringArg: string) => {
|
||||
return plugins.jsBase64.encodeURI(stringArg)
|
||||
},
|
||||
/**
|
||||
* encodes a stringArg to base64 uri style
|
||||
*/
|
||||
encodeUri: (stringArg: string) => {
|
||||
return plugins.jsBase64.encodeURI(stringArg)
|
||||
},
|
||||
|
||||
/**
|
||||
* decodes a base64 encoded string
|
||||
*/
|
||||
decode: (stringArg: string) => {
|
||||
return plugins.jsBase64.decode(stringArg)
|
||||
}
|
||||
/**
|
||||
* decodes a base64 encoded string
|
||||
*/
|
||||
decode: (stringArg: string) => {
|
||||
return plugins.jsBase64.decode(stringArg)
|
||||
}
|
||||
}
|
||||
|
@ -6,13 +6,13 @@ import * as plugins from './smartstring.plugins'
|
||||
* @returns {}
|
||||
*/
|
||||
export let makeEnvObject = function (envArrayArg: string[]) {
|
||||
let returnObject = {}
|
||||
let regexString = /(.*)=(.*)/
|
||||
if (typeof envArrayArg !== 'undefined') {
|
||||
for (let envKey in envArrayArg) {
|
||||
let regexMatches = regexString.exec(envArrayArg[envKey])
|
||||
returnObject[regexMatches[1]] = regexMatches[2]
|
||||
};
|
||||
let returnObject = {}
|
||||
let regexString = /(.*)=(.*)/
|
||||
if (typeof envArrayArg !== 'undefined') {
|
||||
for (let envKey in envArrayArg) {
|
||||
let regexMatches = regexString.exec(envArrayArg[ envKey ])
|
||||
returnObject[ regexMatches[ 1 ] ] = regexMatches[ 2 ]
|
||||
}
|
||||
return returnObject
|
||||
}
|
||||
return returnObject
|
||||
}
|
||||
|
@ -1,62 +1,62 @@
|
||||
import * as plugins from './smartstring.plugins';
|
||||
|
||||
export class Domain {
|
||||
fullName: string
|
||||
level1: string
|
||||
level2: string
|
||||
level3: string
|
||||
level4: string
|
||||
level5: string
|
||||
protocol: string
|
||||
zoneName: string
|
||||
// aliases
|
||||
topLevel: string
|
||||
domainName
|
||||
subDomain
|
||||
constructor(domainStringArg:string){
|
||||
let regexMatches = domainRegex(domainStringArg);
|
||||
this.fullName = ''
|
||||
for (let i = 1; i <= 5; i++) {
|
||||
if (regexMatches[i - 1]) {
|
||||
let localMatch = regexMatches[i - 1]
|
||||
this['level' + i.toString()] = localMatch
|
||||
if (this.fullName === ''){
|
||||
this.fullName = localMatch
|
||||
} else {
|
||||
this.fullName = localMatch + '.' + this.fullName
|
||||
}
|
||||
} else {
|
||||
this['level' + i.toString()] = undefined
|
||||
};
|
||||
};
|
||||
this.protocol = protocolRegex(domainStringArg)
|
||||
this.zoneName = this.level2 + '.' + this.level1
|
||||
fullName: string
|
||||
level1: string
|
||||
level2: string
|
||||
level3: string
|
||||
level4: string
|
||||
level5: string
|
||||
protocol: string
|
||||
zoneName: string
|
||||
// aliases
|
||||
topLevel: string
|
||||
domainName
|
||||
subDomain
|
||||
constructor(domainStringArg: string) {
|
||||
let regexMatches = domainRegex(domainStringArg);
|
||||
this.fullName = ''
|
||||
for (let i = 1; i <= 5; i++) {
|
||||
if (regexMatches[ i - 1 ]) {
|
||||
let localMatch = regexMatches[ i - 1 ]
|
||||
this[ 'level' + i.toString() ] = localMatch
|
||||
if (this.fullName === '') {
|
||||
this.fullName = localMatch
|
||||
} else {
|
||||
this.fullName = localMatch + '.' + this.fullName
|
||||
}
|
||||
} else {
|
||||
this[ 'level' + i.toString() ] = undefined
|
||||
};
|
||||
};
|
||||
this.protocol = protocolRegex(domainStringArg)
|
||||
this.zoneName = this.level2 + '.' + this.level1
|
||||
|
||||
// aliases
|
||||
this.topLevel = this.level1
|
||||
this.domainName = this.level2
|
||||
this.subDomain = this.level3
|
||||
}
|
||||
// aliases
|
||||
this.topLevel = this.level1
|
||||
this.domainName = this.level2
|
||||
this.subDomain = this.level3
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
let domainRegex = function(stringArg:string){
|
||||
let 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}$/
|
||||
let 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
|
||||
let regexMatchesFiltered = regexMatches.filter(function(stringArg:string){
|
||||
return(stringArg !== '')
|
||||
});
|
||||
return regexMatchesFiltered
|
||||
let domainRegex = function (stringArg: string) {
|
||||
let 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}$/
|
||||
let 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
|
||||
let regexMatchesFiltered = regexMatches.filter(function (stringArg: string) {
|
||||
return (stringArg !== '')
|
||||
});
|
||||
return regexMatchesFiltered
|
||||
};
|
||||
|
||||
let protocolRegex = function(stringArg:string){
|
||||
let regexString = /^([a-zA-Z0-9]*):\/\//
|
||||
let regexMatches = regexString.exec(stringArg)
|
||||
if(regexMatches) {
|
||||
return regexMatches[1]
|
||||
} else {
|
||||
return undefined
|
||||
}
|
||||
let protocolRegex = function (stringArg: string) {
|
||||
let regexString = /^([a-zA-Z0-9]*):\/\//
|
||||
let regexMatches = regexString.exec(stringArg)
|
||||
if (regexMatches) {
|
||||
return regexMatches[ 1 ]
|
||||
} else {
|
||||
return undefined
|
||||
}
|
||||
}
|
||||
|
@ -4,49 +4,49 @@ import * as plugins from './smartstring.plugins'
|
||||
* ------------------ classes ------------------- *
|
||||
* ---------------------------------------------- */
|
||||
export class GitRepo {
|
||||
host: string
|
||||
user: string
|
||||
repo: string
|
||||
accessToken: string
|
||||
sshUrl: string
|
||||
httpsUrl: string
|
||||
constructor(stringArg: string,tokenArg?: string) {
|
||||
let regexMatches = gitRegex(stringArg)
|
||||
this.host = regexMatches[1]
|
||||
this.user = regexMatches[2]
|
||||
this.repo = regexMatches[3]
|
||||
this.accessToken = tokenArg
|
||||
this.sshUrl = gitLink(this.host,this.user,this.repo,this.accessToken, 'ssh')
|
||||
this.httpsUrl = gitLink(this.host,this.user,this.repo,this.accessToken, 'https')
|
||||
}
|
||||
host: string
|
||||
user: string
|
||||
repo: string
|
||||
accessToken: string
|
||||
sshUrl: string
|
||||
httpsUrl: string
|
||||
constructor (stringArg: string, tokenArg?: string) {
|
||||
let regexMatches = gitRegex(stringArg)
|
||||
this.host = regexMatches[ 1 ]
|
||||
this.user = regexMatches[ 2 ]
|
||||
this.repo = regexMatches[ 3 ]
|
||||
this.accessToken = tokenArg
|
||||
this.sshUrl = gitLink(this.host, this.user, this.repo, this.accessToken, 'ssh')
|
||||
this.httpsUrl = gitLink(this.host, this.user, this.repo, this.accessToken, 'https')
|
||||
}
|
||||
}
|
||||
|
||||
/* ---------------------------------------------- *
|
||||
* ------------------ helpers ------------------- *
|
||||
* ---------------------------------------------- */
|
||||
let gitRegex = function(stringArg:string){
|
||||
let regexString = /([a-zA-Z0-9\-\.]*)(?:\/|\:)([a-zA-Z0-9\-\.]*)(?:\/)([a-zA-Z0-9\-\.]*)(?:\.git)/
|
||||
let regexMatches = regexString.exec(stringArg)
|
||||
return regexMatches
|
||||
let gitRegex = function (stringArg: string) {
|
||||
let regexString = /([a-zA-Z0-9\-\.]*)(?:\/|\:)([a-zA-Z0-9\-\.]*)(?:\/)([a-zA-Z0-9\-\.]*)(?:\.git)/
|
||||
let regexMatches = regexString.exec(stringArg)
|
||||
return regexMatches
|
||||
}
|
||||
|
||||
let gitLink = function(hostArg: string, userArg: string, repoArg: string, tokenArg: string = '', linkTypeArg): string{
|
||||
let returnString
|
||||
if (tokenArg !== '') {
|
||||
tokenArg = tokenArg + '@'
|
||||
}
|
||||
switch (linkTypeArg) {
|
||||
case 'https':
|
||||
returnString = 'https://' +
|
||||
tokenArg + hostArg + '/' + userArg + '/' + repoArg + '.git'
|
||||
break
|
||||
case 'ssh':
|
||||
returnString = 'git@' +
|
||||
hostArg + ':' + userArg + '/' + repoArg + '.git';
|
||||
break
|
||||
default:
|
||||
console.error('Link Type ' + linkTypeArg + ' not known')
|
||||
break
|
||||
}
|
||||
return returnString
|
||||
let gitLink = function (hostArg: string, userArg: string, repoArg: string, tokenArg: string = '', linkTypeArg): string {
|
||||
let returnString
|
||||
if (tokenArg !== '') {
|
||||
tokenArg = tokenArg + '@'
|
||||
}
|
||||
switch (linkTypeArg) {
|
||||
case 'https':
|
||||
returnString = 'https://' +
|
||||
tokenArg + hostArg + '/' + userArg + '/' + repoArg + '.git'
|
||||
break
|
||||
case 'ssh':
|
||||
returnString = 'git@' +
|
||||
hostArg + ':' + userArg + '/' + repoArg + '.git'
|
||||
break
|
||||
default:
|
||||
console.error('Link Type ' + linkTypeArg + ' not known')
|
||||
break
|
||||
}
|
||||
return returnString
|
||||
}
|
||||
|
@ -1,64 +1,90 @@
|
||||
import * as plugins from './smartstring.plugins'
|
||||
|
||||
let splitString = (stringArg: string): string[] => {
|
||||
let resultArray = stringArg.split('\n')
|
||||
return cleanStringArray(resultArray)
|
||||
};
|
||||
|
||||
let joinString = (stringArrayArg: string[]): string => {
|
||||
let resultString: string = ''
|
||||
for (let line of stringArrayArg){
|
||||
resultString = resultString + line + '\n'
|
||||
}
|
||||
return resultString
|
||||
/**
|
||||
* splits a string into an array
|
||||
* @param stringArg
|
||||
*/
|
||||
let splitStringAtLineBreak = (stringArg: string): string[] => {
|
||||
let resultArray = stringArg.split('\n')
|
||||
return cleanStringArray(resultArray)
|
||||
}
|
||||
|
||||
/**
|
||||
* joins a string together again
|
||||
* @param stringArrayArg
|
||||
*/
|
||||
let joinStringWithLineBreaks = (stringArrayArg: string[]): string => {
|
||||
let resultString: string = ''
|
||||
for (let line of stringArrayArg) {
|
||||
resultString = resultString + line + '\n' // add new line at end
|
||||
}
|
||||
return resultString
|
||||
}
|
||||
|
||||
/**
|
||||
* cleans first and last line in case they are empty
|
||||
* @param stringArrayArg
|
||||
*/
|
||||
let cleanStringArray = (stringArrayArg: string[]): string[] => {
|
||||
let testRegex = /^[\s]*$/
|
||||
if (testRegex.test(stringArrayArg[0])) {
|
||||
stringArrayArg.shift()
|
||||
}
|
||||
if (testRegex.test(stringArrayArg[stringArrayArg.length - 1])) {
|
||||
stringArrayArg.pop()
|
||||
};
|
||||
return stringArrayArg
|
||||
let testRegex = /^[\s]*$/
|
||||
if (testRegex.test(stringArrayArg[ 0 ])) {
|
||||
stringArrayArg.shift()
|
||||
}
|
||||
if (testRegex.test(stringArrayArg[ stringArrayArg.length - 1 ])) {
|
||||
stringArrayArg.pop()
|
||||
}
|
||||
return stringArrayArg
|
||||
}
|
||||
|
||||
/**
|
||||
* indent an array
|
||||
* @param stringArg
|
||||
* @param spaceAmount
|
||||
*/
|
||||
export let indent = (stringArg: string, spaceAmount: number): string => {
|
||||
let resultString: string
|
||||
return resultString
|
||||
let localStringArray = splitStringAtLineBreak(stringArg)
|
||||
for (let stringArg of localStringArray) {
|
||||
stringArg = ' '.repeat(spaceAmount) + stringArg
|
||||
}
|
||||
let resultString = joinStringWithLineBreaks(localStringArray)
|
||||
return resultString
|
||||
}
|
||||
|
||||
export let indentWithPrefix = (stringArg: string,prefixArg: string): string => {
|
||||
let resultString: string
|
||||
let stringArray = splitString(stringArg)
|
||||
let resultArray: string[] = []
|
||||
for (let stringItem of stringArray){
|
||||
resultArray.push(prefixArg + stringItem)
|
||||
};
|
||||
resultString = joinString(resultArray)
|
||||
return resultString
|
||||
/**
|
||||
* indents a string with prefix
|
||||
* @param stringArg
|
||||
* @param prefixArg
|
||||
*/
|
||||
export let indentWithPrefix = (stringArg: string, prefixArg: string): string => {
|
||||
let resultString: string
|
||||
let stringArray = splitStringAtLineBreak(stringArg)
|
||||
let resultArray: string[] = []
|
||||
for (let stringItem of stringArray) {
|
||||
resultArray.push(prefixArg + stringItem)
|
||||
}
|
||||
resultString = joinStringWithLineBreaks(resultArray)
|
||||
return resultString
|
||||
}
|
||||
|
||||
export let normalize = (stringArg: string): string => {
|
||||
let resultString: string
|
||||
let splitStringArray: string[] = splitString(stringArg)
|
||||
let minCommonLeftOffset: number
|
||||
let deIndentRegex = /^(\s*)/
|
||||
let emptyLineRegex = /^(\s*)$/
|
||||
for (let stringItem of splitStringArray){
|
||||
let offsetString = deIndentRegex.exec(stringItem)[1]
|
||||
if (
|
||||
(typeof minCommonLeftOffset === 'undefined' || offsetString.length < minCommonLeftOffset)
|
||||
&& !emptyLineRegex.test(stringItem)
|
||||
) {
|
||||
minCommonLeftOffset = offsetString.length
|
||||
}
|
||||
};
|
||||
let resultSplitStringArray = []
|
||||
for (let stringItem of splitStringArray) {
|
||||
resultSplitStringArray.push(stringItem.substr(minCommonLeftOffset))
|
||||
};
|
||||
resultString = joinString(resultSplitStringArray)
|
||||
return resultString
|
||||
let resultString: string
|
||||
let splitStringArray: string[] = splitStringAtLineBreak(stringArg)
|
||||
let minCommonLeftOffset: number
|
||||
let deIndentRegex = /^(\s*)/
|
||||
let emptyLineRegex = /^(\s*)$/
|
||||
for (let stringItem of splitStringArray) {
|
||||
let offsetString = deIndentRegex.exec(stringItem)[ 1 ]
|
||||
if (
|
||||
(typeof minCommonLeftOffset === 'undefined' || offsetString.length < minCommonLeftOffset)
|
||||
&& !emptyLineRegex.test(stringItem)
|
||||
) {
|
||||
minCommonLeftOffset = offsetString.length
|
||||
}
|
||||
};
|
||||
let resultSplitStringArray = []
|
||||
for (let stringItem of splitStringArray) {
|
||||
resultSplitStringArray.push(stringItem.substr(minCommonLeftOffset))
|
||||
}
|
||||
resultString = joinStringWithLineBreaks(resultSplitStringArray)
|
||||
return resultString
|
||||
}
|
||||
|
22
ts/smartstring.normalize.ts
Normal file
22
ts/smartstring.normalize.ts
Normal file
@ -0,0 +1,22 @@
|
||||
import * as plugins from './smartstring.plugins'
|
||||
|
||||
/**
|
||||
* replaces all occurences of something in a string
|
||||
* @param stringArg
|
||||
* @param searchRegExp
|
||||
* @param replacementString
|
||||
*/
|
||||
export let replaceAll = (stringArg: string, searchRegExp: any, replacementString: string) => {
|
||||
return stringArg.replace(new RegExp(searchRegExp, 'g'), replacementString)
|
||||
}
|
||||
|
||||
/**
|
||||
* normalizes a string
|
||||
* @param stringArg
|
||||
*/
|
||||
export let standard = (stringArg: string): string => {
|
||||
let fix1 = plugins.stripIndent(stringArg) // fix indention
|
||||
let fix2 = plugins.normalizeNewline(fix1) // fix newlines
|
||||
let fix3 = replaceAll(fix2, /\t/, ' ') // fix tabs
|
||||
return fix3
|
||||
}
|
@ -1,2 +1,5 @@
|
||||
import 'typings-global'
|
||||
export let jsBase64 = require('js-base64').Base64
|
||||
|
||||
export let stripIndent = require('strip-indent')
|
||||
export let normalizeNewline = require('normalize-newline')
|
||||
|
Reference in New Issue
Block a user