Skip to content

Commit 89c7f0e

Browse files
committed
Add _nth
1 parent 1c39815 commit 89c7f0e

File tree

9 files changed

+164
-33
lines changed

9 files changed

+164
-33
lines changed

README.md

Lines changed: 79 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,41 +6,51 @@ Version 2.0.1
66

77
gdash is a functional utility library for GML, inspired by [lodash](https://lodash.com/). It aims to add useful, broad-purposed functions to help aid in game development. If you are doing any kind of data manipulation in your game, gdash can help out.
88

9+
## Table of Contents
10+
11+
<!-- toc -->
12+
13+
- [Install](#install)
14+
- [API](#api)
15+
* [`_and(valueA, valueB)`](#_andvaluea-valueb)
16+
* [`_arrayOf(value1, value2 ... value14)`](#_arrayofvalue1-value2--value14)
17+
* [`_cloneArray(array)`](#_clonearrayarray)
18+
* [`_collect(objectType)`](#_collectobjecttype)
19+
* [`_concat(arrayA, arrayB)`](#_concatarraya-arrayb)
20+
* [`_contains(collection, target, fromIndex = 0)`](#_containscollection-target-fromindex--0)
21+
* [`_destroy(instance)`](#_destroyinstance)
22+
* [`_filter(array, filterScript)`](#_filterarray-filterscript)
23+
* [`_find(array, findScript)`](#_findarray-findscript)
24+
* [`_free(resourceId [, dsType])`](#_freeresourceid--dstype)
25+
* [`_get(mapId, locationString [, default])`](#_getmapid-locationstring--default)
26+
* [`_indexOf(collection, value)`](#_indexofcollection-value)
27+
* [`_isEqual(valueA, valueB)`](#_isequalvaluea-valueb)
28+
* [`_join(array, joiner)`](#_joinarray-joiner)
29+
* [`_keys(dsMapId)`](#_keysdsmapid)
30+
* [`_length(collection)`](#_lengthcollection)
31+
* [`_log(anything)`](#_loganything)
32+
* [`_map(colletion, mapScript)`](#_mapcolletion-mapscript)
33+
* [`_nth(collection, n)`](#_nthcollection-n)
34+
* [`_or(valueA, valueB)`](#_orvaluea-valueb)
35+
* [`_partial(script, arg0, arg1 ... arg13)`](#_partialscript-arg0-arg1--arg13)
36+
* [`_push(array, value)`](#_pusharray-value)
37+
* [`_reduce(collection, reducerScript)`](#_reducecollection-reducerscript)
38+
* [`_run(script, arg0, arg1 ... arg13)`](#_runscript-arg0-arg1--arg13)
39+
* [`_set(mapId, locationString, value)`](#_setmapid-locationstring-value)
40+
* [`_split(string, splitter)`](#_splitstring-splitter)
41+
* [`_spread(script, argArray)`](#_spreadscript-argarray)
42+
* [`_times(script)`](#_timesscript)
43+
* [`_typeOf(value)`](#_typeofvalue)
44+
* [`_uniq(array)`](#_uniqarray)
45+
46+
<!-- tocstop -->
47+
948
## Install
1049

1150
Download [the latest release](https://github.com/twisterghost/gdash/releases) and import the gml files into your project's scripts. For GameMaker: Studio 2, you can just drag and drop the files into the editor.
1251

1352
## API
1453

15-
* [_and](#_andvaluea-valueb)
16-
* [_arrayOf](#_arrayofvalue1-value2--value14)
17-
* [_cloneArray](#_clonearrayarray)
18-
* [_collect](#_collectobjecttype)
19-
* [_concat](#_concatarraya-arrayb)
20-
* [_contains](#_containscollection-target-fromindex--0)
21-
* [_destroy](#_destroyinstance)
22-
* [_filter](#_filterarray-filterscript)
23-
* [_find](#_findarray-findscript)
24-
* [_free](#_freeresourceid--dstype)
25-
* [_get](#_getmapid-locationstring--default)
26-
* [_indexOf](#_indexofcollection-value)
27-
* [_isEqual](#_isequalvaluea-valueb)
28-
* [_join](#_joinarray-joiner)
29-
* [_keys](#_keysdsmapid)
30-
* [_length](#_lengthcollection)
31-
* [_log](#_loganything)
32-
* [_map](#_mapcolletion-mapscript)
33-
* [_partial](#_partialscript-arg0-arg1--arg13)
34-
* [_push](#_pusharray-value)
35-
* [_reduce](#_reducecollection-reducerscript)
36-
* [_run](#_runscript-arg0-arg1--arg13)
37-
* [_set](#_setmapid-locationstring-value)
38-
* [_split](#_splitstring-splitter)
39-
* [_spread](#_spreadscript-argarray)
40-
* [_times](#_timesscript)
41-
* [_typeOf](#_typeofvalue)
42-
* [_uniq](#_uniqarray)
43-
4454
### `_and(valueA, valueB)`
4555

4656
Returns the value of the provided arguments after a boolean `and`
@@ -193,7 +203,7 @@ __something(2);
193203
_free(__something);
194204
```
195205

196-
### `_get(mapId, locationString [, default)`
206+
### `_get(mapId, locationString [, default])`
197207

198208
Gets a nested value following a dot notation
199209

@@ -349,6 +359,45 @@ _map(map, divideByTwo, ds_type_map);
349359
// => [3, 5]
350360
```
351361

362+
### `_nth(collection, n)`
363+
364+
Returns the nth index of the given array or ds_list. If n is negative, the nth element from the end is returned.
365+
366+
```
367+
@param collection
368+
@param n
369+
370+
@example
371+
var list = ds_list_create();
372+
list[| 0] = "hello"
373+
list[| 1] = "world";
374+
_nth(list, 0);
375+
// => "hello";
376+
377+
_nth(list, -1);
378+
// => "world";
379+
```
380+
381+
### `_or(valueA, valueB)`
382+
383+
Returns the value of the provided arguments after a boolean `or`
384+
385+
```
386+
@param {*} Some first input
387+
@param {*} A value to || the first input with
388+
@returns {Boolean} The value of the provided arguments after an ||
389+
390+
@example
391+
_or(true, true);
392+
// => true
393+
394+
_or(false, true);
395+
// => true
396+
397+
_or(false, false);
398+
// => false
399+
```
400+
352401
### `_partial(script, arg0, arg1 ... arg13)`
353402

354403
Creates a partial function identifier for use in place of raw scripts in gdash functions, or with the use of _run.

src/gdash/gdash.yyp

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,30 @@
1-
test_suite_pass();
1+
/// _nth
2+
test_start("_nth", "Returns the nth element of the collection");
3+
4+
var list = ds_list_create();
5+
list[| 0] = "first";
6+
list[| 1] = "second";
7+
list[| 2] = "third";
8+
9+
var array;
10+
array[0] = "first";
11+
array[1] = "second";
12+
array[2] = "third";
13+
14+
assert_equal(_nth(list, 0), list[| 0]);
15+
assert_equal(_nth(list, 1), list[| 1]);
16+
assert_equal(_nth(list, 2), list[| 2]);
17+
18+
assert_equal(_nth(list, -1), list[| 2]);
19+
assert_equal(_nth(list, -2), list[| 1]);
20+
assert_equal(_nth(list, -3), list[| 0]);
21+
22+
assert_equal(_nth(array, 0), array[0]);
23+
assert_equal(_nth(array, 1), array[1]);
24+
assert_equal(_nth(array, 2), array[2]);
25+
26+
assert_equal(_nth(array, -1), array[2]);
27+
assert_equal(_nth(array, -2), array[1]);
28+
assert_equal(_nth(array, -3), array[0]);
29+
30+
test_end();
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test_suite_pass();

src/gdash/objects/obj_gdash_test_2/obj_gdash_test_2.yy

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/gdash/scripts/_nth/_nth.gml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/// @desc Returns the nth index of the given array or ds_list. If n is negative, the nth element from the end is returned.
2+
/// @param collection
3+
/// @param n
4+
5+
var collection = argument0;
6+
var n = argument1;
7+
var type = _typeOf(collection);
8+
9+
10+
if (type == "real") {
11+
if (n >= 0) {
12+
return collection[| n];
13+
} else {
14+
return collection[| ds_list_size(collection) + n];
15+
}
16+
} else if (type == "array") {
17+
if (n >= 0) {
18+
return collection[@ n];
19+
} else {
20+
return collection[@ array_length_1d(collection) + n];
21+
}
22+
} else {
23+
show_error("Trying to return nth element of an incorrect type: " + type + "\nCollection must be a ds_list or array.", false);
24+
}

src/gdash/scripts/_nth/_nth.yy

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/gdash/scripts/gamatas_create_assert_error/gamatas_create_assert_error.gml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
* Helper method for asserts to create standardized error
33
* messages. Not meant for external use.
44
*/
5-
return "ASSERT ERROR - " + string(argument0) + " - ASSERT #" + string(global.testAssertNumber);
5+
return "ASSERT ERROR - " + string(argument0) + " - ASSERT #" + string(global.gamatas_testAssertNumber);

src/gdash/views/8ebd4763-7203-42aa-9554-cc55d03a802b.yy

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)