Skip to content

Commit 46edd4b

Browse files
author
Sven Ulrich
committed
Merge branch 'feature/hexadecimal'
2 parents 1f4c25b + c4728e9 commit 46edd4b

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

dist/index.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ export class String {
9999
});
100100
}
101101

102-
private static parsePattern(match: 'L' | 'U' | 'd' | 's' | 'n' | string, arg: string | Date | number | any): string {
102+
private static parsePattern(match: 'L' | 'U' | 'd' | 's' | 'n' | 'x' | 'X' | string, arg: string | Date | number | any): string {
103103
switch (match) {
104104
case 'L': {
105105
arg = arg.toLocaleLowerCase();
@@ -152,6 +152,12 @@ export class String {
152152
arg = output + (parts.length > 1 ? ',' + parts[1] : '');
153153
return arg;
154154
}
155+
case 'x': {
156+
return this.decimalToHexString(arg);
157+
}
158+
case 'X': {
159+
return this.decimalToHexString(arg, true)
160+
}
155161
default: {
156162
break;
157163
}
@@ -164,6 +170,11 @@ export class String {
164170
return arg;
165171
}
166172

173+
private static decimalToHexString(number: number, upperCase: boolean = false) {
174+
const hexNumber = number.toString(16);
175+
return upperCase ? hexNumber.toLocaleUpperCase() : hexNumber;
176+
}
177+
167178
private static getDisplayDateFromString(input: string): string {
168179
let splitted: string[];
169180
splitted = input.split('-');

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

tests/tests.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,38 @@ describe('String.Format Number Pattern', () => {
183183
expect(result).to.equal(expectedValue);
184184
});
185185
});
186+
187+
describe('hexadecimal', () => {
188+
it('number should be converted to hex lowercase', () => {
189+
let result = String.Format('{0:x}', 500);
190+
expect(result).to.equal('1f4');
191+
});
192+
193+
it('number should be converted to hex uppercase', () => {
194+
let result = String.Format('{0:X}', 500);
195+
expect(result).to.equal('1F4');
196+
});
197+
198+
it('decimal should be converted to hex lowercase', () => {
199+
let result = String.Format('{0:x}', 321.124);
200+
expect(result).to.equal('141.1fbe76c8b44');
201+
});
202+
203+
it('decimal should be converted to hex uppercase', () => {
204+
let result = String.Format('{0:X}', 321.124);
205+
expect(result).to.equal('141.1FBE76C8B44');
206+
});
207+
208+
it('minus decimal should be converted to hex lowercase', () => {
209+
let result = String.Format('{0:x}', -321.124);
210+
expect(result).to.equal('-141.1fbe76c8b44');
211+
});
212+
213+
it('minus decimal should be converted to hex uppercase', () => {
214+
let result = String.Format('{0:X}', -321.124);
215+
expect(result).to.equal('-141.1FBE76C8B44');
216+
});
217+
});
186218
});
187219

188220
});

0 commit comments

Comments
 (0)