DEV: implements some of ember-truth-helpers (#12667)

DEV: implements some of ember-truth-helpers (#12667)

The implemented helpers, are helper which might be in Ember core in the future:

  • and
  • or
  • not
  • eq
  • not-eq
  • lt
  • lte
  • gt
  • gte

They follow the implementation of ember-truth-helpers: GitHub - jmurphyau/ember-truth-helpers: Ember HTMLBars Helpers for {{if}} & {{unless}}: not, and, or, eq & is-array

Note 1: Ember rfcs are still debating going with {{not-eq}} or {{neq}}, should be easy to support in the future whatever is finally chosen.

Note 2: this commit also moves it to its own addon, and removes the {{not}} test, to simplify further updates.

diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js
index 8adedfd..a42435a 100644
--- a/app/assets/javascripts/application.js
+++ b/app/assets/javascripts/application.js
@@ -1,3 +1,4 @@
+//= require_tree ./truth-helpers/addon
 //= require_tree ./discourse-common/addon
 //= require ./polyfills
 //= require_tree ./select-kit/addon
diff --git a/app/assets/javascripts/discourse/app/helpers/not.js b/app/assets/javascripts/discourse/app/helpers/not.js
deleted file mode 100644
index e1afe32..0000000
--- a/app/assets/javascripts/discourse/app/helpers/not.js
+++ /dev/null
@@ -1,14 +0,0 @@
-// https://github.com/jmurphyau/ember-truth-helpers/blob/master/addon/helpers/not.js
-import Helper from "@ember/component/helper";
-import truthConvert from "discourse/lib/truth-convert";
-
-export function not(params) {
-  for (let i = 0, len = params.length; i < len; i++) {
-    if (truthConvert(params[i]) === true) {
-      return false;
-    }
-  }
-  return true;
-}
-
-export default Helper.helper(not);
diff --git a/app/assets/javascripts/discourse/app/lib/truth-convert.js b/app/assets/javascripts/discourse/app/lib/truth-convert.js
deleted file mode 100644
index 2bd0fd9..0000000
--- a/app/assets/javascripts/discourse/app/lib/truth-convert.js
+++ /dev/null
@@ -1,16 +0,0 @@
-// https://github.com/jmurphyau/ember-truth-helpers/blob/master/addon/utils/truth-convert.js
-import { isArray } from "@ember/array";
-import { get } from "@ember/object";
-
-export default function truthConvert(result) {
-  const truthy = result && get(result, "isTruthy");
-  if (typeof truthy === "boolean") {
-    return truthy;
-  }
-
-  if (isArray(result)) {
-    return get(result, "length") !== 0;
-  } else {
-    return !!result;
-  }
-}
diff --git a/app/assets/javascripts/discourse/tests/unit/helpers/not-test.js b/app/assets/javascripts/discourse/tests/unit/helpers/not-test.js
deleted file mode 100644
index 3802bae..0000000
--- a/app/assets/javascripts/discourse/tests/unit/helpers/not-test.js
+++ /dev/null
@@ -1,34 +0,0 @@
-// https://github.com/jmurphyau/ember-truth-helpers/blob/master/tests/unit/helpers/not-test.js
-import componentTest, {
-  setupRenderingTest,
-} from "discourse/tests/helpers/component-test";
-import { discourseModule, query } from "discourse/tests/helpers/qunit-helpers";
-import hbs from "htmlbars-inline-precompile";
-
-discourseModule("Unit | Helper | not", function (hooks) {
-  setupRenderingTest(hooks);
-
-  componentTest("simple test 1", {
-    template: hbs`<div id="not-test">[{{not true}}] [{{not false}}] [{{not null}}] [{{not undefined}}] [{{not ''}}] [{{not ' '}}]</div>`,
-
-    test(assert) {
-      assert.equal(
-        query("#not-test").textContent,
-        "[false] [true] [true] [true] [true] [false]",
-        'value should be "[false] [true] [true] [true] [true] [false]"'
-      );
-    },
-  });
-
-  componentTest("simple test 2", {
-    template: hbs`<div id="not-test">[{{not true false}}] [{{not true false}}] [{{not null null false null}}] [{{not false null ' ' true}}]</div>`,
-
-    test(assert) {
-      assert.equal(
-        query("#not-test").textContent,
-        "[false] [false] [true] [false]",
-        'value should be "[false] [false] [true] [false]"'
-      );
-    },
-  });
-});
diff --git a/app/assets/javascripts/truth-helpers/addon/.gitkeep b/app/assets/javascripts/truth-helpers/addon/.gitkeep
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/app/assets/javascripts/truth-helpers/addon/.gitkeep
diff --git a/app/assets/javascripts/truth-helpers/addon/helpers/and.js b/app/assets/javascripts/truth-helpers/addon/helpers/and.js
new file mode 100644
index 0000000..07db192
--- /dev/null
+++ b/app/assets/javascripts/truth-helpers/addon/helpers/and.js
@@ -0,0 +1,13 @@
+import Helper from "@ember/component/helper";
+import truthConvert from "../utils/truth-convert";
+
+export function and(params) {
+  for (let i = 0, len = params.length; i < len; i++) {
+    if (truthConvert(params[i]) === false) {
+      return params[i];
+    }
+  }
+  return params[params.length - 1];
+}
+
+export default Helper.helper(and);
diff --git a/app/assets/javascripts/truth-helpers/addon/helpers/eq.js b/app/assets/javascripts/truth-helpers/addon/helpers/eq.js
new file mode 100644
index 0000000..45d0359
--- /dev/null
+++ b/app/assets/javascripts/truth-helpers/addon/helpers/eq.js
@@ -0,0 +1,7 @@
+import Helper from "@ember/component/helper";
+
+export function eq(params) {
+  return params[0] === params[1];
+}
+
+export default Helper.helper(eq);
diff --git a/app/assets/javascripts/truth-helpers/addon/helpers/gt.js b/app/assets/javascripts/truth-helpers/addon/helpers/gt.js
new file mode 100644
index 0000000..c90fc98
--- /dev/null
+++ b/app/assets/javascripts/truth-helpers/addon/helpers/gt.js
@@ -0,0 +1,15 @@
+import Helper from "@ember/component/helper";
+
+export function gt([left, right], hash) {
+  if (hash.forceNumber) {
+    if (typeof left !== "number") {
+      left = Number(left);
+    }
+    if (typeof right !== "number") {
+      right = Number(right);
+    }
+  }
+  return left > right;
+}
+
+export default Helper.helper(gt);
diff --git a/app/assets/javascripts/truth-helpers/addon/helpers/gte.js b/app/assets/javascripts/truth-helpers/addon/helpers/gte.js
new file mode 100644
index 0000000..e364dc3
--- /dev/null
+++ b/app/assets/javascripts/truth-helpers/addon/helpers/gte.js
@@ -0,0 +1,15 @@
+import Helper from "@ember/component/helper";
+
+export function gte([left, right], hash) {
+  if (hash.forceNumber) {
+    if (typeof left !== "number") {
+      left = Number(left);
+    }
+    if (typeof right !== "number") {
+      right = Number(right);
+    }
+  }
+  return left >= right;
+}
+
+export default Helper.helper(gte);
diff --git a/app/assets/javascripts/truth-helpers/addon/helpers/lt.js b/app/assets/javascripts/truth-helpers/addon/helpers/lt.js
new file mode 100644
index 0000000..0546f79
--- /dev/null
+++ b/app/assets/javascripts/truth-helpers/addon/helpers/lt.js
@@ -0,0 +1,15 @@
+import Helper from "@ember/component/helper";
+
+export function lt([left, right], hash) {
+  if (hash.forceNumber) {
+    if (typeof left !== "number") {
+      left = Number(left);
+    }
+    if (typeof right !== "number") {
+      right = Number(right);
+    }
+  }
+  return left < right;
+}
+
+export default Helper.helper(lt);
diff --git a/app/assets/javascripts/truth-helpers/addon/helpers/lte.js b/app/assets/javascripts/truth-helpers/addon/helpers/lte.js
new file mode 100644
index 0000000..375a15e
--- /dev/null
+++ b/app/assets/javascripts/truth-helpers/addon/helpers/lte.js
@@ -0,0 +1,15 @@
+import Helper from "@ember/component/helper";
+
+export function lte([left, right], hash) {
+  if (hash.forceNumber) {
+    if (typeof left !== "number") {
+      left = Number(left);
+    }
+    if (typeof right !== "number") {
+      right = Number(right);
+    }
+  }
+  return left <= right;
+}
+
+export default Helper.helper(lte);
diff --git a/app/assets/javascripts/truth-helpers/addon/helpers/not-eq.js b/app/assets/javascripts/truth-helpers/addon/helpers/not-eq.js
new file mode 100644
index 0000000..8971d1b
--- /dev/null
+++ b/app/assets/javascripts/truth-helpers/addon/helpers/not-eq.js
@@ -0,0 +1,7 @@
+import Helper from "@ember/component/helper";
+
+export function notEqualHelper(params) {
+  return params[0] !== params[1];
+}
+
+export default Helper.helper(notEqualHelper);
diff --git a/app/assets/javascripts/truth-helpers/addon/helpers/not.js b/app/assets/javascripts/truth-helpers/addon/helpers/not.js
new file mode 100644
index 0000000..8b7942e
--- /dev/null
+++ b/app/assets/javascripts/truth-helpers/addon/helpers/not.js
@@ -0,0 +1,13 @@
+import Helper from "@ember/component/helper";
+import truthConvert from "../utils/truth-convert";
+
+export function not(params) {
+  for (let i = 0, len = params.length; i < len; i++) {
+    if (truthConvert(params[i]) === true) {
+      return false;
+    }
+  }
+  return true;
+}
+

[... diff too long, it was truncated ...]

GitHub sha: 18777d91

This commit appears in #12667 which was approved by CvX. It was merged by jjaffeux.