This repository has been archived by the owner on Oct 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
count.js
65 lines (56 loc) · 1.75 KB
/
count.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
var partial = require('../lib/partial')
, expect = require('must')
, count = require('../lib/count')
, seq = require('../lib/seq')
describe('count', function() {
describe('when given an uncountable object', function() {
it('should throw a TypeError', function() {
expect(partial(count, false)).to.throw(TypeError)
})
})
describe('when given an array', function() {
it('should return its length', function() {
expect(count([1, 2, 3])).to.equal(3)
})
})
describe('when given an object', function() {
it('should return the number of enumerable properties', function() {
expect(count({ foo: 1, bar: 2 })).to.equal(2)
})
it('should not care about properties called length', function() {
expect(count({ length: 5 })).to.equal(1)
})
})
describe('when given a string', function() {
it('should return its length', function() {
expect(count('Hello')).to.equal(5)
})
})
describe('when given an arguments object', function() {
it('should return its length', function(done) {
expect(count(arguments)).to.equal(1)
done()
})
})
describe('when given a seq', function () {
describe('and when the length is defined', function() {
it('should return it', function() {
var s = seq([1, 2, 3])
expect(count(s)).to.equal(3)
})
})
describe('but when the length is undefined', function() {
it('should return undefined', function() {
var s = seq(Math.random)
expect(count(s)).to.equal(undefined)
})
})
})
describe('when given anything empty', function() {
it('should return 0', function() {
[0, null, undefined, "", [], {}].forEach(function(falsy) {
expect(count(falsy)).to.equal(0)
})
})
})
})