Skip to content
This repository has been archived by the owner on Jun 18, 2019. It is now read-only.

Commit

Permalink
feat(Config): adds some new methods: set, get, remove, has
Browse files Browse the repository at this point in the history
  • Loading branch information
Marat Dreizin authored and mdreizin committed Jun 10, 2016
1 parent 20676cc commit d61b2eb
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 2 deletions.
49 changes: 47 additions & 2 deletions src/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import {
isFunction,
isObject,
defaultsDeep,
mergeWith
mergeWith,
set,
unset,
get,
has
} from 'lodash';
import ConfigLoader from './ConfigLoader';
import ConfigTransform from './ConfigTransform';
Expand Down Expand Up @@ -228,7 +232,7 @@ class Config {
const properties = {};

for (const [key, value] of Object.entries(this)) {
if (this.hasOwnProperty(key)) {
if (this.has(key)) {
properties[key] = value;
}
}
Expand All @@ -238,6 +242,47 @@ class Config {
return properties;
}

/**
* Sets `value` at `path`
* @param {String} path
* @param {*} value
* @return {Config}
*/
set(path, value) {
set(this, path, value);

return this;
}

/**
* Gets `value` at `path`
* @param {String} path
* @return {*}
*/
get(path) {
return get(this, path);
}

/**
* Removes `value` at `path`
* @param {String} path
* @return {Config}
*/
remove(path) {
unset(this, path);

return this;
}

/**
* Checks if `value` exist at `path`
* @param {String} path
* @return {Boolean}
*/
has(path) {
return has(this, path);
}

/**
* Initializes new {@link Config} with specific `values`
* @param {...Object} values
Expand Down
48 changes: 48 additions & 0 deletions test/Config.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,54 @@ describe('Config', () => {
});
});

describe('#set()', () => {
it('should add `value` at `path`', () => {
config.set('foo', {
bar: 'bar1'
});

expect(config.toObject()).toEqual({
foo: {
bar: 'bar1'
}
});
});
});

describe('#remove()', () => {
it('should remove `value` at `path`', () => {
config.merge({
foo: 'foo1'
}).remove('foo');

expect(config.toObject()).toEqual({});
});
});

describe('#get()', () => {
it('should get `value` at `path`', () => {
config.merge({
foo: 'foo1'
});

expect(config.get('foo')).toEqual('foo1');
});
});

describe('#has()', () => {
it('should return `true` if `path` exist', () => {
config.merge({
foo: 'foo1'
});

expect(config.has('foo')).toEqual(true);
});

it('should return `false` if `path` absent', () => {
expect(config.has('foo')).toEqual(false);
});
});

describe('.initWith()', () => {
it('should return `Config`', () => {
config = Config.initWith({
Expand Down

0 comments on commit d61b2eb

Please sign in to comment.