map.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. var map = require('../');
  2. var test = require('tape');
  3. test('numbers -> letters', function (t) {
  4. t.plan(2);
  5. var a = map([97,98,99], function (c) {
  6. return String.fromCharCode(c);
  7. });
  8. t.equal(a.join(''), 'abc');
  9. var b = map(cripple([97,98,99]), function (c) {
  10. return String.fromCharCode(c);
  11. });
  12. t.equal(b.join(''), 'abc');
  13. });
  14. test('elements and indexes', function (t) {
  15. t.plan(8);
  16. var x = { q: 5 }, y = 3, z = null;
  17. t.deepEqual(
  18. map([x,y,z], function (c, i) { return i }),
  19. [ 0, 1, 2 ],
  20. 'index check'
  21. );
  22. t.deepEqual(
  23. map([x,y,z], function (c, i) { return i }),
  24. [ 0, 1, 2 ],
  25. 'crippled index check'
  26. );
  27. var xs0 = [ x, y, z ];
  28. map(xs0, function (c, i, xs) {
  29. t.strictEqual(xs, xs0, 'argument[2]');
  30. });
  31. var xs1 = [ x, y, z ];
  32. map(xs1, function (c, i, xs) {
  33. t.strictEqual(xs, xs1, 'crippled argument[2]');
  34. });
  35. });
  36. test('ignore holes', function (t) {
  37. t.plan(2);
  38. t.deepEqual(
  39. map(new Array(5), function (x) { return x }),
  40. []
  41. );
  42. t.deepEqual(
  43. map(cripple(new Array(5)), function (x) { return x }),
  44. []
  45. );
  46. });
  47. test('sparse map', function (t) {
  48. t.plan(2);
  49. var xs = new Array(5);
  50. xs[2] = 'a';
  51. xs[4] = 'b';
  52. t.equal(
  53. map(xs, function (x, i) { return x + i }).join(''),
  54. 'a2b4'
  55. );
  56. var ys = new Array(5);
  57. ys[2] = 'a';
  58. ys[4] = 'b';
  59. t.equal(
  60. map(cripple(xs), function (x, i) { return x + i }).join(''),
  61. 'a2b4'
  62. );
  63. t.end();
  64. });
  65. function cripple (xs) {
  66. xs.map = undefined;
  67. return xs;
  68. }