DEV: introduces {{not}} helper (#12651)
Code is coming from GitHub - jmurphyau/ember-truth-helpers: Ember HTMLBars Helpers for {{if}} & {{unless}}: not, and, or, eq & is-array, for now I only ported {{not}} which would have tons of use cases in our code base.
We might want to use more helpers in the future, also Ember should have this kind of helpers natively in the future:
- rfcs/0000-add-logical-operators.md at add-logical-operators-to-templates · cibernox/rfcs · GitHub
- rfcs/0000-add-equality-operators.md at add-equality-operators-to-templates · cibernox/rfcs · GitHub
- rfcs/0561-add-numeric-comparison-operators.md at add-numeric-comparison-operators-to-templates · cibernox/rfcs · GitHub
diff --git a/app/assets/javascripts/discourse/app/helpers/not.js b/app/assets/javascripts/discourse/app/helpers/not.js
new file mode 100644
index 0000000..e1afe32
--- /dev/null
+++ b/app/assets/javascripts/discourse/app/helpers/not.js
@@ -0,0 +1,14 @@
+// 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
new file mode 100644
index 0000000..2bd0fd9
--- /dev/null
+++ b/app/assets/javascripts/discourse/app/lib/truth-convert.js
@@ -0,0 +1,16 @@
+// 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
new file mode 100644
index 0000000..3802bae
--- /dev/null
+++ b/app/assets/javascripts/discourse/tests/unit/helpers/not-test.js
@@ -0,0 +1,34 @@
+// 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]"'
+ );
+ },
+ });
+});
GitHub sha: e463f5ce