Skip to content

NEWLINE: added eol for different environments and updated dependencies #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 19 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
thousandsseperator: added specifier and doc
  • Loading branch information
Sven Ulrich committed Mar 16, 2023
commit d7046231b7ff23c10f3834be569fbd8c6ab28f9b
37 changes: 29 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,19 @@ formatString("image_{0}.jpg", id) // or String.format()
output: "image_2db5da20-1c5d-4f1a-8fd4-b41e34c8c5b5.jpg";
```

Locale Settings:

Currently only browsers are considered for this.
But you can edit your locale Settings by importing `locale`.
```typescript

// to adjust your locale:
import { locale } from 'typescript-string-operations'
locale.lang = 'en-EN';

```


Specifier available!
```typescript
var value = formatString("{0:L}", "APPLE"); //output "apple"
Expand All @@ -54,6 +67,11 @@ value = formatString("{0:s}", "21.03.2017 22:15:01") //output "2017-03-21T22:15:
value = formatString("{0:n}", 1000000);
//output "1.000.000"

value = formatString("{0:N}", 10000000000.01);
//output depends on your locale settings.
// en-EN -> 10,000,000,000
// de-DE -> 10.000.000.000,01

value = formatString("{0:00}", 1);
//output "01"
```
Expand All @@ -74,14 +92,17 @@ format("the {type:U} is {color:L} shipped on {shippingDate:s} with an amount of
```


| Specifier | Result |
| :-------------: |:---------------------------:|
| `L` | LowerCase |
| `U` | UpperCase |
| `d` | ShortDatePattern |
| `s` | SortableDateTimePattern |
| `n` | Thousand seperator |
| `00` | Padding numbers |
| Specifier | Result |
| :-------------: | :---------------------------------------------: |
| `L` | LowerCase |
| `U` | UpperCase |
| `d` | ShortDatePattern |
| `s` | SortableDateTimePattern |
| `n` | Thousand seperator |
| `N` | Thousand seperator, with respecting locale |
| `x` | Hexadecimal |
| `X` | Hexadecimal Uppercase |
| `00` | Padding numbers |



Expand Down
21 changes: 0 additions & 21 deletions environment.ts

This file was deleted.

54 changes: 50 additions & 4 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,40 @@
import { newLine } from './environment';
let newLine = '\r\n';
const locale = {
lang: '',
};
let env;

function isNode() {
try {
return this === global;
}
catch {
return false;
}
}

if (isNode()) {
const isWindows = typeof process != 'undefined' && 'win32' === process.platform;

env = process?.env;
locale.lang = env.LC_ALL || env.LC_MESSAGES || env.LANG || env.LANGUAGE;

if (!isWindows) {
newLine = '\n';
}
}
else {
try {
if (typeof navigator != undefined) {
locale.lang = navigator.language;
}
}
catch {
locale.lang = '';
}
}

export { locale, newLine };

export const emptyString = '';

Expand Down Expand Up @@ -180,7 +216,18 @@ export class String {

break;
}
case 'n': {//Tausender Trennzeichen
case 'N': { // thousands seperator respecting locale

if (locale?.lang) {
const result = parseFloat(arg);

return result.toLocaleString(locale.lang);
}

break;
}
case 'n': { // thousands seperator

if (typeof (arg) !== 'string')
arg = arg.toString();

Expand Down Expand Up @@ -401,5 +448,4 @@ export class StringBuilder {
public Clear() {
this.clear();
}

}
}
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@
"string operations",
"stringbuilder",
"string builder",
"angular"
"angular",
"browser",
"node",
"nodejs"
],
"author": "Sven Ulrich <mail@sven-ulrich.net> (sven-ulrich.net)",
"author": "Sven Ulrich",
"license": "MIT",
"bugs": {
"url": "https://github.com/sevensc/typescript-string-operations/issues"
Expand Down
34 changes: 32 additions & 2 deletions tests/tests.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { String, StringBuilder, isNullOrWhiteSpace, formatString, joinString } from '..';
import { newLine } from './../environment';
import { String, StringBuilder, isNullOrWhiteSpace, formatString, joinString, newLine, locale } from '..';
import { Fruit } from './fruit';
import { expect } from 'chai';
import 'mocha';

locale.lang = 'de-DE';

describe('String.IsNullOrWhitespace', () => {

it('should return true on null string', () => {
Expand Down Expand Up @@ -281,13 +282,42 @@ describe('String.Format Number Pattern', () => {
result = formatString(template, valueToInsert);
expect(result).to.equal(expectedValue);
});

it('should set the correct thousands seperator keeping the decimals', () => {
const template = '{0:n}';
const valueToInsert = '10000000000,12345';
const expectedValue = '10.000.000.000,12345';

let result = String.Format(template, valueToInsert);

expect(result).to.equal(expectedValue);
result = String.format(template, valueToInsert);
expect(result).to.equal(expectedValue);
result = formatString(template, valueToInsert);
expect(result).to.equal(expectedValue);
});

it('should set the correct thousands seperator, respecting locale', () => {
const template = '{0:N}';
const valueToInsert = '10000000000.01';
const expectedValue = '10.000.000.000,01';

let result = String.Format(template, valueToInsert);

expect(result).to.equal(expectedValue);
result = String.format(template, valueToInsert);
expect(result).to.equal(expectedValue);
result = formatString(template, valueToInsert);
expect(result).to.equal(expectedValue);
});

it('should set the correct thousands seperator keeping the decimals, respecting locale', () => {
const template = '{0:N}';
const valueToInsert = '10000000000.12345';
const expectedValue = '10.000.000.000,123';

let result = String.Format(template, valueToInsert);

expect(result).to.equal(expectedValue);
result = String.format(template, valueToInsert);
expect(result).to.equal(expectedValue);
Expand Down