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: https://github.com/jmurphyau/ember-truth-helpers

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.
This commit is contained in:
Joffrey JAFFEUX 2021-04-12 11:40:00 +02:00 committed by GitHub
parent 094c9e5ea3
commit 18777d9108
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 267 additions and 37 deletions

View File

@ -1,3 +1,4 @@
//= require_tree ./truth-helpers/addon
//= require_tree ./discourse-common/addon
//= require ./polyfills
//= require_tree ./select-kit/addon

View File

@ -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]"'
);
},
});
});

View File

@ -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);

View File

@ -0,0 +1,7 @@
import Helper from "@ember/component/helper";
export function eq(params) {
return params[0] === params[1];
}
export default Helper.helper(eq);

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -0,0 +1,7 @@
import Helper from "@ember/component/helper";
export function notEqualHelper(params) {
return params[0] !== params[1];
}
export default Helper.helper(notEqualHelper);

View File

@ -1,6 +1,5 @@
// 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";
import truthConvert from "../utils/truth-convert";
export function not(params) {
for (let i = 0, len = params.length; i < len; i++) {

View File

@ -0,0 +1,13 @@
import Helper from "@ember/component/helper";
import truthConvert from "../utils/truth-convert";
export function or(params) {
for (let i = 0, len = params.length; i < len; i++) {
if (truthConvert(params[i]) === true) {
return params[i];
}
}
return params[params.length - 1];
}
export default Helper.helper(or);

View File

@ -1,4 +1,3 @@
// https://github.com/jmurphyau/ember-truth-helpers/blob/master/addon/utils/truth-convert.js
import { isArray } from "@ember/array";
import { get } from "@ember/object";

View File

@ -0,0 +1,89 @@
"use strict";
const getChannelURL = require("ember-source-channel-url");
module.exports = async function () {
return {
useYarn: true,
scenarios: [
{
name: "ember-lts-3.8",
npm: {
devDependencies: {
"ember-source": "~3.8.0",
},
},
},
{
name: "ember-lts-3.12",
npm: {
devDependencies: {
"ember-source": "~3.12.0",
},
},
},
{
name: "ember-release",
npm: {
devDependencies: {
"ember-source": await getChannelURL("release"),
},
},
},
{
name: "ember-beta",
npm: {
devDependencies: {
"ember-source": await getChannelURL("beta"),
},
},
},
{
name: "ember-canary",
npm: {
devDependencies: {
"ember-source": await getChannelURL("canary"),
},
},
},
// The default `.travis.yml` runs this scenario via `yarn test`,
// not via `ember try`. It's still included here so that running
// `ember try:each` manually or from a customized CI config will run it
// along with all the other scenarios.
{
name: "ember-default",
npm: {
devDependencies: {},
},
},
{
name: "ember-default-with-jquery",
env: {
EMBER_OPTIONAL_FEATURES: JSON.stringify({
"jquery-integration": true,
}),
},
npm: {
devDependencies: {
"@ember/jquery": "^0.5.1",
},
},
},
{
name: "ember-classic",
env: {
EMBER_OPTIONAL_FEATURES: JSON.stringify({
"application-template-wrapper": true,
"default-async-observers": false,
"template-only-glimmer-components": false,
}),
},
npm: {
ember: {
edition: "classic",
},
},
},
],
};
};

View File

@ -0,0 +1,5 @@
"use strict";
module.exports = function (/* environment, appConfig */) {
return {};
};

View File

@ -0,0 +1,9 @@
"use strict";
const EmberAddon = require("ember-cli/lib/broccoli/ember-addon");
module.exports = function (defaults) {
let app = new EmberAddon(defaults, {});
return app.toTree();
};

View File

@ -0,0 +1,9 @@
"use strict";
module.exports = {
name: require("./package").name,
options: {},
isDevelopingAddon() {
return true;
},
};

View File

@ -0,0 +1,52 @@
{
"name": "truth-helpers",
"version": "1.0.0",
"description": "Partial copy of ember truth helpers",
"author": "Discourse",
"license": "GPL-2.0-only",
"keywords": [
"ember-addon"
],
"repository": "",
"scripts": {
"build": "ember build",
"lint:hbs": "ember-template-lint .",
"lint:js": "eslint .",
"start": "ember serve"
},
"dependencies": {
"ember-cli-babel": "^7.13.0",
"ember-cli-htmlbars": "^4.2.0",
"ember-auto-import": "^1.5.3"
},
"devDependencies": {
"@ember/optional-features": "^1.1.0",
"@glimmer/component": "^1.0.0",
"babel-eslint": "^10.0.3",
"broccoli-asset-rev": "^3.0.0",
"ember-cli": "~3.15.2",
"ember-cli-dependency-checker": "^3.2.0",
"ember-cli-eslint": "^5.1.0",
"ember-cli-inject-live-reload": "^2.0.1",
"ember-cli-sri": "^2.1.1",
"ember-cli-template-lint": "^1.0.0-beta.3",
"ember-cli-uglify": "^3.0.0",
"ember-disable-prototype-extensions": "^1.1.3",
"ember-export-application-global": "^2.0.1",
"ember-load-initializers": "^2.1.1",
"ember-maybe-import-regenerator": "^0.1.6",
"ember-resolver": "^7.0.0",
"ember-source": "~3.15.0",
"ember-source-channel-url": "^2.0.1",
"ember-try": "^1.4.0",
"eslint-plugin-ember": "^7.7.1",
"eslint-plugin-node": "^10.0.0",
"loader.js": "^4.7.0"
},
"engines": {
"node": "8.* || >= 10.*"
},
"ember": {
"edition": "octane"
}
}

View File

@ -1,3 +1,4 @@
//= require_tree ./truth-helpers/addon
//= require_tree ./discourse-common/addon
//= require_tree ./select-kit/addon
//= require wizard/router