Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added each, typeOf, & tests wait for tests to load

  • Loading branch information...
commit 51c7d697117275a88e6974299bb898fd2b46755f 1 parent a0b7c9c
@ryanflorence authored
View
1  bin/phantom → bin/run_tests
@@ -1,3 +1,4 @@
#!/bin/sh
+transpile
phantomjs bin/ci_tests.js spec/index.html
View
7 bin/transpile
@@ -1,13 +1,14 @@
#!/bin/sh
-# remove old stuff
+echo "# removing transpiled/"
rm -rf transpiled
-# transpile source
+echo "# transpiling source"
coffee -o transpiled/lib -cb lib/
-# transpile specs
+echo "# transpiling specs"
coffee -o transpiled/spec -cb spec/
+echo "# creating spec bootstrapper"
./bin/generate_all_specs
View
5 lib/fn/bind.coffee
@@ -1,9 +1,10 @@
# sets the context of a `fn` to `context`, optionally prepends `args...`
-# (partial application) to the returned function
+# to the returned function
define ->
push = Array.prototype.push
- (fn, context, args...) ->
+ (context, fn, args...) ->
->
push.apply args, arguments
fn.apply context, args
+
View
3  lib/fn/hitch.coffee
@@ -1,6 +1,5 @@
# returns a function bound to `obj` that calls obj's method `method`,
# optionally prepends `args` to returned function.
define ['fn/bind'], (bind) ->
- (obj, method, args...) ->
- bind obj[method], obj, args...
+ (obj, method, args...) -> bind obj, obj[method], args...
View
19 lib/list/each.coffee
@@ -0,0 +1,19 @@
+# iterates array, object, string or arraylike object `list` with `iterator`,
+# optionally changing the context of `iterator` from `list` to `context`
+# returns `list`
+define ['util/typeOf'], (typeOf) ->
+ arylike = ['array', 'arraylike', 'string']
+
+ (list, iterator, context = list) ->
+ type = typeOf list
+
+ if type in arylike
+ for item, i in list when list[i]
+ iterator.call context, item, i, list
+
+ if type is 'object'
+ for key, val of list when list.hasOwnProperty key
+ iterator.call context, val, key, list
+
+ list
+
View
5 lib/list/map.coffee
@@ -0,0 +1,5 @@
+# maps `ary` with `iterator`, optionally set context of `iterator` to `context`
+define ->
+ (ary, iterator, context = ary) ->
+ iterator.call ary, item, index, ary for item, index in ary
+
View
11 lib/util/typeOf.coffee
@@ -20,13 +20,24 @@ define ->
return type if type
+ # get rid of [object ... ]
type = string.slice 8, -1
+ # get rid of HTMLDiv in HTMLDivElement and such
return 'element' if type.slice(-7) is 'Element'
if type is 'Object'
+ # forgot what these are, honestly :\
if item.nodeName
return 'element' if item.nodeType is 1
return 'textnode' if item.nodeType is 3
+ # stuff like { 0: 'foo', 1: 'stuff', length: 2 }
+ return 'arraylike' if item.length?
+
+ # PhantomJS thinks both of these are object
+ return 'undefined' if item is undefined
+ return 'null' if item is null
+
+ # ¯\(º o)/¯
'object'
View
5 spec/index.html
@@ -34,7 +34,10 @@ <h2 id="qunit-userAgent"></h2>
</script>
<script src="../vendor/require.js"></script>
<script>
- require(['spec']);
+ stop();
+ require(['spec'], function () {
+ start();
+ });
</script>
</body>
View
7 spec/tests/fn/bind.coffee
@@ -1,18 +1,15 @@
define ['fn/bind'], (bind) ->
module 'fn/bind'
-
test 'setting the context of a function', ->
obj = x: 2
- whoami = -> @
-
- bound = bind whoami, obj
+ bound = bind obj, -> @
strictEqual bound(), obj, 'set context to obj'
test 'prepend arguments', ->
concat = (strings...) ->
strings.join ' '
- bound = bind concat, {}, 'foo', 'bar'
+ bound = bind {}, concat, 'foo', 'bar'
equal bound(), 'foo bar'
equal bound('baz'), 'foo bar baz'
View
61 spec/tests/list/each.coffee
@@ -0,0 +1,61 @@
+define ['obj/create', 'list/each'], (create, each) ->
+ module 'list/each'
+
+ test 'iterating an array', 6, ->
+ ary = [1, 2]
+
+ returns = each ary, (item, index, list) ->
+ strictEqual list, ary, 'list is ary'
+ equal item, ary[index], 'item and index are correct'
+
+ strictEqual returns, ary, 'returns ary'
+
+ context = {}
+ iterator = -> strictEqual @, context
+ each [1], iterator, context
+
+ test 'iterating an object', 6, ->
+ obj = foo: 'bar', baz: 'quux'
+
+ returns = each obj, (value, key, list) ->
+ strictEqual list, obj, 'list is obj'
+ equal value, obj[key], 'value and key are correct'
+
+ strictEqual returns, obj, 'returns obj'
+
+ context = {}
+ iterator = -> strictEqual @, context
+ each foo: 1, iterator, context
+
+ test 'iterating an array-like object', 8, ->
+ ary = 0: 'bar', 1: 'quux', length: 2
+
+ returns = each ary, (item, index, list) ->
+ strictEqual list, ary, 'list is ary'
+ equal item, ary[index], 'item and index are correct'
+
+ strictEqual returns, ary, 'returns ary'
+
+ context = {}
+ iterator = -> strictEqual @, context
+ each 0: 1, iterator, context
+
+ obj = 0: 'foo', 2: 'bar', length: 3
+ each obj, (item, index) ->
+ equal item, obj[index], 'messy objects should not freak out'
+
+ test 'iterating should ignore prototype properties', ->
+ parent = foo: 'bar'
+ child = create parent
+ child.bar = 'baz'
+ equal child.foo, 'bar', 'child inherited properly'
+
+ count = 0
+ each child, -> count++
+ equal count, 1, 'iterated only once, prototype properties ignored'
+
+ test 'iterating strings', 2, ->
+ str = 'hi'
+ each str, (item, index) ->
+ equal item, str[index], 'item and index are correct'
+
View
3  spec/tests/obj/create.coffee
@@ -1,6 +1,7 @@
define ['obj/create'], (create) ->
+ module 'obj/create'
- test 'obj/create', ->
+ test 'objects inheriting from objects', ->
parent = foo: 'foo'
child = create parent
strictEqual child.__proto__, parent, "parent is child's prototype"
View
1  spec/tests/obj/merge.coffee
@@ -1,5 +1,4 @@
define ['obj/merge'], (merge) ->
-
module 'obj/merge'
test 'target object is returned and extended', ->
View
1  spec/tests/obj/mergeDeep.coffee
@@ -1,5 +1,4 @@
define ['obj/mergeDeep'], (mergeDeep) ->
-
module 'obj/mergeDeep'
test 'target object is returned and extended', ->
View
25 spec/tests/util/typeOf.coffee
@@ -0,0 +1,25 @@
+define ['util/typeOf'], (typeOf) ->
+
+ module 'util/typeOf'
+
+ test 'different types', ->
+ cite = document.createElement 'cite'
+ document.body.appendChild cite
+
+ equal typeOf(undefined), 'undefined'
+ equal typeOf(null), 'null'
+ equal typeOf(document.createTextNode('')), 'textnode'
+ equal typeOf([]), 'array'
+ equal typeOf(''), 'string'
+ equal typeOf(->), 'function'
+ equal typeOf(new Date), 'date'
+ equal typeOf(document.getElementsByTagName('cite')), 'nodelist'
+ do -> equal typeOf(arguments), 'arguments'
+ equal typeOf(//), 'regexp'
+ equal typeOf(true), 'boolean'
+ equal typeOf(false), 'boolean'
+ equal typeOf(1), 'number'
+ equal typeOf(cite), 'element'
+ equal typeOf({}), 'object'
+ equal typeOf(0: 1, length: 1), 'arraylike'
+

0 comments on commit 51c7d69

Please sign in to comment.
Something went wrong with that request. Please try again.