8000 Return value of extend.myNamespace intentional? by jasonkarns · Pull Request #1 · searls/extend.js · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Return value of extend.myNamespace intentional? #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file removed app/js/.gitkeep
Empty file.
9 changes: 5 additions & 4 deletions app/js/extend.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ originalExtend = window.extend
window.extend = makeExtender(window)

#sprinkle on utility functions
window.extend.myNamespace = (namespace) -> namespace.extend = makeExtender(namespace)
window.extend.myNamespace = (namespace) ->
_(namespace or {}).tap (ns) ->
ns.extend = makeExtender(ns)

window.extend.noConflict = ->
ourExtend = window.extend
window.extend = originalExtend
ourExtend
_(window.extend).tap ->
window.extend = originalExtend
90 changes: 52 additions & 38 deletions spec/extend-spec.coffee
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
window.context = window.describe

behavesLikeAnExtender = (top) ->
itBehavesLikeAnExtender = ->
describe "the names you might specify", ->
value = {}
context "passed a single identifier", ->
beforeEach ->
top.extend("panda", value)
@subject.extend("panda", value)

it "creates a single object on the top", ->
expect(top.panda).toBe(value)
expect(@subject.panda).toBe(value)

context "passed period-delimited identifiers", ->
beforeEach ->
top.extend("code.retreat", value)
@subject.extend("code.retreat", value)

it "creates an object for each identifier", ->
expect(top.code.retreat).toBe(value)
expect(@subject.code.retreat).toBe(value)

context "passed forward-slash-delimited identifiers", ->
beforeEach ->
top.extend("pants/sale/time", value)
@subject.extend("pants/sale/time", value)

it "creates an object for each identifier", ->
expect(top.pants.sale.time).toBe(value)
expect(@subject.pants.sale.time).toBe(value)

context "passed back-slash-delimited identifiers", ->
beforeEach ->
top.extend("test\\pollution\\sucks", value)
@subject.extend("test\\pollution\\sucks", value)

it "creates an object for each identifier", ->
expect(top.test.pollution.sucks).toBe(value)
expect(@subject.test.pollution.sucks).toBe(value)


context "passed a humorously deep number of identifiers", ->
beforeEach ->
top.extend("a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.$", value)
@subject.extend("a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.$", value)

it "still works", ->
expect(top.a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.$).toBe(value)
expect(@subject.a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.$).toBe(value)

describe "the stuff you might pass it", ->
name = "panda"
Expand All @@ -50,38 +50,38 @@ behavesLikeAnExtender = (top) ->

context "passed a new function", ->
10000 beforeEach ->
result = top.extend(name, func)
result = @subject.extend(name, func)

it "defines the function", ->
expect(top[name]).toBe(func)
expect(@subject[name]).toBe(func)

it "returns the function too", ->
expect(result).toBe(func)


context "passed a function when one already exists", ->
beforeEach ->
top.extend(name, func)
result = top.extend(name, func2)
@subject.extend(name, func)
result = @subject.extend(name, func2)

it "adds properties to the existing function object", ->
expect(top.panda.prop).toBe("tea")
expect(@subject.panda.prop).toBe("tea")

it "returns the existing function", ->
expect(result).toBe(func)

context "passed multiple functions", ->
beforeEach ->
result = top.extend(name, func, func2)
result = @subject.extend(name, func, func2)

it "extends the 'first' function", ->
expect(top.panda).toBe(func)
expect(@subject.panda).toBe(func)

it "returns the extended function", ->
expect(result).toBe(func)

it "adds properties to the first function object", ->
expect(top.panda.prop).toBe("tea")
expect(@subject.panda.prop).toBe("tea")


context "like objects", ->
Expand All @@ -91,19 +91,19 @@ behavesLikeAnExtender = (top) ->

context "passed a new object", ->
beforeEach ->
result = top.extend(name, obj)
result = @subject.extend(name, obj)

it "defines the object", ->
expect(top[name]).toBe(obj)
expect(@subject[name]).toBe(obj)

it "returns the object", ->
expect(result).toBe(obj)


context "passed an object when one already exists", ->
beforeEach ->
top.extend(name, obj)
result = top.extend(name,
@subject.extend(name, obj)
result = @subject.extend(name,
b: "B'"
c: "C"
)
Expand All @@ -119,8 +119,8 @@ behavesLikeAnExtender = (top) ->

context "passed multiple objects", ->
beforeEach ->
top.extend(name, obj)
result = top.extend name,
@subject.extend(name, obj)
result = @subject.extend name,
b: "B'"
c: "C"
,
Expand All @@ -139,39 +139,53 @@ behavesLikeAnExtender = (top) ->
result = undefined
context "when nothing exists", ->
beforeEach ->
result = top.extend(name)
result = @subject.extend(name)

it "returns undefined", ->
expect(result).not.toBeDefined()


context "when something already exists", ->
beforeEach ->
top.extend name, "fun!"
result = top.extend(name)
@subject.extend name, "fun!"
result = @subject.extend(name)

it "returns that something", ->
expect(result).toBe("fun!")

afterEach ->
delete top[name]
delete @subject[name]

describe ".extend", ->
behavesLikeAnExtender(window)
beforeEach ->
@subject = window
itBehavesLikeAnExtender()

describe "extend.myNamespace", ->
namespace = {}
result = undefined
beforeEach ->
result = extend.myNamespace(namespace)
@subject = {}
@result = undefined

context "with an existing namespace object", ->
beforeEach ->
@result = extend.myNamespace(@subject)

itBehavesLikeAnExtender()

it "it adds an 'extend' function to an arbitrary object", ->
expect(@subject.extend).toBeDefined()

it "returns the namespace object", ->
expect(@result).toBe(@subject)

it "it adds an 'extend' function to an arbitrary object", ->
expect(namespace.extend).toBeDefined()
context "without an existing namespace object", ->
beforeEach ->
@subject = extend.myNamespace()

it "returns the new extend method", ->
expect(result).toBe(namespace.extend)
itBehavesLikeAnExtender()

behavesLikeAnExtender(namespace)
it "it adds an 'extend' function to a new namespace object", ->
expect(@subject.extend).toBeDefined()

describe "extend.noConflict", ->
theExtendBeingSpecifiedHere = undefined
Expand Down
0