8
0

utils.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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' ], function( lodashIncludes, lodash ) {
  13. var 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. var spy = function() {
  25. spy.called = true;
  26. };
  27. return spy;
  28. },
  29. /**
  30. * Returns a unique id. This id is a number (starting from 1) which will never get repeated on successive calls
  31. * to this method.
  32. *
  33. * @returns {Number} A number representing the id.
  34. */
  35. uid: ( function() {
  36. var next = 1;
  37. return function() {
  38. return next++;
  39. };
  40. } )(),
  41. /**
  42. * Checks if value implements iterator interface.
  43. *
  44. * @param {Mixed} value The value to check.
  45. * @returns {Boolean} True if value implements iterator interface.
  46. */
  47. isIterable( value ) {
  48. return !!( value && value[ Symbol.iterator ] );
  49. },
  50. /**
  51. * Compares how given arrays relate to each other. One array can be: same as another array, prefix of another array
  52. * or completely different.
  53. *
  54. * compareArrays( [ 0, 2 ], [ 0, 2 ] ); // SAME
  55. * compareArrays( [ 0, 2 ], [ 0 ] ); // PREFIX
  56. * compareArrays( [ 0, 2 ], [ 0, 2, 1 ] ); // DIFFERENT
  57. * compareArrays( [ 0, 2 ], [ 1, 3 ] ); // DIFFERENT
  58. *
  59. * @param {Array} a Array that is compared.
  60. * @param {Array} b Array to compare with.
  61. * @returns {Number} How array `a` is related to array `b`. Represented by one of flags:
  62. * `a` is {@link utils.compareArrays#SAME same}, `a` is a {@link utils.compareArrays#PREFIX prefix}
  63. * or `a` is {@link utils.compareArrays#DIFFERENT different}.
  64. */
  65. compareArrays( a, b ) {
  66. var minLen = Math.min( a.length, b.length );
  67. for ( var i = 0; i < minLen; i++ ) {
  68. if ( a[ i ] != b[ i ] ) {
  69. // The arrays are different.
  70. return this.DIFFERENT;
  71. }
  72. }
  73. // Both arrays were same at all points.
  74. if ( a.length == b.length ) {
  75. // If their length is also same, they are the same.
  76. return this.SAME;
  77. } else if ( a.length < b.length ) {
  78. // Compared array is shorter so it is a prefix of the other array.
  79. return this.PREFIX;
  80. }
  81. // In other case, the arrays are different.
  82. return this.DIFFERENT;
  83. }
  84. };
  85. /**
  86. * Flag for "is same as" relation between arrays.
  87. *
  88. * @type {number}
  89. */
  90. utils.compareArrays.SAME = 0;
  91. /**
  92. * Flag for "is a prefix of" relation between arrays.
  93. *
  94. * @type {number}
  95. */
  96. utils.compareArrays.PREFIX = 1;
  97. /**
  98. * Flag for "is different than" relation between arrays.
  99. *
  100. * @type {number}
  101. */
  102. utils.compareArrays.DIFFERENT = 2;
  103. // Extend "utils" with Lo-Dash methods.
  104. for ( var i = 0; i < lodashIncludes.length; i++ ) {
  105. utils[ lodashIncludes[ i ] ] = lodash[ lodashIncludes[ i ] ];
  106. }
  107. return utils;
  108. } );