Home Reference Source Repository
import EgoJSUtils from 'egojs/src/utils.js'
public class | source

EgoJSUtils

A set of utlity methods.

Static Method Summary

Static Public Methods
public static

mergeObjects(objects: ...Object): Object

It will merge a given list of Objects into a new one.

public static

rejectedPromise(response: *): Promise<null, *>

Returns an already rejected promise.

public static

Wraps a Request call into a Promise.

public static

resolvedPromise(response: *): Promise<*, null>

Returns an already resolved promise.

Static Public Methods

public static mergeObjects(objects: ...Object): Object source

It will merge a given list of Objects into a new one. It works recursively, so any "sub objects" will also be merged. This method returns a new Object, so none of the targets will be modified.

Params:

NameTypeAttributeDescription
objects ...Object

The list of objects to merge.

Return:

Object

A new object with the merged properties.

Example:

const a = {
    b: 'c',
    d: {
        e: 'f',
        g: {
            h: ['i'],
        },
    },
    j: 'k',
};
const b = {
    j: 'key',
    d: {
        g: {
            h: ['x', 'y', 'z'],
            l: 'm',
        },
    },
};
// The result will be
// {
//     b: 'c',
//     d: {
//         e: 'f',
//         g: {
//             h: ['x', 'y', 'z'],
//             l: 'm',
//         },
//     },
//     j: 'key',
// }
._mergeObjects(a, b);

public static rejectedPromise(response: *): Promise<null, *> source

Returns an already rejected promise.

Params:

NameTypeAttributeDescription
response *

The object to send to the .catch method.

Return:

Promise<null, *>

This promise won't call .then but .catch directly.

Example:

EgoJSUtils.rejectedPromise('error message').catch((e) => {
    // It will log 'error message'
    console.log(e);
});

public static request(data: Object): Promise<Object, Error> source

Wraps a Request call into a Promise.

Params:

NameTypeAttributeDescription
data Object

The request settings. The same you would use with request().

Return:

Promise<Object, Error>

It will be resolved or rejected depending on the response.

Example:

request({uri: 'https://homer0.com/rosario'})
.then((response) => doSomething(response))
.catch((err) => handleErrors(err));

public static resolvedPromise(response: *): Promise<*, null> source

Returns an already resolved promise.

Params:

NameTypeAttributeDescription
response *

The object to send to the .then method.

Return:

Promise<*, null>

This promise won't call .catch.

Example:

EgoJSUtils.rejectedPromise('hello world').then((message) => {
    // It will log 'hello world'
    console.log(message);
});