8
0

utils.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. objectToMap( obj ) {
  85. const map = new Map();
  86. for ( let key in obj ) {
  87. map.set( key, obj[ key ] );
  88. }
  89. return map;
  90. },
  91. mapToObject( map ) {
  92. const obj = {};
  93. map.forEach( ( value, key ) => obj[ key ] = value );
  94. return obj;
  95. },
  96. /**
  97. * Returns `nth` (starts from `0` of course) item of an `iterable`.
  98. *
  99. * @param {Number} index
  100. * @param {Iterable.<*>} iterable
  101. * @returns {*}
  102. */
  103. nth( index, iterable ) {
  104. for ( let item of iterable ) {
  105. if ( index === 0 ) {
  106. return item;
  107. }
  108. index -= 1;
  109. }
  110. return null;
  111. },
  112. /**
  113. * Copies enumerable properties and symbols from the objects given as 2nd+ parameters to the
  114. * prototype of first object (a constructor).
  115. *
  116. * class Editor {
  117. * ...
  118. * }
  119. *
  120. * const SomeMixin = {
  121. * a() {
  122. * return 'a';
  123. * }
  124. * };
  125. *
  126. * utils.mix( Editor, SomeMixin, ... );
  127. *
  128. * new Editor().a(); // -> 'a'
  129. *
  130. * Note: Properties which already exist in the base class will not be overriden.
  131. *
  132. * @param {Function} [baseClass] Class which prototype will be extended.
  133. * @param {Object} [...mixins] Objects from which to get properties.
  134. */
  135. mix( baseClass, ...mixins ) {
  136. mixins.forEach( ( mixin ) => {
  137. Object.getOwnPropertyNames( mixin ).concat( Object.getOwnPropertySymbols( mixin ) )
  138. .forEach( ( key ) => {
  139. if ( key in baseClass.prototype ) {
  140. return;
  141. }
  142. const sourceDescriptor = Object.getOwnPropertyDescriptor( mixin, key );
  143. sourceDescriptor.enumerable = false;
  144. Object.defineProperty( baseClass.prototype, key, sourceDescriptor );
  145. } );
  146. } );
  147. }
  148. };
  149. export default utils;