diff --git a/README.md b/README.md index a15452c..69f69ba 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,7 @@ gdash is a functional utility library for GML, inspired by [lodash](https://loda * [`_split(string, splitter)`](#_splitstring-splitter) * [`_spread(script, argArray)`](#_spreadscript-argarray) * [`_times(script)`](#_timesscript) + * [`_toArray(list)`](#_toarraylist) * [`_typeOf(value)`](#_typeofvalue) * [`_uniq(array)`](#_uniqarray) @@ -542,6 +543,16 @@ _times(3, returnTheValue5); // => [5, 5, 5]; ``` +### `_toArray(list)` + +Returns the given ds_list as an array + +``` +@param {DS_List} the list to convert +@returns {Array} An array representation of the given list +Note: If the given list is of size 0, this will return undefined +``` + ### `_typeOf(value)` Returns the variable type of the given argument diff --git a/src/gdash/objects/obj_gdash_test_2/Other_23.gml b/src/gdash/objects/obj_gdash_test_2/Other_23.gml index 6b3ba84..d8e745f 100644 --- a/src/gdash/objects/obj_gdash_test_2/Other_23.gml +++ b/src/gdash/objects/obj_gdash_test_2/Other_23.gml @@ -1 +1,13 @@ -test_suite_pass(); \ No newline at end of file +/// _toArray +test_start("_toArray", "Converts a list to an array"); + +var list = ds_list_create(); +ds_list_add(1, 2, 3, 4, 5); + +var arr = _toArray(list); + +for (var i = 0; i < _length(arr); i++) { + assert_equal(list[| i], arr[i]); +} + +test_end(); \ No newline at end of file diff --git a/src/gdash/objects/obj_gdash_test_2/Other_24.gml b/src/gdash/objects/obj_gdash_test_2/Other_24.gml new file mode 100644 index 0000000..6b3ba84 --- /dev/null +++ b/src/gdash/objects/obj_gdash_test_2/Other_24.gml @@ -0,0 +1 @@ +test_suite_pass(); \ No newline at end of file diff --git a/src/gdash/objects/obj_gdash_test_2/obj_gdash_test_2.yy b/src/gdash/objects/obj_gdash_test_2/obj_gdash_test_2.yy index 82067c3..faa0d05 100644 --- a/src/gdash/objects/obj_gdash_test_2/obj_gdash_test_2.yy +++ b/src/gdash/objects/obj_gdash_test_2/obj_gdash_test_2.yy @@ -153,6 +153,16 @@ "enumb": 23, "eventtype": 7, "m_owner": "7893bfef-671e-4406-83f9-e70a21e68cea" + }, + { + "id": "7a14f5b4-0625-45c6-a874-ce6ad00985ee", + "modelName": "GMEvent", + "mvc": "1.0", + "IsDnD": false, + "collisionObjectId": "00000000-0000-0000-0000-000000000000", + "enumb": 24, + "eventtype": 7, + "m_owner": "7893bfef-671e-4406-83f9-e70a21e68cea" } ], "maskSpriteId": "00000000-0000-0000-0000-000000000000", diff --git a/src/gdash/scripts/_toArray/_toArray.gml b/src/gdash/scripts/_toArray/_toArray.gml index c501d65..ff1f0a7 100644 --- a/src/gdash/scripts/_toArray/_toArray.gml +++ b/src/gdash/scripts/_toArray/_toArray.gml @@ -1,12 +1,18 @@ /// @desc Converts the given ds_list to an array /// @param list +/// Note: If the given list is of size 0, this will return undefined. var list = argument0; var listSize = ds_list_size(list); var array; +array[0] = undefined; for (var i = listSize - 1; i >= 0; i--) { array[i] = list[| i]; } +if (is_undefined(array[0])) { + return undefined; +} + return array; \ No newline at end of file