utils.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. /**
  7. * An index at which arrays differ. If arrays are same at all indexes, it represents how arrays are related.
  8. * In this case, possible values are: 'SAME', 'PREFIX' or 'EXTENSION'.
  9. *
  10. * @typedef {String|Number} utils.ArrayRelation
  11. */
  12. const utils = {
  13. /**
  14. * Creates a spy function (ala Sinon.js) that can be used to inspect call to it.
  15. *
  16. * The following are the present features:
  17. *
  18. * * spy.called: property set to `true` if the function has been called at least once.
  19. *
  20. * @returns {Function} The spy function.
  21. */
  22. spy() {
  23. return function spy() {
  24. spy.called = true;
  25. };
  26. },
  27. /**
  28. * Returns a unique id. This id is a number (starting from 1) which will never get repeated on successive calls
  29. * to this method.
  30. *
  31. * @returns {Number} A number representing the id.
  32. */
  33. uid: ( () => {
  34. let next = 1;
  35. return () => {
  36. return next++;
  37. };
  38. } )(),
  39. /**
  40. * Checks if value implements iterator interface.
  41. *
  42. * @param {*} value The value to check.
  43. * @returns {Boolean} True if value implements iterator interface.
  44. */
  45. isIterable( value ) {
  46. return !!( value && value[ Symbol.iterator ] );
  47. },
  48. /**
  49. * Compares how given arrays relate to each other. One array can be: same as another array, prefix of another array
  50. * or completely different. If arrays are different, first index at which they differ is returned. Otherwise,
  51. * a flag specifying the relation is returned. Flags are negative numbers, so whenever a number >= 0 is returned
  52. * it means that arrays differ.
  53. *
  54. * compareArrays( [ 0, 2 ], [ 0, 2 ] ); // 'SAME'
  55. * compareArrays( [ 0, 2 ], [ 0, 2, 1 ] ); // 'PREFIX'
  56. * compareArrays( [ 0, 2 ], [ 0 ] ); // 'EXTENSION'
  57. * compareArrays( [ 0, 2 ], [ 1, 2 ] ); // 0
  58. * compareArrays( [ 0, 2 ], [ 0, 1 ] ); // 1
  59. *
  60. * @param {Array} a Array that is compared.
  61. * @param {Array} b Array to compare with.
  62. * @returns {utils.ArrayRelation} How array `a` is related to `b`.
  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 i;
  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 'SAME';
  76. } else if ( a.length < b.length ) {
  77. // Compared array is shorter so it is a prefix of the other array.
  78. return 'PREFIX';
  79. } else {
  80. // Compared array is longer so it is an extension of the other array.
  81. return '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. * Copies enumerable properties and symbols from the objects given as 2nd+ parameters to the
  102. * prototype of first object (a constructor).
  103. *
  104. * class SpecificEditor extends utils.mix( Editor, SomeMixin1, SomeMixin2 ) {
  105. * ...
  106. * }
  107. *
  108. * @param {Function} [baseClass] Class which prototype will be extended.
  109. * @param {Object} [...mixins] Objects from which to get properties.
  110. */
  111. mix( baseClass, ...mixins ) {
  112. mixins.forEach( ( mixin ) => {
  113. Object.getOwnPropertyNames( mixin ).concat( Object.getOwnPropertySymbols( mixin ) )
  114. .forEach( ( key ) => {
  115. const sourceDescriptor = Object.getOwnPropertyDescriptor( mixin, key );
  116. sourceDescriptor.enumerable = false;
  117. Object.defineProperty( baseClass.prototype, key, sourceDescriptor );
  118. } );
  119. } );
  120. }
  121. };
  122. export default utils;