Skip to content

Commit 2c70c6a

Browse files
committed
Updated max function to accept key and default kwargs
Updated max function to work with all values instead of just numbers Added additional autotests for max
1 parent 4a9e2db commit 2c70c6a

File tree

3 files changed

+55
-16
lines changed

3 files changed

+55
-16
lines changed

transcrypt/development/automated_tests/transcrypt/module_builtin/__init__.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,18 @@ def canonizeStringList (stringList):
1414
def run (autoTester):
1515
autoTester.check ('min', min (-1.1, -1, -3))
1616
autoTester.check ('max', max (-1.1, -1, -3))
17+
autoTester.check ('max', max ([-1.1, -1, -3]))
18+
autoTester.check ('max', max ((-1.1, -1, -3)))
19+
autoTester.check ('max', max ('abc', 'ABC', 'xyz', 'XYZ'))
20+
autoTester.check ('max', max ('abc', 'ABC', 'xyz', 'XYZ', key=lambda v: v[1]))
21+
autoTester.check ('max', max (['abc', 'ABC', 'xyz', 'XYZ']))
22+
autoTester.check ('max', autoTester.expectException(lambda: max ([])))
23+
autoTester.check ('max', max ([], default='zzz'))
1724
autoTester.check ('abs', abs (-1), abs (1), abs (0), abs (-0.1), abs (0.1))
25+
1826
autoTester.check ('ord', ord ('a'), ord ('e´'[0])) # This is the 2 codepoint version
19-
27+
autoTester.check ('chr', chr (97), chr (122), chr (65), chr (90)) # a z A Z
28+
2029
autoTester.check ('round',
2130
round (4.006),
2231
round (4.006, 2),
@@ -104,21 +113,23 @@ def run (autoTester):
104113
'<br>'
105114
)
106115

107-
autoTester.check("".isalpha())
108-
autoTester.check("123".isalpha())
109-
autoTester.check("abc".isalpha())
110-
autoTester.check("abc123".isalpha())
116+
autoTester.check("isalpha",
117+
"".isalpha(),
118+
"123".isalpha(),
119+
"abc".isalpha(),
120+
"abc123".isalpha(),
121+
)
111122

112123
enumerate_list = ['a', 'b', 'c', 'd', 'e']
113124
# JS does not have tuples so coerce to list of lists
114-
autoTester.check(
125+
autoTester.check("enumerate",
115126
[list(item) for item in enumerate(enumerate_list)],
116127
[list(item) for item in enumerate(enumerate_list, 1)],
117128
[list(item) for item in enumerate(enumerate_list, start=2)]
118129
)
119130

120131
replace_test = "abcabcabcabc"
121-
autoTester.check(
132+
autoTester.check("replace",
122133
replace_test.replace("c", "x"),
123134
replace_test.replace("c", "x", -1),
124135
replace_test.replace("c", "x", 0),
@@ -127,7 +138,7 @@ def run (autoTester):
127138
replace_test.replace("c", "x", 10),
128139
)
129140

130-
autoTester.check(
141+
autoTester.check("bin-oct-hex",
131142
bin(42),
132143
oct(42),
133144
hex(42),
@@ -140,7 +151,8 @@ def run (autoTester):
140151
)
141152

142153
string_test = "abcdefghijkl"
143-
autoTester.check(string_test.startswith(""),
154+
autoTester.check("startswith",
155+
string_test.startswith(""),
144156
string_test.startswith("abcd"),
145157
string_test.startswith("efgh"),
146158
string_test.startswith("efgh", 2),
@@ -157,7 +169,7 @@ def run (autoTester):
157169
string_test.startswith(("abc", "defgh"), 3, 6),
158170
)
159171

160-
autoTester.check(
172+
autoTester.check("endswith",
161173
string_test.endswith(""),
162174
string_test.endswith("ijkl"),
163175
string_test.endswith("efgh"),

transcrypt/modules/org/transcrypt/__builtin__.js

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ export function py_typeof (anObject) {
553553
}
554554
}
555555
else {
556-
return ( // Odly, the braces are required here
556+
return ( // Oddly, the braces are required here
557557
aType == 'boolean' ? bool :
558558
aType == 'string' ? str :
559559
aType == 'number' ? (anObject % 1 == 0 ? int : float) :
@@ -667,10 +667,38 @@ export function ord (aChar) {
667667
return aChar.charCodeAt (0);
668668
};
669669

670-
// Maximum of n numbers
671-
export function max (nrOrSeq) {
672-
return arguments.length == 1 ? Math.max (...nrOrSeq) : Math.max (...arguments);
673-
};
670+
// Maximum of n values
671+
export function max (...args) {
672+
// Assume no kwargs
673+
let dflt = undefined;
674+
function key(x) {return x}
675+
676+
if (args.length > 0) {
677+
if (args[args.length-1] && args[args.length-1].hasOwnProperty ("__kwargtrans__")) {
678+
const kwargs = args[args.length - 1];
679+
args = args.slice(0, -1);
680+
if (kwargs.hasOwnProperty('key')) key = kwargs['key'];
681+
if (kwargs.hasOwnProperty('py_default')) dflt = kwargs['py_default'];
682+
}
683+
}
684+
685+
if (args.length === 0) throw TypeError("expected at least 1 argument, got 0", new Error ());
686+
if (args.length > 1 && dflt !== undefined) throw TypeError("Cannot specify a default with multiple positional arguments", new Error ());
687+
if (args.length === 1){
688+
if (Object.prototype.toString.call(args[0]) === '[object Array]'){
689+
args = args[0];
690+
} else {
691+
throw TypeError("object is not iterable", new Error());
692+
}
693+
}
694+
if (args === null || args.length === 0){
695+
if (dflt === undefined) throw ValueError ("max() arg is an empty sequence", new Error ());
696+
return dflt
697+
}
698+
699+
return args.reduce((max_val, cur_val) => key(cur_val) > key(max_val) ? cur_val : max_val);
700+
// return arguments.length == 1 ? Math.max (...nrOrSeq) : Math.max (...arguments);
701+
}
674702

675703
// Minimum of n numbers
676704
export function min (nrOrSeq) {

transcrypt/modules/org/transcrypt/__runtime__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@ def sorted (iterable, key = None, reverse = False):
123123
#__pragma__ ('nokwargs')
124124

125125
def map (func, *iterables):
126-
# return [func (item) for item in iterable]
127126
return [func(*items) for items in zip(*iterables)]
128127

129128
def filter (func, iterable):

0 commit comments

Comments
 (0)