@open-pioneer/core 2.3.0 → 2.4.0-dev.20241120085048
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +17 -0
- package/NumberParser.d.ts +15 -0
- package/NumberParser.js +41 -0
- package/NumberParser.js.map +1 -0
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/index.js.map +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,22 @@
|
|
|
1
1
|
# @open-pioneer/core
|
|
2
2
|
|
|
3
|
+
## 2.4.0-dev.20241120085048
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- e3802fb: Introduce the `NumberParser` class for parsing strings in a specified locale.
|
|
8
|
+
|
|
9
|
+
```js
|
|
10
|
+
import { NumberParser } from "@open-pioneer/core";
|
|
11
|
+
|
|
12
|
+
const parser = new NumberParser("de-DE");
|
|
13
|
+
const number = parser.parse("1.234,56");
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
### Patch Changes
|
|
17
|
+
|
|
18
|
+
- 1b63ebe: Update dependencies
|
|
19
|
+
|
|
3
20
|
## 2.3.0
|
|
4
21
|
|
|
5
22
|
## 2.2.0
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This class allows to parse numbers from strings according to the given locale.
|
|
3
|
+
* Currently, in JavaScript there is no built-in way to parse numbers according to the current locale.
|
|
4
|
+
* Only arabic numerals are supported.
|
|
5
|
+
*/
|
|
6
|
+
export declare class NumberParser {
|
|
7
|
+
private readonly decimalRegexPattern;
|
|
8
|
+
private readonly groupingRegexPattern;
|
|
9
|
+
constructor(locales: Intl.LocalesArgument);
|
|
10
|
+
/**
|
|
11
|
+
* Parses a number from a string considering the locale.
|
|
12
|
+
*/
|
|
13
|
+
parse(numberString: string): number;
|
|
14
|
+
private escapeRegExp;
|
|
15
|
+
}
|
package/NumberParser.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
class NumberParser {
|
|
2
|
+
decimalRegexPattern;
|
|
3
|
+
groupingRegexPattern;
|
|
4
|
+
constructor(locales) {
|
|
5
|
+
const numberFormatOptions = {
|
|
6
|
+
useGrouping: true
|
|
7
|
+
};
|
|
8
|
+
const formattedParts = new Intl.NumberFormat(locales, numberFormatOptions).formatToParts(
|
|
9
|
+
12345.6
|
|
10
|
+
);
|
|
11
|
+
const decimalSeparator = formattedParts.find((part) => part.type === "decimal")?.value;
|
|
12
|
+
if (!decimalSeparator) {
|
|
13
|
+
throw new Error("Could not determine decimal separator.");
|
|
14
|
+
}
|
|
15
|
+
const groupingSeparator = formattedParts.find((part) => part.type === "group")?.value;
|
|
16
|
+
if (!groupingSeparator) {
|
|
17
|
+
throw new Error("Could not determine grouping separator.");
|
|
18
|
+
}
|
|
19
|
+
this.decimalRegexPattern = new RegExp(`${this.escapeRegExp(decimalSeparator)}`, "g");
|
|
20
|
+
if (groupingSeparator.trim() === "") {
|
|
21
|
+
this.groupingRegexPattern = /\s/g;
|
|
22
|
+
} else {
|
|
23
|
+
this.groupingRegexPattern = new RegExp(`${this.escapeRegExp(groupingSeparator)}`, "g");
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Parses a number from a string considering the locale.
|
|
28
|
+
*/
|
|
29
|
+
parse(numberString) {
|
|
30
|
+
numberString = numberString.trim();
|
|
31
|
+
const parsedNumberString = numberString.replace(this.groupingRegexPattern, "").replace(this.decimalRegexPattern, ".");
|
|
32
|
+
return !parsedNumberString ? NaN : +parsedNumberString;
|
|
33
|
+
}
|
|
34
|
+
// see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions#escaping
|
|
35
|
+
escapeRegExp(string) {
|
|
36
|
+
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export { NumberParser };
|
|
41
|
+
//# sourceMappingURL=NumberParser.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"NumberParser.js","sources":["NumberParser.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2023 Open Pioneer project (https://github.com/open-pioneer)\n// SPDX-License-Identifier: Apache-2.0\n\n/**\n * This class allows to parse numbers from strings according to the given locale.\n * Currently, in JavaScript there is no built-in way to parse numbers according to the current locale.\n * Only arabic numerals are supported.\n */\nexport class NumberParser {\n private readonly decimalRegexPattern: RegExp;\n private readonly groupingRegexPattern: RegExp;\n\n constructor(locales: Intl.LocalesArgument) {\n const numberFormatOptions: Intl.NumberFormatOptions = {\n useGrouping: true\n };\n const formattedParts = new Intl.NumberFormat(locales, numberFormatOptions).formatToParts(\n 12345.6\n );\n\n const decimalSeparator = formattedParts.find((part) => part.type === \"decimal\")?.value;\n if (!decimalSeparator) {\n throw new Error(\"Could not determine decimal separator.\");\n }\n\n const groupingSeparator = formattedParts.find((part) => part.type === \"group\")?.value;\n if (!groupingSeparator) {\n throw new Error(\"Could not determine grouping separator.\");\n }\n\n this.decimalRegexPattern = new RegExp(`${this.escapeRegExp(decimalSeparator)}`, \"g\");\n if (groupingSeparator.trim() === \"\") {\n // if a language uses a whitespace as grouping separator, remove all whitespaces (as numbers may not contain whitespaces)\n this.groupingRegexPattern = /\\s/g;\n } else {\n this.groupingRegexPattern = new RegExp(`${this.escapeRegExp(groupingSeparator)}`, \"g\");\n }\n }\n\n /**\n * Parses a number from a string considering the locale.\n */\n parse(numberString: string): number {\n numberString = numberString.trim();\n\n const parsedNumberString = numberString\n .replace(this.groupingRegexPattern, \"\")\n .replace(this.decimalRegexPattern, \".\");\n return !parsedNumberString ? NaN : +parsedNumberString;\n }\n\n // see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions#escaping\n private escapeRegExp(string: string) {\n return string.replace(/[.*+?^${}()|[\\]\\\\]/g, \"\\\\$&\"); // $& means the whole matched string\n }\n}\n"],"names":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8B,EAAA;AAChC,IAAA,YAAA,GAAe,aAAa,IAAK,EAAA,CAAA;AAEjC,IAAM,MAAA,kBAAA,GAAqB,YACtB,CAAA,OAAA,CAAQ,IAAK,CAAA,oBAAA,EAAsB,EAAE,CACrC,CAAA,OAAA,CAAQ,IAAK,CAAA,mBAAA,EAAqB,GAAG,CAAA,CAAA;AAC1C,IAAO,OAAA,CAAC,kBAAqB,GAAA,GAAA,GAAM,CAAC,kBAAA,CAAA;AAAA,GACxC;AAAA;AAAA,EAGQ,aAAa,MAAgB,EAAA;AACjC,IAAO,OAAA,MAAA,CAAO,OAAQ,CAAA,qBAAA,EAAuB,MAAM,CAAA,CAAA;AAAA,GACvD;AACJ;;;;"}
|
package/index.d.ts
CHANGED
|
@@ -3,3 +3,4 @@ export { EventEmitter, type EventSource, type EventNames } from "./events";
|
|
|
3
3
|
export { destroyResource, destroyResources, type Resource } from "./resources";
|
|
4
4
|
export { createLogger, type Logger, type LogLevel, type LogMethod } from "./Logger";
|
|
5
5
|
export { createManualPromise, type ManualPromise } from "./utils";
|
|
6
|
+
export { NumberParser } from "./NumberParser";
|
package/index.js
CHANGED
|
@@ -3,4 +3,5 @@ export { EventEmitter } from './events.js';
|
|
|
3
3
|
export { destroyResource, destroyResources } from './resources.js';
|
|
4
4
|
export { createLogger } from './Logger.js';
|
|
5
5
|
export { createManualPromise } from './utils.js';
|
|
6
|
+
export { NumberParser } from './NumberParser.js';
|
|
6
7
|
//# sourceMappingURL=index.js.map
|
package/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;"}
|
package/package.json
CHANGED