How to use the rambda.defaultTo 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 selfrefactor / rambda / _typings_tests / defaultTo-spec.ts View on Github external
it('happy with curry', () => {
    const fn = defaultTo('foo')
    const x = fn(undefined, 'bar', null); // $ExpectType string
    x // $ExpectType string
    const y = fn(undefined); // $ExpectType string
    y // $ExpectType string
  });
github selfrefactor / rambda / _typings_tests / defaultTo-spec.ts View on Github external
it('with two types', () => {
    const x = defaultTo('foo',undefined, 1, null, 2, 'bar'); // $ExpectType string | number
    x // $ExpectType string | number
  });
});
github selfrefactor / rambda / _typings_tests / defaultTo-spec.ts View on Github external
it('with one type', () => {
    const x = defaultTo('foo','bar'); // $ExpectType string
    x // $ExpectType string
  });
  it('with two types', () => {
github selfrefactor / rambda / _typings_tests / defaultTo-spec.ts View on Github external
it('fallback', () => {
    const x = defaultTo('foo',undefined); // $ExpectType "foo"
    x // $ExpectType "foo"
    const y = defaultTo('foo','bar'); // $ExpectType "foo" | "bar"
    y // $ExpectType "foo" | "bar"
  });
  it('with one type', () => {
github selfrefactor / rambda / _typings_tests / defaultTo-spec.ts View on Github external
it('with two types', () => {
    const x = defaultTo('foo',1); // $ExpectType string | number
    x // $ExpectType string | number
  });
});
github selfrefactor / rambda / _typings_tests / defaultTo-spec.ts View on Github external
it('happy', () => {
    const x = defaultTo('foo',undefined, 'bar'); // $ExpectType string
    x // $ExpectType string
  });
github selfrefactor / rambda / _typings_tests / defaultTo-spec.ts View on Github external
it('fallback', () => {
    const x = defaultTo('foo',undefined); // $ExpectType "foo"
    x // $ExpectType "foo"
    const y = defaultTo('foo','bar'); // $ExpectType "foo" | "bar"
    y // $ExpectType "foo" | "bar"
  });
  it('with one type', () => {
github terascope / teraslice / packages / elasticsearch-store / src / utils / fp.ts View on Github external
export function getTimeByField(field: string = ''): (input: any) => number {
    return R.ifElse(
        R.has(field),
        R.pipe(R.path(field), (input: any) => new Date(input).getTime()),
        () => Date.now()
    );
}

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')
    ),
github terascope / teraslice / packages / elasticsearch-store / src / utils / fp.ts View on Github external
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']);
}

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']);
github terascope / teraslice / packages / elasticsearch-store / src / utils / errors.ts View on Github external
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;