8
0

utils.js 3.6 KB

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