@themobilefirstcompany/react-native-data-detector 0.1.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/LICENSE +21 -0
- package/README.md +133 -0
- package/android/build.gradle +28 -0
- package/android/src/main/AndroidManifest.xml +2 -0
- package/android/src/main/java/expo/modules/datadetector/ReactNativeDataDetectorModule.kt +115 -0
- package/build/ReactNativeDataDetector.d.ts +13 -0
- package/build/ReactNativeDataDetector.d.ts.map +1 -0
- package/build/ReactNativeDataDetector.js +19 -0
- package/build/ReactNativeDataDetector.js.map +1 -0
- package/build/ReactNativeDataDetector.types.d.ts +27 -0
- package/build/ReactNativeDataDetector.types.d.ts.map +1 -0
- package/build/ReactNativeDataDetector.types.js +2 -0
- package/build/ReactNativeDataDetector.types.js.map +1 -0
- package/build/index.d.ts +3 -0
- package/build/index.d.ts.map +1 -0
- package/build/index.js +2 -0
- package/build/index.js.map +1 -0
- package/expo-module.config.json +9 -0
- package/ios/ReactNativeDataDetector.podspec +21 -0
- package/ios/ReactNativeDataDetectorModule.swift +105 -0
- package/package.json +50 -0
- package/src/ReactNativeDataDetector.ts +26 -0
- package/src/ReactNativeDataDetector.types.ts +33 -0
- package/src/index.ts +2 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Pablo Giraud-Carrier
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
# react-native-data-detector
|
|
2
|
+
|
|
3
|
+
Cross-platform text data detection for React Native. Uses **NSDataDetector** on iOS and **ML Kit Entity Extraction** on Android to detect phone numbers, URLs, emails, dates, and addresses — returning structured results to JavaScript.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Phone numbers** — Detect and extract phone numbers
|
|
8
|
+
- **URLs** — Detect web links
|
|
9
|
+
- **Emails** — Detect email addresses
|
|
10
|
+
- **Addresses** — Detect street addresses with parsed components (iOS)
|
|
11
|
+
- **Dates** — Detect dates and times with ISO 8601 output
|
|
12
|
+
- **Native accuracy** — Uses battle-tested platform APIs instead of regex
|
|
13
|
+
- **Expo Modules API** — Built with the modern Expo native module system
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm install react-native-data-detector
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
### iOS
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
npx pod-install
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
### Android
|
|
28
|
+
|
|
29
|
+
The ML Kit entity extraction model (~5.6MB) is downloaded on the user's device at runtime. You can control when this happens using [`downloadModel()`](#downloadmodel) — for example, calling it at app startup to ensure `detect()` works offline later. If you don't call it explicitly, the model will be downloaded automatically on the first `detect()` call.
|
|
30
|
+
|
|
31
|
+
## Usage
|
|
32
|
+
|
|
33
|
+
```typescript
|
|
34
|
+
import { detect, downloadModel } from 'react-native-data-detector';
|
|
35
|
+
|
|
36
|
+
// Pre-download the ML Kit model at app startup (Android only, no-op on iOS)
|
|
37
|
+
await downloadModel();
|
|
38
|
+
|
|
39
|
+
// Detect all entity types
|
|
40
|
+
const entities = await detect('Call me at 555-1234 or email john@example.com');
|
|
41
|
+
// [
|
|
42
|
+
// { type: 'phoneNumber', text: '555-1234', start: 14, end: 22, data: { phoneNumber: '555-1234' } },
|
|
43
|
+
// { type: 'email', text: 'john@example.com', start: 32, end: 48, data: { email: 'john@example.com' } }
|
|
44
|
+
// ]
|
|
45
|
+
|
|
46
|
+
// Detect only specific types
|
|
47
|
+
const phones = await detect('Call 555-1234 or visit https://example.com', {
|
|
48
|
+
types: ['phoneNumber'],
|
|
49
|
+
});
|
|
50
|
+
// [
|
|
51
|
+
// { type: 'phoneNumber', text: '555-1234', start: 5, end: 13, data: { phoneNumber: '555-1234' } }
|
|
52
|
+
// ]
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## API
|
|
56
|
+
|
|
57
|
+
### `downloadModel()`
|
|
58
|
+
|
|
59
|
+
Pre-downloads the ML Kit entity extraction model on Android. On iOS, this is a no-op that resolves immediately — `NSDataDetector` is built into the OS and requires no model download.
|
|
60
|
+
|
|
61
|
+
Call this at app startup or before the first `detect()` call to ensure the model is available offline.
|
|
62
|
+
|
|
63
|
+
**Returns:** `Promise<boolean>` — `true` when the model is ready.
|
|
64
|
+
|
|
65
|
+
| Platform | Behavior |
|
|
66
|
+
|----------|----------|
|
|
67
|
+
| **iOS** | No-op, resolves `true` immediately |
|
|
68
|
+
| **Android** | Downloads the ML Kit model (~5.6MB) if not already cached. Requires internet on first call. |
|
|
69
|
+
|
|
70
|
+
### `detect(text, options?)`
|
|
71
|
+
|
|
72
|
+
Detects entities in the given text using native platform APIs.
|
|
73
|
+
|
|
74
|
+
**Parameters:**
|
|
75
|
+
|
|
76
|
+
| Parameter | Type | Description |
|
|
77
|
+
|-----------|------|-------------|
|
|
78
|
+
| `text` | `string` | The text to analyze |
|
|
79
|
+
| `options` | `DetectOptions` | Optional configuration |
|
|
80
|
+
|
|
81
|
+
**DetectOptions:**
|
|
82
|
+
|
|
83
|
+
| Property | Type | Default | Description |
|
|
84
|
+
|----------|------|---------|-------------|
|
|
85
|
+
| `types` | `DetectionType[]` | All types | Which entity types to detect |
|
|
86
|
+
|
|
87
|
+
**DetectionType:** `'phoneNumber' | 'link' | 'email' | 'address' | 'date'`
|
|
88
|
+
|
|
89
|
+
**Returns:** `Promise<DetectedEntity[]>`
|
|
90
|
+
|
|
91
|
+
### `DetectedEntity`
|
|
92
|
+
|
|
93
|
+
| Property | Type | Description |
|
|
94
|
+
|----------|------|-------------|
|
|
95
|
+
| `type` | `DetectionType` | The type of detected entity |
|
|
96
|
+
| `text` | `string` | The matched text substring |
|
|
97
|
+
| `start` | `number` | Start index in the original string |
|
|
98
|
+
| `end` | `number` | End index (exclusive) in the original string |
|
|
99
|
+
| `data` | `Record<string, string>` | Additional structured data (see below) |
|
|
100
|
+
|
|
101
|
+
### Entity Data by Type
|
|
102
|
+
|
|
103
|
+
| Type | Data fields |
|
|
104
|
+
|------|-------------|
|
|
105
|
+
| `phoneNumber` | `{ phoneNumber }` |
|
|
106
|
+
| `link` | `{ url }` |
|
|
107
|
+
| `email` | `{ email }` |
|
|
108
|
+
| `address` | `{ street, city, state, zip, country }` (iOS) / `{ address }` (Android) |
|
|
109
|
+
| `date` | `{ date }` ISO 8601 string |
|
|
110
|
+
|
|
111
|
+
## Platform Differences
|
|
112
|
+
|
|
113
|
+
| Feature | iOS | Android |
|
|
114
|
+
|---------|-----|---------|
|
|
115
|
+
| Engine | NSDataDetector | ML Kit Entity Extraction |
|
|
116
|
+
| Offline | Always | After `downloadModel()` or first `detect()` call |
|
|
117
|
+
| Model download | Not needed | ~5.6MB, on-device at runtime |
|
|
118
|
+
| Address parsing | Structured components | Raw string |
|
|
119
|
+
| Date output | ISO 8601 | ISO 8601 |
|
|
120
|
+
|
|
121
|
+
## Requirements
|
|
122
|
+
|
|
123
|
+
- iOS 15.1+
|
|
124
|
+
- Android API 24+ (minSdk)
|
|
125
|
+
- Expo SDK 50+ or bare React Native with `expo-modules-core`
|
|
126
|
+
|
|
127
|
+
## Contributing
|
|
128
|
+
|
|
129
|
+
Contributions are welcome! Please open an issue or submit a pull request.
|
|
130
|
+
|
|
131
|
+
## License
|
|
132
|
+
|
|
133
|
+
MIT
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
apply plugin: 'com.android.library'
|
|
2
|
+
apply plugin: 'org.jetbrains.kotlin.android'
|
|
3
|
+
|
|
4
|
+
group = 'expo.modules.datadetector'
|
|
5
|
+
version = '0.1.0'
|
|
6
|
+
|
|
7
|
+
android {
|
|
8
|
+
namespace 'expo.modules.datadetector'
|
|
9
|
+
compileSdkVersion safeExtGet("compileSdkVersion", 34)
|
|
10
|
+
|
|
11
|
+
defaultConfig {
|
|
12
|
+
minSdkVersion safeExtGet("minSdkVersion", 24)
|
|
13
|
+
targetSdkVersion safeExtGet("targetSdkVersion", 34)
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
lintOptions {
|
|
17
|
+
abortOnError false
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
dependencies {
|
|
22
|
+
implementation project(':expo-modules-core')
|
|
23
|
+
implementation "com.google.mlkit:entity-extraction:16.0.0-beta5"
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
def safeExtGet(prop, fallback) {
|
|
27
|
+
rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
|
|
28
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
package expo.modules.datadetector
|
|
2
|
+
|
|
3
|
+
import com.google.mlkit.nl.entityextraction.Entity
|
|
4
|
+
import com.google.mlkit.nl.entityextraction.EntityExtraction
|
|
5
|
+
import com.google.mlkit.nl.entityextraction.EntityExtractionParams
|
|
6
|
+
import com.google.mlkit.nl.entityextraction.EntityExtractorOptions
|
|
7
|
+
import expo.modules.kotlin.Promise
|
|
8
|
+
import expo.modules.kotlin.modules.Module
|
|
9
|
+
import expo.modules.kotlin.modules.ModuleDefinition
|
|
10
|
+
import java.text.SimpleDateFormat
|
|
11
|
+
import java.util.Date
|
|
12
|
+
import java.util.Locale
|
|
13
|
+
import java.util.TimeZone
|
|
14
|
+
|
|
15
|
+
class ReactNativeDataDetectorModule : Module() {
|
|
16
|
+
override fun definition() = ModuleDefinition {
|
|
17
|
+
Name("ReactNativeDataDetector")
|
|
18
|
+
|
|
19
|
+
AsyncFunction("downloadModel") { promise: Promise ->
|
|
20
|
+
val options = EntityExtractorOptions.Builder(EntityExtractorOptions.ENGLISH)
|
|
21
|
+
.build()
|
|
22
|
+
val extractor = EntityExtraction.getClient(options)
|
|
23
|
+
|
|
24
|
+
extractor.downloadModelIfNeeded()
|
|
25
|
+
.addOnSuccessListener {
|
|
26
|
+
promise.resolve(true)
|
|
27
|
+
extractor.close()
|
|
28
|
+
}
|
|
29
|
+
.addOnFailureListener { e ->
|
|
30
|
+
promise.reject("MODEL_DOWNLOAD_ERROR", e.message ?: "Failed to download ML Kit model", e)
|
|
31
|
+
extractor.close()
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
AsyncFunction("detect") { text: String, types: List<String>, promise: Promise ->
|
|
36
|
+
val options = EntityExtractorOptions.Builder(EntityExtractorOptions.ENGLISH)
|
|
37
|
+
.build()
|
|
38
|
+
val extractor = EntityExtraction.getClient(options)
|
|
39
|
+
|
|
40
|
+
extractor.downloadModelIfNeeded()
|
|
41
|
+
.addOnSuccessListener {
|
|
42
|
+
val params = EntityExtractionParams.Builder(text).build()
|
|
43
|
+
|
|
44
|
+
extractor.annotate(params)
|
|
45
|
+
.addOnSuccessListener { annotations ->
|
|
46
|
+
val results = mutableListOf<Map<String, Any>>()
|
|
47
|
+
|
|
48
|
+
for (annotation in annotations) {
|
|
49
|
+
for (entity in annotation.entities) {
|
|
50
|
+
val type = mapEntityType(entity) ?: continue
|
|
51
|
+
if (!types.contains(type)) continue
|
|
52
|
+
|
|
53
|
+
val data = mutableMapOf<String, String>()
|
|
54
|
+
populateEntityData(entity, type, data, annotation.annotatedText)
|
|
55
|
+
|
|
56
|
+
results.add(
|
|
57
|
+
mapOf(
|
|
58
|
+
"type" to type,
|
|
59
|
+
"text" to annotation.annotatedText,
|
|
60
|
+
"start" to annotation.start,
|
|
61
|
+
"end" to annotation.end,
|
|
62
|
+
"data" to data
|
|
63
|
+
)
|
|
64
|
+
)
|
|
65
|
+
break // One result per annotation
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
promise.resolve(results)
|
|
70
|
+
extractor.close()
|
|
71
|
+
}
|
|
72
|
+
.addOnFailureListener { e ->
|
|
73
|
+
promise.reject("DETECTION_ERROR", e.message ?: "Entity extraction failed", e)
|
|
74
|
+
extractor.close()
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
.addOnFailureListener { e ->
|
|
78
|
+
promise.reject("MODEL_DOWNLOAD_ERROR", e.message ?: "Failed to download ML Kit model", e)
|
|
79
|
+
extractor.close()
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
private fun mapEntityType(entity: Entity): String? {
|
|
85
|
+
return when (entity.type) {
|
|
86
|
+
Entity.TYPE_PHONE -> "phoneNumber"
|
|
87
|
+
Entity.TYPE_URL -> "link"
|
|
88
|
+
Entity.TYPE_EMAIL -> "email"
|
|
89
|
+
Entity.TYPE_ADDRESS -> "address"
|
|
90
|
+
Entity.TYPE_DATE_TIME -> "date"
|
|
91
|
+
else -> null
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
private fun populateEntityData(
|
|
96
|
+
entity: Entity,
|
|
97
|
+
type: String,
|
|
98
|
+
data: MutableMap<String, String>,
|
|
99
|
+
annotatedText: String
|
|
100
|
+
) {
|
|
101
|
+
when (type) {
|
|
102
|
+
"phoneNumber" -> data["phoneNumber"] = annotatedText
|
|
103
|
+
"link" -> data["url"] = annotatedText
|
|
104
|
+
"email" -> data["email"] = annotatedText
|
|
105
|
+
"address" -> data["address"] = annotatedText
|
|
106
|
+
"date" -> {
|
|
107
|
+
entity.asDateTimeEntity()?.let { dateTime ->
|
|
108
|
+
val formatter = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US)
|
|
109
|
+
formatter.timeZone = TimeZone.getTimeZone("UTC")
|
|
110
|
+
data["date"] = formatter.format(Date(dateTime.timestampMillis))
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { DetectedEntity, DetectOptions } from './ReactNativeDataDetector.types';
|
|
2
|
+
/**
|
|
3
|
+
* Pre-downloads the ML Kit entity extraction model on Android.
|
|
4
|
+
* No-op on iOS (NSDataDetector requires no model download).
|
|
5
|
+
* Call this at app startup to ensure `detect()` works offline later.
|
|
6
|
+
*/
|
|
7
|
+
export declare function downloadModel(): Promise<boolean>;
|
|
8
|
+
/**
|
|
9
|
+
* Detects entities (phone numbers, URLs, emails, addresses, dates) in the given text
|
|
10
|
+
* using native platform APIs (NSDataDetector on iOS, ML Kit on Android).
|
|
11
|
+
*/
|
|
12
|
+
export declare function detect(text: string, options?: DetectOptions): Promise<DetectedEntity[]>;
|
|
13
|
+
//# sourceMappingURL=ReactNativeDataDetector.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ReactNativeDataDetector.d.ts","sourceRoot":"","sources":["../src/ReactNativeDataDetector.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,iCAAiC,CAAC;AAIrF;;;;GAIG;AACH,wBAAsB,aAAa,IAAI,OAAO,CAAC,OAAO,CAAC,CAEtD;AAED;;;GAGG;AACH,wBAAsB,MAAM,CAC1B,IAAI,EAAE,MAAM,EACZ,OAAO,CAAC,EAAE,aAAa,GACtB,OAAO,CAAC,cAAc,EAAE,CAAC,CAG3B"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { requireNativeModule } from 'expo-modules-core';
|
|
2
|
+
const NativeModule = requireNativeModule('ReactNativeDataDetector');
|
|
3
|
+
/**
|
|
4
|
+
* Pre-downloads the ML Kit entity extraction model on Android.
|
|
5
|
+
* No-op on iOS (NSDataDetector requires no model download).
|
|
6
|
+
* Call this at app startup to ensure `detect()` works offline later.
|
|
7
|
+
*/
|
|
8
|
+
export async function downloadModel() {
|
|
9
|
+
return NativeModule.downloadModel();
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Detects entities (phone numbers, URLs, emails, addresses, dates) in the given text
|
|
13
|
+
* using native platform APIs (NSDataDetector on iOS, ML Kit on Android).
|
|
14
|
+
*/
|
|
15
|
+
export async function detect(text, options) {
|
|
16
|
+
const types = options?.types ?? ['phoneNumber', 'link', 'email', 'address', 'date'];
|
|
17
|
+
return NativeModule.detect(text, types);
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=ReactNativeDataDetector.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ReactNativeDataDetector.js","sourceRoot":"","sources":["../src/ReactNativeDataDetector.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,mBAAmB,CAAC;AAIxD,MAAM,YAAY,GAAG,mBAAmB,CAAC,yBAAyB,CAAC,CAAC;AAEpE;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,aAAa;IACjC,OAAO,YAAY,CAAC,aAAa,EAAE,CAAC;AACtC,CAAC;AAED;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,MAAM,CAC1B,IAAY,EACZ,OAAuB;IAEvB,MAAM,KAAK,GAAG,OAAO,EAAE,KAAK,IAAI,CAAC,aAAa,EAAE,MAAM,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,CAAC,CAAC;IACpF,OAAO,YAAY,CAAC,MAAM,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;AAC1C,CAAC","sourcesContent":["import { requireNativeModule } from 'expo-modules-core';\n\nimport type { DetectedEntity, DetectOptions } from './ReactNativeDataDetector.types';\n\nconst NativeModule = requireNativeModule('ReactNativeDataDetector');\n\n/**\n * Pre-downloads the ML Kit entity extraction model on Android.\n * No-op on iOS (NSDataDetector requires no model download).\n * Call this at app startup to ensure `detect()` works offline later.\n */\nexport async function downloadModel(): Promise<boolean> {\n return NativeModule.downloadModel();\n}\n\n/**\n * Detects entities (phone numbers, URLs, emails, addresses, dates) in the given text\n * using native platform APIs (NSDataDetector on iOS, ML Kit on Android).\n */\nexport async function detect(\n text: string,\n options?: DetectOptions\n): Promise<DetectedEntity[]> {\n const types = options?.types ?? ['phoneNumber', 'link', 'email', 'address', 'date'];\n return NativeModule.detect(text, types);\n}\n"]}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The type of entity detected in the text.
|
|
3
|
+
*/
|
|
4
|
+
export type DetectionType = 'phoneNumber' | 'link' | 'email' | 'address' | 'date';
|
|
5
|
+
/**
|
|
6
|
+
* A single detected entity within the text.
|
|
7
|
+
*/
|
|
8
|
+
export interface DetectedEntity {
|
|
9
|
+
/** The type of detected entity. */
|
|
10
|
+
type: DetectionType;
|
|
11
|
+
/** The matched text substring. */
|
|
12
|
+
text: string;
|
|
13
|
+
/** Start index of the match in the original string. */
|
|
14
|
+
start: number;
|
|
15
|
+
/** End index (exclusive) of the match in the original string. */
|
|
16
|
+
end: number;
|
|
17
|
+
/** Additional data depending on the type (e.g., URL string, phone number, date ISO string). */
|
|
18
|
+
data?: Record<string, string>;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Options for the detect function.
|
|
22
|
+
*/
|
|
23
|
+
export interface DetectOptions {
|
|
24
|
+
/** Which entity types to detect. Defaults to all types. */
|
|
25
|
+
types?: DetectionType[];
|
|
26
|
+
}
|
|
27
|
+
//# sourceMappingURL=ReactNativeDataDetector.types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ReactNativeDataDetector.types.d.ts","sourceRoot":"","sources":["../src/ReactNativeDataDetector.types.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,MAAM,aAAa,GACrB,aAAa,GACb,MAAM,GACN,OAAO,GACP,SAAS,GACT,MAAM,CAAC;AAEX;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B,mCAAmC;IACnC,IAAI,EAAE,aAAa,CAAC;IACpB,kCAAkC;IAClC,IAAI,EAAE,MAAM,CAAC;IACb,uDAAuD;IACvD,KAAK,EAAE,MAAM,CAAC;IACd,iEAAiE;IACjE,GAAG,EAAE,MAAM,CAAC;IACZ,+FAA+F;IAC/F,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAC/B;AAED;;GAEG;AACH,MAAM,WAAW,aAAa;IAC5B,2DAA2D;IAC3D,KAAK,CAAC,EAAE,aAAa,EAAE,CAAC;CACzB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ReactNativeDataDetector.types.js","sourceRoot":"","sources":["../src/ReactNativeDataDetector.types.ts"],"names":[],"mappings":"","sourcesContent":["/**\n * The type of entity detected in the text.\n */\nexport type DetectionType =\n | 'phoneNumber'\n | 'link'\n | 'email'\n | 'address'\n | 'date';\n\n/**\n * A single detected entity within the text.\n */\nexport interface DetectedEntity {\n /** The type of detected entity. */\n type: DetectionType;\n /** The matched text substring. */\n text: string;\n /** Start index of the match in the original string. */\n start: number;\n /** End index (exclusive) of the match in the original string. */\n end: number;\n /** Additional data depending on the type (e.g., URL string, phone number, date ISO string). */\n data?: Record<string, string>;\n}\n\n/**\n * Options for the detect function.\n */\nexport interface DetectOptions {\n /** Which entity types to detect. Defaults to all types. */\n types?: DetectionType[];\n}\n"]}
|
package/build/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAC;AAClE,YAAY,EAAE,cAAc,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,iCAAiC,CAAC"}
|
package/build/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAC","sourcesContent":["export { detect, downloadModel } from './ReactNativeDataDetector';\nexport type { DetectedEntity, DetectionType, DetectOptions } from './ReactNativeDataDetector.types';\n"]}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
require 'json'
|
|
2
|
+
|
|
3
|
+
package = JSON.parse(File.read(File.join(__dir__, '..', 'package.json')))
|
|
4
|
+
|
|
5
|
+
Pod::Spec.new do |s|
|
|
6
|
+
s.name = 'ReactNativeDataDetector'
|
|
7
|
+
s.version = package['version']
|
|
8
|
+
s.summary = package['description']
|
|
9
|
+
s.description = package['description']
|
|
10
|
+
s.license = package['license']
|
|
11
|
+
s.author = package['author']
|
|
12
|
+
s.homepage = package['homepage']
|
|
13
|
+
s.platforms = { :ios => '15.1' }
|
|
14
|
+
s.swift_version = '5.9'
|
|
15
|
+
s.source = { git: "#{package['repository']['url']}" }
|
|
16
|
+
s.static_framework = true
|
|
17
|
+
|
|
18
|
+
s.dependency 'ExpoModulesCore'
|
|
19
|
+
|
|
20
|
+
s.source_files = '**/*.swift'
|
|
21
|
+
end
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import ExpoModulesCore
|
|
2
|
+
import Foundation
|
|
3
|
+
|
|
4
|
+
public class ReactNativeDataDetectorModule: Module {
|
|
5
|
+
public func definition() -> ModuleDefinition {
|
|
6
|
+
Name("ReactNativeDataDetector")
|
|
7
|
+
|
|
8
|
+
// No-op on iOS — NSDataDetector requires no model download
|
|
9
|
+
AsyncFunction("downloadModel") { () -> Bool in
|
|
10
|
+
return true
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
AsyncFunction("detect") { (text: String, types: [String]) -> [[String: Any]] in
|
|
14
|
+
var checkingTypes: NSTextCheckingResult.CheckingType = []
|
|
15
|
+
|
|
16
|
+
for type in types {
|
|
17
|
+
switch type {
|
|
18
|
+
case "phoneNumber":
|
|
19
|
+
checkingTypes.insert(.phoneNumber)
|
|
20
|
+
case "link", "email":
|
|
21
|
+
checkingTypes.insert(.link)
|
|
22
|
+
case "address":
|
|
23
|
+
checkingTypes.insert(.address)
|
|
24
|
+
case "date":
|
|
25
|
+
checkingTypes.insert(.date)
|
|
26
|
+
default:
|
|
27
|
+
break
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
guard let detector = try? NSDataDetector(types: checkingTypes.rawValue) else {
|
|
32
|
+
return []
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
let range = NSRange(text.startIndex..., in: text)
|
|
36
|
+
let matches = detector.matches(in: text, options: [], range: range)
|
|
37
|
+
|
|
38
|
+
var results: [[String: Any]] = []
|
|
39
|
+
|
|
40
|
+
for match in matches {
|
|
41
|
+
guard let matchRange = Range(match.range, in: text) else { continue }
|
|
42
|
+
let matchedText = String(text[matchRange])
|
|
43
|
+
|
|
44
|
+
var type: String
|
|
45
|
+
var data: [String: String] = [:]
|
|
46
|
+
|
|
47
|
+
switch match.resultType {
|
|
48
|
+
case .phoneNumber:
|
|
49
|
+
guard types.contains("phoneNumber") else { continue }
|
|
50
|
+
type = "phoneNumber"
|
|
51
|
+
if let phone = match.phoneNumber {
|
|
52
|
+
data["phoneNumber"] = phone
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
case .link:
|
|
56
|
+
guard let url = match.url else { continue }
|
|
57
|
+
if url.scheme == "mailto" {
|
|
58
|
+
guard types.contains("email") else { continue }
|
|
59
|
+
type = "email"
|
|
60
|
+
data["email"] = url.absoluteString.replacingOccurrences(of: "mailto:", with: "")
|
|
61
|
+
} else {
|
|
62
|
+
guard types.contains("link") else { continue }
|
|
63
|
+
type = "link"
|
|
64
|
+
data["url"] = url.absoluteString
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
case .address:
|
|
68
|
+
guard types.contains("address") else { continue }
|
|
69
|
+
type = "address"
|
|
70
|
+
if let components = match.addressComponents {
|
|
71
|
+
if let street = components[.street] { data["street"] = street }
|
|
72
|
+
if let city = components[.city] { data["city"] = city }
|
|
73
|
+
if let state = components[.state] { data["state"] = state }
|
|
74
|
+
if let zip = components[.zip] { data["zip"] = zip }
|
|
75
|
+
if let country = components[.country] { data["country"] = country }
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
case .date:
|
|
79
|
+
guard types.contains("date") else { continue }
|
|
80
|
+
type = "date"
|
|
81
|
+
if let date = match.date {
|
|
82
|
+
let formatter = ISO8601DateFormatter()
|
|
83
|
+
data["date"] = formatter.string(from: date)
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
default:
|
|
87
|
+
continue
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
let start = text.distance(from: text.startIndex, to: matchRange.lowerBound)
|
|
91
|
+
let end = text.distance(from: text.startIndex, to: matchRange.upperBound)
|
|
92
|
+
|
|
93
|
+
results.append([
|
|
94
|
+
"type": type,
|
|
95
|
+
"text": matchedText,
|
|
96
|
+
"start": start,
|
|
97
|
+
"end": end,
|
|
98
|
+
"data": data,
|
|
99
|
+
])
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return results
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@themobilefirstcompany/react-native-data-detector",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Cross-platform text data detection for React Native. Uses NSDataDetector on iOS and ML Kit Entity Extraction on Android to detect phone numbers, URLs, emails, dates, and addresses.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"react-native",
|
|
7
|
+
"expo",
|
|
8
|
+
"data-detector",
|
|
9
|
+
"nsdatadetector",
|
|
10
|
+
"mlkit",
|
|
11
|
+
"entity-extraction",
|
|
12
|
+
"phone-number",
|
|
13
|
+
"url",
|
|
14
|
+
"email",
|
|
15
|
+
"address",
|
|
16
|
+
"date"
|
|
17
|
+
],
|
|
18
|
+
"homepage": "https://github.com/pablogdcr/react-native-data-detector#readme",
|
|
19
|
+
"bugs": {
|
|
20
|
+
"url": "https://github.com/pablogdcr/react-native-data-detector/issues"
|
|
21
|
+
},
|
|
22
|
+
"repository": {
|
|
23
|
+
"type": "git",
|
|
24
|
+
"url": "git+https://github.com/pablogdcr/react-native-data-detector.git"
|
|
25
|
+
},
|
|
26
|
+
"license": "MIT",
|
|
27
|
+
"author": "pablogdcr",
|
|
28
|
+
"type": "commonjs",
|
|
29
|
+
"main": "build/index.js",
|
|
30
|
+
"types": "build/index.d.ts",
|
|
31
|
+
"scripts": {
|
|
32
|
+
"build": "expo-module build",
|
|
33
|
+
"clean": "expo-module clean",
|
|
34
|
+
"lint": "expo-module lint",
|
|
35
|
+
"test": "expo-module test",
|
|
36
|
+
"prepare": "expo-module prepare",
|
|
37
|
+
"prepublishOnly": "expo-module prepublishOnly",
|
|
38
|
+
"expo-module": "expo-module"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"expo-module-scripts": "^4.0.0",
|
|
42
|
+
"expo-modules-core": "^2.0.0",
|
|
43
|
+
"typescript": "^5.3.0"
|
|
44
|
+
},
|
|
45
|
+
"peerDependencies": {
|
|
46
|
+
"expo": "*",
|
|
47
|
+
"react": "*",
|
|
48
|
+
"react-native": "*"
|
|
49
|
+
}
|
|
50
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { requireNativeModule } from 'expo-modules-core';
|
|
2
|
+
|
|
3
|
+
import type { DetectedEntity, DetectOptions } from './ReactNativeDataDetector.types';
|
|
4
|
+
|
|
5
|
+
const NativeModule = requireNativeModule('ReactNativeDataDetector');
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Pre-downloads the ML Kit entity extraction model on Android.
|
|
9
|
+
* No-op on iOS (NSDataDetector requires no model download).
|
|
10
|
+
* Call this at app startup to ensure `detect()` works offline later.
|
|
11
|
+
*/
|
|
12
|
+
export async function downloadModel(): Promise<boolean> {
|
|
13
|
+
return NativeModule.downloadModel();
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Detects entities (phone numbers, URLs, emails, addresses, dates) in the given text
|
|
18
|
+
* using native platform APIs (NSDataDetector on iOS, ML Kit on Android).
|
|
19
|
+
*/
|
|
20
|
+
export async function detect(
|
|
21
|
+
text: string,
|
|
22
|
+
options?: DetectOptions
|
|
23
|
+
): Promise<DetectedEntity[]> {
|
|
24
|
+
const types = options?.types ?? ['phoneNumber', 'link', 'email', 'address', 'date'];
|
|
25
|
+
return NativeModule.detect(text, types);
|
|
26
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The type of entity detected in the text.
|
|
3
|
+
*/
|
|
4
|
+
export type DetectionType =
|
|
5
|
+
| 'phoneNumber'
|
|
6
|
+
| 'link'
|
|
7
|
+
| 'email'
|
|
8
|
+
| 'address'
|
|
9
|
+
| 'date';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* A single detected entity within the text.
|
|
13
|
+
*/
|
|
14
|
+
export interface DetectedEntity {
|
|
15
|
+
/** The type of detected entity. */
|
|
16
|
+
type: DetectionType;
|
|
17
|
+
/** The matched text substring. */
|
|
18
|
+
text: string;
|
|
19
|
+
/** Start index of the match in the original string. */
|
|
20
|
+
start: number;
|
|
21
|
+
/** End index (exclusive) of the match in the original string. */
|
|
22
|
+
end: number;
|
|
23
|
+
/** Additional data depending on the type (e.g., URL string, phone number, date ISO string). */
|
|
24
|
+
data?: Record<string, string>;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Options for the detect function.
|
|
29
|
+
*/
|
|
30
|
+
export interface DetectOptions {
|
|
31
|
+
/** Which entity types to detect. Defaults to all types. */
|
|
32
|
+
types?: DetectionType[];
|
|
33
|
+
}
|
package/src/index.ts
ADDED