All files / lib/commands BITFIELD.ts

100% Statements 15/15
100% Branches 8/8
100% Functions 1/1
100% Lines 15/15

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 58 59 60 61 62 63 64 65 66 671x   1x   1x                                           1x 7x   7x 2x             7x 2x               7x 2x               7x 2x           7x     1x  
import { transformReplyNumber } from './generic-transformers';
 
export const FIRST_KEY_INDEX = 1;
 
export const IS_READ_ONLY = true;
 
type BitFieldType = string; // TODO 'i[1-64]' | 'u[1-63]'
 
interface BitFieldOptions {
    GET?: {
        type: BitFieldType;
        offset: number | string;
    };
    SET?: {
        type: BitFieldType;
        offset: number | string;
        value: number;
    };
    INCRBY?: {
        type: BitFieldType;
        offset: number | string;
        increment: number;
    };
    OVERFLOW?: 'WRAP' | 'SAT' | 'FAIL';
}
 
export function transformArguments(key: string, options?: BitFieldOptions) {
    const args = ['BITFIELD', key];
 
    if (options?.GET) {
        args.push(
            'GET',
            options.GET.type,
            options.GET.offset.toString()
        );
    }
 
    if (options?.SET) {
        args.push(
            'SET',
            options.SET.type,
            options.SET.offset.toString(),
            options.SET.value.toString()
        );
    }
 
    if (options?.INCRBY) {
        args.push(
            'INCRBY',
            options.INCRBY.type,
            options.INCRBY.offset.toString(),
            options.INCRBY.increment.toString()
        );
    }
 
    if (options?.OVERFLOW) {
        args.push(
            'OVERFLOW',
            options.OVERFLOW
        );
    }
 
    return args;
}
 
export const transformReply = transformReplyNumber;