Skip to content

Commit e2d8315

Browse files
committed
added description for padding, fixed a bug where numbers without specifiers where padded;
1 parent 822da22 commit e2d8315

File tree

6 files changed

+20
-5
lines changed

6 files changed

+20
-5
lines changed

dist/index.js

Lines changed: 1 addition & 1 deletion
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))
120+
if ((typeof (arg) === 'number' || !isNaN(arg)) && !isNaN(+match))
121121
return String.formatNumber(arg, match);
122122
return arg;
123123
};

dist/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ export class String {
120120
default:
121121
break;
122122
}
123-
124-
if (typeof (arg) === 'number' || !isNaN(arg))
123+
124+
if ((typeof (arg) === 'number' || !isNaN(arg)) && !isNaN(+match))
125125
return String.formatNumber(arg, match);
126126

127127
return arg;
@@ -167,7 +167,7 @@ export class String {
167167

168168
private static formatNumber(input: number, formatTemplate: string): string {
169169
let count = formatTemplate.length;
170-
let stringValue = input.toString();
170+
let stringValue = input.toString();
171171
if (count <= stringValue.length)
172172
return stringValue;
173173

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.2",
3+
"version": "1.2.3",
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
@@ -40,6 +40,9 @@ value = String.Format("{0:s}", "21.03.2017 22:15:01") //output "2017-03-21T22:15
4040

4141
value = String.Format("{0:n}", 1000000);
4242
//output "1.000.000"
43+
44+
value = String.Format("{0:00}", 1);
45+
//output "01"
4346
```
4447

4548
| Specifier | Result |
@@ -49,6 +52,7 @@ value = String.Format("{0:n}", 1000000);
4952
| `d` | ShortDatePattern |
5053
| `s` | SortableDateTimePattern |
5154
| `n` | Thousand seperator |
55+
| `00` | Padding numbers |
5256

5357

5458

tests/test.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<html>
2+
<script>var exports = {};</script>
3+
<script src="../dist/index.js"></script>
4+
</html>

tests/tests.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,13 @@ describe('String.Format', () => {
117117
});
118118

119119
describe('numbers', () => {
120+
121+
it('should not pad without specifier using {0}', () => {
122+
let template = '{0}';
123+
let result = String.Format(template, 5);
124+
expect(result).to.equal('5');
125+
});
126+
120127
it('should pad 5 to 05 using {0:00}', () => {
121128
let template = '{0:00}';
122129
let result = String.Format(template, 5);

0 commit comments

Comments
 (0)