utils.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. * Transform object to map.
  86. *
  87. * const map = utils.objectToMap( { 'foo': 1, 'bar': 2 } );
  88. * map.get( 'foo' ); // 1
  89. *
  90. * @param {Object} obj Object to transform.
  91. * @returns {Map} Map created from object.
  92. */
  93. objectToMap( obj ) {
  94. const map = new Map();
  95. for ( let key in obj ) {
  96. map.set( key, obj[ key ] );
  97. }
  98. return map;
  99. },
  100. /**
  101. * Checks whether given {Map}s are equal, that is has same size and same key-value pairs.
  102. *
  103. * @returns {Boolean} `true` if given maps are equal, `false` otherwise.
  104. */
  105. mapsEqual( mapA, mapB ) {
  106. if ( mapA.size != mapB.size ) {
  107. return false;
  108. }
  109. for ( let attr of mapA.entries() ) {
  110. let valA = JSON.stringify( attr[ 1 ] );
  111. let valB = JSON.stringify( mapB.get( attr[ 0 ] ) );
  112. if ( valA !== valB ) {
  113. return false;
  114. }
  115. }
  116. return true;
  117. },
  118. /**
  119. * Returns `nth` (starts from `0` of course) item of an `iterable`.
  120. *
  121. * @param {Number} index
  122. * @param {Iterable.<*>} iterable
  123. * @returns {*}
  124. */
  125. nth( index, iterable ) {
  126. for ( let item of iterable ) {
  127. if ( index === 0 ) {
  128. return item;
  129. }
  130. index -= 1;
  131. }
  132. return null;
  133. },
  134. /**
  135. * Copies enumerable properties and symbols from the objects given as 2nd+ parameters to the
  136. * prototype of first object (a constructor).
  137. *
  138. * class Editor {
  139. * ...
  140. * }
  141. *
  142. * const SomeMixin = {
  143. * a() {
  144. * return 'a';
  145. * }
  146. * };
  147. *
  148. * utils.mix( Editor, SomeMixin, ... );
  149. *
  150. * new Editor().a(); // -> 'a'
  151. *
  152. * Note: Properties which already exist in the base class will not be overriden.
  153. *
  154. * @param {Function} [baseClass] Class which prototype will be extended.
  155. * @param {Object} [...mixins] Objects from which to get properties.
  156. */
  157. mix( baseClass, ...mixins ) {
  158. mixins.forEach( ( mixin ) => {
  159. Object.getOwnPropertyNames( mixin ).concat( Object.getOwnPropertySymbols( mixin ) )
  160. .forEach( ( key ) => {
  161. if ( key in baseClass.prototype ) {
  162. return;
  163. }
  164. const sourceDescriptor = Object.getOwnPropertyDescriptor( mixin, key );
  165. sourceDescriptor.enumerable = false;
  166. Object.defineProperty( baseClass.prototype, key, sourceDescriptor );
  167. } );
  168. } );
  169. }
  170. };
  171. export default utils;