utils.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. *
  60. * @param {Array} a Array that is compared.
  61. * @param {Array} b Array to compare with.
  62. * @returns {Number} An index at which arrays differ, or if they do not differ, how array `a` is related to array `b`.
  63. * This is represented by one of flags: `a` is {@link utils.compareArrays#SAME same}, `a` is
  64. * a {@link utils.compareArrays#PREFIX prefix) or `a` is an {@link utils.compareArrays#EXTENSION extension}.
  65. */
  66. compareArrays( a, b ) {
  67. const minLen = Math.min( a.length, b.length );
  68. for ( let i = 0; i < minLen; i++ ) {
  69. if ( a[ i ] != b[ i ] ) {
  70. // The arrays are different.
  71. return i;
  72. }
  73. }
  74. // Both arrays were same at all points.
  75. if ( a.length == b.length ) {
  76. // If their length is also same, they are the same.
  77. return utils.compareArrays.SAME;
  78. } else if ( a.length < b.length ) {
  79. // Compared array is shorter so it is a prefix of the other array.
  80. return utils.compareArrays.PREFIX;
  81. } else {
  82. // Compared array is longer so it is an extension of the other array.
  83. return utils.compareArrays.EXTENSION;
  84. }
  85. },
  86. /**
  87. * Returns `nth` (starts from `0` of course) item of an `iterable`.
  88. *
  89. * @param {Number} index
  90. * @param {Iterable.<*>} iterable
  91. * @returns {*}
  92. */
  93. nth( index, iterable ) {
  94. for ( let item of iterable ) {
  95. if ( index === 0 ) {
  96. return item;
  97. }
  98. index -= 1;
  99. }
  100. return null;
  101. }
  102. };
  103. /**
  104. * Flag for "is same as" relation between arrays.
  105. *
  106. * @type {Number}
  107. */
  108. utils.compareArrays.SAME = -1;
  109. /**
  110. * Flag for "is a prefix of" relation between arrays.
  111. *
  112. * @type {Number}
  113. */
  114. utils.compareArrays.PREFIX = -2;
  115. /**
  116. * Flag for "is a suffix of" relation between arrays.
  117. *
  118. * @type {number}
  119. */
  120. utils.compareArrays.EXTENSION = -3;
  121. // Extend "utils" with Lo-Dash methods.
  122. for ( let i = 0; i < lodashIncludes.length; i++ ) {
  123. utils[ lodashIncludes[ i ] ] = lodash[ lodashIncludes[ i ] ];
  124. }
  125. return utils;
  126. } );