utils.js 3.8 KB

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