react-native-nitro-input 0.3.6 → 0.3.8
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 +58 -0
- package/README.md +4 -3
- package/android/CMakeLists.txt +15 -1
- package/cpp/RollingEngine.cpp +68 -5
- package/cpp/RollingEngine.hpp +13 -0
- package/cpp/numberformat/FormatFigure.cpp +134 -0
- package/cpp/numberformat/FormatFigure.hpp +65 -0
- package/cpp/numberformat/HybridNitroNumberFormat.cpp +32 -10
- package/cpp/numberformat/HybridNitroNumberFormat.hpp +9 -1
- package/cpp/worklets/NitroInputWorkletsBridge.cpp +14 -2
- package/lib/commonjs/src/NitroNumber.js +19 -11
- package/lib/commonjs/src/NumberFormat.js +20 -0
- package/lib/commonjs/src/formatProps.js +57 -81
- package/lib/module/src/NitroNumber.js +20 -12
- package/lib/module/src/NumberFormat.js +18 -0
- package/lib/module/src/formatProps.js +54 -81
- package/lib/typescript/src/NitroNumber.d.ts +25 -7
- package/lib/typescript/src/NumberFormat.d.ts +15 -1
- package/lib/typescript/src/formatProps.d.ts +23 -18
- package/lib/typescript/src/specs/NumberFormat.nitro.d.ts +33 -0
- package/nitrogen/generated/shared/c++/HybridNitroNumberFormatSpec.cpp +2 -0
- package/nitrogen/generated/shared/c++/HybridNitroNumberFormatSpec.hpp +8 -0
- package/nitrogen/generated/shared/c++/NumberFigure.hpp +95 -0
- package/nitrogen/generated/shared/c++/NumberFormatLayout.hpp +112 -0
- package/package.json +2 -2
- package/src/NitroNumber.tsx +48 -19
- package/src/NumberFormat.ts +22 -0
- package/src/formatProps.ts +59 -99
- package/src/specs/NumberFormat.nitro.ts +35 -0
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { NumberFormatOptions, NumberFormatPart, ResolvedNumberFormatOptions } from './specs/NumberFormat.nitro';
|
|
1
|
+
import type { NumberFigure, NumberFormatLayout, NumberFormatOptions, NumberFormatPart, ResolvedNumberFormatOptions } from './specs/NumberFormat.nitro';
|
|
2
2
|
/**
|
|
3
3
|
* A number formatter with the API of `Intl.NumberFormat`, formatted natively.
|
|
4
4
|
*
|
|
@@ -44,4 +44,18 @@ export interface NumberFormatConstructor {
|
|
|
44
44
|
* @internal
|
|
45
45
|
*/
|
|
46
46
|
export declare function sharedFormatterOf(value: unknown): object | undefined;
|
|
47
|
+
/**
|
|
48
|
+
* What `format` shows for `value`, as `NitroNumber` rolls it: the figure
|
|
49
|
+
* rounded and trimmed as the format prints it, and the text around it. Read
|
|
50
|
+
* in C++ off the formatter's own parts, from the number, bigint or decimal
|
|
51
|
+
* string itself.
|
|
52
|
+
* @internal
|
|
53
|
+
*/
|
|
54
|
+
export declare function figureOf(format: NumberFormat, value: number | bigint | string): NumberFigure;
|
|
55
|
+
/**
|
|
56
|
+
* The prefix, suffix, separators, sign placement, grouping and digits of
|
|
57
|
+
* `format`, read in C++ off its parts.
|
|
58
|
+
* @internal
|
|
59
|
+
*/
|
|
60
|
+
export declare function layoutOf(format: NumberFormat): NumberFormatLayout;
|
|
47
61
|
export declare const NumberFormat: NumberFormatConstructor;
|
|
@@ -1,11 +1,16 @@
|
|
|
1
|
-
import { NumberFormat } from './NumberFormat';
|
|
1
|
+
import { type NumberFormat } from './NumberFormat';
|
|
2
|
+
export { figureOf } from './NumberFormat';
|
|
2
3
|
/** What `NitroNumber` and `NitroInput` take from a `NumberFormat` through their `format` prop. */
|
|
3
4
|
export interface FormatProps {
|
|
4
5
|
prefix: string;
|
|
5
6
|
suffix: string;
|
|
6
7
|
groupingSeparator: string;
|
|
7
8
|
decimalSeparator: string;
|
|
8
|
-
/**
|
|
9
|
+
/**
|
|
10
|
+
* The digits after the decimal separator: the format's maximum. What one
|
|
11
|
+
* value shows can be fewer (`trailingZeroDisplay`, a minimum below the
|
|
12
|
+
* maximum): `figureOf` says per value.
|
|
13
|
+
*/
|
|
9
14
|
fractionDigits: number;
|
|
10
15
|
minimumIntegerDigits: number;
|
|
11
16
|
/** Whether a negative amount's sign comes before the prefix ("-$5") or after it ("$-5"). */
|
|
@@ -20,28 +25,28 @@ export interface FormatProps {
|
|
|
20
25
|
digitGlyphs: string[];
|
|
21
26
|
/** The first digit group and every later one, from the decimal point ([3, 2]: 12,34,567). */
|
|
22
27
|
groupingSizes: number[];
|
|
23
|
-
/** Compact notation: the prefix, suffix and digits follow the value (`
|
|
28
|
+
/** Compact notation: the prefix, suffix and digits follow the value (`figureOf`). */
|
|
24
29
|
compact: boolean;
|
|
25
30
|
}
|
|
26
31
|
/**
|
|
27
|
-
* The prefix, suffix, separators and digit counts of `format`, read
|
|
28
|
-
* parts once and cached per formatter. The components keep their own
|
|
29
|
-
* (a sign, a prefix, digits in groups, a suffix), so a format outside it
|
|
32
|
+
* The prefix, suffix, separators and digit counts of `format`, read in C++
|
|
33
|
+
* off its parts once and cached per formatter. The components keep their own
|
|
34
|
+
* model (a sign, a prefix, digits in groups, a suffix), so a format outside it
|
|
30
35
|
* (accounting parentheses, a unit between the digits) is followed as far as
|
|
31
36
|
* that model goes.
|
|
32
37
|
*/
|
|
33
38
|
export declare function formatProps(format: NumberFormat): FormatProps;
|
|
34
|
-
/**
|
|
35
|
-
export
|
|
36
|
-
/** The figure as the digits show it: 1.2 for "1.2K". */
|
|
37
|
-
value: number;
|
|
38
|
-
fractionDigits: number;
|
|
39
|
-
prefix: string;
|
|
40
|
-
suffix: string;
|
|
41
|
-
}
|
|
39
|
+
/** A value as `NumberFormat` takes it: a number, a bigint or a decimal string. */
|
|
40
|
+
export type NumericInput = number | bigint | string;
|
|
42
41
|
/**
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
* a
|
|
42
|
+
* `value` read in units of 10^-`minorDigits`, exactly: 123456n with 2 minor
|
|
43
|
+
* digits is "1234.56". A bigint or a numeric string is shifted as text, so no
|
|
44
|
+
* digit goes through a double: a decimal ("1234.5"), one with an exponent
|
|
45
|
+
* ("1e5" becomes "1e3"), or a 0x / 0o / 0b integer, as `Intl.NumberFormat`
|
|
46
|
+
* reads strings. A number that is a safe integer is shifted the same way, any
|
|
47
|
+
* other number is divided. A string that is not a number ("1,234.56") comes
|
|
48
|
+
* back as it is, which the format shows as NaN, never as a wrong amount.
|
|
46
49
|
*/
|
|
47
|
-
export declare function
|
|
50
|
+
export declare function withMinorDigits(value: NumericInput, minorDigits: number | undefined): NumericInput;
|
|
51
|
+
/** `value` as a double, for comparisons and for the engine when there is no format. */
|
|
52
|
+
export declare function toNumber(value: NumericInput): number;
|
|
@@ -62,6 +62,35 @@ export interface NumberFormatPart {
|
|
|
62
62
|
type: string;
|
|
63
63
|
value: string;
|
|
64
64
|
}
|
|
65
|
+
/**
|
|
66
|
+
* One value as `NitroNumber` rolls it: the figure its digits show, rounded and
|
|
67
|
+
* trimmed as the format prints it, and the text around it.
|
|
68
|
+
*/
|
|
69
|
+
export interface NumberFigure {
|
|
70
|
+
/** The figure as the digits show it, signed: 1.2 for "1.2K", 2.12 for "$2.12"; 0 (never -0) when it rounds to nothing, or for NaN and infinities. */
|
|
71
|
+
value: number;
|
|
72
|
+
/** The fraction digits this value shows ("$1,234" 0, "$1,234.50" 2); the format's maximum for NaN and infinities. */
|
|
73
|
+
fractionDigits: number;
|
|
74
|
+
/** The text before the number, without the sign. */
|
|
75
|
+
prefix: string;
|
|
76
|
+
/** The text after the number, without the sign. */
|
|
77
|
+
suffix: string;
|
|
78
|
+
}
|
|
79
|
+
/** The shape of a format, as `NitroNumber` and `NitroInput` lay a number out. */
|
|
80
|
+
export interface NumberFormatLayout {
|
|
81
|
+
prefix: string;
|
|
82
|
+
suffix: string;
|
|
83
|
+
/** Empty when the format does not group. */
|
|
84
|
+
groupingSeparator: string;
|
|
85
|
+
decimalSeparator: string;
|
|
86
|
+
/** A negative amount's sign comes after the prefix ("$-5"), not before it ("-$5"). */
|
|
87
|
+
signAfterAffix: boolean;
|
|
88
|
+
minusSign: string;
|
|
89
|
+
/** The group nearest the decimal point and every one before it ([3, 2]: 12,34,567). */
|
|
90
|
+
groupingSizes: number[];
|
|
91
|
+
/** The format's digits 0…9, or empty for Latin ones. */
|
|
92
|
+
digitGlyphs: string[];
|
|
93
|
+
}
|
|
65
94
|
/**
|
|
66
95
|
* A number formatter with the API of `Intl.NumberFormat`. It learns a
|
|
67
96
|
* locale's format once from the platform (Foundation on iOS, ICU on Android)
|
|
@@ -76,6 +105,10 @@ export interface NitroNumberFormat extends HybridObject<{
|
|
|
76
105
|
format(value: number | Int64 | string): string;
|
|
77
106
|
formatToParts(value: number | Int64 | string): NumberFormatPart[];
|
|
78
107
|
resolvedOptions(): ResolvedNumberFormatOptions;
|
|
108
|
+
/** For the components, not part of `Intl.NumberFormat`: what `value` shows, as a figure. */
|
|
109
|
+
figure(value: number | Int64 | string): NumberFigure;
|
|
110
|
+
/** For the components, not part of `Intl.NumberFormat`: the shape of the format. */
|
|
111
|
+
layout(): NumberFormatLayout;
|
|
79
112
|
}
|
|
80
113
|
export interface NitroNumberFormatFactory extends HybridObject<{
|
|
81
114
|
ios: 'c++';
|
|
@@ -17,6 +17,8 @@ namespace margelo::nitro::nitroinput {
|
|
|
17
17
|
prototype.registerHybridMethod("format", &HybridNitroNumberFormatSpec::format);
|
|
18
18
|
prototype.registerHybridMethod("formatToParts", &HybridNitroNumberFormatSpec::formatToParts);
|
|
19
19
|
prototype.registerHybridMethod("resolvedOptions", &HybridNitroNumberFormatSpec::resolvedOptions);
|
|
20
|
+
prototype.registerHybridMethod("figure", &HybridNitroNumberFormatSpec::figure);
|
|
21
|
+
prototype.registerHybridMethod("layout", &HybridNitroNumberFormatSpec::layout);
|
|
20
22
|
});
|
|
21
23
|
}
|
|
22
24
|
|
|
@@ -17,12 +17,18 @@
|
|
|
17
17
|
namespace margelo::nitro::nitroinput { struct NumberFormatPart; }
|
|
18
18
|
// Forward declaration of `ResolvedNumberFormatOptions` to properly resolve imports.
|
|
19
19
|
namespace margelo::nitro::nitroinput { struct ResolvedNumberFormatOptions; }
|
|
20
|
+
// Forward declaration of `NumberFigure` to properly resolve imports.
|
|
21
|
+
namespace margelo::nitro::nitroinput { struct NumberFigure; }
|
|
22
|
+
// Forward declaration of `NumberFormatLayout` to properly resolve imports.
|
|
23
|
+
namespace margelo::nitro::nitroinput { struct NumberFormatLayout; }
|
|
20
24
|
|
|
21
25
|
#include <string>
|
|
22
26
|
#include <variant>
|
|
23
27
|
#include "NumberFormatPart.hpp"
|
|
24
28
|
#include <vector>
|
|
25
29
|
#include "ResolvedNumberFormatOptions.hpp"
|
|
30
|
+
#include "NumberFigure.hpp"
|
|
31
|
+
#include "NumberFormatLayout.hpp"
|
|
26
32
|
|
|
27
33
|
namespace margelo::nitro::nitroinput {
|
|
28
34
|
|
|
@@ -58,6 +64,8 @@ namespace margelo::nitro::nitroinput {
|
|
|
58
64
|
virtual std::string format(const std::variant<int64_t, double, std::string>& value) = 0;
|
|
59
65
|
virtual std::vector<NumberFormatPart> formatToParts(const std::variant<int64_t, double, std::string>& value) = 0;
|
|
60
66
|
virtual ResolvedNumberFormatOptions resolvedOptions() = 0;
|
|
67
|
+
virtual NumberFigure figure(const std::variant<int64_t, double, std::string>& value) = 0;
|
|
68
|
+
virtual NumberFormatLayout layout() = 0;
|
|
61
69
|
|
|
62
70
|
protected:
|
|
63
71
|
// Hybrid Setup
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// NumberFigure.hpp
|
|
3
|
+
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
|
+
/// https://github.com/mrousavy/nitro
|
|
5
|
+
/// Copyright © Marc Rousavy @ Margelo
|
|
6
|
+
///
|
|
7
|
+
|
|
8
|
+
#pragma once
|
|
9
|
+
|
|
10
|
+
#if __has_include(<NitroModules/JSIConverter.hpp>)
|
|
11
|
+
#include <NitroModules/JSIConverter.hpp>
|
|
12
|
+
#else
|
|
13
|
+
#error NitroModules cannot be found! Are you sure you installed NitroModules properly?
|
|
14
|
+
#endif
|
|
15
|
+
#if __has_include(<NitroModules/NitroDefines.hpp>)
|
|
16
|
+
#include <NitroModules/NitroDefines.hpp>
|
|
17
|
+
#else
|
|
18
|
+
#error NitroModules cannot be found! Are you sure you installed NitroModules properly?
|
|
19
|
+
#endif
|
|
20
|
+
#if __has_include(<NitroModules/JSIHelpers.hpp>)
|
|
21
|
+
#include <NitroModules/JSIHelpers.hpp>
|
|
22
|
+
#else
|
|
23
|
+
#error NitroModules cannot be found! Are you sure you installed NitroModules properly?
|
|
24
|
+
#endif
|
|
25
|
+
#if __has_include(<NitroModules/PropNameIDCache.hpp>)
|
|
26
|
+
#include <NitroModules/PropNameIDCache.hpp>
|
|
27
|
+
#else
|
|
28
|
+
#error NitroModules cannot be found! Are you sure you installed NitroModules properly?
|
|
29
|
+
#endif
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
#include <string>
|
|
34
|
+
|
|
35
|
+
namespace margelo::nitro::nitroinput {
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* A struct which can be represented as a JavaScript object (NumberFigure).
|
|
39
|
+
*/
|
|
40
|
+
struct NumberFigure final {
|
|
41
|
+
public:
|
|
42
|
+
double value SWIFT_PRIVATE;
|
|
43
|
+
double fractionDigits SWIFT_PRIVATE;
|
|
44
|
+
std::string prefix SWIFT_PRIVATE;
|
|
45
|
+
std::string suffix SWIFT_PRIVATE;
|
|
46
|
+
|
|
47
|
+
public:
|
|
48
|
+
NumberFigure() = default;
|
|
49
|
+
explicit NumberFigure(double value, double fractionDigits, std::string prefix, std::string suffix): value(value), fractionDigits(fractionDigits), prefix(prefix), suffix(suffix) {}
|
|
50
|
+
|
|
51
|
+
public:
|
|
52
|
+
friend bool operator==(const NumberFigure& lhs, const NumberFigure& rhs) = default;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
} // namespace margelo::nitro::nitroinput
|
|
56
|
+
|
|
57
|
+
namespace margelo::nitro {
|
|
58
|
+
|
|
59
|
+
// C++ NumberFigure <> JS NumberFigure (object)
|
|
60
|
+
template <>
|
|
61
|
+
struct JSIConverter<margelo::nitro::nitroinput::NumberFigure> final {
|
|
62
|
+
static inline margelo::nitro::nitroinput::NumberFigure fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
|
|
63
|
+
jsi::Object obj = arg.asObject(runtime);
|
|
64
|
+
return margelo::nitro::nitroinput::NumberFigure(
|
|
65
|
+
JSIConverter<double>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "value"))),
|
|
66
|
+
JSIConverter<double>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "fractionDigits"))),
|
|
67
|
+
JSIConverter<std::string>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "prefix"))),
|
|
68
|
+
JSIConverter<std::string>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "suffix")))
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
static inline jsi::Value toJSI(jsi::Runtime& runtime, const margelo::nitro::nitroinput::NumberFigure& arg) {
|
|
72
|
+
jsi::Object obj(runtime);
|
|
73
|
+
obj.setProperty(runtime, PropNameIDCache::get(runtime, "value"), JSIConverter<double>::toJSI(runtime, arg.value));
|
|
74
|
+
obj.setProperty(runtime, PropNameIDCache::get(runtime, "fractionDigits"), JSIConverter<double>::toJSI(runtime, arg.fractionDigits));
|
|
75
|
+
obj.setProperty(runtime, PropNameIDCache::get(runtime, "prefix"), JSIConverter<std::string>::toJSI(runtime, arg.prefix));
|
|
76
|
+
obj.setProperty(runtime, PropNameIDCache::get(runtime, "suffix"), JSIConverter<std::string>::toJSI(runtime, arg.suffix));
|
|
77
|
+
return obj;
|
|
78
|
+
}
|
|
79
|
+
static inline bool canConvert(jsi::Runtime& runtime, const jsi::Value& value) {
|
|
80
|
+
if (!value.isObject()) {
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
jsi::Object obj = value.getObject(runtime);
|
|
84
|
+
if (!nitro::isPlainObject(runtime, obj)) {
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
if (!JSIConverter<double>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "value")))) return false;
|
|
88
|
+
if (!JSIConverter<double>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "fractionDigits")))) return false;
|
|
89
|
+
if (!JSIConverter<std::string>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "prefix")))) return false;
|
|
90
|
+
if (!JSIConverter<std::string>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "suffix")))) return false;
|
|
91
|
+
return true;
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
} // namespace margelo::nitro
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// NumberFormatLayout.hpp
|
|
3
|
+
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
|
+
/// https://github.com/mrousavy/nitro
|
|
5
|
+
/// Copyright © Marc Rousavy @ Margelo
|
|
6
|
+
///
|
|
7
|
+
|
|
8
|
+
#pragma once
|
|
9
|
+
|
|
10
|
+
#if __has_include(<NitroModules/JSIConverter.hpp>)
|
|
11
|
+
#include <NitroModules/JSIConverter.hpp>
|
|
12
|
+
#else
|
|
13
|
+
#error NitroModules cannot be found! Are you sure you installed NitroModules properly?
|
|
14
|
+
#endif
|
|
15
|
+
#if __has_include(<NitroModules/NitroDefines.hpp>)
|
|
16
|
+
#include <NitroModules/NitroDefines.hpp>
|
|
17
|
+
#else
|
|
18
|
+
#error NitroModules cannot be found! Are you sure you installed NitroModules properly?
|
|
19
|
+
#endif
|
|
20
|
+
#if __has_include(<NitroModules/JSIHelpers.hpp>)
|
|
21
|
+
#include <NitroModules/JSIHelpers.hpp>
|
|
22
|
+
#else
|
|
23
|
+
#error NitroModules cannot be found! Are you sure you installed NitroModules properly?
|
|
24
|
+
#endif
|
|
25
|
+
#if __has_include(<NitroModules/PropNameIDCache.hpp>)
|
|
26
|
+
#include <NitroModules/PropNameIDCache.hpp>
|
|
27
|
+
#else
|
|
28
|
+
#error NitroModules cannot be found! Are you sure you installed NitroModules properly?
|
|
29
|
+
#endif
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
#include <string>
|
|
34
|
+
#include <vector>
|
|
35
|
+
|
|
36
|
+
namespace margelo::nitro::nitroinput {
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* A struct which can be represented as a JavaScript object (NumberFormatLayout).
|
|
40
|
+
*/
|
|
41
|
+
struct NumberFormatLayout final {
|
|
42
|
+
public:
|
|
43
|
+
std::string prefix SWIFT_PRIVATE;
|
|
44
|
+
std::string suffix SWIFT_PRIVATE;
|
|
45
|
+
std::string groupingSeparator SWIFT_PRIVATE;
|
|
46
|
+
std::string decimalSeparator SWIFT_PRIVATE;
|
|
47
|
+
bool signAfterAffix SWIFT_PRIVATE;
|
|
48
|
+
std::string minusSign SWIFT_PRIVATE;
|
|
49
|
+
std::vector<double> groupingSizes SWIFT_PRIVATE;
|
|
50
|
+
std::vector<std::string> digitGlyphs SWIFT_PRIVATE;
|
|
51
|
+
|
|
52
|
+
public:
|
|
53
|
+
NumberFormatLayout() = default;
|
|
54
|
+
explicit NumberFormatLayout(std::string prefix, std::string suffix, std::string groupingSeparator, std::string decimalSeparator, bool signAfterAffix, std::string minusSign, std::vector<double> groupingSizes, std::vector<std::string> digitGlyphs): prefix(prefix), suffix(suffix), groupingSeparator(groupingSeparator), decimalSeparator(decimalSeparator), signAfterAffix(signAfterAffix), minusSign(minusSign), groupingSizes(groupingSizes), digitGlyphs(digitGlyphs) {}
|
|
55
|
+
|
|
56
|
+
public:
|
|
57
|
+
friend bool operator==(const NumberFormatLayout& lhs, const NumberFormatLayout& rhs) = default;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
} // namespace margelo::nitro::nitroinput
|
|
61
|
+
|
|
62
|
+
namespace margelo::nitro {
|
|
63
|
+
|
|
64
|
+
// C++ NumberFormatLayout <> JS NumberFormatLayout (object)
|
|
65
|
+
template <>
|
|
66
|
+
struct JSIConverter<margelo::nitro::nitroinput::NumberFormatLayout> final {
|
|
67
|
+
static inline margelo::nitro::nitroinput::NumberFormatLayout fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
|
|
68
|
+
jsi::Object obj = arg.asObject(runtime);
|
|
69
|
+
return margelo::nitro::nitroinput::NumberFormatLayout(
|
|
70
|
+
JSIConverter<std::string>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "prefix"))),
|
|
71
|
+
JSIConverter<std::string>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "suffix"))),
|
|
72
|
+
JSIConverter<std::string>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "groupingSeparator"))),
|
|
73
|
+
JSIConverter<std::string>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "decimalSeparator"))),
|
|
74
|
+
JSIConverter<bool>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "signAfterAffix"))),
|
|
75
|
+
JSIConverter<std::string>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "minusSign"))),
|
|
76
|
+
JSIConverter<std::vector<double>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "groupingSizes"))),
|
|
77
|
+
JSIConverter<std::vector<std::string>>::fromJSI(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "digitGlyphs")))
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
static inline jsi::Value toJSI(jsi::Runtime& runtime, const margelo::nitro::nitroinput::NumberFormatLayout& arg) {
|
|
81
|
+
jsi::Object obj(runtime);
|
|
82
|
+
obj.setProperty(runtime, PropNameIDCache::get(runtime, "prefix"), JSIConverter<std::string>::toJSI(runtime, arg.prefix));
|
|
83
|
+
obj.setProperty(runtime, PropNameIDCache::get(runtime, "suffix"), JSIConverter<std::string>::toJSI(runtime, arg.suffix));
|
|
84
|
+
obj.setProperty(runtime, PropNameIDCache::get(runtime, "groupingSeparator"), JSIConverter<std::string>::toJSI(runtime, arg.groupingSeparator));
|
|
85
|
+
obj.setProperty(runtime, PropNameIDCache::get(runtime, "decimalSeparator"), JSIConverter<std::string>::toJSI(runtime, arg.decimalSeparator));
|
|
86
|
+
obj.setProperty(runtime, PropNameIDCache::get(runtime, "signAfterAffix"), JSIConverter<bool>::toJSI(runtime, arg.signAfterAffix));
|
|
87
|
+
obj.setProperty(runtime, PropNameIDCache::get(runtime, "minusSign"), JSIConverter<std::string>::toJSI(runtime, arg.minusSign));
|
|
88
|
+
obj.setProperty(runtime, PropNameIDCache::get(runtime, "groupingSizes"), JSIConverter<std::vector<double>>::toJSI(runtime, arg.groupingSizes));
|
|
89
|
+
obj.setProperty(runtime, PropNameIDCache::get(runtime, "digitGlyphs"), JSIConverter<std::vector<std::string>>::toJSI(runtime, arg.digitGlyphs));
|
|
90
|
+
return obj;
|
|
91
|
+
}
|
|
92
|
+
static inline bool canConvert(jsi::Runtime& runtime, const jsi::Value& value) {
|
|
93
|
+
if (!value.isObject()) {
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
jsi::Object obj = value.getObject(runtime);
|
|
97
|
+
if (!nitro::isPlainObject(runtime, obj)) {
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
if (!JSIConverter<std::string>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "prefix")))) return false;
|
|
101
|
+
if (!JSIConverter<std::string>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "suffix")))) return false;
|
|
102
|
+
if (!JSIConverter<std::string>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "groupingSeparator")))) return false;
|
|
103
|
+
if (!JSIConverter<std::string>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "decimalSeparator")))) return false;
|
|
104
|
+
if (!JSIConverter<bool>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "signAfterAffix")))) return false;
|
|
105
|
+
if (!JSIConverter<std::string>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "minusSign")))) return false;
|
|
106
|
+
if (!JSIConverter<std::vector<double>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "groupingSizes")))) return false;
|
|
107
|
+
if (!JSIConverter<std::vector<std::string>>::canConvert(runtime, obj.getProperty(runtime, PropNameIDCache::get(runtime, "digitGlyphs")))) return false;
|
|
108
|
+
return true;
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
} // namespace margelo::nitro
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-native-nitro-input",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.8",
|
|
4
4
|
"description": "Native text for React Native, built with Nitro: a text and amount input with native formatting, masks and a Torph-style reflow, and an animated number (odometer, SwiftUI-style numeric text, ticker) whose digits roll or swap natively, with currency layouts and animated currency switches, shrink-to-fit, a loading shimmer and a jackpot reveal.",
|
|
5
5
|
"main": "lib/commonjs/src/index.js",
|
|
6
6
|
"module": "lib/module/src/index.js",
|
|
@@ -39,7 +39,7 @@
|
|
|
39
39
|
"specs": "nitrogen --logLevel=\"debug\"",
|
|
40
40
|
"prepack": "bun run build",
|
|
41
41
|
"test": "jest",
|
|
42
|
-
"test:cpp": "mkdir -p .cache && clang++ -std=c++20 -O1 -Wall -Wextra -Icpp cpp/ReflowEngine.cpp cpp/AmountFormatter.cpp cpp/__tests__/ReflowEngineTest.cpp -o .cache/reflow-engine-test && .cache/reflow-engine-test && clang++ -std=c++20 -O1 -Wall -Wextra -Icpp cpp/AmountFormatter.cpp cpp/__tests__/AmountFormatterTest.cpp -o .cache/amount-formatter-test && .cache/amount-formatter-test && clang++ -std=c++20 -O1 -Wall -Wextra -Icpp cpp/AmountFormatter.cpp cpp/MaskEngine.cpp cpp/__tests__/MaskEngineTest.cpp -o .cache/mask-engine-test && .cache/mask-engine-test && clang++ -std=c++20 -O1 -Wall -Wextra -Icpp cpp/AmountFormatter.cpp cpp/MaskEngine.cpp cpp/__tests__/MaskEngineCorpusTest.cpp -o .cache/mask-engine-corpus-test && .cache/mask-engine-corpus-test && clang++ -std=c++20 -O1 -Wall -Wextra -Icpp cpp/OutlineGeometry.cpp cpp/__tests__/OutlineGeometryTest.cpp -o .cache/outline-geometry-test && .cache/outline-geometry-test && clang++ -std=c++20 -O1 -Wall -Wextra -Icpp cpp/RollingEngine.cpp cpp/__tests__/RollingEngineTest.cpp -o .cache/rolling-engine-test && .cache/rolling-engine-test && clang++ -std=c++20 -O1 -Wall -Wextra -Icpp cpp/numberformat/NumberFormatCore.cpp cpp/__tests__/NumberFormatCoreTest.cpp -o .cache/number-format-core-test && .cache/number-format-core-test && clang++ -std=c++20 -O1 -Wall -Wextra -Icpp cpp/numberformat/NumberFormatCore.cpp cpp/numberformat/NumberFormatProbe.cpp cpp/__tests__/NumberFormatProbeTest.cpp -o .cache/number-format-probe-test && .cache/number-format-probe-test",
|
|
42
|
+
"test:cpp": "mkdir -p .cache && clang++ -std=c++20 -O1 -Wall -Wextra -Icpp cpp/ReflowEngine.cpp cpp/AmountFormatter.cpp cpp/__tests__/ReflowEngineTest.cpp -o .cache/reflow-engine-test && .cache/reflow-engine-test && clang++ -std=c++20 -O1 -Wall -Wextra -Icpp cpp/AmountFormatter.cpp cpp/__tests__/AmountFormatterTest.cpp -o .cache/amount-formatter-test && .cache/amount-formatter-test && clang++ -std=c++20 -O1 -Wall -Wextra -Icpp cpp/AmountFormatter.cpp cpp/MaskEngine.cpp cpp/__tests__/MaskEngineTest.cpp -o .cache/mask-engine-test && .cache/mask-engine-test && clang++ -std=c++20 -O1 -Wall -Wextra -Icpp cpp/AmountFormatter.cpp cpp/MaskEngine.cpp cpp/__tests__/MaskEngineCorpusTest.cpp -o .cache/mask-engine-corpus-test && .cache/mask-engine-corpus-test && clang++ -std=c++20 -O1 -Wall -Wextra -Icpp cpp/OutlineGeometry.cpp cpp/__tests__/OutlineGeometryTest.cpp -o .cache/outline-geometry-test && .cache/outline-geometry-test && clang++ -std=c++20 -O1 -Wall -Wextra -Icpp cpp/RollingEngine.cpp cpp/__tests__/RollingEngineTest.cpp -o .cache/rolling-engine-test && .cache/rolling-engine-test && clang++ -std=c++20 -O1 -Wall -Wextra -Icpp cpp/numberformat/NumberFormatCore.cpp cpp/numberformat/FormatFigure.cpp cpp/__tests__/NumberFormatCoreTest.cpp -o .cache/number-format-core-test && .cache/number-format-core-test && clang++ -std=c++20 -O1 -Wall -Wextra -Icpp cpp/numberformat/NumberFormatCore.cpp cpp/numberformat/NumberFormatProbe.cpp cpp/__tests__/NumberFormatProbeTest.cpp -o .cache/number-format-probe-test && .cache/number-format-probe-test",
|
|
43
43
|
"test:cpp:sanitize": "mkdir -p .cache && clang++ -std=c++20 -O1 -g -fsanitize=address,undefined -fno-omit-frame-pointer -Icpp cpp/RollingEngine.cpp cpp/__tests__/RollingEngineTest.cpp -o .cache/rolling-engine-sanitize && .cache/rolling-engine-sanitize",
|
|
44
44
|
"release": "release-it"
|
|
45
45
|
},
|
package/src/NitroNumber.tsx
CHANGED
|
@@ -31,7 +31,7 @@ import type {
|
|
|
31
31
|
NitroNumberTextAlign,
|
|
32
32
|
} from './specs/NitroNumber.nitro'
|
|
33
33
|
import { toNumericWeight, toProcessedColor } from './styleHelpers'
|
|
34
|
-
import {
|
|
34
|
+
import { figureOf, formatProps, toNumber, withMinorDigits } from './formatProps'
|
|
35
35
|
import type { NumberFormat } from './NumberFormat'
|
|
36
36
|
|
|
37
37
|
/**
|
|
@@ -53,11 +53,28 @@ export interface NitroNumberProps extends Omit<ViewProps, 'children'> {
|
|
|
53
53
|
/**
|
|
54
54
|
* The number to display. Every change rolls each digit natively to its new
|
|
55
55
|
* glyph, in the direction of the change. The first value is shown instantly.
|
|
56
|
-
*
|
|
57
|
-
*
|
|
56
|
+
*
|
|
57
|
+
* A `bigint` or a decimal string (`"1234.56"`) is taken as is, with no
|
|
58
|
+
* conversion on your side: money kept as minor units passes straight in
|
|
59
|
+
* with `minorDigits`. With a `format`, it is rounded by the format, in
|
|
60
|
+
* decimal, so it never goes through a double first. The digits themselves
|
|
61
|
+
* animate as a double: exact while `|value| × 10^fractionDigits` stays
|
|
62
|
+
* within 2^53, and at most 18 digits are shown.
|
|
63
|
+
*/
|
|
64
|
+
value: number | bigint | string
|
|
65
|
+
/**
|
|
66
|
+
* The decimal places `value` is counted in: `value={123456n}` with
|
|
67
|
+
* `minorDigits={2}` is 1234.56, cents to dollars without dividing. Applied
|
|
68
|
+
* to a bigint or a decimal string exactly. Default: `0`.
|
|
69
|
+
*/
|
|
70
|
+
minorDigits?: number
|
|
71
|
+
/**
|
|
72
|
+
* Digits shown after the decimal separator. Default: `0`, or with a
|
|
73
|
+
* `format`, what the format shows for each value: its
|
|
74
|
+
* `trailingZeroDisplay`, a minimum below its maximum and significant
|
|
75
|
+
* digits all decide it per value, and the decimal columns roll in and out as
|
|
76
|
+
* the count changes. Set, it holds every value to this many.
|
|
58
77
|
*/
|
|
59
|
-
value: number
|
|
60
|
-
/** Digits shown after the decimal separator. Default: `0`. */
|
|
61
78
|
fractionDigits?: number
|
|
62
79
|
/** Zero-pads the integer part to at least this many digits. Default: `1`. */
|
|
63
80
|
minimumIntegerDigits?: number
|
|
@@ -231,9 +248,10 @@ export interface NitroNumberProps extends Omit<ViewProps, 'children'> {
|
|
|
231
248
|
tabularNums?: boolean
|
|
232
249
|
/**
|
|
233
250
|
* A `NumberFormat` the number follows: its prefix and suffix (the currency
|
|
234
|
-
* where the locale puts it), grouping and decimal separators,
|
|
235
|
-
* digits and
|
|
236
|
-
*
|
|
251
|
+
* where the locale puts it), grouping and decimal separators, minimum
|
|
252
|
+
* integer digits, and per value the digits it prints: rounded with its
|
|
253
|
+
* `roundingMode`, trimmed by its `trailingZeroDisplay`. The figure is what
|
|
254
|
+
* `format.format(value)` shows. The individual props override what it says.
|
|
237
255
|
*/
|
|
238
256
|
format?: NumberFormat
|
|
239
257
|
/**
|
|
@@ -379,6 +397,7 @@ export const NitroNumber = forwardRef<NitroNumberHandle, NitroNumberProps>(
|
|
|
379
397
|
function NitroNumber(
|
|
380
398
|
{
|
|
381
399
|
value,
|
|
400
|
+
minorDigits,
|
|
382
401
|
fractionDigits,
|
|
383
402
|
minimumIntegerDigits,
|
|
384
403
|
groupingSeparator,
|
|
@@ -463,8 +482,10 @@ export const NitroNumber = forwardRef<NitroNumberHandle, NitroNumberProps>(
|
|
|
463
482
|
latestOnRevealEnd.current = onRevealEnd
|
|
464
483
|
const latestOnRevealMilestone = useRef(onRevealMilestone)
|
|
465
484
|
latestOnRevealMilestone.current = onRevealMilestone
|
|
466
|
-
|
|
467
|
-
|
|
485
|
+
// The value as the format takes it: minor units shifted in, exactly.
|
|
486
|
+
const input = withMinorDigits(value, minorDigits)
|
|
487
|
+
const latestValue = useRef(input)
|
|
488
|
+
latestValue.current = input
|
|
468
489
|
const latestOnAnimationStart = useRef(onAnimationStart)
|
|
469
490
|
latestOnAnimationStart.current = onAnimationStart
|
|
470
491
|
const latestOnAnimationEnd = useRef(onAnimationEnd)
|
|
@@ -523,7 +544,7 @@ export const NitroNumber = forwardRef<NitroNumberHandle, NitroNumberProps>(
|
|
|
523
544
|
const onAnimationEndCallback = useMemo(
|
|
524
545
|
() =>
|
|
525
546
|
callback((shown: number) => {
|
|
526
|
-
latestOnAnimationEnd.current?.(compactRef.current ? latestValue.current : shown)
|
|
547
|
+
latestOnAnimationEnd.current?.(compactRef.current ? toNumber(latestValue.current) : shown)
|
|
527
548
|
}),
|
|
528
549
|
[]
|
|
529
550
|
)
|
|
@@ -545,7 +566,8 @@ export const NitroNumber = forwardRef<NitroNumberHandle, NitroNumberProps>(
|
|
|
545
566
|
jumpTo: (next) => nativeRef.current?.jumpTo(next),
|
|
546
567
|
animateTo: (next) => nativeRef.current?.animateTo(next),
|
|
547
568
|
revealTo: (next) => nativeRef.current?.revealTo(next),
|
|
548
|
-
getValue: () =>
|
|
569
|
+
getValue: () =>
|
|
570
|
+
compactRef.current ? toNumber(latestValue.current) : nativeRef.current?.value ?? toNumber(latestValue.current),
|
|
549
571
|
get native() {
|
|
550
572
|
return nativeRef.current
|
|
551
573
|
},
|
|
@@ -572,14 +594,21 @@ export const NitroNumber = forwardRef<NitroNumberHandle, NitroNumberProps>(
|
|
|
572
594
|
const resolvedFontSize = fontSize ?? 32
|
|
573
595
|
const resolvedAffixAlign = affixAlign ?? 'baseline'
|
|
574
596
|
const derived = format ? formatProps(format) : undefined
|
|
575
|
-
//
|
|
576
|
-
|
|
597
|
+
// What the format shows for this value: the figure rounded its way, in the
|
|
598
|
+
// fraction digits it prints. Compact notation also swaps the suffix: "1.2K"
|
|
599
|
+
// rolls the figure 1.2.
|
|
600
|
+
const figure = useMemo(() => (format ? figureOf(format, input) : undefined), [format, input])
|
|
601
|
+
const compact = derived?.compact ? figure : undefined
|
|
577
602
|
const compactRef = useRef(false)
|
|
578
603
|
compactRef.current = compact !== undefined
|
|
579
604
|
// Which way the value moved, for a compact figure that fell while the value grew (999 → 1K).
|
|
580
|
-
const
|
|
581
|
-
const
|
|
582
|
-
|
|
605
|
+
const numericValue = toNumber(input)
|
|
606
|
+
const previousValue = useRef(numericValue)
|
|
607
|
+
const compactDirection =
|
|
608
|
+
compact && direction === undefined ? (numericValue >= previousValue.current ? 'up' : 'down') : undefined
|
|
609
|
+
previousValue.current = numericValue
|
|
610
|
+
// Without a format the engine rounds the double, as it always has.
|
|
611
|
+
const shownValue = figure ? figure.value : numericValue
|
|
583
612
|
const processedPrefixColor = useMemo(() => toProcessedColor(prefixColor) ?? Infinity, [prefixColor])
|
|
584
613
|
const processedSuffixColor = useMemo(() => toProcessedColor(suffixColor) ?? Infinity, [suffixColor])
|
|
585
614
|
const processedFractionColor = useMemo(() => toProcessedColor(fractionColor) ?? Infinity, [fractionColor])
|
|
@@ -627,8 +656,8 @@ export const NitroNumber = forwardRef<NitroNumberHandle, NitroNumberProps>(
|
|
|
627
656
|
{...viewProps}
|
|
628
657
|
style={[autoSize, style]}
|
|
629
658
|
hybridRef={hybridRef}
|
|
630
|
-
value={
|
|
631
|
-
fractionDigits={fractionDigits ??
|
|
659
|
+
value={shownValue}
|
|
660
|
+
fractionDigits={fractionDigits ?? figure?.fractionDigits ?? 0}
|
|
632
661
|
minimumIntegerDigits={minimumIntegerDigits ?? derived?.minimumIntegerDigits ?? 1}
|
|
633
662
|
groupingSeparator={groupingSeparator ?? derived?.groupingSeparator ?? ''}
|
|
634
663
|
decimalSeparator={decimalSeparator ?? derived?.decimalSeparator ?? '.'}
|
package/src/NumberFormat.ts
CHANGED
|
@@ -2,6 +2,8 @@ import { NitroModules } from 'react-native-nitro-modules'
|
|
|
2
2
|
import type {
|
|
3
3
|
NitroNumberFormat,
|
|
4
4
|
NitroNumberFormatFactory,
|
|
5
|
+
NumberFigure,
|
|
6
|
+
NumberFormatLayout,
|
|
5
7
|
NumberFormatOptions,
|
|
6
8
|
NumberFormatPart,
|
|
7
9
|
ResolvedNumberFormatOptions,
|
|
@@ -390,6 +392,26 @@ export function sharedFormatterOf(value: unknown): object | undefined {
|
|
|
390
392
|
return typeof value === 'object' && value !== null ? slots.get(value)?.native : undefined
|
|
391
393
|
}
|
|
392
394
|
|
|
395
|
+
/**
|
|
396
|
+
* What `format` shows for `value`, as `NitroNumber` rolls it: the figure
|
|
397
|
+
* rounded and trimmed as the format prints it, and the text around it. Read
|
|
398
|
+
* in C++ off the formatter's own parts, from the number, bigint or decimal
|
|
399
|
+
* string itself.
|
|
400
|
+
* @internal
|
|
401
|
+
*/
|
|
402
|
+
export function figureOf(format: NumberFormat, value: number | bigint | string): NumberFigure {
|
|
403
|
+
return nativeOf(format, 'figure').figure(toMathematicalValue(value))
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
/**
|
|
407
|
+
* The prefix, suffix, separators, sign placement, grouping and digits of
|
|
408
|
+
* `format`, read in C++ off its parts.
|
|
409
|
+
* @internal
|
|
410
|
+
*/
|
|
411
|
+
export function layoutOf(format: NumberFormat): NumberFormatLayout {
|
|
412
|
+
return nativeOf(format, 'layout').layout()
|
|
413
|
+
}
|
|
414
|
+
|
|
393
415
|
// `length` is 0 as the specification says: the arguments are read from `arguments`.
|
|
394
416
|
function NumberFormatConstructor(this: unknown): unknown {
|
|
395
417
|
const locales = arguments[0]
|