index.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. 'use strict';
  2. var callBind = require('../');
  3. var bind = require('function-bind');
  4. var test = require('tape');
  5. /*
  6. * older engines have length nonconfigurable
  7. * in io.js v3, it is configurable except on bound functions, hence the .bind()
  8. */
  9. var functionsHaveConfigurableLengths = !!(
  10. Object.getOwnPropertyDescriptor
  11. && Object.getOwnPropertyDescriptor(bind.call(function () {}), 'length').configurable
  12. );
  13. test('callBind', function (t) {
  14. var sentinel = { sentinel: true };
  15. var func = function (a, b) {
  16. // eslint-disable-next-line no-invalid-this
  17. return [this, a, b];
  18. };
  19. t.equal(func.length, 2, 'original function length is 2');
  20. t.deepEqual(func(), [undefined, undefined, undefined], 'unbound func with too few args');
  21. t.deepEqual(func(1, 2), [undefined, 1, 2], 'unbound func with right args');
  22. t.deepEqual(func(1, 2, 3), [undefined, 1, 2], 'unbound func with too many args');
  23. var bound = callBind(func);
  24. t.equal(bound.length, func.length + 1, 'function length is preserved', { skip: !functionsHaveConfigurableLengths });
  25. t.deepEqual(bound(), [undefined, undefined, undefined], 'bound func with too few args');
  26. t.deepEqual(bound(1, 2), [1, 2, undefined], 'bound func with right args');
  27. t.deepEqual(bound(1, 2, 3), [1, 2, 3], 'bound func with too many args');
  28. var boundR = callBind(func, sentinel);
  29. t.equal(boundR.length, func.length, 'function length is preserved', { skip: !functionsHaveConfigurableLengths });
  30. t.deepEqual(boundR(), [sentinel, undefined, undefined], 'bound func with receiver, with too few args');
  31. t.deepEqual(boundR(1, 2), [sentinel, 1, 2], 'bound func with receiver, with right args');
  32. t.deepEqual(boundR(1, 2, 3), [sentinel, 1, 2], 'bound func with receiver, with too many args');
  33. var boundArg = callBind(func, sentinel, 1);
  34. t.equal(boundArg.length, func.length - 1, 'function length is preserved', { skip: !functionsHaveConfigurableLengths });
  35. t.deepEqual(boundArg(), [sentinel, 1, undefined], 'bound func with receiver and arg, with too few args');
  36. t.deepEqual(boundArg(2), [sentinel, 1, 2], 'bound func with receiver and arg, with right arg');
  37. t.deepEqual(boundArg(2, 3), [sentinel, 1, 2], 'bound func with receiver and arg, with too many args');
  38. t.test('callBind.apply', function (st) {
  39. var aBound = callBind.apply(func);
  40. st.deepEqual(aBound(sentinel), [sentinel, undefined, undefined], 'apply-bound func with no args');
  41. st.deepEqual(aBound(sentinel, [1], 4), [sentinel, 1, undefined], 'apply-bound func with too few args');
  42. st.deepEqual(aBound(sentinel, [1, 2], 4), [sentinel, 1, 2], 'apply-bound func with right args');
  43. var aBoundArg = callBind.apply(func);
  44. st.deepEqual(aBoundArg(sentinel, [1, 2, 3], 4), [sentinel, 1, 2], 'apply-bound func with too many args');
  45. st.deepEqual(aBoundArg(sentinel, [1, 2], 4), [sentinel, 1, 2], 'apply-bound func with right args');
  46. st.deepEqual(aBoundArg(sentinel, [1], 4), [sentinel, 1, undefined], 'apply-bound func with too few args');
  47. var aBoundR = callBind.apply(func, sentinel);
  48. st.deepEqual(aBoundR([1, 2, 3], 4), [sentinel, 1, 2], 'apply-bound func with receiver and too many args');
  49. st.deepEqual(aBoundR([1, 2], 4), [sentinel, 1, 2], 'apply-bound func with receiver and right args');
  50. st.deepEqual(aBoundR([1], 4), [sentinel, 1, undefined], 'apply-bound func with receiver and too few args');
  51. st.end();
  52. });
  53. t.end();
  54. });