Skip to content

Instantly share code, notes, and snippets.

@Guichaguri
Created July 17, 2017 14:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Guichaguri/5c7574b31f9584b6a9a0c182fd940a86 to your computer and use it in GitHub Desktop.
Save Guichaguri/5c7574b31f9584b6a9a0c182fd940a86 to your computer and use it in GitHub Desktop.
Conversion to bundle tests - React Native

Code

// Create the int array
WritableArray intArray = Arguments.createArray();
intArray.pushInt(4);
intArray.pushInt(7);
intArray.pushInt(-5);

// Create the string array
WritableArray stringArray = Arguments.createArray();
stringArray.pushString("react native");
stringArray.pushString("is awesome");

// Create the mixed array
WritableArray mixedArray = Arguments.createArray();
mixedArray.pushDouble(2.5);
mixedArray.pushString("hello");
mixedArray.pushBoolean(false);
mixedArray.pushNull();

WritableArray array = Arguments.createArray();
array.pushBoolean(false);
mixedArray.pushArray(array);

WritableMap arrayMap = Arguments.createMap();
arrayMap.putString("foo", "bar");
mixedArray.pushMap(arrayMap);

// Create a map with all arrays
WritableMap map = Arguments.createMap();
map.putArray("ints", intArray);
map.putArray("strings", stringArray);
map.putArray("mixed", mixedArray);

// Convert the map to a bundle
Bundle bundle = Arguments.toBundle(map);

// Log the integer array directly from the bundle
for(int i : bundle.getIntegerArrayList("ints")) {
  Log.i("BundleIntArray", "Entry: " + i);
}

// Log the string array directly from the bundle
for(String i : bundle.getStringArrayList("strings")) {
  Log.i("BundleStringArray", "Entry: " + i);
}

// Log the mixed array directly from the bundle
for(Object i : (ArrayList)bundle.get("mixed")) {
  Log.i("BundleMixedArray", "Entry: " + i);
}

// Let's transform the bundle back to a map, so we can check if it's lossless
WritableMap newMap = Arguments.fromBundle(bundle);

// Log the integer array directly from the new map
ReadableArray newIntArray = newMap.getArray("ints");
for(int i = 0; i < newIntArray.size(); i++) {
  Log.i("MapIntArray", "Entry: " + newIntArray.getInt(i));
}

// Log the string array directly from the new map
ReadableArray newStringArray = newMap.getArray("strings");
for(int i = 0; i < newStringArray.size(); i++) {
  Log.i("MapStringArray", "Entry: " + newStringArray.getString(i));
}

// Log each value from the mixed array directly from the new map
ReadableArray newMixedArray = newMap.getArray("mixed");
Log.i("MapMixedArray", "Double: " + newMixedArray.getDouble(0));
Log.i("MapMixedArray", "String: " + newMixedArray.getString(1));
Log.i("MapMixedArray", "Boolean: " + newMixedArray.getBoolean(2));
Log.i("MapMixedArray", "Is Null: " + newMixedArray.isNull(3));
Log.i("MapMixedArray", "Array Boolean: " + newMixedArray.getArray(4).getBoolean(0));
Log.i("MapMixedArray", "Map String: " + newMixedArray.getMap(5).getString("foo"));

Result

I/BundleIntArray: Entry: 4
I/BundleIntArray: Entry: 7
I/BundleIntArray: Entry: -5
I/BundleStringArray: Entry: react native
I/BundleStringArray: Entry: is awesome
I/BundleMixedArray: Entry: 2.5
I/BundleMixedArray: Entry: hello
I/BundleMixedArray: Entry: false
I/BundleMixedArray: Entry: null
I/BundleMixedArray: Entry: [false]
I/BundleMixedArray: Entry: Bundle[{foo=bar}]
I/MapIntArray: Entry: 4
I/MapIntArray: Entry: 7
I/MapIntArray: Entry: -5
I/MapStringArray: Entry: react native
I/MapStringArray: Entry: is awesome
I/MapMixedArray: Double: 2.5
I/MapMixedArray: String: hello
I/MapMixedArray: Boolean: false
I/MapMixedArray: Is Null: true
I/MapMixedArray: Array Boolean: false
I/MapMixedArray: Map String: bar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment