object.coffee 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. require './_prepare'
  2. object = mod 'object'
  3. test 'isBareObject', ->
  4. object.isBareObject('a').should.equal false
  5. object.isBareObject({'a': 'a'}).should.equal true
  6. test 'typeOf', ->
  7. object.typeOf('s').should.equal 'string'
  8. object.typeOf(0).should.equal 'number'
  9. object.typeOf(false).should.equal 'boolean'
  10. object.typeOf({}).should.equal 'object'
  11. object.typeOf(arguments).should.equal 'arguments'
  12. object.typeOf([]).should.equal 'array'
  13. test 'empty', ->
  14. o =
  15. a: 1
  16. b: 2
  17. object.empty o
  18. o.should.not.have.property 'a'
  19. o.should.not.have.property 'b'
  20. test 'fastEmpty', ->
  21. o =
  22. a: 1
  23. b: 2
  24. object.fastEmpty o
  25. o.should.not.have.property 'a'
  26. o.should.not.have.property 'b'
  27. test 'clone', ->
  28. object.clone([1])[0].should.equal 1
  29. object.clone({a:1}).a.should.equal 1
  30. o = {a: 1}
  31. object.clone(o).should.not.equal o
  32. test 'clone [include prototype]', ->
  33. class C
  34. constructor: (@a) ->
  35. sayA: -> @a + 'a'
  36. a = new C 'a'
  37. a.sayA().should.equal 'aa'
  38. b = object.clone a, yes
  39. b.should.not.equal a
  40. b.constructor.should.equal C
  41. b.a.should.equal 'a'
  42. b.a = 'a2'
  43. b.sayA().should.equal 'a2a'
  44. test 'clone [without prototype]', ->
  45. class C
  46. constructor: (@a) ->
  47. sayA: -> @a + 'a'
  48. a = new C 'a'
  49. a.sayA().should.equal 'aa'
  50. b = object.clone a, no
  51. b.should.equal a
  52. test 'overrideOnto [basic]', ->
  53. onto =
  54. a: 'a'
  55. b:
  56. c: 'c'
  57. d:
  58. e: 'e'
  59. what =
  60. a: 'a2'
  61. b:
  62. c: 'c2'
  63. d:
  64. f: 'f2'
  65. object.overrideOnto onto, what
  66. onto.a.should.equal 'a2'
  67. onto.b.should.have.property 'c'
  68. onto.b.c.should.equal 'c2'
  69. onto.b.d.should.not.have.property 'f'
  70. onto.b.d.e.should.equal 'e'
  71. test 'override', ->
  72. onto =
  73. a: 'a'
  74. b:
  75. c: 'c'
  76. d:
  77. e: 'e'
  78. what =
  79. a: 'a2'
  80. b:
  81. c: 'c2'
  82. d:
  83. f: 'f2'
  84. onto2 = object.override onto, what
  85. onto2.a.should.equal 'a2'
  86. onto2.b.should.have.property 'c'
  87. onto2.b.c.should.equal 'c2'
  88. onto2.b.d.should.not.have.property 'f'
  89. onto2.b.d.e.should.equal 'e'
  90. onto.should.not.equal onto2
  91. do ->
  92. what =
  93. a: 'a2'
  94. c: ->
  95. z: 'z'
  96. y:
  97. a: 'a'
  98. onto =
  99. a: 'a'
  100. b: 'b'
  101. test 'appendOnto [basic]', ->
  102. object.appendOnto onto, what
  103. onto.a.should.equal 'a2'
  104. onto.b.should.equal 'b'
  105. onto.z.should.equal 'z'
  106. test "appendOnto [shallow copies instances]", ->
  107. onto.c.should.be.instanceof Function
  108. onto.c.should.equal what.c
  109. test "appendOnto [clones objects]", ->
  110. onto.should.have.property 'y'
  111. onto.y.a.should.equal 'a'
  112. onto.y.should.not.equal what.y
  113. test 'groupProps', ->
  114. obj =
  115. a1: '1'
  116. a2: '2'
  117. b1: '1'
  118. b2: '2'
  119. c1: '1'
  120. c2: '2'
  121. rest1: '1'
  122. rest2: '2'
  123. groups = object.groupProps obj,
  124. a: ['a1', 'a2']
  125. b: [/^b[0-9]+$/]
  126. c: (key) -> key[0] is 'c'
  127. groups.a.should.have.property 'a1'
  128. groups.a.a1.should.equal '1'
  129. groups.a.should.have.property 'a2'
  130. groups.b.should.have.property 'b1'
  131. groups.b.should.have.property 'b2'
  132. groups.c.should.have.property 'c1'
  133. groups.c.should.have.property 'c2'
  134. groups.rest.should.have.property 'rest1'
  135. groups.rest.should.have.property 'rest1'
  136. groups.rest.should.not.have.property 'c1'