# all files / keystone/lib/list/ expandSort.js

16% Statements 4/25
0% Branches 0/14
0% Functions 0/4
16.67% Lines 4/24

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    421×

    1×

    1×

    1×
     var listToArray = require('list-to-array');

    function truthy (i) { return i; }

    function expandSort (input) {
    	var fields = this.fields;
    	var sort = {
    		rawInput: input || this.defaultSort,
    		isDefaultSort: false,
    	};
    	sort.input = sort.rawInput;
    	if (sort.input === '__default__') {
    		sort.isDefaultSort = true;
    		sort.input = this.sortable ? 'sortOrder' : this.namePath;
    	}
    	sort.paths = listToArray(sort.input).map(function (path) {
    		var invert = false;
    		if (path.charAt(0) === '-') {
    			invert = true;
    			path = path.substr(1);
    		}
    		var field = fields[path];
    		if (!field) {
    			return;
    		}
    		return {
    			field: field,
    			invert: invert,
    			path: field.path,
    		};
    	}).filter(truthy);
    	sort.string = sort.paths.map(function (i) {
    		if (i.field.getSortString) {
    			return i.field.getSortString(i);
    		}
    		return i.invert ? '-' + i.path : i.path;
    	}).join(' ');
    	return sort;
    }

    module.exports = expandSort;