Skip to content

Commit 97241c5

Browse files
author
Sven Ulrich
committed
updatedeps: make methods usable without class
1 parent 62410dc commit 97241c5

File tree

12 files changed

+134
-145
lines changed

12 files changed

+134
-145
lines changed

.gitignore

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
node_modules
22
.vscode/*
33

4-
dist/*.js
54

65
# Compiled source #
76
###################
@@ -12,7 +11,8 @@ dist/*.js
1211
*.o
1312
*.so
1413
*.min.js
15-
14+
*.d.ts.map
15+
dist
1616
# Packages #
1717
############
1818
# it's better to unpack these files and commit the raw source
@@ -40,4 +40,4 @@ dist/*.js
4040
.Spotlight-V100
4141
.Trashes
4242
ehthumbs.db
43-
Thumbs.db
43+
Thumbs.db

README.md

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,50 +10,51 @@
1010

1111

1212
```typescript
13-
import { String, StringBuilder } from 'typescript-string-operations';
13+
import { StringBuilder, join, format, isNullOrWhiteSpace } from 'typescript-string-operations';
1414
```
1515

16-
I recently got feedback that sometimes there are issues when using `String`, since its actually replacing the native `String` object from JavaScript. You can use `$String` instead.
16+
When migrating from Version 1.4.1 or lower, you can also import the class `String`. Using this String class would override the native `String` object from JavaScript. We will remove this declaration with the next major release
1717

1818
```typescript
19-
import { $String } from 'typescript-string-operations';
19+
import { String } from 'typescript-string-operations';
2020
```
2121

2222
#### USAGE:
2323

2424
### String.empty
2525
```typescript
26-
var id = String.empty;
26+
var id = empty; // or String.empty
27+
// output: id = '';
2728
```
2829

2930
### String.isNullOrWhiteSpace():
3031
```typescript
3132
var id = image.GetId();
32-
if(String.isNullOrWhiteSpace(id))
33+
if(isNullOrWhiteSpace(id)) // String.isNullOrWhiteSpace(id)
3334
return image;
3435
```
3536
### String.format():
3637

3738
```typescript
3839
var id = image.GetId()
39-
String.format("image_{0}.jpg", id)
40+
format("image_{0}.jpg", id) // or String.format()
4041
output: "image_2db5da20-1c5d-4f1a-8fd4-b41e34c8c5b5.jpg";
4142
```
4243

4344
Specifier available!
4445
```typescript
45-
var value = String.format("{0:L}", "APPLE"); //output "apple"
46+
var value = format("{0:L}", "APPLE"); //output "apple"
4647

47-
value = String.format("{0:U}", "apple"); // output "APPLE"
48+
value = format("{0:U}", "apple"); // output "APPLE"
4849

49-
value = String.format("{0:d}", "2017-01-23 00:00"); //output "23.01.2017"
50+
value = format("{0:d}", "2017-01-23 00:00"); //output "23.01.2017"
5051

51-
value = String.format("{0:s}", "21.03.2017 22:15:01") //output "2017-03-21T22:15:01"
52+
value = format("{0:s}", "21.03.2017 22:15:01") //output "2017-03-21T22:15:01"
5253

53-
value = String.format("{0:n}", 1000000);
54+
value = format("{0:n}", 1000000);
5455
//output "1.000.000"
5556

56-
value = String.format("{0:00}", 1);
57+
value = format("{0:00}", 1);
5758
//output "01"
5859
```
5960

@@ -67,7 +68,7 @@ fruit.color = "RED";
6768
fruit.shippingDate = new Date(2018, 1, 1);
6869
fruit.amount = 10000;
6970

70-
String.format("the {type:U} is {color:L} shipped on {shippingDate:s} with an amount of {amount:n}", fruit);
71+
format("the {type:U} is {color:L} shipped on {shippingDate:s} with an amount of {amount:n}", fruit);
7172
// output: the APPLE is red shipped on 2018-01-01 with an amount of 10.000
7273

7374
```
@@ -84,21 +85,21 @@ String.format("the {type:U} is {color:L} shipped on {shippingDate:s} with an amo
8485

8586

8687

87-
### String.Join():
88+
### String.join():
8889

8990
```typescript
90-
var value = String.join("; ", "Apple", "Banana");
91+
var value = join("; ", "Apple", "Banana"); // or String.join()
9192
//output: "Apple; Banana";
9293
```
9394
#### OR
9495

9596
```typescript
9697
let object = { Name: "Foo", Value: "Bar" };
97-
var value = String.join('.', object);
98+
var value = join('.', object);
9899
//output: "Foo.Bar";
99100

100101
var array = ['Apple', 'Banana']
101-
var value = String.join("; ", array);
102+
var value = join("; ", array);
102103
//output: "Apple; Banana";
103104
```
104105

@@ -140,8 +141,8 @@ var fruits = builder.ToString();
140141
| Method | Type | Description | Parameter |
141142
| :------------------------:|:-----------:|:--------------------------:|:----------:|
142143
| `Append` | `Method` | appends a string. | `value` |
143-
| `AppendFormat` | `Method` | see description for `String.format()`| `format`, `args`|
144+
| `AppendFormat` | `Method` | see description for `format()`| `format`, `args`|
144145
| `AppendLine` | `Method` | appends a string in a new line. | `format`, `args`|
145-
| `AppendLineFormat` | `Method` | like `String.format()` in a new line | `format`, `args`|
146+
| `AppendLineFormat` | `Method` | like `format()` in a new line | `format`, `args`|
146147
| `Clear` | `Method` | clears the `StringBuilder` | |
147148
| `ToString` | `Method` | creates the actual string. | |

index.d.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

index.ts

Lines changed: 59 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,47 @@
11
const EOL = '\r\n';
22

3-
export class $String {
3+
export const empty = '';
4+
5+
export function isNullOrWhiteSpace(value: string | null | undefined): boolean {
6+
return String.isNullOrWhiteSpace(value);
7+
}
8+
9+
export function join(delimiter: string, ...args: (string | object | Array<any>)[]): string {
10+
return String.join(delimiter, ...args);
11+
}
12+
13+
export function format(format: string, ...args: any[]): string {
14+
return String.format(format, ...args);
15+
}
16+
17+
export class String {
418
private static readonly regexNumber = /{(\d+(:\w*)?)}/g;
519
private static readonly regexObject = /{(\w+(:\w*)?)}/g;
620
public static empty = '';
21+
/**
22+
* @deprecated The property should not be used, and will be removed in future versions! Use `String.empty` instead.
23+
*/
724
public static Empty = '';
825

26+
/**
27+
* @deprecated The method should not be used, and will be removed in future versions! Use `String.isNullOrWhiteSpace()` instead.
28+
*/
929
public static IsNullOrWhiteSpace(value: string | null | undefined): boolean {
10-
return $String.isNullOrWhiteSpace(value);
30+
return String.isNullOrWhiteSpace(value);
1131
}
1232

33+
/**
34+
* @deprecated The method should not be used, and will be removed in future versions! Use `String.join()` instead.
35+
*/
1336
public static Join(delimiter: string, ...args: (string | object | Array<any>)[]): string {
14-
return $String.join(delimiter, ...args);
37+
return String.join(delimiter, ...args);
1538
}
1639

40+
/**
41+
* @deprecated The method should not be used, and will be removed in future version!s Use `String.format()` instead.
42+
*/
1743
public static Format(format: string, ...args: any[]): string {
18-
return $String.format(format, ...args);
44+
return String.format(format, ...args);
1945
}
2046

2147
public static isNullOrWhiteSpace(value: string | null | undefined): boolean {
@@ -36,7 +62,7 @@ export class $String {
3662
try {
3763
const firstArg = args[0];
3864
if (Array.isArray(firstArg) || firstArg instanceof Array) {
39-
let tempString = $String.empty;
65+
let tempString = String.empty;
4066

4167
for (let i = 0; i < firstArg.length; i++) {
4268
const current = firstArg[i];
@@ -51,7 +77,7 @@ export class $String {
5177
return tempString;
5278
}
5379
else if (typeof firstArg === 'object') {
54-
let tempString = $String.empty;
80+
let tempString = String.empty;
5581
const objectArg = firstArg;
5682
const keys = Object.keys(firstArg); //get all Properties of the Object as Array
5783
keys.forEach(element => { tempString += (<any>objectArg)[element] + delimiter; });
@@ -61,29 +87,29 @@ export class $String {
6187

6288
const stringArray = <string[]>args;
6389

64-
return $String.joinString(delimiter, ...stringArray);
90+
return String.joinString(delimiter, ...stringArray);
6591
}
6692
catch (e) {
6793
console.log(e);
68-
return $String.empty;
94+
return String.empty;
6995
}
7096
}
7197

7298
public static format(format: string, ...args: any[]): string {
7399
try {
74-
if (format.match($String.regexNumber)) {
75-
return $String.formatString($String.regexNumber, format, args);
100+
if (format.match(String.regexNumber)) {
101+
return String.formatString(String.regexNumber, format, args);
76102
}
77103

78-
if (format.match($String.regexObject)) {
79-
return $String.formatString($String.regexObject, format, args, true);
104+
if (format.match(String.regexObject)) {
105+
return String.formatString(String.regexObject, format, args, true);
80106
}
81107

82108
return format;
83109
}
84110
catch (e) {
85111
console.log(e);
86-
return $String.empty;
112+
return String.empty;
87113
}
88114
}
89115

@@ -107,8 +133,8 @@ export class $String {
107133
return arg;
108134
}
109135

110-
arg = $String.parsePattern(match, arg);
111-
return typeof arg != 'undefined' && arg != null ? arg : $String.empty;
136+
arg = String.parsePattern(match, arg);
137+
return typeof arg != 'undefined' && arg != null ? arg : String.empty;
112138
});
113139
}
114140

@@ -124,19 +150,19 @@ export class $String {
124150
}
125151
case 'd': {
126152
if (typeof (arg) === 'string') {
127-
return $String.getDisplayDateFromString(arg);
153+
return String.getDisplayDateFromString(arg);
128154
}
129155
else if (arg instanceof Date) {
130-
return $String.format('{0:00}.{1:00}.{2:0000}', arg.getDate(), arg.getMonth(), arg.getFullYear());
156+
return String.format('{0:00}.{1:00}.{2:0000}', arg.getDate(), arg.getMonth(), arg.getFullYear());
131157
}
132158
break;
133159
}
134160
case 's': {
135161
if (typeof (arg) === 'string') {
136-
return $String.getSortableDateFromString(arg);
162+
return String.getSortableDateFromString(arg);
137163
}
138164
else if (arg instanceof Date) {
139-
return $String.format('{0:0000}-{1:00}-{2:00}', arg.getFullYear(), arg.getMonth(), arg.getDate());
165+
return String.format('{0:0000}-{1:00}-{2:00}', arg.getFullYear(), arg.getMonth(), arg.getDate());
140166
}
141167
break;
142168
}
@@ -152,16 +178,16 @@ export class $String {
152178
let parts = numberparts;
153179

154180
if (numberparts.length > 1) {
155-
parts = [$String.joinString('', ...(numberparts.splice(0, numberparts.length - 1))), numberparts[numberparts.length - 1]];
181+
parts = [String.joinString('', ...(numberparts.splice(0, numberparts.length - 1))), numberparts[numberparts.length - 1]];
156182
}
157183

158184
const integer = parts[0];
159185

160186
const mod = integer.length % 3;
161-
let output = (mod > 0 ? (integer.substring(0, mod)) : $String.empty);
187+
let output = (mod > 0 ? (integer.substring(0, mod)) : String.empty);
162188

163189
const remainingGroups = integer.substring(mod).match(/.{3}/g);
164-
output = output + '.' + $String.join('.', remainingGroups);
190+
output = output + '.' + String.join('.', remainingGroups);
165191
arg = output + (parts.length > 1 ? ',' + parts[1] : '');
166192
return arg;
167193
}
@@ -176,8 +202,8 @@ export class $String {
176202
}
177203
}
178204

179-
if ((typeof (arg) === 'number' || !isNaN(arg)) && !isNaN(+match) && !$String.isNullOrWhiteSpace(arg)) {
180-
return $String.formatNumber(arg, match);
205+
if ((typeof (arg) === 'number' || !isNaN(arg)) && !isNaN(+match) && !String.isNullOrWhiteSpace(arg)) {
206+
return String.formatNumber(arg, match);
181207
}
182208

183209
return arg;
@@ -212,7 +238,7 @@ export class $String {
212238
}
213239

214240
const times = splitted[splitted.length - 1].split(' ');
215-
let time = $String.empty;
241+
let time = String.empty;
216242
if (times.length > 1) {
217243
time = times[times.length - 1];
218244
}
@@ -222,7 +248,7 @@ export class $String {
222248
const day = splitted[splitted.length - 3];
223249
let result = `${year}-${month}-${day}`;
224250

225-
if (!$String.isNullOrWhiteSpace(time) && time.length > 1) {
251+
if (!String.isNullOrWhiteSpace(time) && time.length > 1) {
226252
result += `T${time}`;
227253
}
228254
else {
@@ -246,17 +272,17 @@ export class $String {
246272
}
247273

248274
private static joinString(delimiter: string, ...args: string[]): string {
249-
let temp = $String.empty;
275+
let temp = String.empty;
250276
for (let i = 0; i < args.length; i++) {
251-
if ((typeof args[i] == 'string' && $String.isNullOrWhiteSpace(args[i]))
277+
if ((typeof args[i] == 'string' && String.isNullOrWhiteSpace(args[i]))
252278
|| (typeof args[i] != 'number' && typeof args[i] != 'string')) {
253279
continue;
254280
}
255281

256282
const arg = '' + args[i];
257283
temp += arg;
258284
for (let i2 = i + 1; i2 < args.length; i2++) {
259-
if ($String.isNullOrWhiteSpace(args[i2])) {
285+
if (String.isNullOrWhiteSpace(args[i2])) {
260286
continue;
261287
}
262288

@@ -270,21 +296,20 @@ export class $String {
270296
}
271297
}
272298

273-
export class String extends $String { }
274299

275300
export class StringBuilder {
276301
public Values: string[];
277302

278303
constructor(value = '') {
279304
this.Values = [];
280305

281-
if (!$String.isNullOrWhiteSpace(value)) {
306+
if (!String.isNullOrWhiteSpace(value)) {
282307
this.Values = new Array(value);
283308
}
284309
}
285310

286311
public toString() {
287-
return this.Values.join($String.empty);
312+
return this.Values.join(String.empty);
288313
}
289314

290315
public ToString() {
@@ -308,15 +333,15 @@ export class StringBuilder {
308333
}
309334

310335
public appendFormat(format: string, ...args: any[]) {
311-
this.Values.push($String.format(format, ...args));
336+
this.Values.push(String.format(format, ...args));
312337
}
313338

314339
public AppendFormat(format: string, ...args: any[]) {
315340
this.appendFormat(format, ...args);
316341
}
317342

318343
public appendLineFormat(format: string, ...args: any[]) {
319-
this.Values.push(EOL + $String.format(format, ...args));
344+
this.Values.push(EOL + String.format(format, ...args));
320345
}
321346

322347
public AppendLineFormat(format: string, ...args: any[]) {

0 commit comments

Comments
 (0)