Sha256: 89e77ca0cf4d7b1c5b9e688a4a273dacd578abd9cabc6b8cc5c2e6e49004b129

Contents?: true

Size: 1.61 KB

Versions: 19

Compression:

Stored size: 1.61 KB

Contents

const PATH_SEPARATOR = '.';

/**
 * Retreive the nested value at `path`.
 * @param {Object} object The object to search
 * @param {string} path The period-delimited path to traverse
 * @return {Object} An object of the structure { ok: boolean, value: any }
 */
function dig(object, path) {
    const ret = {
        ok: false,
        value: undefined,
    };
    const keys = path.split(PATH_SEPARATOR);
    let k;
    let value = object;

    /* eslint-disable no-cond-assign */
    while (k = keys.shift()) {
        value = value[k];

        if (typeof value === 'undefined') {
            return ret;
        }
    }

    ret.ok = true;
    ret.value = value;

    return ret;
}

export default class VConfig {
    constructor(config = {}) {
        this.config = config;
    }

    /**
     * Retrieve the entire configuration object.
     * @return {Object}
     */
    all() {
        return this.config;
    }

    /**
     * Determine whether `key` is present.
     * @param {string} key [description]
     * @return {Boolean} [description]
     */
    has(key) {
        return dig(this.config, key).ok;
    }

    /**
     * Retrieve the nested value at `key`. If any keys during traversal do
     * not exist, `fallback` is returned.
     * @param {string} key The period-delimited path to traverse
     * @param {*} fallback
     * @return {*}
     */
    get(key, fallback) {
        if (!this.has(key)) {
            return fallback;
        }

        const keys = key.split(PATH_SEPARATOR);
        const last = keys.pop();
        const obj = dig(this.config, keys.join(PATH_SEPARATOR)).value;

        return obj[last];
    }
}

Version data entries

19 entries across 19 versions & 2 rubygems

Version Path
coprl-3.0.0.beta.12 views/mdc/assets/js/utils/config.js
coprl-3.0.0.beta.11 views/mdc/assets/js/utils/config.js
coprl-3.0.0.beta.10 views/mdc/assets/js/utils/config.js
coprl-3.0.0.beta.9 views/mdc/assets/js/utils/config.js
coprl-3.0.0.beta.8 views/mdc/assets/js/utils/config.js
coprl-3.0.0.beta.7 views/mdc/assets/js/utils/config.js
coprl-3.0.0.beta.6 views/mdc/assets/js/utils/config.js
coprl-3.0.0.beta.5 views/mdc/assets/js/utils/config.js
voom-presenters-2.1.2 views/mdc/assets/js/utils/config.js
coprl-3.0.0.beta.4 views/mdc/assets/js/utils/config.js
coprl-3.0.0.beta.3 views/mdc/assets/js/utils/config.js
coprl-3.0.0.beta.2 views/mdc/assets/js/utils/config.js
coprl-3.0.0.beta.1 views/mdc/assets/js/utils/config.js
voom-presenters-2.1.0 views/mdc/assets/js/utils/config.js
voom-presenters-2.0.3 views/mdc/assets/js/utils/config.js
voom-presenters-2.0.2 views/mdc/assets/js/utils/config.js
voom-presenters-2.0.1 views/mdc/assets/js/utils/config.js
voom-presenters-2.0.0 views/mdc/assets/js/utils/config.js
voom-presenters-0.2.0 views/mdc/assets/js/utils/config.js