utils.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. const utils = {
  7. /**
  8. * Creates a spy function (ala Sinon.js) that can be used to inspect call to it.
  9. *
  10. * The following are the present features:
  11. *
  12. * * spy.called: property set to `true` if the function has been called at least once.
  13. *
  14. * @returns {Function} The spy function.
  15. */
  16. spy() {
  17. return function spy() {
  18. spy.called = true;
  19. };
  20. },
  21. /**
  22. * Returns a unique id. This id is a number (starting from 1) which will never get repeated on successive calls
  23. * to this method.
  24. *
  25. * @returns {Number} A number representing the id.
  26. */
  27. uid: ( () => {
  28. let next = 1;
  29. return () => {
  30. return next++;
  31. };
  32. } )(),
  33. /**
  34. * Checks if value implements iterator interface.
  35. *
  36. * @param {Mixed} value The value to check.
  37. * @returns {Boolean} True if value implements iterator interface.
  38. */
  39. isIterable( value ) {
  40. return !!( value && value[ Symbol.iterator ] );
  41. },
  42. /**
  43. * Compares how given arrays relate to each other. One array can be: same as another array, prefix of another array
  44. * or completely different. If arrays are different, first index at which they differ is returned. Otherwise,
  45. * a flag specifying the relation is returned. Flags are negative numbers, so whenever a number >= 0 is returned
  46. * it means that arrays differ.
  47. *
  48. * compareArrays( [ 0, 2 ], [ 0, 2 ] ); // SAME
  49. * compareArrays( [ 0, 2 ], [ 0, 2, 1 ] ); // PREFIX
  50. * compareArrays( [ 0, 2 ], [ 0 ] ); // EXTENSION
  51. * compareArrays( [ 0, 2 ], [ 1, 2 ] ); // 0
  52. * compareArrays( [ 0, 2 ], [ 0, 1 ] ); // 1
  53. *
  54. * @param {Array} a Array that is compared.
  55. * @param {Array} b Array to compare with.
  56. * @returns {Number} An index at which arrays differ, or if they do not differ, how array `a` is related to array `b`.
  57. * This is represented by one of flags: `a` is {@link utils.compareArrays#SAME same}, `a` is
  58. * a {@link utils.compareArrays#PREFIX prefix) or `a` is an {@link utils.compareArrays#EXTENSION extension}.
  59. */
  60. compareArrays( a, b ) {
  61. const minLen = Math.min( a.length, b.length );
  62. for ( let i = 0; i < minLen; i++ ) {
  63. if ( a[ i ] != b[ i ] ) {
  64. // The arrays are different.
  65. return i;
  66. }
  67. }
  68. // Both arrays were same at all points.
  69. if ( a.length == b.length ) {
  70. // If their length is also same, they are the same.
  71. return utils.compareArrays.SAME;
  72. } else if ( a.length < b.length ) {
  73. // Compared array is shorter so it is a prefix of the other array.
  74. return utils.compareArrays.PREFIX;
  75. } else {
  76. // Compared array is longer so it is an extension of the other array.
  77. return utils.compareArrays.EXTENSION;
  78. }
  79. },
  80. /**
  81. * Returns `nth` (starts from `0` of course) item of an `iterable`.
  82. *
  83. * @param {Number} index
  84. * @param {Iterable.<*>} iterable
  85. * @returns {*}
  86. */
  87. nth( index, iterable ) {
  88. for ( let item of iterable ) {
  89. if ( index === 0 ) {
  90. return item;
  91. }
  92. index -= 1;
  93. }
  94. return null;
  95. }
  96. };
  97. /**
  98. * Flag for "is same as" relation between arrays.
  99. *
  100. * @type {Number}
  101. */
  102. utils.compareArrays.SAME = -1;
  103. /**
  104. * Flag for "is a prefix of" relation between arrays.
  105. *
  106. * @type {Number}
  107. */
  108. utils.compareArrays.PREFIX = -2;
  109. /**
  110. * Flag for "is a suffix of" relation between arrays.
  111. *
  112. * @type {number}
  113. */
  114. utils.compareArrays.EXTENSION = -3;
  115. export default utils;