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

Commit

Permalink
feat(namespace): added set method to ns
Browse files Browse the repository at this point in the history
  • Loading branch information
mjancarik committed Jun 15, 2019
1 parent fad84b6 commit a9d36b1
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
30 changes: 30 additions & 0 deletions __tests__/NamespaceSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Namespace } from '../Namespace';

describe('Namespace', () => {
let ns = null;
let path = 'a.b.c.ClassConstructor';
class ClassConstructor {}

beforeEach(() => {
ns = new Namespace();
ns.namespace(path);
});

it('should create defined namespace', () => {
expect(ns.a.b.c.ClassConstructor).toEqual({});
});

it('should return stored value in namespace', () => {
expect(ns.get(path)).toEqual({});
});

it('should return true if namespace exists', () => {
expect(ns.has(path)).toEqual(true);
});

it('should set value for defined namespace', () => {
ns.set(path, ClassConstructor);

expect(ns.a.b.c.ClassConstructor).toEqual(ClassConstructor);
});
});
14 changes: 14 additions & 0 deletions namespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,20 @@ export class Namespace {

return self;
}

/**
* Set value for the specified namespace path point.
*
* @param {string} path The namespace path to set.
* @param {*} value
*/
set(path, value) {
let levels = path.split('.');
const lastKey = levels.pop();
let namespace = this.namespace(levels.join('.'));

namespace[lastKey] = value;
}
}

export default new Namespace();

0 comments on commit a9d36b1

Please sign in to comment.