mirror of
https://github.com/discourse/discourse.git
synced 2024-11-26 14:23:39 +08:00
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:
parent
094c9e5ea3
commit
18777d9108
|
@ -1,3 +1,4 @@
|
|||
//= require_tree ./truth-helpers/addon
|
||||
//= require_tree ./discourse-common/addon
|
||||
//= require ./polyfills
|
||||
//= require_tree ./select-kit/addon
|
||||
|
|
|
@ -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]"'
|
||||
);
|
||||
},
|
||||
});
|
||||
});
|
0
app/assets/javascripts/truth-helpers/addon/.gitkeep
Normal file
0
app/assets/javascripts/truth-helpers/addon/.gitkeep
Normal file
13
app/assets/javascripts/truth-helpers/addon/helpers/and.js
Normal file
13
app/assets/javascripts/truth-helpers/addon/helpers/and.js
Normal 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);
|
7
app/assets/javascripts/truth-helpers/addon/helpers/eq.js
Normal file
7
app/assets/javascripts/truth-helpers/addon/helpers/eq.js
Normal 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);
|
15
app/assets/javascripts/truth-helpers/addon/helpers/gt.js
Normal file
15
app/assets/javascripts/truth-helpers/addon/helpers/gt.js
Normal 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);
|
15
app/assets/javascripts/truth-helpers/addon/helpers/gte.js
Normal file
15
app/assets/javascripts/truth-helpers/addon/helpers/gte.js
Normal 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);
|
15
app/assets/javascripts/truth-helpers/addon/helpers/lt.js
Normal file
15
app/assets/javascripts/truth-helpers/addon/helpers/lt.js
Normal 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);
|
15
app/assets/javascripts/truth-helpers/addon/helpers/lte.js
Normal file
15
app/assets/javascripts/truth-helpers/addon/helpers/lte.js
Normal 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);
|
|
@ -0,0 +1,7 @@
|
|||
import Helper from "@ember/component/helper";
|
||||
|
||||
export function notEqualHelper(params) {
|
||||
return params[0] !== params[1];
|
||||
}
|
||||
|
||||
export default Helper.helper(notEqualHelper);
|
|
@ -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++) {
|
13
app/assets/javascripts/truth-helpers/addon/helpers/or.js
Normal file
13
app/assets/javascripts/truth-helpers/addon/helpers/or.js
Normal 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);
|
|
@ -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";
|
||||
|
0
app/assets/javascripts/truth-helpers/app/.gitkeep
Normal file
0
app/assets/javascripts/truth-helpers/app/.gitkeep
Normal file
89
app/assets/javascripts/truth-helpers/config/ember-try.js
Normal file
89
app/assets/javascripts/truth-helpers/config/ember-try.js
Normal 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",
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
};
|
|
@ -0,0 +1,5 @@
|
|||
"use strict";
|
||||
|
||||
module.exports = function (/* environment, appConfig */) {
|
||||
return {};
|
||||
};
|
9
app/assets/javascripts/truth-helpers/ember-cli-build.js
Normal file
9
app/assets/javascripts/truth-helpers/ember-cli-build.js
Normal 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();
|
||||
};
|
9
app/assets/javascripts/truth-helpers/index.js
Normal file
9
app/assets/javascripts/truth-helpers/index.js
Normal file
|
@ -0,0 +1,9 @@
|
|||
"use strict";
|
||||
|
||||
module.exports = {
|
||||
name: require("./package").name,
|
||||
options: {},
|
||||
isDevelopingAddon() {
|
||||
return true;
|
||||
},
|
||||
};
|
52
app/assets/javascripts/truth-helpers/package.json
Normal file
52
app/assets/javascripts/truth-helpers/package.json
Normal 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"
|
||||
}
|
||||
}
|
|
@ -1,3 +1,4 @@
|
|||
//= require_tree ./truth-helpers/addon
|
||||
//= require_tree ./discourse-common/addon
|
||||
//= require_tree ./select-kit/addon
|
||||
//= require wizard/router
|
||||
|
|
Loading…
Reference in New Issue
Block a user