slice-clause-tool.spec.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import {expect} from 'chai';
  2. import {format} from '@e22m4u/js-format';
  3. import {SliceClauseTool} from './slice-clause-tool.js';
  4. const S = new SliceClauseTool();
  5. describe('SliceClauseTool', function () {
  6. describe('slice', function () {
  7. it('requires the first argument to be an array', function () {
  8. const throwable = v => () => S.slice(v);
  9. const error = v =>
  10. format(
  11. 'The first argument of SliceClauseTool.slice ' +
  12. 'should be an Array, but %s given.',
  13. v,
  14. );
  15. expect(throwable('str')).to.throw(error('"str"'));
  16. expect(throwable('')).to.throw(error('""'));
  17. expect(throwable(10)).to.throw(error('10'));
  18. expect(throwable(0)).to.throw(error('0'));
  19. expect(throwable(true)).to.throw(error('true'));
  20. expect(throwable(false)).to.throw(error('false'));
  21. expect(throwable({})).to.throw(error('Object'));
  22. expect(throwable(undefined)).to.throw(error('undefined'));
  23. expect(throwable(null)).to.throw(error('null'));
  24. expect(throwable([{foo: 'bar'}])()).to.be.eql([{foo: 'bar'}]);
  25. expect(throwable([])()).to.be.eql([]);
  26. });
  27. it('requires the provided second argument to be a number', function () {
  28. const items = [{foo: 'bar'}];
  29. const throwable = v => () => S.slice(items, v);
  30. const error = v =>
  31. format(
  32. 'The provided option "skip" should be a Number, but %s given.',
  33. v,
  34. );
  35. expect(throwable('str')).to.throw(error('"str"'));
  36. expect(throwable('')).to.throw(error('""'));
  37. expect(throwable(true)).to.throw(error('true'));
  38. expect(throwable(false)).to.throw(error('false'));
  39. expect(throwable({})).to.throw(error('Object'));
  40. expect(throwable([])).to.throw(error('Array'));
  41. expect(throwable(10)()).to.be.eql([]);
  42. expect(throwable(0)()).to.be.eql(items);
  43. expect(throwable(undefined)()).to.be.eql(items);
  44. expect(throwable(null)()).to.be.eql(items);
  45. });
  46. it('requires the provided third argument to be a number', function () {
  47. const items = [{foo: 'bar'}];
  48. const throwable = v => () => S.slice(items, undefined, v);
  49. const error = v =>
  50. format(
  51. 'The provided option "limit" should be a Number, but %s given.',
  52. v,
  53. );
  54. expect(throwable('str')).to.throw(error('"str"'));
  55. expect(throwable('')).to.throw(error('""'));
  56. expect(throwable(true)).to.throw(error('true'));
  57. expect(throwable(false)).to.throw(error('false'));
  58. expect(throwable({})).to.throw(error('Object'));
  59. expect(throwable([])).to.throw(error('Array'));
  60. expect(throwable(10)()).to.be.eql(items);
  61. expect(throwable(0)()).to.be.eql(items);
  62. expect(throwable(undefined)()).to.be.eql(items);
  63. expect(throwable(null)()).to.be.eql(items);
  64. });
  65. it('does nothing if no "skip" and "limit" options provided', function () {
  66. const objects = [{id: 1}, {id: 2}, {id: 3}];
  67. const result = S.slice(objects);
  68. expect(result).to.be.eql(objects);
  69. });
  70. it('does nothing if the given "skip" option is zero', function () {
  71. const objects = [{id: 1}, {id: 2}, {id: 3}];
  72. const result = S.slice(objects, 0);
  73. expect(result).to.be.eql(objects);
  74. });
  75. it('uses the given "skip" option to exclude array elements from start', function () {
  76. const objects = [{id: 1}, {id: 2}, {id: 3}];
  77. const result = S.slice(objects, 2);
  78. expect(result).to.have.length(1);
  79. expect(result[0]).to.be.eql(objects[2]);
  80. });
  81. it('returns an empty array when "skip" option overflows a size of the given array ', function () {
  82. const objects = [{id: 1}, {id: 2}, {id: 3}];
  83. const result = S.slice(objects, 10);
  84. expect(result).to.have.length(0);
  85. });
  86. it('does nothing if the given "limit" option is zero', function () {
  87. const objects = [{id: 1}, {id: 2}, {id: 3}];
  88. const result = S.slice(objects, undefined, 0);
  89. expect(result).to.be.eql(objects);
  90. });
  91. it('uses the given "limit" option to trim the given array', function () {
  92. const objects = [{id: 1}, {id: 2}, {id: 3}];
  93. const result = S.slice(objects, undefined, 2);
  94. expect(result).to.have.length(2);
  95. expect(result[0]).to.be.eql(objects[0]);
  96. expect(result[1]).to.be.eql(objects[1]);
  97. });
  98. it('uses the "skip" and "limit" options to slice the given array', function () {
  99. const objects = [{id: 1}, {id: 2}, {id: 3}];
  100. const result = S.slice(objects, 1, 1);
  101. expect(result).to.have.length(1);
  102. expect(result[0]).to.be.eql(objects[1]);
  103. });
  104. });
  105. describe('validateSkipClause', function () {
  106. it('requires a number value', function () {
  107. const throwable = v => () => SliceClauseTool.validateSkipClause(v);
  108. const error = v =>
  109. format(
  110. 'The provided option "skip" should be a Number, but %s given.',
  111. v,
  112. );
  113. expect(throwable('str')).to.throw(error('"str"'));
  114. expect(throwable('')).to.throw(error('""'));
  115. expect(throwable(true)).to.throw(error('true'));
  116. expect(throwable(false)).to.throw(error('false'));
  117. expect(throwable({})).to.throw(error('Object'));
  118. expect(throwable([])).to.throw(error('Array'));
  119. throwable(10)();
  120. throwable(0)();
  121. throwable(undefined)();
  122. throwable(null)();
  123. });
  124. });
  125. describe('validateLimitClause', function () {
  126. it('requires a number value or a falsy value', function () {
  127. const throwable = v => () => SliceClauseTool.validateLimitClause(v);
  128. const error = v =>
  129. format(
  130. 'The provided option "limit" should be a Number, but %s given.',
  131. v,
  132. );
  133. expect(throwable('str')).to.throw(error('"str"'));
  134. expect(throwable('')).to.throw(error('""'));
  135. expect(throwable(true)).to.throw(error('true'));
  136. expect(throwable(false)).to.throw(error('false'));
  137. expect(throwable({})).to.throw(error('Object'));
  138. expect(throwable([])).to.throw(error('Array'));
  139. throwable(10)();
  140. throwable(0)();
  141. throwable(undefined)();
  142. throwable(null)();
  143. });
  144. });
  145. });