array.coffee 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. require './_prepare'
  2. array = mod 'array'
  3. test 'from', ->
  4. array.from([1]).should.be.an.instanceOf Array
  5. array.from([1])[0].should.equal 1
  6. # test 'clone', ->
  7. # a = [0, 1, 2]
  8. # b = array.clone a
  9. # b[0].should.equal 0
  10. # b[1].should.equal 1
  11. # b[0] = 3
  12. # a[0].should.equal 0
  13. test 'pluck', ->
  14. a = [0, 1, 2, 3]
  15. after = array.pluck a, 1
  16. after.length.should.equal 3
  17. after[0].should.equal 0
  18. after[1].should.equal 2
  19. after[2].should.equal 3
  20. after.should.equal a
  21. test 'pluckMultiple', ->
  22. a = [0, 1, 2, 3, 4, 5, 6]
  23. array.pluckMultiple a, [0, 4, 2, 6]
  24. a.length.should.equal 3
  25. a[0].should.equal 1
  26. a[1].should.equal 3
  27. a[2].should.equal 5
  28. test 'pluckItem', ->
  29. a = [0, 1, 2, 3, 2, 4, 2]
  30. array.pluckItem a, 2
  31. a[0].should.equal 0
  32. a[1].should.equal 1
  33. a[2].should.equal 3
  34. a[3].should.equal 4
  35. array.pluckItem([1], 2).length.should.equal 1
  36. test 'pluckOneItem', ->
  37. a = [0, 1, 2, 3, 2, 4, 2]
  38. array.pluckOneItem a, 2
  39. a[0].should.equal 0
  40. a[1].should.equal 1
  41. a[2].should.equal 3
  42. a[3].should.equal 2
  43. a[4].should.equal 4
  44. a[5].should.equal 2
  45. a = [1, 2]
  46. array.pluckOneItem a, 1
  47. a.length.should.equal 1
  48. a[0].should.equal 2
  49. array.pluckOneItem([], 1).length.should.equal 0
  50. array.pluckOneItem([1], 2).length.should.equal 1
  51. test 'plcukByCallback', ->
  52. a = [0, 1, 2, 3]
  53. array.pluckByCallback a, (val, i) ->
  54. return yes if val is 2
  55. return no
  56. a[0].should.equal 0
  57. a[1].should.equal 1
  58. a[2].should.equal 3
  59. test 'injectByCallback', ->
  60. shouldInject = (valA, valB, toInject) ->
  61. unless valA?
  62. return yes if toInject <= valB
  63. return no
  64. unless valB?
  65. return yes if valA <= toInject
  66. return no
  67. return yes if valA <= toInject <= valB
  68. return no
  69. a = [0.5, 1, 2.5, 2.5, 2.75, 2.75, 3]
  70. array.injectByCallback a, 0, shouldInject
  71. a[0].should.equal 0
  72. a[1].should.equal 0.5
  73. a[7].should.equal 3
  74. a = [0.5, 1, 2.5, 2.5, 2.75, 2.75, 3]
  75. array.injectByCallback a, 2.7, shouldInject
  76. a[0].should.equal 0.5
  77. a[4].should.equal 2.7
  78. a[5].should.equal 2.75
  79. a[7].should.equal 3
  80. a = [0.5, 1, 2.5, 2.5, 2.75, 2.75, 3]
  81. array.injectByCallback a, 3.2, shouldInject
  82. a[0].should.equal 0.5
  83. a[4].should.equal 2.75
  84. a[6].should.equal 3
  85. a[7].should.equal 3.2