All files / lib/commands SORT.ts

100% Statements 20/20
100% Branches 14/14
100% Functions 2/2
100% Lines 20/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 581x   1x                             1x 10x   10x 2x     10x 2x             10x 3x 4x       10x 2x     10x 2x     10x 2x     10x       1x 1x    
export const FIRST_KEY_INDEX = 1;
 
export const IS_READ_ONLY = true;
 
 
interface SortOptions {
    BY?: string;
    LIMIT?: {
        offset: number;
        count: number;
    },
    GET?: string | Array<string>;
    DIRECTION?: 'ASC' | 'DESC';
    ALPHA?: true;
    STORE?: string;
}
 
export function transformArguments(key: string, options?: SortOptions): Array<string> {
    const args = ['SORT', key];
 
    if (options?.BY) {
        args.push('BY', options.BY);
    }
 
    if (options?.LIMIT) {
        args.push(
            'LIMIT',
            options.LIMIT.offset.toString(), 
            options.LIMIT.count.toString()
        );
    }
 
    if (options?.GET) {
        for (const pattern of (typeof options.GET === 'string' ? [options.GET] : options.GET)) {
            args.push('GET', pattern);
        }
    }
 
    if (options?.DIRECTION) {
        args.push(options.DIRECTION);
    }
 
    if (options?.ALPHA) {
        args.push('ALPHA');
    }
 
    if (options?.STORE) {
        args.push('STORE', options.STORE);
    }
 
    return args;
}
 
// integer when using `STORE`
export function transformReply(reply: Array<string> | number): Array<string> | number {
    return reply;
}