Demo entry 1487118

namespace init in CS

   

Submitted by Jesus Franco on Apr 17, 2015 at 17:46
Language: CoffeeScript. Code size: 1.4 kB.

extend = (inject, template) ->
  (template[prop] = inject[prop]) for own prop of inject
  null

init_namespace = (nameString, obj, container) ->
    container ?= window
    names = nameString.split('.')
    (container[name] or= {} ;
    container = container[name]) while name = names.shift()
    extend(obj, container)

describe "Namespace DRY initializer", ->
    describe "A simple name init", ->
        it "a simple name should be available in global object", ->
            init_namespace "simple"
            expect(window.simple).toBeDefined()

    describe "A two levels nested name path", ->
        beforeEach ->
            window.simple = null
            init_namespace "simple.nested"

        it "makes available its top name in container", ->
            expect(window.simple).toBeDefined()
        it "makes available its child name in container", ->
            expect(window.simple.nested).toBeDefined()

    describe "A custom passed object", ->
        it "set its properties to ns' deepest child", ->
            window.simple = null
            init_namespace("simple.nested.ns", 
                      custom: true
                      queryable: false
                )
            ns=window.simple.nested.ns
            expect(ns.custom).toBeTruthy()
            expect(ns.queryable).toBeFalsy()

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).