Skip to content

Commit 7748b24

Browse files
committed
.join() method
added join and push methods (push is just a copy of add and append)
1 parent 50f4844 commit 7748b24

File tree

4 files changed

+91
-22
lines changed

4 files changed

+91
-22
lines changed

ArrayClass.gml

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,36 @@ function Array() constructor {
298298
return self;
299299
}
300300

301+
///@function join(separator)
302+
///@description returns a string, containing all of the array values separated by 'sep'
303+
///@tip to join part of the array, use array.slice().join()
304+
///@param {string} separator
305+
///@param {bool} show_bounds
306+
static join = function(sep, show_bounds) {
307+
if is_undefined(sep)
308+
sep = ", "
309+
if is_undefined(show_bounds)
310+
show_bounds = true
311+
312+
_sep = sep
313+
314+
if show_bounds
315+
str = "["
316+
else
317+
str = ""
318+
319+
forEach(function(el, i) {
320+
str += string(el)
321+
if(i < size-1)
322+
str += _sep
323+
})
324+
325+
if show_bounds
326+
str += "]"
327+
328+
return str
329+
}
330+
301331
///@function lambda(func)
302332
///@description Loops through the array and applies the function to each element
303333
///@param {function} func(x, *pos)
@@ -393,6 +423,19 @@ function Array() constructor {
393423
return ans;
394424
}
395425

426+
///@function push(value, value2, ..)
427+
///@description Mirrors append() method
428+
///@param {any} value
429+
static push = function(value) {
430+
for(var i = 0; i < argument_count; ++i) {
431+
var val = argument[i]
432+
content[size] = val;
433+
++size;
434+
}
435+
436+
return self;
437+
}
438+
396439
///@function pushBack(value)
397440
///@description inserts a value to the beginning of the array
398441
///@param {any} value
@@ -616,17 +659,7 @@ function Array() constructor {
616659

617660

618661
static toString = function() {
619-
str = "[";
620-
621-
forEach(function(el, i) {
622-
str += string(el);
623-
if(i < size-1)
624-
str += ", ";
625-
});
626-
627-
str += "]";
628-
629-
return str;
662+
return self.join()
630663
}
631664
}
632665

ArrayClass.yymps

194 Bytes
Binary file not shown.

Sample+Tests/scripts/ArrayClass/ArrayClass.gml

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,36 @@ function Array() constructor {
298298
return self;
299299
}
300300

301+
///@function join(separator)
302+
///@description returns a string, containing all of the array values separated by 'sep'
303+
///@tip to join part of the array, use array.slice().join()
304+
///@param {string} separator
305+
///@param {bool} show_bounds
306+
static join = function(sep, show_bounds) {
307+
if is_undefined(sep)
308+
sep = ", "
309+
if is_undefined(show_bounds)
310+
show_bounds = true
311+
312+
_sep = sep
313+
314+
if show_bounds
315+
str = "["
316+
else
317+
str = ""
318+
319+
forEach(function(el, i) {
320+
str += string(el)
321+
if(i < size-1)
322+
str += _sep
323+
})
324+
325+
if show_bounds
326+
str += "]"
327+
328+
return str
329+
}
330+
301331
///@function lambda(func)
302332
///@description Loops through the array and applies the function to each element
303333
///@param {function} func(x, *pos)
@@ -393,6 +423,19 @@ function Array() constructor {
393423
return ans;
394424
}
395425

426+
///@function push(value, value2, ..)
427+
///@description Mirrors append() method
428+
///@param {any} value
429+
static push = function(value) {
430+
for(var i = 0; i < argument_count; ++i) {
431+
var val = argument[i]
432+
content[size] = val;
433+
++size;
434+
}
435+
436+
return self;
437+
}
438+
396439
///@function pushBack(value)
397440
///@description inserts a value to the beginning of the array
398441
///@param {any} value
@@ -616,17 +659,7 @@ function Array() constructor {
616659

617660

618661
static toString = function() {
619-
str = "[";
620-
621-
forEach(function(el, i) {
622-
str += string(el);
623-
if(i < size-1)
624-
str += ", ";
625-
});
626-
627-
str += "]";
628-
629-
return str;
662+
return self.join()
630663
}
631664
}
632665

Sample+Tests/scripts/Tests/Tests.gml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ function __Array_test() {
120120
show_debug_message("Sliced 0-4. Result: "+string(array_slice))
121121

122122

123+
// Joining
124+
show_debug_message("#### PRINTING (JOINING) ####")
125+
show_debug_message(array.join("|"))
123126

124127

125128
// sorting (slow, don't do every frame)

0 commit comments

Comments
 (0)