UNPKG

dtable-utils

Version:

dtable common utils

41 lines (36 loc) 1.26 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var sort = require('../../constants/sort.js'); /** * Sort single-select * @param {string} leftOptionId the id of option * @param {string} rightOptionId * @param {string} sort_type e.g. 'up' | 'down' * @param {object} option_id_index_map e.g. { [option.id]: 0, ... } * @returns number */ var sortSingleSelect = function sortSingleSelect(leftOptionId, rightOptionId, _ref) { var sort_type = _ref.sort_type, option_id_index_map = _ref.option_id_index_map; var currentOptionIdIndex = option_id_index_map[leftOptionId]; var nextOptionIdIndex = option_id_index_map[rightOptionId]; var emptyLeftOptionId = !currentOptionIdIndex && currentOptionIdIndex !== 0; var emptyRightOptionId = !nextOptionIdIndex && nextOptionIdIndex !== 0; if (emptyLeftOptionId && emptyRightOptionId) { return 0; } if (emptyLeftOptionId) { return 1; } if (emptyRightOptionId) { return -1; } if (currentOptionIdIndex > nextOptionIdIndex) { return sort_type === sort.SORT_TYPE.UP ? 1 : -1; } if (currentOptionIdIndex < nextOptionIdIndex) { return sort_type === sort.SORT_TYPE.UP ? -1 : 1; } return 0; }; exports.sortSingleSelect = sortSingleSelect;