dress
Version:
missing javascript prototypes(experimental)
179 lines (152 loc) • 5.57 kB
text/coffeescript
describe 'Object::$set', ->
it 'should change itself with the giving object', (done) ->
object = { a: 1, b: 2, c: 3, d: 4 }
object.$set { d: 10, e: 20 }
expect(object).to.eql { d: 10, e: 20 }
done()
describe 'Object::$pick', ->
it 'should pick correctly', (done) ->
object = { a: 1, b: 2, c: 3, d: 4 }
object = object.$pick 'a', 'd'
expect(object).to.eql { a: 1, d: 4 }
done()
describe 'Object::$$pick', ->
it 'should pick and change itself', (done) ->
object = { a: 1, b: 2, c: 3, d: 4 }
object.$$pick 'a', 'c'
expect(object).to.eql { a: 1, c: 3 }
done()
describe 'Object::$keys', ->
it 'should return the valus correctly', (done) ->
object = { a: 1, b: 2, c: 3, d: 4 }
expect(object.$keys()).to.eql ['a', 'b', 'c', 'd']
done()
describe 'Object::$values', ->
it 'should return the valus correctly', (done) ->
object = { a: 1, b: 2, c: 3, d: 4 }
expect(object.$values()).to.eql [1, 2, 3, 4]
done()
describe 'Object::$is_function', ->
it 'should detect whether it\'s a function', (done) ->
expect((->).$is_function()).to.be.true
expect({}.$is_function()).to.be.false
expect([].$is_function()).to.be.false
done()
describe 'Object::$is_array', ->
it 'should detect whether it\'s an array', (done) ->
expect((->).$is_array()).to.be.false
expect({}.$is_array()).to.be.false
expect([].$is_array()).to.be.true
done()
describe 'Object::$is_empty', ->
it 'should return true/false whether it\'s empty', (done)->
expect({}.$is_empty()).to.be.true
expect({a: 1}.$is_empty()).to.be.false
done()
describe 'Object::$is_string', ->
it 'should return true/false whether it\'s string', (done)->
expect("walter white".$is_string()).to.be.true
expect({}.$is_string()).to.be.false
done()
describe 'Object::$is_number', ->
it 'should return true/false whether it\'s number', (done)->
expect((4).$is_number()).to.be.true
expect({}.$is_number()).to.be.false
done()
describe 'Object::$is_boolean', ->
it 'should return true/false whether it\'s boolean', (done)->
expect((true).$is_boolean()).to.be.true
expect({}.$is_boolean()).to.be.false
done()
describe 'Object::$is_date', ->
it 'should return true/false whether it\'s date', (done)->
expect((new Date()).$is_date()).to.be.true
expect({}.$is_date()).to.be.false
done()
describe 'Object::$invert', ->
it 'should displace keys and values', (done) ->
object = { a: 1, b: 2, c: 3 }
expect(object.$invert()).to.eql { '1': 'a', '2': 'b', '3': 'c' }
done()
describe 'Object::$$invert', ->
it 'should change itself', (done) ->
object = { a: 1, b: 2, c: 3 }
object.$$invert()
expect(object).to.eql { '1': 'a', '2': 'b', '3': 'c' }
done()
describe 'Object::$each', ->
it 'should iterate for every key', (done)->
object = { a: 1, b: 2, c: 3 }
result = []
object.$each (k, v) ->
result.push object[k]
expect(result).to.eql [1, 2, 3]
done()
describe 'Object::$pairs', ->
it 'should return the right pairs', (done)->
object = { a: 1, b: 2, c: 3 }
expect(object.$pairs()).to.eql [['a', 1], ['b', 2], ['c', 3]]
done()
describe 'Object::$functions', ->
it 'should return the functions in the object', (done) ->
expect({ a: 1, b: 2, c: -> 2 }.$functions()).to.eql ['c']
done()
describe 'Object::$merge', ->
it 'should merge with the given object', (done) ->
object = { a: 1, b: 2, c: 3 }
object2 = { d: 2, e: 7 }
expect(object.$merge(object2)).to.eql { a: 1, b: 2, c: 3, d: 2, e: 7 }
done()
it 'should override by the given object', (done) ->
object = { a: 1, b: 2, c: 3 }
object2 = { b: 5, c: 7 }
expect(object.$merge(object2)).to.eql { a: 1, b: 5, c: 7 }
done()
describe 'Object::$$merge', ->
it 'should change itself', (done) ->
object = { a: 1, b: 2 }
object.$$merge(c: 3, d: 4)
expect(object).to.eql { a: 1, b: 2, c: 3, d: 4 }
done()
describe 'Object::$omit', ->
it 'should remove the given parameters', (done) ->
object = { a: 1, b: 2, c: 3 }
expect(object.$omit('b', 'c')).to.be.eql { a: 1 }
done()
it 'should remove the given array parameter', (done) ->
object = { a: 1, b: 2, c: 3 }
expect(object.$omit(['a', 'c'])).to.be.eql { b: 2 }
done()
describe 'Object::$$omit', ->
it 'should change itself', (done) ->
object = { a: 1, b: 2, c: 3 }
object.$$omit('a', 'b')
expect(object).to.eql { c: 3 }
done()
describe 'Object::$defaults', ->
it 'should add missing items', (done) ->
object = { a: 1, b: 2, c: 3 }
expect(object.$defaults(d: 1, e: 7)).to.eql { a: 1, b: 2, c: 3, d: 1, e: 7 }
done()
it 'should not override item', (done) ->
object = { a: 1, b: 2, c: 3 }
expect(object.$defaults(a: 6, e: 9)).to.eql { a: 1, b: 2, c: 3, e: 9 }
done()
describe 'Object::$clone', ->
it 'should return the same hash', (done) ->
object = { a: 1, b: 2 }
expect(object.$clone()).to.eql { a: 1, b: 2 }
expect(object.$clone()).not.to.equal object
done()
describe 'Object::$tap', ->
it 'should run the function and return itself', (done) ->
object = { a: 1, b: 2 }
result = false
expect(object.$defaults({ c: 1 }).$tap(-> result = true).$omit('b')).to.eql { a: 1, c: 1 }
expect(result).to.be.true
done()
describe 'Object::$json', ->
it 'should return json string', (done) ->
expect({ a: 12, b: 'hey' }.$json()).to.eql '{"a":12,"b":"hey"}'
expect([1, 2, 3].$json()).to.equal '[1,2,3]'
done()