UNPKG

@spalger/kibana

Version:

Kibana is an open source (Apache Licensed), browser based analytics and search dashboard for Elasticsearch. Kibana is a snap to setup and start using. Kibana strives to be easy to get started with, while also being flexible and powerful, just like Elastic

45 lines (35 loc) 1.19 kB
define(function (require) { var _ = require('lodash'); /** * Take a sorting array and make it into an object * @param {array} 2 item array [fieldToSort, directionToSort] * @param {object} indexPattern used for determining default sort * @returns {object} a sort object suitable for returning to elasticsearch */ function getSort(sort, indexPattern) { var sortObj = {}; var field; var direction; function isSortable(field) { return (indexPattern.fields.byName[field] && indexPattern.fields.byName[field].sortable); } if (_.isArray(sort) && sort.length === 2 && isSortable(sort[0])) { // At some point we need to refact the sorting logic, this array sucks. field = sort[0]; direction = sort[1]; } else if (indexPattern.timeFieldName && isSortable(indexPattern.timeFieldName)) { field = indexPattern.timeFieldName; direction = 'desc'; } if (field) { sortObj[field] = direction; } else { sortObj._score = 'desc'; } return sortObj; } getSort.array = function (sort, indexPattern) { return _(getSort(sort, indexPattern)).pairs().pop(); }; return getSort; });