Skip to content

Instantly share code, notes, and snippets.

@Maxim-Filimonov
Last active September 7, 2015 07:38
Show Gist options
  • Save Maxim-Filimonov/fdc1b5ad7375c7c98bc2 to your computer and use it in GitHub Desktop.
Save Maxim-Filimonov/fdc1b5ad7375c7c98bc2 to your computer and use it in GitHub Desktop.
filterMenuItems = function (menuItems, level, parentId) {
var childLevel = level + 1;
// filter out admin-only items if needed
if (!Users.is.admin(Meteor.user())) {
menuItems = _.reject(menuItems, function (item) {
return item.adminOnly;
});
}
menuItems = _.filter(menuItems, function (item) {
if (level === 0) {
// if this is the root level, return elements with no parentId
return typeof item.parentId === "undefined";
} else {
// else, return elements with the correct parentId
return item.parentId === parentId;
}
});
// decorate child item with their level
menuItems = _.map(menuItems, function (item) {
item.level = childLevel;
return item;
});
return menuItems;
};
var _ = require('ramda');
// Utils
trace = _.curry(function (tag, x) {
console.log(tag, x);
return x;
});
var byAdminOnly = function (isAdmin, item) {
trace("admin", isAdmin());
return _.propEq('adminOnly', isAdmin(), item);
};
var filterAdminOnly = _.filter(byAdminOnly);
var isRootLevel = function (level) {
return level == 0;
};
var itemsWithoutParent = function (item) {
return typeof item.parentId === "undefined";
};
var itemsForSpecificParent = _.propEq('parentId');
var filterByParent = function (conditions, items) {
return _.filter(_.ifElse(isRootLevel(conditions.level), itemsWithoutParent, itemsForSpecificParent(conditions.parentId)));
};
function _filterMenuItems(conditions) {
return _.compose(filterByParent(conditions),filterAdminOnly(conditions));
}
function filterMenuItems(menuItems, level, parentId, isAdmin) {
if (!isAdmin) {
isAdmin = function () {
Users.is.admin(Meteor.user())
};
}
return _filterMenuItems({
level: level, parentId: parentId, isAdmin: isAdmin
})(menuItems);
}
module.exports = {
filterMenuItems: filterMenuItems,
filterAdminOnly: filterAdminOnly,
filterByParent: filterByParent
};
var expect = require("chai").expect;
var _ = require('ramda');
var filters = require("../ramda_version.js");
var filterMenuItems = filters.filterMenuItems;
var filterAdminOnly = filters.filterAdminOnly;
var filterByParent = filters.filterByParent;
describe("filterItems", function () {
var expectedItems = function (expected, actual) {
expected = _.append(expected, []);
expect(actual).to.deep.include.members(expected);
};
it("returns empty array for empty array", function () {
expect(filterMenuItems([],0, 0)).to.be.empty;
});
it("filters admin only items for non admin", function () {
items = filterAdminOnly(_.always(false),[{adminOnly: true}, {adminOnly: false}]);
expectedItems({adminOnly: false}, items);
});
it("shows admin only items for admin", function () {
items = filterAdminOnly(_.always(true), [{adminOnly: true}, {adminOnly: false}]);
expectedItems({adminOnly: true}, items);
});
it("filter items from child level for root level", function () {
items = filterByParent([{parentId: 1}, {id: 'no_parent'}], {level: 0});
expectedItems({id: 'no_parent'}, items);
})
});
@Svetixbot
Copy link

what is the purpose of setting up the -parentId and item.level? Why your menuItems don't have those properties set up by default?

@Maxim-Filimonov
Copy link
Author

Don't know ask telescope developers :D , I'm just refactoring it

@Svetixbot
Copy link

can you provide a test for this?

@Svetixbot
Copy link

where is filterItemsBasedOnParent?

@Svetixbot
Copy link

Can you inline this:
if (!isAdmin) {
isAdmin = function () {
Users.is.admin(Meteor.user())
};
}

into isAdmin: Users.is.admin(Meteor.user())

@Maxim-Filimonov
Copy link
Author

added specs and fixed name

@Maxim-Filimonov
Copy link
Author

i can't because it's ability to override isAdmin function. filterMenuItems is unpure version maybe I should be testing from _filterMenuItems instead.
I'm trying to keep the old interface but separate all unpure parts from _filterMenuItems

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment