Skip to content

Commit a9aba12

Browse files
committed
introduced formatting for objects,increased performance,specifier bugfix
1 parent bb9af9b commit a9aba12

File tree

7 files changed

+131
-39
lines changed

7 files changed

+131
-39
lines changed

dist/index.js

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,24 +55,36 @@ var String = (function () {
5555
args[_i - 1] = arguments[_i];
5656
}
5757
try {
58-
return format.replace(/{(\d+(:\w*)?)}/g, function (match, i) {
59-
var s = match.split(':');
60-
if (s.length > 1) {
61-
i = i[0];
62-
match = s[1].replace('}', '');
63-
}
64-
var arg = args[i];
65-
if (arg == null || arg == undefined || match.match(/{d+}/))
66-
return arg;
67-
arg = String.parsePattern(match, arg);
68-
return typeof arg != 'undefined' && arg != null ? arg : String.Empty;
69-
});
58+
if (format.match(String.regexNumber))
59+
return String.format(String.regexNumber, format, args);
60+
if (format.match(String.regexObject))
61+
return String.format(String.regexObject, format, args, true);
62+
return String.Empty;
7063
}
7164
catch (e) {
7265
console.log(e);
7366
return String.Empty;
7467
}
7568
};
69+
String.format = function (regex, format, args, parseByObject) {
70+
if (parseByObject === void 0) { parseByObject = false; }
71+
return format.replace(regex, function (match, x) {
72+
var s = match.split(':');
73+
if (s.length > 1) {
74+
x = s[0].replace('{', '');
75+
match = s[1].replace('}', '');
76+
}
77+
var arg;
78+
if (parseByObject)
79+
arg = args[0][x];
80+
else
81+
arg = args[x];
82+
if (arg == null || arg == undefined || match.match(/{\d+}/))
83+
return arg;
84+
arg = String.parsePattern(match, arg);
85+
return typeof arg != 'undefined' && arg != null ? arg : String.Empty;
86+
});
87+
};
7688
String.parsePattern = function (match, arg) {
7789
switch (match) {
7890
case 'L':
@@ -98,6 +110,8 @@ var String = (function () {
98110
}
99111
break;
100112
case 'n':
113+
if (typeof (arg) !== "string")
114+
arg = arg.toString();
101115
var replacedString = arg.replace(/,/g, '.');
102116
if (isNaN(parseFloat(replacedString)) || replacedString.length <= 3)
103117
break;
@@ -181,7 +195,9 @@ var String = (function () {
181195
}
182196
return temp;
183197
};
184-
String.Empty = "";
198+
String.regexNumber = /{(\d+(:\w*)?)}/g;
199+
String.regexObject = /{(\w+(:\w*)?)}/g;
200+
String.Empty = '';
185201
return String;
186202
}());
187203
exports.String = String;

dist/index.ts

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
export class String {
2-
public static Empty: string = "";
2+
private static readonly regexNumber = /{(\d+(:\w*)?)}/g;
3+
private static readonly regexObject = /{(\w+(:\w*)?)}/g;
4+
5+
public static Empty: string = '';
36

47
public static IsNullOrWhiteSpace(value: string): boolean {
58
try {
@@ -51,27 +54,42 @@ export class String {
5154

5255
public static Format(format: string, ...args: any[]): string {
5356
try {
54-
return format.replace(/{(\d+(:\w*)?)}/g, function (match, i) { //0
55-
let s = match.split(':');
56-
if (s.length > 1) {
57-
i = i[0];
58-
match = s[1].replace('}', ''); //U
59-
}
57+
if (format.match(String.regexNumber))
58+
return String.format(String.regexNumber, format, args);
59+
60+
if (format.match(String.regexObject))
61+
return String.format(String.regexObject, format, args, true);
6062

61-
let arg = args[i];
62-
if (arg == null || arg == undefined || match.match(/{d+}/))
63-
return arg;
64-
65-
arg = String.parsePattern(match, arg);
66-
return typeof arg != 'undefined' && arg != null ? arg : String.Empty;
67-
});
63+
return String.Empty;
6864
}
6965
catch (e) {
7066
console.log(e);
7167
return String.Empty;
7268
}
7369
}
7470

71+
private static format(regex: any, format: string, args: any, parseByObject: boolean = false): string {
72+
return format.replace(regex, function (match, x) { //0
73+
let s = match.split(':');
74+
if (s.length > 1) {
75+
x = s[0].replace('{', '');
76+
match = s[1].replace('}', ''); //U
77+
}
78+
79+
let arg;
80+
if (parseByObject)
81+
arg = args[0][x];
82+
else
83+
arg = args[x];
84+
85+
if (arg == null || arg == undefined || match.match(/{\d+}/))
86+
return arg;
87+
88+
arg = String.parsePattern(match, arg);
89+
return typeof arg != 'undefined' && arg != null ? arg : String.Empty;
90+
});
91+
}
92+
7593
private static parsePattern(match: 'L' | 'U' | 'd' | 's' | 'n' | string, arg: string | Date | number | any): string {
7694
switch (match) {
7795
case 'L':
@@ -97,15 +115,17 @@ export class String {
97115
}
98116
break;
99117
case 'n': //Tausender Trennzeichen
100-
let replacedString = arg.replace(/,/g,'.');
118+
if (typeof (arg) !== "string")
119+
arg = arg.toString();
120+
let replacedString = arg.replace(/,/g, '.');
101121
if (isNaN(parseFloat(replacedString)) || replacedString.length <= 3)
102122
break;
103123

104124
let numberparts = replacedString.split(/[^0-9]+/g);
105125
let parts = numberparts;
106126

107-
if(numberparts.length > 1){
108-
parts = [String.join('',...(numberparts.splice(0, numberparts.length -1 ))), numberparts[numberparts.length-1]];
127+
if (numberparts.length > 1) {
128+
parts = [String.join('', ...(numberparts.splice(0, numberparts.length - 1))), numberparts[numberparts.length - 1]];
109129
}
110130

111131
let integer = parts[0];
@@ -114,8 +134,8 @@ export class String {
114134
var output = (mod > 0 ? (integer.substring(0, mod)) : String.Empty);
115135
var firstGroup = output;
116136
var remainingGroups = integer.substring(mod).match(/.{3}/g);
117-
output = output + '.' + String.Join('.',remainingGroups);
118-
arg = output + (parts.length > 1 ? ','+ parts[1] : '');
137+
output = output + '.' + String.Join('.', remainingGroups);
138+
arg = output + (parts.length > 1 ? ',' + parts[1] : '');
119139
return arg;
120140
default:
121141
break;
@@ -151,24 +171,24 @@ export class String {
151171
let times = splitted[splitted.length - 1].split(' ');
152172
let time = String.Empty;
153173
if (times.length > 1)
154-
time = times[times.length - 1];
174+
time = times[times.length - 1];
155175

156176
let year = splitted[splitted.length - 1].split(' ')[0];
157177
let month = splitted[splitted.length - 2];
158178
let day = splitted[splitted.length - 3];
159179
let result = `${year}-${month}-${day}`
160-
180+
161181
if (!String.IsNullOrWhiteSpace(time) && time.length > 1)
162182
result += `T${time}`;
163183
else
164-
result += "T00:00:00";
184+
result += "T00:00:00";
165185

166186
return result;
167187
}
168188

169189
private static formatNumber(input: number, formatTemplate: string): string {
170190
let count = formatTemplate.length;
171-
let stringValue = input.toString();
191+
let stringValue = input.toString();
172192
if (count <= stringValue.length)
173193
return stringValue;
174194

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "typescript-string-operations",
3-
"version": "1.2.4",
3+
"version": "1.3.0",
44
"description": "Simple lightweight string operation library for Typescript, works with Angular",
55
"main": "dist/index.min.js",
66
"scripts": {

readme.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,22 @@ value = String.Format("{0:00}", 1);
4545
//output "01"
4646
```
4747

48+
## UPDATE
49+
#### String Format for Objects including specifiers
50+
51+
```typescript
52+
var fruit = new Fruit();
53+
fruit.type = "apple";
54+
fruit.color = "RED";
55+
fruit.shippingDate = "";
56+
fruit.amount = 10000;
57+
58+
String.Format("the {type:U} is {color:L} shipped on {shippingDate:s} with an amount of {amount:n}", fruit);
59+
// output: the APPLE is red shipped on with an amount of 10.000
60+
61+
```
62+
63+
4864
| Specifier | Result |
4965
| :-------------: |:---------------------------:|
5066
| `L` | LowerCase |

tests/fruit.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export class Fruit {
2+
public type: string;
3+
public color: string;
4+
public shippingDate: string | Date;
5+
public amount: number | string;
6+
7+
constructor(type: string, color: string, shippingDate: string | Date, amount: number | string) {
8+
this.type = type;
9+
this.color = color;
10+
this.shippingDate = shippingDate;
11+
this.amount = amount;
12+
}
13+
}

tests/test.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<html>
22
<script>var exports = {};</script>
3-
<script src="../dist/index.js"></script>
3+
<script src="../dist/index.js"></script>
44
</html>

tests/tests.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { String } from '../dist/index';
2+
import { Fruit } from './fruit';
23
import { expect } from 'chai';
34
import 'mocha';
45

@@ -29,7 +30,7 @@ describe('String.IsNullOrWhitespace', () => {
2930
});
3031
});
3132

32-
describe('String.Format', () => {
33+
describe('String.Format Number Pattern', () => {
3334
describe('Placeholders', () => {
3435
it('should format the string correct', () => {
3536
let template = "{0}";
@@ -179,6 +180,32 @@ describe('String.Format', () => {
179180

180181
});
181182

183+
describe('String.Format Text Pattern', () => {
184+
describe("formatting", () => {
185+
it("Should parse out the word", () => {
186+
// Arrange
187+
var fruit = { type: "apple", color: "red" };
188+
189+
// Act
190+
var formatted = String.Format("the {type} is {color}", fruit);
191+
192+
// Assert
193+
expect(formatted).to.equal("the apple is red");
194+
});
195+
196+
it("Should parse out the word with specifiers and TS Class", () => {
197+
// Arrange
198+
var fruit: Fruit = new Fruit("apple", "RED", "31.12.2018 01:02:03", "10000");
199+
200+
// Act
201+
var formatted = String.Format("the {type:U} is {color:L} shipped on {shippingDate:s} with an amount of {amount:n}", fruit);
202+
203+
// Assert
204+
expect(formatted).to.equal("the APPLE is red shipped on 2018-12-31T01:02:03 with an amount of 10.000");
205+
});
206+
});
207+
});
208+
182209
describe('String.Join', () => {
183210
it('should join the given strings passed as args', () => {
184211
let stringOne = "red", stringTwo = "yellow", stringThree = "blue";

0 commit comments

Comments
 (0)