How to use the rambda.pathOr function in rambda

To help you get started, we’ve selected a few rambda examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github terascope / teraslice / packages / elasticsearch-store / src / utils / fp.ts View on Github external
R.defaultTo(500)
);

type Shard = { primary: boolean, stage: string };

export function shardsPath(index: string): (stats: any) => Shard[] {
    return R.pathOr([], [index, 'shards']);
}

export const verifyIndexShards: (shards: Shard[]) => boolean = R.pipe(
    // @ts-ignore
    R.filter((shard: Shard) => shard.primary),
    R.all((shard: Shard) => shard.stage === 'DONE')
);

export const getRolloverFrequency = R.pathOr('monthly', ['indexSchema', 'rollover_frequency']);

type indexFn = (config?: i.IndexSchema) => boolean;

export const isSimpleIndex: indexFn = R.both(
    isNotNil,
    R.both(
        R.has('mapping'),
        R.pipe(R.path('template'), R.isNil)
    )
);

export const isTemplatedIndex: indexFn = R.both(
    isNotNil,
    R.both(
        R.has('mapping'),
        R.propEq('template', true),
github terascope / teraslice / packages / elasticsearch-store / src / utils / fp.ts View on Github external
export const getErrorMessage: (error: i.ErrorLike) => string = R.pipe(
    R.ifElse(
        R.has('message'),
        R.path('message'),
        R.path('msg'),
    ),
    R.defaultTo('Unknown Error'),
);

export const getErrorMessages: (errors: i.ErrorLike[]) => string = R.pipe(
    // @ts-ignore
    R.map(getErrorMessage),
    R.join(', '),
);

export const getErrorType = R.pathOr('', ['error', 'type']);

export const getStatusCode: (error: i.ErrorLike) => number = R.pipe(
    R.ifElse(
        R.has('statusCode'),
        R.path('statusCode'),
        R.path('status')
    ),
    R.defaultTo(500)
);

type Shard = { primary: boolean, stage: string };

export function shardsPath(index: string): (stats: any) => Shard[] {
    return R.pathOr([], [index, 'shards']);
}
github terascope / teraslice / packages / elasticsearch-store / src / utils / elasticsearch.ts View on Github external
export function shardsPath(index: string): (stats: any) => i.Shard[] {
    return R.pathOr([], [index, 'shards']);
}
github terascope / teraslice / packages / elasticsearch-store / src / utils / misc.ts View on Github external
import * as R from 'rambda';

export const isNotNil = (input: any) => input != null;

export function getFirstValue(input: { [key: string]: T }): T | undefined {
    return Object.values(input)[0];
}
export function getFirstKey(input: T): (keyof T) | undefined {
    return Object.keys(input)[0] as keyof T;
}

export const getIndexMapping = R.path(['index_schema', 'mapping']);

export const getRolloverFrequency = R.pathOr('monthly', ['index_schema', 'rollover_frequency']);

export const getSchemaVersion = R.pathOr(1, ['index_schema', 'version']);
export const getSchemaVersionStr: (config: any) => string = R.pipe(
    getSchemaVersion,
    R.toString as any,
    R.prepend('s')
) as any;

export const getDataVersion = R.pathOr(1, ['version']);
export const getDataVersionStr: (config: any) => string = R.pipe(
    getDataVersion,
    R.toString as any,
    R.prepend('v')
) as any;

export const formatIndexName: (strs: (string | undefined)[]) => string = R.pipe(
github terascope / teraslice / packages / elasticsearch-store / src / utils / misc.ts View on Github external
export function getFirstKey(input: T): (keyof T) | undefined {
    return Object.keys(input)[0] as keyof T;
}

export const getIndexMapping = R.path(['index_schema', 'mapping']);

export const getRolloverFrequency = R.pathOr('monthly', ['index_schema', 'rollover_frequency']);

export const getSchemaVersion = R.pathOr(1, ['index_schema', 'version']);
export const getSchemaVersionStr: (config: any) => string = R.pipe(
    getSchemaVersion,
    R.toString as any,
    R.prepend('s')
) as any;

export const getDataVersion = R.pathOr(1, ['version']);
export const getDataVersionStr: (config: any) => string = R.pipe(
    getDataVersion,
    R.toString as any,
    R.prepend('v')
) as any;

export const formatIndexName: (strs: (string | undefined)[]) => string = R.pipe(
    R.reject((v: string) => !v) as any,
    R.map(R.trim),
    R.join('-')
);

export const buildNestPath: (paths: (string | undefined)[]) => string = R.pipe(
    R.reject((v: string) => !v) as any,
    R.map(R.trim),
    R.join('.')
github terascope / teraslice / packages / elasticsearch-store / src / utils / errors.ts View on Github external
throw error;
}

export function getErrorMessage(err: ErrorLike): string {
    const defaultErrorMsg = 'Unknown Error';
    if (err && ts.isString(err)) {
        return err;
    }

    const message: string = R.path(['message'], err) || R.pathOr(defaultErrorMsg, ['msg'], err);
    const prefix = R.path(['dataPath'], err);

    return `${prefix ? `${prefix} ` : ''}${message}`;
}

export const getErrorType = R.pathOr('', ['error', 'type']);

export const getStatusCode: (error: ErrorLike) => number = R.pipe(
    R.ifElse(R.has('statusCode'), R.path(['statusCode']), R.path(['status'])),
    R.defaultTo(500)
);

export type ErrorLike =
    | {
        message?: string;
        msg?: string;
        statusCode?: number;
        status?: number;
    }
    | ajv.ErrorObject
    | string;
github terascope / teraslice / packages / elasticsearch-store / src / utils / fp.ts View on Github external
export function shardsPath(index: string): (stats: any) => Shard[] {
    return R.pathOr([], [index, 'shards']);
}
github terascope / teraslice / packages / elasticsearch-store / src / utils / errors.ts View on Github external
export function getErrorMessage(err: ErrorLike): string {
    const defaultErrorMsg = 'Unknown Error';
    if (err && ts.isString(err)) {
        return err;
    }

    const message: string = R.path(['message'], err) || R.pathOr(defaultErrorMsg, ['msg'], err);
    const prefix = R.path(['dataPath'], err);

    return `${prefix ? `${prefix} ` : ''}${message}`;
}
github terascope / teraslice / packages / elasticsearch-store / src / utils / misc.ts View on Github external
import * as R from 'rambda';

export const isNotNil = (input: any) => input != null;

export function getFirstValue(input: { [key: string]: T }): T | undefined {
    return Object.values(input)[0];
}
export function getFirstKey(input: T): (keyof T) | undefined {
    return Object.keys(input)[0] as keyof T;
}

export const getIndexMapping = R.path(['index_schema', 'mapping']);

export const getRolloverFrequency = R.pathOr('monthly', ['index_schema', 'rollover_frequency']);

export const getSchemaVersion = R.pathOr(1, ['index_schema', 'version']);
export const getSchemaVersionStr: (config: any) => string = R.pipe(
    getSchemaVersion,
    R.toString as any,
    R.prepend('s')
) as any;

export const getDataVersion = R.pathOr(1, ['version']);
export const getDataVersionStr: (config: any) => string = R.pipe(
    getDataVersion,
    R.toString as any,
    R.prepend('v')
) as any;

export const formatIndexName: (strs: (string | undefined)[]) => string = R.pipe(
    R.reject((v: string) => !v) as any,
    R.map(R.trim),