From 24b522c7fcc17a8fefd18f9efcc84056497b263d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Mon, 31 Oct 2016 17:20:44 +0100 Subject: [PATCH] Make Bool PYSON evaluate to false empty Time and TimeDelta As Time and TimeDelta are moment Object, Bool should specially evaluate those instances. issue5993 review25781002 --- src/pyson.js | 7 ++++++- tests/sao.js | 12 ++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/pyson.js b/src/pyson.js index 94d71df521..041ba2b01b 100644 --- a/src/pyson.js +++ b/src/pyson.js @@ -197,7 +197,12 @@ }); Sao.PYSON.Bool.eval_ = function(value, context) { - if (value.v instanceof Object) { + if (moment.isMoment(value.v) && value.v.isTime) { + return Boolean(value.v.hour() || value.v.minute() || + value.v.second() || value.v.millisecond()); + } else if (moment.isDuration(value.v)) { + return Boolean(value.v.valueOf()); + } else if (value.v instanceof Object) { return !jQuery.isEmptyObject(value.v); } else { return Boolean(value.v); diff --git a/tests/sao.js b/tests/sao.js index 8375dee8dd..3736d916d4 100644 --- a/tests/sao.js +++ b/tests/sao.js @@ -149,6 +149,18 @@ eval_ = new Sao.PYSON.Encoder().encode(new Sao.PYSON.Bool({})); QUnit.strictEqual(new Sao.PYSON.Decoder().decode(eval_), false, 'decode(Bool({}))'); + + eval_ = new Sao.PYSON.Encoder().encode( + new Sao.PYSON.Bool(new Sao.PYSON.Eval('value'))); + QUnit.strictEqual(new Sao.PYSON.Decoder({value: Sao.Time(0)}) + .decode(eval_), false, "decode(Bool(Sao.Time(0)))"); + QUnit.strictEqual(new Sao.PYSON.Decoder({value: Sao.Time(1)}) + .decode(eval_), true, "decode(Bool(Sao.Time(1)))"); + QUnit.strictEqual(new Sao.PYSON.Decoder({value: Sao.TimeDelta()}) + .decode(eval_), false, "decode(Bool(Sao.TimeDelta()))"); + QUnit.strictEqual(new Sao.PYSON.Decoder({value: Sao.TimeDelta(1)}) + .decode(eval_), true, "decode(Bool(Sao.TimeDelta(1)))"); + QUnit.strictEqual(new Sao.PYSON.Bool('test').toString(), "Bool(\"test\")"); });