1111from abc import abstractmethod
1212from typing import Any , Dict , Generator , Iterable , List , Optional , Tuple , Union
1313
14- from django import VERSION as DJANGO_VERSION
1514from django .conf import settings
1615from django .core .serializers .json import DjangoJSONEncoder
1716from django .template .context import Context
@@ -1116,81 +1115,6 @@ def impl() -> Generator[str, None, None]:
11161115 self .outdent ()
11171116 yield "}"
11181117
1119- def deep_equal (self ) -> Generator [Optional [str ], None , None ]:
1120- """
1121- The recursive deepEqual function.
1122- :yield: The JavaScript jdoc comment lines and deepEqual function.
1123- """
1124-
1125- def impl () -> Generator [str , None , None ]:
1126- """deepEqual default implementation"""
1127- yield "if (!(this.isObject(object1) && this.isObject(object2))) {"
1128- self .indent ()
1129- yield "return object1 === object2;"
1130- self .outdent ()
1131- yield "}"
1132- yield "const keys1 = Object.keys(object1);"
1133- yield "const keys2 = Object.keys(object2);"
1134- yield "if (keys1.length !== keys2.length) {"
1135- self .indent ()
1136- yield "return false;"
1137- self .outdent ()
1138- yield "}"
1139- yield "for (let key of keys1) {"
1140- self .indent ()
1141- yield "const val1 = object1[key];"
1142- yield "const val2 = object2[key];"
1143- yield "const areObjects = this.isObject(val1) && this.isObject(val2);"
1144- yield "if ("
1145- self .indent ()
1146- yield "(areObjects && !this.deepEqual(val1, val2)) ||"
1147- yield "(!areObjects && val1 !== val2)"
1148- yield ") { return false; }"
1149- self .outdent ()
1150- yield "}"
1151- self .outdent ()
1152- yield "return true;"
1153-
1154- if "deepEqual" in self .overrides_ :
1155- yield from self .transpile_override ("deepEqual" , impl ())
1156- else :
1157- for comment_line in """
1158- /**
1159- * Given two values, do a deep equality comparison. If the values are
1160- * objects, all keys and values are recursively compared.
1161- *
1162- * @param {Object} object1 - The first object to compare.
1163- * @param {Object} object2 - The second object to compare.
1164- */""" .split ("\n " ):
1165- yield comment_line [12 :]
1166- yield "deepEqual(object1, object2) {"
1167- self .indent ()
1168- yield from impl ()
1169- self .outdent ()
1170- yield "}"
1171-
1172- def is_object (self ) -> Generator [Optional [str ], None , None ]:
1173- """
1174- The isObject() function.
1175- :yield: The JavaScript jdoc comment lines and isObject function.
1176- """
1177- impl = 'return object != null && typeof object === "object";'
1178- if "isObject" in self .overrides_ :
1179- yield from self .transpile_override ("isObject" , impl )
1180- else :
1181- for comment_line in """
1182- /**
1183- * Given a variable, return true if it is an object.
1184- *
1185- * @param {Object} object - The variable to check.
1186- */""" .split ("\n " ):
1187- yield comment_line [12 :]
1188- yield "isObject(object) {"
1189- self .indent ()
1190- yield impl
1191- self .outdent ()
1192- yield "}"
1193-
11941118 def match (self ) -> Generator [Optional [str ], None , None ]:
11951119 """
11961120 The #match() function.
@@ -1206,24 +1130,21 @@ def impl() -> Generator[str, None, None]:
12061130 self .indent ()
12071131 yield "if (kwargs.hasOwnProperty(key)) {"
12081132 self .indent ()
1209- if DJANGO_VERSION [0 :2 ] >= (4 , 1 ):
1210- # there was a change in Django 4.1 that seems to coerce kwargs
1211- # given to the default kwarg type of the same name if one
1212- # exists for the purposes of reversal. Thus 1 will == '1'
1213- # In javascript we attempt string conversion and hope for the
1214- # best. In 4.1 given kwargs will also override default kwargs
1215- # for kwargs the reversal is expecting. This seems to have
1216- # been a byproduct of the differentiation of captured_kwargs
1217- # and extra_kwargs - that this wasn't caught in Django's CI is
1218- # evidence that previous behavior wasn't considered spec.
1219- yield (
1220- "if (kwargs[key] !== val && "
1221- "JSON.stringify(kwargs[key]) !== JSON.stringify(val) "
1222- "&& !expected.includes(key)) "
1223- "{ return false; }"
1224- )
1225- else :
1226- yield "if (!this.deepEqual(kwargs[key], val)) { return false; }"
1133+ # there was a change in Django 4.1 that seems to coerce kwargs
1134+ # given to the default kwarg type of the same name if one
1135+ # exists for the purposes of reversal. Thus 1 will == '1'
1136+ # In javascript we attempt string conversion and hope for the
1137+ # best. In 4.1 given kwargs will also override default kwargs
1138+ # for kwargs the reversal is expecting. This seems to have
1139+ # been a byproduct of the differentiation of captured_kwargs
1140+ # and extra_kwargs - that this wasn't caught in Django's CI is
1141+ # evidence that previous behavior wasn't considered spec.
1142+ yield (
1143+ "if (kwargs[key] !== val && "
1144+ "JSON.stringify(kwargs[key]) !== JSON.stringify(val) "
1145+ "&& !expected.includes(key)) "
1146+ "{ return false; }"
1147+ )
12271148 yield "if (!expected.includes(key)) { delete kwargs[key]; }"
12281149 self .outdent ()
12291150 yield "}"
@@ -1341,11 +1262,6 @@ class code.
13411262 yield ""
13421263 yield from self .match ()
13431264 yield ""
1344- if DJANGO_VERSION [0 :2 ] < (4 , 1 ):
1345- yield from self .deep_equal ()
1346- yield ""
1347- yield from self .is_object ()
1348- yield ""
13491265 yield from self .reverse ()
13501266 yield ""
13511267 yield "urls = {"
0 commit comments