react-native-nitro-input 0.1.0 → 0.3.0
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 +343 -3
- package/NitroInput.podspec +4 -4
- package/README.md +420 -53
- package/android/CMakeLists.txt +9 -3
- package/android/gradle.properties +1 -1
- package/android/src/main/cpp/JOutlineGeometry.hpp +1 -1
- package/android/src/main/cpp/JReflowEngine.cpp +132 -0
- package/android/src/main/cpp/{JMorphEngine.hpp → JReflowEngine.hpp} +7 -7
- package/android/src/main/cpp/JRollingEngine.cpp +223 -0
- package/android/src/main/cpp/JRollingEngine.hpp +68 -0
- package/android/src/main/cpp/cpp-adapter.cpp +4 -2
- package/android/src/main/java/com/margelo/nitro/nitroinput/AmountFormatter.kt +1 -1
- package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroInputView.kt +21 -1
- package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroNumberFormatPlatform.kt +209 -0
- package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroNumberView.kt +496 -0
- package/android/src/main/java/com/margelo/nitro/nitroinput/NitroInputPackage.kt +2 -1
- package/android/src/main/java/com/margelo/nitro/nitroinput/NitroInputView.kt +107 -26
- package/android/src/main/java/com/margelo/nitro/nitroinput/NitroNumberView.kt +1881 -0
- package/android/src/main/java/com/margelo/nitro/nitroinput/{MorphEngine.kt → ReflowEngine.kt} +2 -2
- package/android/src/main/java/com/margelo/nitro/nitroinput/RollingEngine.kt +60 -0
- package/cpp/AmountFormatter.cpp +5 -0
- package/cpp/AmountFormatter.hpp +10 -2
- package/cpp/{MorphEngine.cpp → ReflowEngine.cpp} +52 -47
- package/cpp/{MorphEngine.hpp → ReflowEngine.hpp} +12 -4
- package/cpp/RollingEngine.cpp +1234 -0
- package/cpp/RollingEngine.hpp +475 -0
- package/cpp/SwiftNonCopyable.hpp +22 -0
- package/cpp/numberformat/HybridNitroNumberFormat.cpp +498 -0
- package/cpp/numberformat/HybridNitroNumberFormat.hpp +74 -0
- package/cpp/numberformat/HybridNitroNumberFormatFactory.hpp +11 -0
- package/cpp/numberformat/NumberFormatCore.cpp +629 -0
- package/cpp/numberformat/NumberFormatCore.hpp +154 -0
- package/cpp/numberformat/NumberFormatProbe.cpp +354 -0
- package/cpp/numberformat/NumberFormatProbe.hpp +63 -0
- package/cpp/worklets/NitroInputWorkletsBridge.cpp +3 -3
- package/cpp/worklets/NitroInputWorkletsBridge.hpp +1 -1
- package/ios/{HybridMorphInputView.swift → HybridNitroInputView.swift} +16 -1
- package/ios/HybridNitroNumberFormatPlatform.swift +348 -0
- package/ios/HybridNitroNumberView.swift +458 -0
- package/ios/NitroInputView.swift +148 -64
- package/ios/NitroNumberView.swift +2074 -0
- package/lib/commonjs/nitrogen/generated/shared/json/NitroInputViewConfig.json +5 -0
- package/lib/commonjs/nitrogen/generated/shared/json/NitroNumberViewConfig.json +61 -0
- package/lib/commonjs/src/NitroInput.js +27 -56
- package/lib/commonjs/src/NitroNumber.js +141 -0
- package/lib/commonjs/src/NumberFormat.js +525 -0
- package/lib/commonjs/src/formatProps.js +48 -0
- package/lib/commonjs/src/index.js +6 -3
- package/lib/commonjs/src/specs/NitroNumber.nitro.js +2 -0
- package/lib/commonjs/src/specs/NumberFormat.nitro.js +2 -0
- package/lib/commonjs/src/styleHelpers.js +36 -0
- package/lib/module/nitrogen/generated/shared/json/NitroInputViewConfig.json +5 -0
- package/lib/module/nitrogen/generated/shared/json/NitroNumberViewConfig.json +61 -0
- package/lib/module/src/NitroInput.js +18 -47
- package/lib/module/src/NitroNumber.js +102 -0
- package/lib/module/src/NumberFormat.js +521 -0
- package/lib/module/src/formatProps.js +45 -0
- package/lib/module/src/index.js +2 -1
- package/lib/module/src/specs/NitroNumber.nitro.js +1 -0
- package/lib/module/src/specs/NumberFormat.nitro.js +1 -0
- package/lib/module/src/styleHelpers.js +32 -0
- package/lib/typescript/src/NitroInput.d.ts +50 -21
- package/lib/typescript/src/NitroNumber.d.ts +246 -0
- package/lib/typescript/src/NumberFormat.d.ts +47 -0
- package/lib/typescript/src/formatProps.d.ts +21 -0
- package/lib/typescript/src/index.d.ts +5 -2
- package/lib/typescript/src/specs/NitroInput.nitro.d.ts +19 -9
- package/lib/typescript/src/specs/NitroNumber.nitro.d.ts +250 -0
- package/lib/typescript/src/specs/NumberFormat.nitro.d.ts +139 -0
- package/lib/typescript/src/styleHelpers.d.ts +3 -0
- package/nitro.json +26 -0
- package/nitrogen/generated/android/NitroInput+autolinking.cmake +10 -0
- package/nitrogen/generated/android/NitroInputOnLoad.cpp +48 -0
- package/nitrogen/generated/android/c++/JFunc_void.hpp +75 -0
- package/nitrogen/generated/android/c++/JFunc_void_double_double.hpp +10 -10
- package/nitrogen/generated/android/c++/JHybridNitroInputViewSpec.cpp +45 -0
- package/nitrogen/generated/android/c++/JHybridNitroInputViewSpec.hpp +10 -0
- package/nitrogen/generated/android/c++/JHybridNitroNumberFormatPlatformSpec.cpp +118 -0
- package/nitrogen/generated/android/c++/JHybridNitroNumberFormatPlatformSpec.hpp +64 -0
- package/nitrogen/generated/android/c++/JHybridNitroNumberViewSpec.cpp +590 -0
- package/nitrogen/generated/android/c++/JHybridNitroNumberViewSpec.hpp +168 -0
- package/nitrogen/generated/android/c++/JHybridNitroPlatformNumberFormatterSpec.cpp +65 -0
- package/nitrogen/generated/android/c++/JHybridNitroPlatformNumberFormatterSpec.hpp +64 -0
- package/nitrogen/generated/android/c++/JNitroNumberAffixAlign.hpp +64 -0
- package/nitrogen/generated/android/c++/JNitroNumberDirection.hpp +64 -0
- package/nitrogen/generated/android/c++/JNitroNumberEasing.hpp +67 -0
- package/nitrogen/generated/android/c++/JNitroNumberRevealStyle.hpp +58 -0
- package/nitrogen/generated/android/c++/JNitroNumberTextAlign.hpp +64 -0
- package/nitrogen/generated/android/c++/JNitroNumberTransition.hpp +61 -0
- package/nitrogen/generated/android/c++/JNumberFormatCompactDisplay.hpp +58 -0
- package/nitrogen/generated/android/c++/JNumberFormatCurrencyDisplay.hpp +64 -0
- package/nitrogen/generated/android/c++/JNumberFormatCurrencySign.hpp +58 -0
- package/nitrogen/generated/android/c++/JNumberFormatNotation.hpp +64 -0
- package/nitrogen/generated/android/c++/JNumberFormatPlatformOptions.hpp +161 -0
- package/nitrogen/generated/android/c++/JNumberFormatPlatformSymbols.hpp +89 -0
- package/nitrogen/generated/android/c++/JNumberFormatRoundingMode.hpp +79 -0
- package/nitrogen/generated/android/c++/JNumberFormatSignDisplay.hpp +67 -0
- package/nitrogen/generated/android/c++/JNumberFormatStyle.hpp +64 -0
- package/nitrogen/generated/android/c++/JNumberFormatUnitDisplay.hpp +61 -0
- package/nitrogen/generated/android/c++/views/JHybridNitroInputViewStateUpdater.cpp +25 -0
- package/nitrogen/generated/android/c++/views/JHybridNitroNumberViewStateUpdater.cpp +329 -0
- package/nitrogen/generated/android/c++/views/JHybridNitroNumberViewStateUpdater.hpp +54 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/Func_void.kt +78 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/Func_void_double_double.kt +9 -9
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/HybridNitroInputViewSpec.kt +30 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/HybridNitroNumberFormatPlatformSpec.kt +60 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/HybridNitroNumberViewSpec.kt +399 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/HybridNitroPlatformNumberFormatterSpec.kt +62 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroNumberAffixAlign.kt +25 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroNumberDirection.kt +25 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroNumberEasing.kt +26 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroNumberRevealStyle.kt +23 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroNumberTextAlign.kt +25 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroNumberTransition.kt +24 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NumberFormatCompactDisplay.kt +23 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NumberFormatCurrencyDisplay.kt +25 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NumberFormatCurrencySign.kt +23 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NumberFormatNotation.kt +25 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NumberFormatPlatformOptions.kt +136 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NumberFormatPlatformSymbols.kt +91 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NumberFormatRoundingMode.kt +30 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NumberFormatSignDisplay.kt +26 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NumberFormatStyle.kt +25 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NumberFormatUnitDisplay.kt +24 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/views/HybridNitroNumberViewManager.kt +96 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/views/HybridNitroNumberViewStateUpdater.kt +23 -0
- package/nitrogen/generated/ios/NitroInput-Swift-Cxx-Bridge.cpp +59 -0
- package/nitrogen/generated/ios/NitroInput-Swift-Cxx-Bridge.hpp +311 -0
- package/nitrogen/generated/ios/NitroInput-Swift-Cxx-Umbrella.hpp +63 -0
- package/nitrogen/generated/ios/NitroInputAutolinking.mm +26 -0
- package/nitrogen/generated/ios/NitroInputAutolinking.swift +24 -0
- package/nitrogen/generated/ios/c++/HybridNitroInputViewSpecSwift.hpp +30 -0
- package/nitrogen/generated/ios/c++/HybridNitroNumberFormatPlatformSpecSwift.cpp +11 -0
- package/nitrogen/generated/ios/c++/HybridNitroNumberFormatPlatformSpecSwift.hpp +122 -0
- package/nitrogen/generated/ios/c++/HybridNitroNumberViewSpecSwift.cpp +11 -0
- package/nitrogen/generated/ios/c++/HybridNitroNumberViewSpecSwift.hpp +474 -0
- package/nitrogen/generated/ios/c++/HybridNitroPlatformNumberFormatterSpecSwift.cpp +11 -0
- package/nitrogen/generated/ios/c++/HybridNitroPlatformNumberFormatterSpecSwift.hpp +94 -0
- package/nitrogen/generated/ios/c++/views/HybridNitroInputViewComponent.mm +30 -0
- package/nitrogen/generated/ios/c++/views/HybridNitroNumberViewComponent.mm +450 -0
- package/nitrogen/generated/ios/swift/Func_void.swift +46 -0
- package/nitrogen/generated/ios/swift/Func_void_double_double.swift +5 -5
- package/nitrogen/generated/ios/swift/HybridNitroInputViewSpec.swift +5 -0
- package/nitrogen/generated/ios/swift/HybridNitroInputViewSpec_cxx.swift +55 -0
- package/nitrogen/generated/ios/swift/HybridNitroNumberFormatPlatformSpec.swift +56 -0
- package/nitrogen/generated/ios/swift/HybridNitroNumberFormatPlatformSpec_cxx.swift +159 -0
- package/nitrogen/generated/ios/swift/HybridNitroNumberViewSpec.swift +108 -0
- package/nitrogen/generated/ios/swift/HybridNitroNumberViewSpec_cxx.swift +1387 -0
- package/nitrogen/generated/ios/swift/HybridNitroPlatformNumberFormatterSpec.swift +56 -0
- package/nitrogen/generated/ios/swift/HybridNitroPlatformNumberFormatterSpec_cxx.swift +155 -0
- package/nitrogen/generated/ios/swift/NitroNumberAffixAlign.swift +48 -0
- package/nitrogen/generated/ios/swift/NitroNumberDirection.swift +48 -0
- package/nitrogen/generated/ios/swift/NitroNumberEasing.swift +52 -0
- package/nitrogen/generated/ios/swift/NitroNumberRevealStyle.swift +40 -0
- package/nitrogen/generated/ios/swift/NitroNumberTextAlign.swift +48 -0
- package/nitrogen/generated/ios/swift/NitroNumberTransition.swift +44 -0
- package/nitrogen/generated/ios/swift/NumberFormatCompactDisplay.swift +40 -0
- package/nitrogen/generated/ios/swift/NumberFormatCurrencyDisplay.swift +48 -0
- package/nitrogen/generated/ios/swift/NumberFormatCurrencySign.swift +40 -0
- package/nitrogen/generated/ios/swift/NumberFormatNotation.swift +48 -0
- package/nitrogen/generated/ios/swift/NumberFormatPlatformOptions.swift +159 -0
- package/nitrogen/generated/ios/swift/NumberFormatPlatformSymbols.swift +69 -0
- package/nitrogen/generated/ios/swift/NumberFormatRoundingMode.swift +68 -0
- package/nitrogen/generated/ios/swift/NumberFormatSignDisplay.swift +52 -0
- package/nitrogen/generated/ios/swift/NumberFormatStyle.swift +48 -0
- package/nitrogen/generated/ios/swift/NumberFormatUnitDisplay.swift +44 -0
- package/nitrogen/generated/shared/c++/HybridNitroInputViewSpec.cpp +10 -0
- package/nitrogen/generated/shared/c++/HybridNitroInputViewSpec.hpp +10 -0
- package/nitrogen/generated/shared/c++/HybridNitroNumberFormatFactorySpec.cpp +22 -0
- package/nitrogen/generated/shared/c++/HybridNitroNumberFormatFactorySpec.hpp +70 -0
- package/nitrogen/generated/shared/c++/HybridNitroNumberFormatPlatformSpec.cpp +22 -0
- package/nitrogen/generated/shared/c++/HybridNitroNumberFormatPlatformSpec.hpp +70 -0
- package/nitrogen/generated/shared/c++/HybridNitroNumberFormatSpec.cpp +23 -0
- package/nitrogen/generated/shared/c++/HybridNitroNumberFormatSpec.hpp +71 -0
- package/nitrogen/generated/shared/c++/HybridNitroNumberViewSpec.cpp +127 -0
- package/nitrogen/generated/shared/c++/HybridNitroNumberViewSpec.hpp +187 -0
- package/nitrogen/generated/shared/c++/HybridNitroPlatformNumberFormatterSpec.cpp +23 -0
- package/nitrogen/generated/shared/c++/HybridNitroPlatformNumberFormatterSpec.hpp +65 -0
- package/nitrogen/generated/shared/c++/NitroNumberAffixAlign.hpp +84 -0
- package/nitrogen/generated/shared/c++/NitroNumberDirection.hpp +84 -0
- package/nitrogen/generated/shared/c++/NitroNumberEasing.hpp +88 -0
- package/nitrogen/generated/shared/c++/NitroNumberRevealStyle.hpp +76 -0
- package/nitrogen/generated/shared/c++/NitroNumberTextAlign.hpp +84 -0
- package/nitrogen/generated/shared/c++/NitroNumberTransition.hpp +80 -0
- package/nitrogen/generated/shared/c++/NumberFormatCompactDisplay.hpp +76 -0
- package/nitrogen/generated/shared/c++/NumberFormatCurrencyDisplay.hpp +84 -0
- package/nitrogen/generated/shared/c++/NumberFormatCurrencySign.hpp +76 -0
- package/nitrogen/generated/shared/c++/NumberFormatGrouping.hpp +80 -0
- package/nitrogen/generated/shared/c++/NumberFormatNotation.hpp +84 -0
- package/nitrogen/generated/shared/c++/NumberFormatOptions.hpp +193 -0
- package/nitrogen/generated/shared/c++/NumberFormatPart.hpp +87 -0
- package/nitrogen/generated/shared/c++/NumberFormatPlatformOptions.hpp +176 -0
- package/nitrogen/generated/shared/c++/NumberFormatPlatformSymbols.hpp +115 -0
- package/nitrogen/generated/shared/c++/NumberFormatRoundingMode.hpp +104 -0
- package/nitrogen/generated/shared/c++/NumberFormatRoundingPriority.hpp +80 -0
- package/nitrogen/generated/shared/c++/NumberFormatSignDisplay.hpp +88 -0
- package/nitrogen/generated/shared/c++/NumberFormatStyle.hpp +84 -0
- package/nitrogen/generated/shared/c++/NumberFormatTrailingZeroDisplay.hpp +76 -0
- package/nitrogen/generated/shared/c++/NumberFormatUnitDisplay.hpp +80 -0
- package/nitrogen/generated/shared/c++/ResolvedNumberFormatOptions.hpp +197 -0
- package/nitrogen/generated/shared/c++/views/HybridNitroInputViewComponent.cpp +10 -0
- package/nitrogen/generated/shared/c++/views/HybridNitroInputViewComponent.hpp +15 -0
- package/nitrogen/generated/shared/c++/views/HybridNitroNumberViewComponent.cpp +136 -0
- package/nitrogen/generated/shared/c++/views/HybridNitroNumberViewComponent.hpp +245 -0
- package/nitrogen/generated/shared/json/NitroInputViewConfig.json +5 -0
- package/nitrogen/generated/shared/json/NitroNumberViewConfig.json +61 -0
- package/package.json +15 -7
- package/src/NitroInput.tsx +79 -67
- package/src/NitroNumber.tsx +531 -0
- package/src/NumberFormat.ts +563 -0
- package/src/formatProps.ts +59 -0
- package/src/index.ts +38 -6
- package/src/specs/NitroInput.nitro.ts +19 -9
- package/src/specs/NitroNumber.nitro.ts +271 -0
- package/src/specs/NumberFormat.nitro.ts +145 -0
- package/src/styleHelpers.ts +32 -0
- package/android/src/main/cpp/JMorphEngine.cpp +0 -132
- package/lib/commonjs/src/MorphInput.js +0 -42
- package/lib/module/src/MorphInput.js +0 -6
- package/lib/typescript/src/MorphInput.d.ts +0 -18
- package/src/MorphInput.tsx +0 -30
|
@@ -0,0 +1,348 @@
|
|
|
1
|
+
//
|
|
2
|
+
// HybridNitroNumberFormatPlatform.swift
|
|
3
|
+
// NitroInput
|
|
4
|
+
//
|
|
5
|
+
// Foundation's `NumberFormatter`, for the C++ `NumberFormat`: C++ learns a
|
|
6
|
+
// locale's format from it once (see NumberFormatProbe) and formats with it
|
|
7
|
+
// only for what C++ does not draw itself (compact and scientific notation,
|
|
8
|
+
// currency names). Hermes' `Intl.NumberFormat` uses the same formatter on
|
|
9
|
+
// iOS, so the two agree.
|
|
10
|
+
//
|
|
11
|
+
|
|
12
|
+
import Foundation
|
|
13
|
+
import NitroModules
|
|
14
|
+
|
|
15
|
+
final class HybridNitroNumberFormatPlatform: HybridNitroNumberFormatPlatformSpec {
|
|
16
|
+
func create(options: NumberFormatPlatformOptions) throws -> any HybridNitroPlatformNumberFormatterSpec {
|
|
17
|
+
return try HybridNitroPlatformNumberFormatter(options: options)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
func supportedLocalesOf(locales: [String]) throws -> [String] {
|
|
21
|
+
return locales.compactMap { NumberFormatLocales.match($0)?.tag }
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/// ECMA-402's lookup matcher over the locales Foundation has data for.
|
|
26
|
+
enum NumberFormatLocales {
|
|
27
|
+
private static let available = Set(Locale.availableIdentifiers)
|
|
28
|
+
|
|
29
|
+
struct Match {
|
|
30
|
+
/// The matched locale as a BCP 47 tag, without extensions ("de-DE").
|
|
31
|
+
let tag: String
|
|
32
|
+
/// Foundation's identifier for it ("de_DE").
|
|
33
|
+
let identifier: String
|
|
34
|
+
/// The numbering system asked for with `-u-nu-`, if any.
|
|
35
|
+
let numberingSystem: String?
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
static func match(_ requested: String) -> Match? {
|
|
39
|
+
let lowered = requested.replacingOccurrences(of: "_", with: "-").lowercased()
|
|
40
|
+
// "de-DE-u-nu-latn": the base tag and the numbering system of its Unicode extension.
|
|
41
|
+
var base = lowered
|
|
42
|
+
var numberingSystem: String?
|
|
43
|
+
if let range = lowered.range(of: "-u-") {
|
|
44
|
+
base = String(lowered[..<range.lowerBound])
|
|
45
|
+
let keys = lowered[range.upperBound...].split(separator: "-")
|
|
46
|
+
if let nu = keys.firstIndex(of: "nu"), nu + 1 < keys.endIndex { numberingSystem = String(keys[nu + 1]) }
|
|
47
|
+
}
|
|
48
|
+
// Canonical case: language lower, script title, region upper ("zh-hant-tw" → zh_Hant_TW).
|
|
49
|
+
var subtags = base.split(separator: "-").enumerated().map { index, tag -> String in
|
|
50
|
+
let tag = String(tag)
|
|
51
|
+
if index == 0 { return tag }
|
|
52
|
+
if tag.count == 4 { return tag.prefix(1).uppercased() + tag.dropFirst() }
|
|
53
|
+
if tag.count == 2 || (tag.count == 3 && tag.allSatisfy(\.isNumber)) { return tag.uppercased() }
|
|
54
|
+
return tag
|
|
55
|
+
}
|
|
56
|
+
// Foundation lists some locales with their script only ("zh_Hant_TW" for zh-TW): try the full form too.
|
|
57
|
+
if #available(iOS 16.0, *), subtags.count > 1 {
|
|
58
|
+
let maximal = Locale.Language(identifier: subtags.joined(separator: "-")).maximalIdentifier.replacingOccurrences(of: "-", with: "_")
|
|
59
|
+
if !available.contains(subtags.joined(separator: "_")), available.contains(maximal) {
|
|
60
|
+
return Match(tag: subtags.joined(separator: "-"), identifier: maximal, numberingSystem: numberingSystem)
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
while !subtags.isEmpty {
|
|
64
|
+
let identifier = subtags.joined(separator: "_")
|
|
65
|
+
if available.contains(identifier) {
|
|
66
|
+
return Match(tag: subtags.joined(separator: "-"), identifier: identifier, numberingSystem: numberingSystem)
|
|
67
|
+
}
|
|
68
|
+
subtags.removeLast()
|
|
69
|
+
}
|
|
70
|
+
return nil
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/// The first requested locale Foundation knows, or the device's.
|
|
74
|
+
static func resolve(_ requested: [String]) -> Match {
|
|
75
|
+
for tag in requested {
|
|
76
|
+
if let match = match(tag) { return match }
|
|
77
|
+
}
|
|
78
|
+
let current = Locale.current.identifier
|
|
79
|
+
let base = current.split(separator: "@").first.map(String.init) ?? current
|
|
80
|
+
return Match(tag: base.replacingOccurrences(of: "_", with: "-"), identifier: base, numberingSystem: nil)
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
final class HybridNitroPlatformNumberFormatter: HybridNitroPlatformNumberFormatterSpec {
|
|
85
|
+
private let formatter = NumberFormatter()
|
|
86
|
+
/// Compact notation, which `NumberFormatter` does not have (iOS 16+).
|
|
87
|
+
private var compact: ((Double) -> String)?
|
|
88
|
+
/// Units, through `MeasurementFormatter` with this formatter for the number.
|
|
89
|
+
private var measure: ((Double) -> String)?
|
|
90
|
+
let symbols: NumberFormatPlatformSymbols
|
|
91
|
+
|
|
92
|
+
init(options o: NumberFormatPlatformOptions) throws {
|
|
93
|
+
let match = NumberFormatLocales.resolve(o.locales)
|
|
94
|
+
// Foundation knows numbering systems only in lower case, and one it does not
|
|
95
|
+
// know leaves the formatter without symbols; Intl ignores those, so drop it.
|
|
96
|
+
var numberingSystem = (o.numberingSystem ?? match.numberingSystem)?.lowercased()
|
|
97
|
+
// The currency goes into the locale too: Foundation otherwise takes some
|
|
98
|
+
// patterns (accounting in tr_TR) from the locale's own currency.
|
|
99
|
+
func makeLocale() -> Locale {
|
|
100
|
+
var keywords: [String] = []
|
|
101
|
+
if o.style == .currency, let code = o.currency { keywords.append("currency=\(code)") }
|
|
102
|
+
if let numberingSystem { keywords.append("numbers=\(numberingSystem)") }
|
|
103
|
+
return Locale(identifier: keywords.isEmpty ? match.identifier : "\(match.identifier)@\(keywords.joined(separator: ";"))")
|
|
104
|
+
}
|
|
105
|
+
var locale = makeLocale()
|
|
106
|
+
formatter.locale = locale
|
|
107
|
+
if numberingSystem != nil, (formatter.minusSign as String?) == nil {
|
|
108
|
+
numberingSystem = nil
|
|
109
|
+
locale = makeLocale()
|
|
110
|
+
formatter.locale = locale
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
var currencyShown = ""
|
|
114
|
+
switch o.style {
|
|
115
|
+
case .decimal:
|
|
116
|
+
formatter.numberStyle = .decimal
|
|
117
|
+
case .percent:
|
|
118
|
+
formatter.numberStyle = .percent
|
|
119
|
+
case .currency:
|
|
120
|
+
switch o.currencyDisplay {
|
|
121
|
+
case .code:
|
|
122
|
+
formatter.numberStyle = .currencyISOCode
|
|
123
|
+
case .name:
|
|
124
|
+
formatter.numberStyle = .currencyPlural
|
|
125
|
+
default:
|
|
126
|
+
formatter.numberStyle = o.currencySign == .accounting ? .currencyAccounting : .currency
|
|
127
|
+
}
|
|
128
|
+
formatter.currencyCode = o.currency ?? "USD"
|
|
129
|
+
if o.currencyDisplay == .narrowsymbol, let narrow = Self.narrowSymbol(o.currency ?? "USD", locale: locale) {
|
|
130
|
+
formatter.currencySymbol = narrow
|
|
131
|
+
}
|
|
132
|
+
currencyShown = (o.currencyDisplay == .code ? formatter.internationalCurrencySymbol as String? : formatter.currencySymbol as String?) ?? (o.currency ?? "")
|
|
133
|
+
case .unit:
|
|
134
|
+
formatter.numberStyle = .decimal
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
switch o.notation {
|
|
138
|
+
case .scientific, .engineering:
|
|
139
|
+
formatter.numberStyle = .scientific
|
|
140
|
+
case .compact, .standard:
|
|
141
|
+
break
|
|
142
|
+
}
|
|
143
|
+
formatter.usesGroupingSeparator = o.useGrouping
|
|
144
|
+
formatter.minimumIntegerDigits = Int(o.minimumIntegerDigits)
|
|
145
|
+
if o.minimumSignificantDigits > 0 {
|
|
146
|
+
formatter.usesSignificantDigits = true
|
|
147
|
+
formatter.minimumSignificantDigits = Int(o.minimumSignificantDigits)
|
|
148
|
+
formatter.maximumSignificantDigits = Int(o.maximumSignificantDigits)
|
|
149
|
+
} else {
|
|
150
|
+
formatter.minimumFractionDigits = Int(o.minimumFractionDigits)
|
|
151
|
+
formatter.maximumFractionDigits = Int(o.maximumFractionDigits)
|
|
152
|
+
}
|
|
153
|
+
formatter.roundingMode = Self.roundingMode(o.roundingMode)
|
|
154
|
+
switch o.signDisplay {
|
|
155
|
+
case .always, .exceptzero:
|
|
156
|
+
formatter.positivePrefix = ((formatter.plusSign as String?) ?? "+") + ((formatter.positivePrefix as String?) ?? "")
|
|
157
|
+
case .never:
|
|
158
|
+
formatter.negativePrefix = formatter.positivePrefix
|
|
159
|
+
formatter.negativeSuffix = formatter.positiveSuffix
|
|
160
|
+
case .auto, .negative:
|
|
161
|
+
break
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
if o.notation == .compact, #available(iOS 16.0, *) {
|
|
165
|
+
var style = FloatingPointFormatStyle<Double>(locale: locale)
|
|
166
|
+
.notation(.compactName)
|
|
167
|
+
.grouping(o.useGrouping ? .automatic : .never)
|
|
168
|
+
if o.minimumSignificantDigits > 0 {
|
|
169
|
+
style = style.precision(.significantDigits(Int(o.minimumSignificantDigits)...Int(o.maximumSignificantDigits)))
|
|
170
|
+
} else {
|
|
171
|
+
style = style.precision(.fractionLength(Int(o.minimumFractionDigits)...Int(o.maximumFractionDigits)))
|
|
172
|
+
}
|
|
173
|
+
compact = { style.format($0) }
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
if o.style == .unit, let unit = o.unit {
|
|
177
|
+
measure = try Self.unitFormatter(unit, display: o.unitDisplay, locale: locale, number: formatter)
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
symbols = NumberFormatPlatformSymbols(
|
|
181
|
+
locale: match.tag,
|
|
182
|
+
numberingSystem: Self.numberingSystem(of: locale),
|
|
183
|
+
minusSign: (formatter.minusSign as String?) ?? "-",
|
|
184
|
+
plusSign: (formatter.plusSign as String?) ?? "+",
|
|
185
|
+
percentSign: (formatter.percentSymbol as String?) ?? "%",
|
|
186
|
+
currency: currencyShown,
|
|
187
|
+
nan: (formatter.notANumberSymbol as String?) ?? "NaN",
|
|
188
|
+
infinity: (formatter.positiveInfinitySymbol as String?) ?? "∞",
|
|
189
|
+
exponentSeparator: (formatter.exponentSymbol as String?) ?? "E"
|
|
190
|
+
)
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
func format(value: Double) throws -> String {
|
|
194
|
+
if let measure { return measure(value) }
|
|
195
|
+
if let compact { return compact(value) }
|
|
196
|
+
return formatter.string(from: NSNumber(value: value)) ?? ""
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
func formatDecimal(value: String) throws -> String {
|
|
200
|
+
if value.hasSuffix("Infinity") || value == "NaN" {
|
|
201
|
+
return try format(value: value == "NaN" ? .nan : value.hasPrefix("-") ? -.infinity : .infinity)
|
|
202
|
+
}
|
|
203
|
+
if compact != nil || measure != nil { return try format(value: Double(value) ?? .nan) }
|
|
204
|
+
let number = NSDecimalNumber(string: value, locale: Locale(identifier: "en_US_POSIX"))
|
|
205
|
+
return formatter.string(from: number) ?? ""
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/// ECMA-402's sanctioned units as Foundation units.
|
|
209
|
+
private static func dimension(_ unit: String) -> Dimension? {
|
|
210
|
+
switch unit {
|
|
211
|
+
case "acre": return UnitArea.acres
|
|
212
|
+
case "hectare": return UnitArea.hectares
|
|
213
|
+
case "bit": return UnitInformationStorage.bits
|
|
214
|
+
case "byte": return UnitInformationStorage.bytes
|
|
215
|
+
case "kilobit": return UnitInformationStorage.kilobits
|
|
216
|
+
case "kilobyte": return UnitInformationStorage.kilobytes
|
|
217
|
+
case "megabit": return UnitInformationStorage.megabits
|
|
218
|
+
case "megabyte": return UnitInformationStorage.megabytes
|
|
219
|
+
case "gigabit": return UnitInformationStorage.gigabits
|
|
220
|
+
case "gigabyte": return UnitInformationStorage.gigabytes
|
|
221
|
+
case "terabit": return UnitInformationStorage.terabits
|
|
222
|
+
case "terabyte": return UnitInformationStorage.terabytes
|
|
223
|
+
case "petabyte": return UnitInformationStorage.petabytes
|
|
224
|
+
case "celsius": return UnitTemperature.celsius
|
|
225
|
+
case "fahrenheit": return UnitTemperature.fahrenheit
|
|
226
|
+
case "centimeter": return UnitLength.centimeters
|
|
227
|
+
case "foot": return UnitLength.feet
|
|
228
|
+
case "inch": return UnitLength.inches
|
|
229
|
+
case "kilometer": return UnitLength.kilometers
|
|
230
|
+
case "meter": return UnitLength.meters
|
|
231
|
+
case "mile": return UnitLength.miles
|
|
232
|
+
case "mile-scandinavian": return UnitLength.scandinavianMiles
|
|
233
|
+
case "millimeter": return UnitLength.millimeters
|
|
234
|
+
case "yard": return UnitLength.yards
|
|
235
|
+
case "degree": return UnitAngle.degrees
|
|
236
|
+
case "fluid-ounce": return UnitVolume.fluidOunces
|
|
237
|
+
case "gallon": return UnitVolume.gallons
|
|
238
|
+
case "liter": return UnitVolume.liters
|
|
239
|
+
case "milliliter": return UnitVolume.milliliters
|
|
240
|
+
case "gram": return UnitMass.grams
|
|
241
|
+
case "kilogram": return UnitMass.kilograms
|
|
242
|
+
case "ounce": return UnitMass.ounces
|
|
243
|
+
case "pound": return UnitMass.pounds
|
|
244
|
+
case "stone": return UnitMass.stones
|
|
245
|
+
case "hour": return UnitDuration.hours
|
|
246
|
+
case "minute": return UnitDuration.minutes
|
|
247
|
+
case "second": return UnitDuration.seconds
|
|
248
|
+
case "millisecond": return UnitDuration.milliseconds
|
|
249
|
+
case "microsecond": return UnitDuration.microseconds
|
|
250
|
+
case "nanosecond": return UnitDuration.nanoseconds
|
|
251
|
+
case "kilometer-per-hour": return UnitSpeed.kilometersPerHour
|
|
252
|
+
case "meter-per-second": return UnitSpeed.metersPerSecond
|
|
253
|
+
case "mile-per-hour": return UnitSpeed.milesPerHour
|
|
254
|
+
case "mile-per-gallon": return UnitFuelEfficiency.milesPerGallon
|
|
255
|
+
case "liter-per-kilometer": return nil
|
|
256
|
+
default: return nil
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/// A unit formatter: Foundation's for the units it has; days to years and
|
|
261
|
+
/// other compound units through DateComponentsFormatter or "x/y".
|
|
262
|
+
private static func unitFormatter(_ unit: String, display: NumberFormatUnitDisplay, locale: Locale, number: NumberFormatter) throws -> (Double) -> String {
|
|
263
|
+
if unit == "percent" {
|
|
264
|
+
let percent = number.copy() as! NumberFormatter
|
|
265
|
+
percent.numberStyle = .percent
|
|
266
|
+
percent.multiplier = 1
|
|
267
|
+
percent.minimumFractionDigits = number.minimumFractionDigits
|
|
268
|
+
percent.maximumFractionDigits = number.maximumFractionDigits
|
|
269
|
+
return { percent.string(from: NSNumber(value: $0)) ?? "" }
|
|
270
|
+
}
|
|
271
|
+
let formatter = MeasurementFormatter()
|
|
272
|
+
formatter.locale = locale
|
|
273
|
+
formatter.unitOptions = .providedUnit
|
|
274
|
+
formatter.numberFormatter = number
|
|
275
|
+
formatter.unitStyle = display == .long ? .long : display == .narrow ? .short : .medium
|
|
276
|
+
if let dimension = dimension(unit) {
|
|
277
|
+
return { (value: Double) -> String in
|
|
278
|
+
let measurement: Measurement<Dimension> = Measurement(value: value, unit: dimension)
|
|
279
|
+
return formatter.string(from: measurement)
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
let durations: [String: NSCalendar.Unit] = ["day": .day, "week": .weekOfMonth, "month": .month, "year": .year]
|
|
283
|
+
if let calendarUnit = durations[unit] {
|
|
284
|
+
let components = DateComponentsFormatter()
|
|
285
|
+
components.allowedUnits = [calendarUnit]
|
|
286
|
+
components.unitsStyle = display == .long ? .full : display == .narrow ? .abbreviated : .short
|
|
287
|
+
var calendar = Calendar(identifier: .gregorian)
|
|
288
|
+
calendar.locale = locale
|
|
289
|
+
components.calendar = calendar
|
|
290
|
+
return { value in
|
|
291
|
+
var parts = DateComponents()
|
|
292
|
+
let whole = Int(value.rounded(.towardZero))
|
|
293
|
+
switch calendarUnit {
|
|
294
|
+
case .day: parts.day = whole
|
|
295
|
+
case .weekOfMonth: parts.weekOfMonth = whole
|
|
296
|
+
case .month: parts.month = whole
|
|
297
|
+
default: parts.year = whole
|
|
298
|
+
}
|
|
299
|
+
return value == value.rounded(.towardZero) ? (components.string(from: parts) ?? "") : "\(number.string(from: NSNumber(value: value)) ?? "") \(unit)"
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
if let per = unit.range(of: "-per-") {
|
|
303
|
+
let first = String(unit[..<per.lowerBound])
|
|
304
|
+
let second = String(unit[per.upperBound...])
|
|
305
|
+
// "x-per-y" as "x/y", with Foundation's symbol for y where it has one.
|
|
306
|
+
let head = try unitFormatter(first, display: display, locale: locale, number: number)
|
|
307
|
+
var suffix = second
|
|
308
|
+
if let b = dimension(second) {
|
|
309
|
+
let symbols = MeasurementFormatter()
|
|
310
|
+
symbols.locale = locale
|
|
311
|
+
symbols.unitStyle = .short
|
|
312
|
+
let unitB: Unit = b
|
|
313
|
+
suffix = symbols.string(from: unitB)
|
|
314
|
+
}
|
|
315
|
+
return { (value: Double) -> String in head(value) + "/" + suffix }
|
|
316
|
+
}
|
|
317
|
+
throw RuntimeError.error(withMessage: "NumberFormat: the unit '\(unit)' is not available on iOS.")
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
private static func roundingMode(_ mode: NumberFormatRoundingMode) -> NumberFormatter.RoundingMode {
|
|
321
|
+
switch mode {
|
|
322
|
+
case .ceil: return .ceiling
|
|
323
|
+
case .floor: return .floor
|
|
324
|
+
case .expand: return .up
|
|
325
|
+
case .trunc: return .down
|
|
326
|
+
case .halftrunc: return .halfDown
|
|
327
|
+
case .halfeven: return .halfEven
|
|
328
|
+
case .halfexpand, .halfceil, .halffloor: return .halfUp
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
private static func numberingSystem(of locale: Locale) -> String {
|
|
333
|
+
if #available(iOS 16.0, *) { return locale.numberingSystem.identifier }
|
|
334
|
+
return "latn"
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/// The currency's narrow symbol ("$" for CA$), read off a formatted zero.
|
|
338
|
+
private static func narrowSymbol(_ code: String, locale: Locale) -> String? {
|
|
339
|
+
guard #available(iOS 16.0, *) else { return nil }
|
|
340
|
+
let text = Decimal(0).formatted(.currency(code: code).locale(locale).presentation(.narrow))
|
|
341
|
+
let symbol = text.unicodeScalars.filter { scalar in
|
|
342
|
+
!CharacterSet.decimalDigits.contains(scalar) && !CharacterSet.whitespaces.contains(scalar) && !CharacterSet.punctuationCharacters.contains(scalar)
|
|
343
|
+
&& !(0x200E...0x200F).contains(scalar.value) && scalar.value != 0x061C
|
|
344
|
+
}
|
|
345
|
+
let result = String(String.UnicodeScalarView(symbol))
|
|
346
|
+
return result.isEmpty ? nil : result
|
|
347
|
+
}
|
|
348
|
+
}
|