Skip to content

Commit 3c90889

Browse files
committed
bugfix sortable dates, added String.Join example for objects to readme.md
1 parent e2d8315 commit 3c90889

File tree

5 files changed

+45
-20
lines changed

5 files changed

+45
-20
lines changed

dist/index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ var String = (function () {
117117
default:
118118
break;
119119
}
120-
if ((typeof (arg) === 'number' || !isNaN(arg)) && !isNaN(+match))
120+
if ((typeof (arg) === 'number' || !isNaN(arg)) && !isNaN(+match) && !String.IsNullOrWhiteSpace(arg))
121121
return String.formatNumber(arg, match);
122122
return arg;
123123
};
@@ -131,24 +131,24 @@ var String = (function () {
131131
var year = splitted[splitted.length - 3];
132132
day = day.split('T')[0];
133133
day = day.split(' ')[0];
134-
return day + '.' + month + '.' + year;
134+
return day + "." + month + "." + year;
135135
};
136136
String.getSortableDateFromString = function (input) {
137137
var splitted = input.replace(',', '').split('.');
138138
if (splitted.length <= 1)
139139
return input;
140140
var times = splitted[splitted.length - 1].split(' ');
141-
var time = splitted[0];
141+
var time = String.Empty;
142142
if (times.length > 1)
143143
time = times[times.length - 1];
144144
var year = splitted[splitted.length - 1].split(' ')[0];
145145
var month = splitted[splitted.length - 2];
146146
var day = splitted[splitted.length - 3];
147147
var result = year + "-" + month + "-" + day;
148-
if (time.length > 1)
148+
if (!String.IsNullOrWhiteSpace(time) && time.length > 1)
149149
result += "T" + time;
150150
else
151-
result += "T" + "00:00:00";
151+
result += "T00:00:00";
152152
return result;
153153
};
154154
String.formatNumber = function (input, formatTemplate) {

dist/index.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export class String {
4949
}
5050
}
5151

52-
public static Format(format: string, ...args: (string | Date | number | any)[]): string {
52+
public static Format(format: string, ...args: any[]): string {
5353
try {
5454
return format.replace(/{(\d+(:\w*)?)}/g, function (match, i) { //0
5555
let s = match.split(':');
@@ -61,7 +61,7 @@ export class String {
6161
let arg = args[i];
6262
if (arg == null || arg == undefined || match.match(/{d+}/))
6363
return arg;
64-
64+
6565
arg = String.parsePattern(match, arg);
6666
return typeof arg != 'undefined' && arg != null ? arg : String.Empty;
6767
});
@@ -120,8 +120,8 @@ export class String {
120120
default:
121121
break;
122122
}
123-
124-
if ((typeof (arg) === 'number' || !isNaN(arg)) && !isNaN(+match))
123+
124+
if ((typeof (arg) === 'number' || !isNaN(arg)) && !isNaN(+match) && !String.IsNullOrWhiteSpace(arg))
125125
return String.formatNumber(arg, match);
126126

127127
return arg;
@@ -140,7 +140,7 @@ export class String {
140140
day = day.split('T')[0];
141141
day = day.split(' ')[0];
142142

143-
return day + '.' + month + '.' + year;
143+
return `${day}.${month}.${year}`;
144144
}
145145

146146
private static getSortableDateFromString(input: string): string {
@@ -149,19 +149,20 @@ export class String {
149149
return input;
150150

151151
let times = splitted[splitted.length - 1].split(' ');
152-
let time = splitted[0];
152+
let time = String.Empty;
153153
if (times.length > 1)
154-
time = times[times.length - 1];
154+
time = times[times.length - 1];
155155

156156
let year = splitted[splitted.length - 1].split(' ')[0];
157157
let month = splitted[splitted.length - 2];
158158
let day = splitted[splitted.length - 3];
159-
160-
let result = year + "-" + month + "-" + day;
161-
if (time.length > 1)
162-
result += "T" + time;
159+
let result = `${year}-${month}-${day}`
160+
161+
if (!String.IsNullOrWhiteSpace(time) && time.length > 1)
162+
result += `T${time}`;
163163
else
164-
result += "T" + "00:00:00";
164+
result += "T00:00:00";
165+
165166
return result;
166167
}
167168

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.3",
3+
"version": "1.2.4",
44
"description": "Simple lightweight string operation library for Typescript, works with Angular",
55
"main": "dist/index.min.js",
66
"scripts": {

readme.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ var value = String.Join("; ", "Apple", "Banana");
6565
#### OR
6666

6767
```typescript
68+
let object = { Name: "Foo", Value: "Bar" };
69+
var value = String.Join('.', object);
70+
//output: "Foo.Bar";
71+
6872
var array = ['Apple', 'Banana']
6973
var value = String.Join("; ", array);
7074
//output: "Apple; Banana";

tests/tests.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ describe('String.Format', () => {
6363

6464
let expectedValue = "13.04.2017";
6565
let result = String.Format(template, valueToInsert);
66+
console.log(result);
6667
expect(result).to.equal(expectedValue);
6768
});
6869

@@ -72,6 +73,7 @@ describe('String.Format', () => {
7273

7374
let expectedValue = "2017-04-13";
7475
let result = String.Format(template, valueToInsert);
76+
console.log(result);
7577
expect(result).to.equal(expectedValue);
7678
});
7779

@@ -81,6 +83,7 @@ describe('String.Format', () => {
8183

8284
let expectedValue = "23.01.2017";
8385
let result = String.Format(template, valueToInsert);
86+
console.log(result);
8487
expect(result).to.equal(expectedValue);
8588
});
8689

@@ -90,6 +93,17 @@ describe('String.Format', () => {
9093

9194
let expectedValue = "2017-03-21T22:15:01";
9295
let result = String.Format(template, valueToInsert);
96+
console.log(result);
97+
expect(result).to.equal(expectedValue);
98+
});
99+
100+
it('should set the correct sortable date without time using string', () => {
101+
let template = "{0:s}";
102+
let valueToInsert = '21.03.2017';
103+
104+
let expectedValue = "2017-03-21T00:00:00";
105+
let result = String.Format(template, valueToInsert);
106+
console.log(result);
93107
expect(result).to.equal(expectedValue);
94108
});
95109
});
@@ -123,7 +137,7 @@ describe('String.Format', () => {
123137
let result = String.Format(template, 5);
124138
expect(result).to.equal('5');
125139
});
126-
140+
127141
it('should pad 5 to 05 using {0:00}', () => {
128142
let template = '{0:00}';
129143
let result = String.Format(template, 5);
@@ -136,6 +150,12 @@ describe('String.Format', () => {
136150
expect(result).to.equal('005');
137151
});
138152

153+
it('should ignore padding when input is longer then template', () => {
154+
let template = '{0:000}';
155+
let result = String.Format(template, 50000);
156+
expect(result).to.equal('50000');
157+
});
158+
139159
it('should set the correct thousands seperator', () => {
140160
let template = '{0:n}';
141161
let valueToInsert = '10000000000';
@@ -176,9 +196,9 @@ describe('String.Join', () => {
176196

177197
it('should join the given object', () => {
178198
let object = { Name: "Foo", Value: "Bar" };
179-
180199
let result = String.Join('.', object);
181200

201+
console.log(result);
182202
expect(result).to.equal("Foo.Bar");
183203
});
184204
});

0 commit comments

Comments
 (0)