improve: filter as a plugin (#4255)
This commit is contained in:
@@ -21,7 +21,8 @@ export default class Operations extends React.Component {
|
|||||||
layoutActions: PropTypes.object.isRequired,
|
layoutActions: PropTypes.object.isRequired,
|
||||||
authActions: PropTypes.object.isRequired,
|
authActions: PropTypes.object.isRequired,
|
||||||
authSelectors: PropTypes.object.isRequired,
|
authSelectors: PropTypes.object.isRequired,
|
||||||
getConfigs: PropTypes.func.isRequired
|
getConfigs: PropTypes.func.isRequired,
|
||||||
|
fn: PropTypes.func.isRequired
|
||||||
};
|
};
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
@@ -30,7 +31,8 @@ export default class Operations extends React.Component {
|
|||||||
getComponent,
|
getComponent,
|
||||||
layoutSelectors,
|
layoutSelectors,
|
||||||
layoutActions,
|
layoutActions,
|
||||||
getConfigs
|
getConfigs,
|
||||||
|
fn
|
||||||
} = this.props
|
} = this.props
|
||||||
|
|
||||||
let taggedOps = specSelectors.taggedOperations()
|
let taggedOps = specSelectors.taggedOperations()
|
||||||
@@ -52,9 +54,7 @@ export default class Operations extends React.Component {
|
|||||||
|
|
||||||
if (filter) {
|
if (filter) {
|
||||||
if (filter !== true) {
|
if (filter !== true) {
|
||||||
taggedOps = taggedOps.filter((tagObj, tag) => {
|
taggedOps = fn.opsFilter(taggedOps, filter)
|
||||||
return tag.indexOf(filter) !== -1
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
9
src/core/plugins/filter/index.js
Normal file
9
src/core/plugins/filter/index.js
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
import opsFilter from "./opsFilter"
|
||||||
|
|
||||||
|
export default function() {
|
||||||
|
return {
|
||||||
|
fn: {
|
||||||
|
opsFilter
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
3
src/core/plugins/filter/opsFilter.js
Normal file
3
src/core/plugins/filter/opsFilter.js
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
export default function(taggedOps, phrase) {
|
||||||
|
return taggedOps.filter((tagObj, tag) => tag.indexOf(phrase) !== -1)
|
||||||
|
}
|
||||||
@@ -12,6 +12,7 @@ import SplitPaneModePlugin from "core/plugins/split-pane-mode"
|
|||||||
import downloadUrlPlugin from "core/plugins/download-url"
|
import downloadUrlPlugin from "core/plugins/download-url"
|
||||||
import configsPlugin from "core/plugins/configs"
|
import configsPlugin from "core/plugins/configs"
|
||||||
import deepLinkingPlugin from "core/plugins/deep-linking"
|
import deepLinkingPlugin from "core/plugins/deep-linking"
|
||||||
|
import filter from "core/plugins/filter"
|
||||||
|
|
||||||
import OperationContainer from "core/containers/OperationContainer"
|
import OperationContainer from "core/containers/OperationContainer"
|
||||||
|
|
||||||
@@ -152,6 +153,7 @@ export default function() {
|
|||||||
ast,
|
ast,
|
||||||
SplitPaneModePlugin,
|
SplitPaneModePlugin,
|
||||||
downloadUrlPlugin,
|
downloadUrlPlugin,
|
||||||
deepLinkingPlugin
|
deepLinkingPlugin,
|
||||||
|
filter
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
25
test/core/plugins/filter/opsFilter.js
Normal file
25
test/core/plugins/filter/opsFilter.js
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
import { Map } from "immutable"
|
||||||
|
import opsFilter from "corePlugins/filter/opsFilter"
|
||||||
|
import expect from "expect"
|
||||||
|
|
||||||
|
describe("opsFilter", function() {
|
||||||
|
const taggedOps = Map([["pet"], ["store"], ["user"]])
|
||||||
|
|
||||||
|
it("should filter taggedOps by tag name", function () {
|
||||||
|
const filtered = opsFilter(taggedOps, "sto")
|
||||||
|
|
||||||
|
expect(filtered.size).toEqual(1)
|
||||||
|
})
|
||||||
|
|
||||||
|
it("should return all taggedOps when search phrase is empty", function () {
|
||||||
|
const filtered = opsFilter(taggedOps, "")
|
||||||
|
|
||||||
|
expect(filtered.size).toEqual(taggedOps.size)
|
||||||
|
})
|
||||||
|
|
||||||
|
it("should return empty result when there is no match", function () {
|
||||||
|
const filtered = opsFilter(taggedOps, "NoMatch")
|
||||||
|
|
||||||
|
expect(filtered.size).toEqual(0)
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -5,6 +5,7 @@ import System from "core/system"
|
|||||||
import { fromJS } from "immutable"
|
import { fromJS } from "immutable"
|
||||||
import { render } from "enzyme"
|
import { render } from "enzyme"
|
||||||
import ViewPlugin from "core/plugins/view/index.js"
|
import ViewPlugin from "core/plugins/view/index.js"
|
||||||
|
import filterPlugin from "core/plugins/filter/index.js"
|
||||||
import { connect, Provider } from "react-redux"
|
import { connect, Provider } from "react-redux"
|
||||||
|
|
||||||
describe("bound system", function(){
|
describe("bound system", function(){
|
||||||
@@ -264,6 +265,22 @@ describe("bound system", function(){
|
|||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe("fn", function() {
|
||||||
|
|
||||||
|
it("should return helper functions", function () {
|
||||||
|
// Given
|
||||||
|
const system = new System({
|
||||||
|
plugins: [
|
||||||
|
filterPlugin
|
||||||
|
]
|
||||||
|
})
|
||||||
|
|
||||||
|
// When
|
||||||
|
const fn = system.getSystem().fn.opsFilter
|
||||||
|
expect(typeof fn).toEqual("function")
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
describe("selectors", function(){
|
describe("selectors", function(){
|
||||||
|
|
||||||
it("should have the first arg be the nested state, and all other args to follow", function(){
|
it("should have the first arg be the nested state, and all other args to follow", function(){
|
||||||
|
|||||||
Reference in New Issue
Block a user