├── .gitattributes ├── .gitignore ├── .npmignore ├── README.md ├── android ├── README.md ├── build.gradle └── src │ └── main │ ├── AndroidManifest.xml │ └── java │ └── com │ └── react-native │ └── create-guid │ ├── CreateGuidModule.java │ └── CreateGuidPackage.java ├── example ├── .buckconfig ├── .eslintrc.js ├── .flowconfig ├── .gitattributes ├── .gitignore ├── .prettierrc.js ├── .watchmanconfig ├── App.js ├── __tests__ │ └── App-test.js ├── android │ ├── app │ │ ├── BUCK │ │ ├── build.gradle │ │ ├── build_defs.bzl │ │ ├── debug.keystore │ │ ├── proguard-rules.pro │ │ └── src │ │ │ ├── debug │ │ │ └── AndroidManifest.xml │ │ │ └── main │ │ │ ├── AndroidManifest.xml │ │ │ ├── java │ │ │ └── com │ │ │ │ └── example │ │ │ │ ├── MainActivity.java │ │ │ │ └── MainApplication.java │ │ │ └── res │ │ │ ├── mipmap-hdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-mdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xhdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xxhdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xxxhdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ │ └── values │ │ │ ├── strings.xml │ │ │ └── styles.xml │ ├── build.gradle │ ├── gradle.properties │ ├── gradle │ │ └── wrapper │ │ │ ├── gradle-wrapper.jar │ │ │ └── gradle-wrapper.properties │ ├── gradlew │ ├── gradlew.bat │ └── settings.gradle ├── app.json ├── babel.config.js ├── index.js ├── ios │ ├── Podfile │ ├── Podfile.lock │ ├── example-tvOS │ │ └── Info.plist │ ├── example-tvOSTests │ │ └── Info.plist │ ├── example.xcodeproj │ │ ├── project.pbxproj │ │ └── xcshareddata │ │ │ └── xcschemes │ │ │ ├── example-tvOS.xcscheme │ │ │ └── example.xcscheme │ ├── example.xcworkspace │ │ └── contents.xcworkspacedata │ ├── example │ │ ├── AppDelegate.h │ │ ├── AppDelegate.m │ │ ├── Base.lproj │ │ │ └── LaunchScreen.xib │ │ ├── Images.xcassets │ │ │ ├── AppIcon.appiconset │ │ │ │ └── Contents.json │ │ │ └── Contents.json │ │ ├── Info.plist │ │ └── main.m │ └── exampleTests │ │ ├── Info.plist │ │ └── exampleTests.m ├── metro.config.js ├── package-lock.json ├── package.json └── yarn.lock ├── index.d.ts ├── index.js ├── ios ├── CreateGuid.h ├── CreateGuid.m ├── CreateGuid.xcodeproj │ └── project.pbxproj └── CreateGuid.xcworkspace │ └── contents.xcworkspacedata ├── package.json └── react-native-create-guid.podspec /.gitattributes: -------------------------------------------------------------------------------- 1 | *.pbxproj -text 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # OSX 2 | # 3 | .DS_Store 4 | 5 | # node.js 6 | # 7 | node_modules/ 8 | npm-debug.log 9 | yarn-error.log 10 | 11 | # Xcode 12 | # 13 | build/ 14 | *.pbxuser 15 | !default.pbxuser 16 | *.mode1v3 17 | !default.mode1v3 18 | *.mode2v3 19 | !default.mode2v3 20 | *.perspectivev3 21 | !default.perspectivev3 22 | xcuserdata 23 | *.xccheckout 24 | *.moved-aside 25 | DerivedData 26 | *.hmap 27 | *.ipa 28 | *.xcuserstate 29 | project.xcworkspace 30 | 31 | # Android/IntelliJ 32 | # 33 | build/ 34 | .idea 35 | .gradle 36 | local.properties 37 | *.iml 38 | 39 | # BUCK 40 | buck-out/ 41 | \.buckd/ 42 | *.keystore 43 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fedeojeda95/react-native-create-guid/c0f6cb844d18e337e39e230c3ba027838c26ac44/.npmignore -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # react-native-create-guid 2 | 3 | Minimal library to create Guids in React Native. It uses the native implementations to generate one and send it over to JS. 4 | 5 | ## Why? 6 | 7 | When faced with the problem of generating a GUID in React Native, I found that there were no current good solutions. Relying on `Math.random` didn't seem like a good idea, and the other alternatives found like [react-native-uuid](https://www.npmjs.com/package/react-native-uuid) that are just ports of node.js libraries (needed to install other libraries buffer). 8 | 9 | Since native platforms already have ways to create GUIDs that are useful, creating a simple native library would be enough to use them in React-Native. 10 | 11 | ## Getting started 12 | 13 | `$ npm install react-native-create-guid --save` 14 | 15 | ### Mostly automatic installation 16 | 17 | **RN < 0.60:** 18 | 19 | `$ react-native link react-native-create-guid` 20 | 21 | **RN >= 0.60:** 22 | 23 | `$ cd ios && pod install` 24 | 25 | ### Manual installation 26 | 27 | #### iOS 28 | 29 | 1. In XCode, in the project navigator, right click `Libraries` ➜ `Add Files to [your project's name]` 30 | 2. Go to `node_modules` ➜ `react-native-create-guid` and add `CreateGuid.xcodeproj` 31 | 3. In XCode, in the project navigator, select your project. Add `libCreateGuid.a` to your project's `Build Phases` ➜ `Link Binary With Libraries` 32 | 4. Run your project (`Cmd+R`)< 33 | 34 | #### Android 35 | 36 | 1. Open up `android/app/src/main/java/[...]/MainApplication.java` 37 | 38 | - Add `import com.reactnative.createguid.CreateGuidPackage;` to the imports at the top of the file 39 | - Add `new CreateGuidPackage()` to the list returned by the `getPackages()` method 40 | 41 | 2. Append the following lines to `android/settings.gradle`: 42 | ``` 43 | include ':react-native-create-guid' 44 | project(':react-native-create-guid').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-create-guid/android') 45 | ``` 46 | 3. Insert the following lines inside the dependencies block in `android/app/build.gradle`: 47 | ``` 48 | compile project(':react-native-create-guid') 49 | ``` 50 | 51 | ## Usage 52 | 53 | Check the example under `example` folder for it's usage. 54 | 55 | ```javascript 56 | import createGuid from "react-native-create-guid"; 57 | 58 | // in an async function; 59 | const guid = await createGuid(); 60 | 61 | // using promises 62 | createGuid().then((guid) => console.log(guid)); 63 | ``` 64 | -------------------------------------------------------------------------------- /android/README.md: -------------------------------------------------------------------------------- 1 | README 2 | ====== 3 | 4 | If you want to publish the lib as a maven dependency, follow these steps before publishing a new version to npm: 5 | 6 | 1. Be sure to have the Android [SDK](https://developer.android.com/studio/index.html) and [NDK](https://developer.android.com/ndk/guides/index.html) installed 7 | 2. Be sure to have a `local.properties` file in this folder that points to the Android SDK and NDK 8 | ``` 9 | ndk.dir=/Users/{username}/Library/Android/sdk/ndk-bundle 10 | sdk.dir=/Users/{username}/Library/Android/sdk 11 | ``` 12 | 3. Delete the `maven` folder 13 | 4. Run `./gradlew installArchives` 14 | 5. Verify that latest set of generated files is in the maven folder with the correct version number 15 | -------------------------------------------------------------------------------- /android/build.gradle: -------------------------------------------------------------------------------- 1 | // android/build.gradle 2 | 3 | def safeExtGet(prop, fallback) { 4 | rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback 5 | } 6 | 7 | buildscript { 8 | // The Android Gradle plugin is only required when opening the android folder stand-alone. 9 | // This avoids unnecessary downloads and potential conflicts when the library is included as a 10 | // module dependency in an application project. 11 | if (project == rootProject) { 12 | repositories { 13 | google() 14 | jcenter() 15 | } 16 | dependencies { 17 | classpath 'com.android.tools.build:gradle:3.4.1' 18 | } 19 | } 20 | } 21 | 22 | apply plugin: 'com.android.library' 23 | apply plugin: 'maven' 24 | 25 | // Matches values in recent template from React Native 0.59 / 0.60 26 | // https://github.com/facebook/react-native/blob/0.59-stable/template/android/build.gradle#L5-L9 27 | // https://github.com/facebook/react-native/blob/0.60-stable/template/android/build.gradle#L5-L9 28 | def DEFAULT_COMPILE_SDK_VERSION = 28 29 | def DEFAULT_BUILD_TOOLS_VERSION = "28.0.3" 30 | def DEFAULT_MIN_SDK_VERSION = 16 31 | def DEFAULT_TARGET_SDK_VERSION = 28 32 | 33 | android { 34 | compileSdkVersion safeExtGet('compileSdkVersion', DEFAULT_COMPILE_SDK_VERSION) 35 | buildToolsVersion safeExtGet('buildToolsVersion', DEFAULT_BUILD_TOOLS_VERSION) 36 | defaultConfig { 37 | minSdkVersion safeExtGet('minSdkVersion', DEFAULT_MIN_SDK_VERSION) 38 | targetSdkVersion safeExtGet('targetSdkVersion', DEFAULT_TARGET_SDK_VERSION) 39 | versionCode 1 40 | versionName "1.0" 41 | } 42 | lintOptions { 43 | abortOnError false 44 | } 45 | } 46 | 47 | repositories { 48 | mavenLocal() 49 | maven { 50 | // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm 51 | url "$rootDir/../node_modules/react-native/android" 52 | } 53 | maven { 54 | // Android JSC is installed from npm 55 | url "$rootDir/../node_modules/jsc-android/dist" 56 | } 57 | google() 58 | jcenter() 59 | } 60 | 61 | dependencies { 62 | // ref: 63 | // https://github.com/facebook/react-native/blob/0.61-stable/template/android/app/build.gradle#L192 64 | //noinspection GradleDynamicVersion 65 | implementation 'com.facebook.react:react-native:+' // From node_modules 66 | } 67 | 68 | def configureReactNativePom(def pom) { 69 | def packageJson = new groovy.json.JsonSlurper().parseText(file('../package.json').text) 70 | 71 | pom.project { 72 | name packageJson.title 73 | artifactId packageJson.name 74 | version = packageJson.version 75 | group = "com.reactnative.createguid" 76 | description packageJson.description 77 | url packageJson.repository.baseUrl 78 | 79 | licenses { 80 | license { 81 | name packageJson.license 82 | url packageJson.repository.baseUrl + '/blob/master/' + packageJson.licenseFilename 83 | distribution 'repo' 84 | } 85 | } 86 | 87 | developers { 88 | developer { 89 | id packageJson.author.username 90 | name packageJson.author.name 91 | } 92 | } 93 | } 94 | } 95 | 96 | afterEvaluate { project -> 97 | // some Gradle build hooks ref: 98 | // https://www.oreilly.com/library/view/gradle-beyond-the/9781449373801/ch03.html 99 | task androidJavadoc(type: Javadoc) { 100 | source = android.sourceSets.main.java.srcDirs 101 | classpath += files(android.bootClasspath) 102 | classpath += files(project.getConfigurations().getByName('compile').asList()) 103 | include '**/*.java' 104 | } 105 | 106 | task androidJavadocJar(type: Jar, dependsOn: androidJavadoc) { 107 | classifier = 'javadoc' 108 | from androidJavadoc.destinationDir 109 | } 110 | 111 | task androidSourcesJar(type: Jar) { 112 | classifier = 'sources' 113 | from android.sourceSets.main.java.srcDirs 114 | include '**/*.java' 115 | } 116 | 117 | android.libraryVariants.all { variant -> 118 | def name = variant.name.capitalize() 119 | task "jar${name}"(type: Jar, dependsOn: variant.javaCompile) { 120 | from variant.javaCompile.destinationDir 121 | } 122 | } 123 | 124 | artifacts { 125 | archives androidSourcesJar 126 | archives androidJavadocJar 127 | } 128 | 129 | task installArchives(type: Upload) { 130 | configuration = configurations.archives 131 | repositories.mavenDeployer { 132 | // Deploy to react-native-event-bridge/maven, ready to publish to npm 133 | repository url: "file://${projectDir}/../android/maven" 134 | configureReactNativePom pom 135 | } 136 | } 137 | } 138 | -------------------------------------------------------------------------------- /android/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /android/src/main/java/com/react-native/create-guid/CreateGuidModule.java: -------------------------------------------------------------------------------- 1 | package com.reactnative.createguid; 2 | 3 | import java.util.UUID; 4 | 5 | import com.facebook.react.bridge.Promise; 6 | import com.facebook.react.bridge.ReactApplicationContext; 7 | import com.facebook.react.bridge.ReactContextBaseJavaModule; 8 | import com.facebook.react.bridge.ReactMethod; 9 | import com.facebook.react.bridge.Callback; 10 | 11 | public class CreateGuidModule extends ReactContextBaseJavaModule { 12 | 13 | private final ReactApplicationContext reactContext; 14 | 15 | public CreateGuidModule(ReactApplicationContext reactContext) { 16 | super(reactContext); 17 | this.reactContext = reactContext; 18 | } 19 | 20 | @Override 21 | public String getName() { 22 | return "CreateGuid"; 23 | } 24 | 25 | @ReactMethod 26 | public void createGuid(Promise promise) { 27 | UUID uuid = UUID.randomUUID(); 28 | String uuidInString = uuid.toString(); 29 | promise.resolve(uuidInString); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /android/src/main/java/com/react-native/create-guid/CreateGuidPackage.java: -------------------------------------------------------------------------------- 1 | package com.reactnative.createguid; 2 | 3 | import java.util.Arrays; 4 | import java.util.Collections; 5 | import java.util.List; 6 | 7 | import com.facebook.react.ReactPackage; 8 | import com.facebook.react.bridge.NativeModule; 9 | import com.facebook.react.bridge.ReactApplicationContext; 10 | import com.facebook.react.uimanager.ViewManager; 11 | import com.facebook.react.bridge.JavaScriptModule; 12 | 13 | public class CreateGuidPackage implements ReactPackage { 14 | @Override 15 | public List createNativeModules(ReactApplicationContext reactContext) { 16 | return Arrays.asList(new CreateGuidModule(reactContext)); 17 | } 18 | 19 | @Override 20 | public List createViewManagers(ReactApplicationContext reactContext) { 21 | return Collections.emptyList(); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /example/.buckconfig: -------------------------------------------------------------------------------- 1 | 2 | [android] 3 | target = Google Inc.:Google APIs:23 4 | 5 | [maven_repositories] 6 | central = https://repo1.maven.org/maven2 7 | -------------------------------------------------------------------------------- /example/.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | root: true, 3 | extends: '@react-native-community', 4 | }; 5 | -------------------------------------------------------------------------------- /example/.flowconfig: -------------------------------------------------------------------------------- 1 | [ignore] 2 | ; We fork some components by platform 3 | .*/*[.]android.js 4 | 5 | ; Ignore "BUCK" generated dirs 6 | /\.buckd/ 7 | 8 | ; Ignore polyfills 9 | node_modules/react-native/Libraries/polyfills/.* 10 | 11 | ; These should not be required directly 12 | ; require from fbjs/lib instead: require('fbjs/lib/warning') 13 | node_modules/warning/.* 14 | 15 | ; Flow doesn't support platforms 16 | .*/Libraries/Utilities/LoadingView.js 17 | 18 | [untyped] 19 | .*/node_modules/@react-native-community/cli/.*/.* 20 | 21 | [include] 22 | 23 | [libs] 24 | node_modules/react-native/Libraries/react-native/react-native-interface.js 25 | node_modules/react-native/flow/ 26 | 27 | [options] 28 | emoji=true 29 | 30 | esproposal.optional_chaining=enable 31 | esproposal.nullish_coalescing=enable 32 | 33 | module.file_ext=.js 34 | module.file_ext=.json 35 | module.file_ext=.ios.js 36 | 37 | munge_underscores=true 38 | 39 | module.name_mapper='^react-native$' -> '/node_modules/react-native/Libraries/react-native/react-native-implementation' 40 | module.name_mapper='^react-native/\(.*\)$' -> '/node_modules/react-native/\1' 41 | module.name_mapper='^[./a-zA-Z0-9$_-]+\.\(bmp\|gif\|jpg\|jpeg\|png\|psd\|svg\|webp\|m4v\|mov\|mp4\|mpeg\|mpg\|webm\|aac\|aiff\|caf\|m4a\|mp3\|wav\|html\|pdf\)$' -> '/node_modules/react-native/Libraries/Image/RelativeImageStub' 42 | 43 | suppress_type=$FlowIssue 44 | suppress_type=$FlowFixMe 45 | suppress_type=$FlowFixMeProps 46 | suppress_type=$FlowFixMeState 47 | 48 | suppress_comment=\\(.\\|\n\\)*\\$FlowFixMe\\($\\|[^(]\\|(\\(\\)? *\\(site=[a-z,_]*react_native\\(_ios\\)?_\\(oss\\|fb\\)[a-z,_]*\\)?)\\) 49 | suppress_comment=\\(.\\|\n\\)*\\$FlowIssue\\((\\(\\)? *\\(site=[a-z,_]*react_native\\(_ios\\)?_\\(oss\\|fb\\)[a-z,_]*\\)?)\\)?:? #[0-9]+ 50 | suppress_comment=\\(.\\|\n\\)*\\$FlowExpectedError 51 | 52 | [lints] 53 | sketchy-null-number=warn 54 | sketchy-null-mixed=warn 55 | sketchy-number=warn 56 | untyped-type-import=warn 57 | nonstrict-import=warn 58 | deprecated-type=warn 59 | unsafe-getters-setters=warn 60 | inexact-spread=warn 61 | unnecessary-invariant=warn 62 | signature-verification-failure=warn 63 | deprecated-utility=error 64 | 65 | [strict] 66 | deprecated-type 67 | nonstrict-import 68 | sketchy-null 69 | unclear-type 70 | unsafe-getters-setters 71 | untyped-import 72 | untyped-type-import 73 | 74 | [version] 75 | ^0.105.0 76 | -------------------------------------------------------------------------------- /example/.gitattributes: -------------------------------------------------------------------------------- 1 | *.pbxproj -text 2 | -------------------------------------------------------------------------------- /example/.gitignore: -------------------------------------------------------------------------------- 1 | # OSX 2 | # 3 | .DS_Store 4 | 5 | # Xcode 6 | # 7 | build/ 8 | *.pbxuser 9 | !default.pbxuser 10 | *.mode1v3 11 | !default.mode1v3 12 | *.mode2v3 13 | !default.mode2v3 14 | *.perspectivev3 15 | !default.perspectivev3 16 | xcuserdata 17 | *.xccheckout 18 | *.moved-aside 19 | DerivedData 20 | *.hmap 21 | *.ipa 22 | *.xcuserstate 23 | 24 | # Android/IntelliJ 25 | # 26 | build/ 27 | .idea 28 | .gradle 29 | local.properties 30 | *.iml 31 | 32 | # node.js 33 | # 34 | node_modules/ 35 | npm-debug.log 36 | yarn-error.log 37 | 38 | # BUCK 39 | buck-out/ 40 | \.buckd/ 41 | *.keystore 42 | !debug.keystore 43 | 44 | # fastlane 45 | # 46 | # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the 47 | # screenshots whenever they are needed. 48 | # For more information about the recommended setup visit: 49 | # https://docs.fastlane.tools/best-practices/source-control/ 50 | 51 | */fastlane/report.xml 52 | */fastlane/Preview.html 53 | */fastlane/screenshots 54 | 55 | # Bundle artifact 56 | *.jsbundle 57 | 58 | # CocoaPods 59 | /ios/Pods/ 60 | -------------------------------------------------------------------------------- /example/.prettierrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | bracketSpacing: false, 3 | jsxBracketSameLine: true, 4 | singleQuote: true, 5 | trailingComma: 'all', 6 | }; 7 | -------------------------------------------------------------------------------- /example/.watchmanconfig: -------------------------------------------------------------------------------- 1 | {} -------------------------------------------------------------------------------- /example/App.js: -------------------------------------------------------------------------------- 1 | import React, {useState} from 'react'; 2 | import {SafeAreaView, StyleSheet, Button, Text, StatusBar} from 'react-native'; 3 | 4 | import {Colors} from 'react-native/Libraries/NewAppScreen'; 5 | 6 | import {NativeModules} from 'react-native'; 7 | const { 8 | CreateGuid: {createGuid}, 9 | } = NativeModules; 10 | 11 | // Commented out because of limitations importing local libraries in a RN project 12 | // import { createGuid } from 'react-native-create-guid'; 13 | 14 | const styles = StyleSheet.create({ 15 | container: { 16 | flex: 1, 17 | alignContent: 'center', 18 | alignItems: 'center', 19 | justifyContent: 'center', 20 | }, 21 | engine: { 22 | position: 'absolute', 23 | right: 0, 24 | }, 25 | sectionTitle: { 26 | fontSize: 24, 27 | fontWeight: '600', 28 | color: Colors.black, 29 | }, 30 | sectionDescription: { 31 | marginTop: 8, 32 | marginBottom: 15, 33 | fontSize: 18, 34 | fontWeight: '400', 35 | color: Colors.dark, 36 | }, 37 | guidShown: { 38 | marginTop: 10, 39 | marginBottom: 10, 40 | }, 41 | }); 42 | 43 | const App = () => { 44 | const [guid, setGuid] = useState(''); 45 | 46 | return ( 47 | <> 48 | 49 | 50 | React Native Create Guid: 51 | Small library to create GUIDs 52 | Current GUID: 53 | {guid} 54 |