diff --git a/__tests__/NamespaceSpec.js b/__tests__/NamespaceSpec.js new file mode 100644 index 00000000..e5b8a793 --- /dev/null +++ b/__tests__/NamespaceSpec.js @@ -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); + }); +}); diff --git a/namespace.js b/namespace.js index 16ec4203..2cbc00d5 100644 --- a/namespace.js +++ b/namespace.js @@ -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();