typescript-string-operations
Advanced tools
Comparing version
{ | ||
"name": "typescript-string-operations", | ||
"version": "1.0.4", | ||
"version": "1.1.0", | ||
"description": "Simple lightweight string operation library for Typescript", | ||
"main": "source.ts", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "mocha -r ts-node/register tests/**/tests.ts", | ||
"build": "gulp" | ||
}, | ||
@@ -18,3 +19,3 @@ "repository": { | ||
"author": "Sven Ulrich <sven_ulrich@gmx.net> (holdmybeer.de)", | ||
"license": "UNLICENSED", | ||
"license": "MIT", | ||
"bugs": { | ||
@@ -26,4 +27,15 @@ "url": "https://github.com/sevensc/typescript-string-operations/issues" | ||
"devDependencies": { | ||
"qunitjs": "^2.1.1" | ||
"@types/chai": "^4.0.1", | ||
"@types/mocha": "^2.2.41", | ||
"chai": "^4.0.2", | ||
"gulp": "^3.9.1", | ||
"gulp-typescript": "^3.2.0", | ||
"mocha": "^3.4.2", | ||
"qunitjs": "^2.1.1", | ||
"ts-node": "^3.2.0", | ||
"typescript": "^2.4.1" | ||
}, | ||
"directories": { | ||
"test": "tests" | ||
} | ||
} |
@@ -63,20 +63,11 @@ [](https://www.npmjs.com/package/typescript-string-operations) | ||
#### Simply reference compiled `source.js` in your project. | ||
```javascript | ||
<script type="text/javascript" src="node_modules/source/source.js"></script> | ||
``` | ||
## Methods | ||
| Method | Type | Description | Parameter | | ||
| ------------- |:-------------:|:-------------:| :-----| | ||
| `Empty` | `Property` | simply returns `""`. | | ||
| `IsNullOrWhiteSpace` | `Method` | returns true value if given parameter is either null, empty or undefined. | `format`, `args` | ||
| Method | Type | Description | Parameter | | ||
| :------------------------:|:-----------:|:--------------------------:|:----------:| | ||
| `Empty` | `Property` | simply returns `""`. | | ||
| `IsNullOrWhiteSpace` | `Method` | returns true value if given parameter is either null, empty or undefined. | `format`, `args` | ||
| `Format` | `Method` | Converts the value of objects to strings based on the formats specified and inserts them into another string. | `format`, `args` | ||
| `Join` | `Method` | Combines arguments delimited by given seperator.| `delimiter`,`args` | ||
| `Join` | `Method` | Combines arguments delimited by given seperator from array. | `delimiter`,`array` | ||
| | ||
| `Format` | `Method` | Converts the value of objects to strings based on the formats specified and inserts them into another string. | `format`, `args` | ||
| `Join` | `Method` | Combines arguments delimited by given seperator.| `delimiter`,`args` | ||
| `Join` | `Method` | Combines arguments delimited by given seperator from array. | `delimiter`,`array` | ||
| |
@@ -0,181 +1,218 @@ | ||
export class StringBuilder { | ||
public Values: string[] = []; | ||
declare var $; | ||
export class String { | ||
public static Empty: string = ""; | ||
constructor(value: string = String.Empty) { | ||
this.Values = new Array(value); | ||
} | ||
public ToString() { | ||
return this.Values.join(''); | ||
} | ||
public Append(value: string) { | ||
this.Values.push(value); | ||
} | ||
public AppendFormat(value: string, ...args: string[]) { | ||
this.Values.push(String.Format(value, ...args)); | ||
} | ||
public Clear() { | ||
this.Values = []; | ||
} | ||
} | ||
public static IsNullOrWhiteSpace(value: string): boolean { | ||
try { | ||
if (value == null || value == 'undefined') | ||
return true; | ||
return value.toString().replace(/\s/g, '').length < 1; | ||
} | ||
catch (e) { | ||
console.log(e); | ||
return false; | ||
} | ||
} | ||
export class String { | ||
public static Empty: string = ""; | ||
public static Join(delimiter, ...args): string { | ||
try { | ||
if ($.isArray(args[0]) || args[0] === typeof Array) { | ||
var tempString = String.Empty; | ||
var count = 0; | ||
public static IsNullOrWhiteSpace(value: string): boolean { | ||
try { | ||
if (value == null || value == 'undefined') | ||
return true; | ||
for (var i = 0; i < args[0].length; i++) { | ||
var current = args[0][i]; | ||
if (i < args[0].length - 1) | ||
tempString += current + delimiter; | ||
else | ||
tempString += current; | ||
} | ||
return value.toString().replace(/\s/g, '').length < 1; | ||
} | ||
catch (e) { | ||
console.log(e); | ||
return false; | ||
} | ||
} | ||
return tempString; | ||
} | ||
else if (typeof args[0] == 'object') { | ||
var tempString = String.Empty; | ||
var count = 0; | ||
$(args[0]).each(function () { | ||
if (count < args[0].length - 1) | ||
tempString += $(this).text() + delimiter; | ||
else | ||
tempString += $(this).text(); | ||
count++; | ||
}); | ||
public static Join(delimiter: string, ...args: (string | object | Array<any>)[]): string { | ||
try { | ||
let firstArg = args[0]; | ||
if (Array.isArray(firstArg) || firstArg instanceof Array) { | ||
let tempString = String.Empty; | ||
let count = 0; | ||
return tempString; | ||
for (let i = 0; i < firstArg.length; i++) { | ||
let current = firstArg[i]; | ||
if (i < firstArg.length - 1) | ||
tempString += current + delimiter; | ||
else | ||
tempString += current; | ||
} | ||
return String.join(delimiter, args); | ||
return tempString; | ||
} | ||
catch (e) { | ||
console.log(e); | ||
return String.Empty; | ||
else if (typeof firstArg === 'object') { | ||
let tempString = String.Empty; | ||
let objectArg = firstArg; | ||
let keys = Object.keys(firstArg); //get all Properties of the Object as Array | ||
keys.forEach(element => { tempString += (<any>objectArg)[element] + delimiter; }); | ||
tempString = tempString.slice(0, tempString.length - delimiter.length); //remove last delimiter | ||
return tempString; | ||
} | ||
let stringArray = <string[]>args; | ||
return String.join(delimiter, ...stringArray); | ||
} | ||
catch (e) { | ||
console.log(e); | ||
return String.Empty; | ||
} | ||
} | ||
public static Format(format, ...args): string { | ||
try { | ||
return format.replace(/{(\d+(:\w*)?)}/g, function (match, i) { //0 | ||
var s = match.split(':'); | ||
if (s.length > 1) { | ||
i = i[0]; | ||
match = s[1].replace('}', ''); //U | ||
} | ||
public static Format(format: string, ...args: (string | Date | number | any)[]): string { | ||
try { | ||
return format.replace(/{(\d+(:\w*)?)}/g, function (match, i) { //0 | ||
let s = match.split(':'); | ||
if (s.length > 1) { | ||
i = i[0]; | ||
match = s[1].replace('}', ''); //U | ||
} | ||
var arg = String.parsePattern(match, args[i]); | ||
return typeof arg != 'undefined' && arg != null ? arg : String.Empty; | ||
}); | ||
} | ||
catch (e) { | ||
console.log(e); | ||
return String.Empty; | ||
} | ||
let arg = String.parsePattern(match, args[i]); | ||
return typeof arg != 'undefined' && arg != null ? arg : String.Empty; | ||
}); | ||
} | ||
catch (e) { | ||
console.log(e); | ||
return String.Empty; | ||
} | ||
} | ||
private static parsePattern(match, arg): string { | ||
if (arg == null || arg == undefined) | ||
private static parsePattern(match: 'L' | 'U' | 'd' | 's' | 'n' | string, arg: string | Date | number | any): string { | ||
if (arg == null || arg == undefined) | ||
return arg; | ||
switch (match) { | ||
case 'L': | ||
arg = arg.toLowerCase(); | ||
return arg; | ||
switch (match) { | ||
case 'L': | ||
arg = arg.toLowerCase(); | ||
case 'U': | ||
arg = arg.toUpperCase(); | ||
return arg; | ||
case 'd': | ||
if (typeof (arg) === 'string') { | ||
return String.getDisplayDateFromString(arg); | ||
} | ||
else if (arg instanceof Date) { | ||
return String.Format('{0:00}.{1:00}.{2:0000}', arg.getDate(), arg.getMonth(), arg.getFullYear()); | ||
} | ||
break; | ||
case 's': | ||
if (typeof (arg) === 'string') { | ||
return String.getSortableDateFromString(arg); | ||
} | ||
else if (arg instanceof Date) { | ||
return String.Format('{0:0000}-{1:00}-{2:00}', arg.getFullYear(), arg.getMonth(), arg.getDate()); | ||
} | ||
break; | ||
case 'n': //Tausender Trennzeichen | ||
let replacedString = arg.replace(/,/g,'.'); | ||
if (isNaN(parseFloat(replacedString)) || replacedString.length <= 3) | ||
break; | ||
case 'U': | ||
arg = arg.toUpperCase(); | ||
break; | ||
case 'd': | ||
var splitted = arg.split('-'); | ||
if (splitted.length <= 1) | ||
return arg; | ||
var day = splitted[splitted.length - 1]; | ||
var month = splitted[splitted.length - 2]; | ||
var year = splitted[splitted.length - 3]; | ||
day = day.split('T')[0]; | ||
day = day.split(' ')[0]; | ||
let numberparts = replacedString.split(/[^0-9]+/g); | ||
let parts = numberparts; | ||
arg = day + '.' + month + '.' + year; | ||
break; | ||
case 's': | ||
var splitted = arg.replace(',', '').split('.'); | ||
if (splitted.length <= 1) | ||
return arg; | ||
if(numberparts.length > 1){ | ||
parts = [String.join('',...(numberparts.splice(0, numberparts.length -1 ))), numberparts[numberparts.length-1]]; | ||
} | ||
var time = splitted[splitted.length - 1].split(' '); | ||
if (time.length > 1) | ||
time = time[time.length - 1]; | ||
let integer = parts[0]; | ||
var year = splitted[splitted.length - 1].split(' ')[0]; | ||
var month = splitted[splitted.length - 2]; | ||
var day = splitted[splitted.length - 3]; | ||
var mod = integer.length % 3; | ||
var output = (mod > 0 ? (integer.substring(0, mod)) : String.Empty); | ||
var firstGroup = output; | ||
var remainingGroups = integer.substring(mod).match(/.{3}/g); | ||
output = output + '.' + String.Join('.',remainingGroups); | ||
arg = output + (parts.length > 1 ? ','+ parts[1] : ''); | ||
return arg; | ||
default: | ||
break; | ||
} | ||
arg = year + "-" + month + "-" + day; | ||
if (time.length > 1) | ||
arg += "T" + time; | ||
else | ||
arg += "T" + "00:00:00"; | ||
break; | ||
case 'n': //Tausender Trennzeichen | ||
if (isNaN(parseInt(arg)) || arg.length <= 3) | ||
break; | ||
if (typeof (arg) === 'number') | ||
return String.formatNumber(arg, match); | ||
arg = arg.toString(); | ||
var mod = arg.length % 3; | ||
var output = (mod > 0 ? (arg.substring(0, mod)) : String.Empty); | ||
for (var i = 0; i < Math.floor(arg.length / 3); i++) { | ||
if ((mod == 0) && (i == 0)) | ||
output += arg.substring(mod + 3 * i, mod + 3 * i + 3); | ||
else | ||
output += '.' + arg.substring(mod + 3 * i, mod + 3 * i + 3); | ||
} | ||
arg = output; | ||
break; | ||
default: | ||
break; | ||
} | ||
return arg; | ||
} | ||
return arg; | ||
} | ||
private static getDisplayDateFromString(input: string): string { | ||
let splitted: string[]; | ||
splitted = input.split('-'); | ||
private static join(delimiter, args): string { | ||
var temp = String.Empty; | ||
for (var i = 0; i < args.length; i++) { | ||
if (String.IsNullOrWhiteSpace(args[i]) || (typeof args[i] != "number" && typeof args[i] != "string")) | ||
continue; | ||
if (splitted.length <= 1) | ||
return input; | ||
var arg = "" + args[i]; | ||
temp += arg; | ||
for (var i2 = i + 1; i2 < args.length; i2++) { | ||
if (String.IsNullOrWhiteSpace(args[i2])) | ||
continue; | ||
let day = splitted[splitted.length - 1]; | ||
let month = splitted[splitted.length - 2]; | ||
let year = splitted[splitted.length - 3]; | ||
day = day.split('T')[0]; | ||
day = day.split(' ')[0]; | ||
temp += delimiter; | ||
i = i2 - 1; | ||
break; | ||
} | ||
} | ||
return temp; | ||
} | ||
return day + '.' + month + '.' + year; | ||
} | ||
export class StringBuilder { | ||
public Values = []; | ||
private static getSortableDateFromString(input: string): string { | ||
let splitted = input.replace(',', '').split('.'); | ||
if (splitted.length <= 1) | ||
return input; | ||
constructor(value: string = String.Empty) { | ||
this.Values = new Array(value); | ||
} | ||
let times = splitted[splitted.length - 1].split(' '); | ||
let time = splitted[0]; | ||
if (times.length > 1) | ||
time = times[times.length - 1]; | ||
public ToString() { | ||
return this.Values.join(''); | ||
let year = splitted[splitted.length - 1].split(' ')[0]; | ||
let month = splitted[splitted.length - 2]; | ||
let day = splitted[splitted.length - 3]; | ||
let result = year + "-" + month + "-" + day; | ||
if (time.length > 1) | ||
result += "T" + time; | ||
else | ||
result += "T" + "00:00:00"; | ||
return result; | ||
} | ||
private static formatNumber(input: number, formatTemplate: string): string { | ||
let count = formatTemplate.length; | ||
let stringValue = input.toString(); | ||
if (count <= stringValue.length) | ||
return stringValue; | ||
let remainingCount = count - stringValue.length; | ||
remainingCount += 1; //Das Array muss einen Eintrag mehr als die benötigten Nullen besitzen | ||
return new Array(remainingCount).join('0') + stringValue; | ||
} | ||
private static join(delimiter: string, ...args: string[]): string { | ||
let temp = String.Empty; | ||
for (let i = 0; i < args.length; i++) { | ||
if ((typeof args[i] == 'string' && String.IsNullOrWhiteSpace(args[i])) || (typeof args[i] != "number" && typeof args[i] != "string")) | ||
continue; | ||
let arg = "" + args[i]; | ||
temp += arg; | ||
for (let i2 = i + 1; i2 < args.length; i2++) { | ||
if (String.IsNullOrWhiteSpace(args[i2])) | ||
continue; | ||
temp += delimiter; | ||
i = i2 - 1; | ||
break; | ||
} | ||
} | ||
public Append(value: string) { | ||
this.Values.push(value); | ||
} | ||
public AppendFormat(value: string, ...args) { | ||
this.Values.push(String.Format(value, args)); | ||
} | ||
public Clear() { | ||
this.Values = []; | ||
} | ||
return temp; | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Explicitly Unlicensed Item
License(Experimental) Something was found which is explicitly marked as unlicensed.
Found 1 instance in 1 package
Misc. License Issues
License(Experimental) A package's licensing information has fine-grained problems.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
26925
69.25%10
100%0
-100%0
-100%100
Infinity%544
63.86%0
-100%9
800%73
-10.98%1
Infinity%