Skip to content

Commit cb31483

Browse files
committed
fix: Improved comparinson with None
1 parent 7f76455 commit cb31483

File tree

8 files changed

+32
-5
lines changed

8 files changed

+32
-5
lines changed

logics-js/logics.html

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
let values = {
1313
"name": "Doe",
1414
"firstname": "John",
15+
"demo_alter": 12,
1516
"person": {
1617
"firstname": "Bernd",
1718
"name": "Bommelunder"
@@ -31,7 +32,9 @@
3132
"roles": ["custom"],
3233
};
3334

34-
document.querySelector("#prompt").addEventListener(
35+
let prompt = document.querySelector("#prompt");
36+
37+
prompt.addEventListener(
3538
"change",
3639
(e) => {
3740
let calc;
@@ -55,9 +58,13 @@
5558
li.style.color = "red";
5659
li.appendChild(document.createTextNode(exception.toString()));
5760
}
58-
5961
}
60-
)
62+
);
63+
64+
// When prompt is already filled, execute imediatelly
65+
if (prompt.value) {
66+
window.setTimeout(() => prompt.dispatchEvent(new Event("change")), 125);
67+
}
6168
</script>
6269
</body>
6370
</html>

logics-js/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "logics-js",
3-
"version": "0.4.0",
3+
"version": "0.4.1",
44
"description": "Logics is a user-friendly formula language with a subset of Python's expression syntax",
55
"main": "logics.js",
66
"type": "module",

logics-js/test/value.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ describe("Value", () => {
88
// assert.strictEqual(none, null);
99
// assert.isNull(none.toNullable());
1010
// assert.isFalse(none.toBoolean());
11+
assert.ok(none.__cmp__(new Value(null)) == 0);
12+
assert.ok(none.__cmp__(new Value(0)) < 0);
1113
assert.strictEqual(none.toBool(), false);
1214
assert.strictEqual(none.toInt(), 0);
1315
assert.strictEqual(none.toFloat(), 0.0);

logics-js/value.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,8 @@ export default class Value {
262262
__cmp__(other) {
263263
let a, b;
264264

265+
// console.log(this.type(), other.type());
266+
265267
// Dict types
266268
if (this.type() === "dict" || other.type() === "dict") {
267269
// fixme: Python cannot compare < and > on dict, only ==. Change this here, too?
@@ -323,6 +325,9 @@ export default class Value {
323325
} else if (this.type() === "float" || other.type() === "float") {
324326
a = this.toFloat();
325327
b = other.toFloat();
328+
} else if (this.type() === "NoneType" || other.type() === "NoneType") {
329+
a = this.type() === "NoneType" ? -1 : 0;
330+
b = other.type() === "NoneType" ? -1 : 0;
326331
} else {
327332
a = this.toInt();
328333
b = other.toInt();

logics-py/logics/value.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,11 @@ def __compare(self, op, other):
250250
value = self.value
251251
other = Value(other).value
252252

253+
if value is None:
254+
value = -1
255+
if other is None:
256+
other = -1
257+
253258
try:
254259
match op:
255260
case "lt":

logics-py/logics/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.4.0"
1+
__version__ = "0.4.1"

logics-py/tests/test_value.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ def test_conversion():
1212
def test_none():
1313
none = Value(None)
1414
assert none == None
15+
assert none != 0
1516
assert bool(none) == False
1617
assert int(none) == 0
1718
assert float(none) == 0.0

tests/none.lgx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#SET:none:None
2+
none == None
3+
#EXPECT:True
4+
none == 0
5+
#EXPECT:False
6+
none < 0
7+
#EXPECT:True

0 commit comments

Comments
 (0)