8
0

utils.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. import isPlainObject from './lib/lodash/isPlainObject.js';
  7. /**
  8. * @namespace core.utils
  9. */
  10. const utils = {
  11. /**
  12. * Creates a spy function (ala Sinon.js) that can be used to inspect call to it.
  13. *
  14. * The following are the present features:
  15. *
  16. * * spy.called: property set to `true` if the function has been called at least once.
  17. *
  18. * @memberOf core.utils
  19. * @returns {Function} The spy function.
  20. */
  21. spy() {
  22. return function spy() {
  23. spy.called = true;
  24. };
  25. },
  26. /**
  27. * Returns a unique id. This id is a number (starting from 1) which will never get repeated on successive calls
  28. * to this method.
  29. *
  30. * @function
  31. * @memberOf core.utils
  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. * @memberOf core.utils
  44. * @param {*} 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. If arrays are different, first index at which they differ is returned. Otherwise,
  53. * a flag specifying the relation is returned. Flags are negative numbers, so whenever a number >= 0 is returned
  54. * it means that arrays differ.
  55. *
  56. * compareArrays( [ 0, 2 ], [ 0, 2 ] ); // 'SAME'
  57. * compareArrays( [ 0, 2 ], [ 0, 2, 1 ] ); // 'PREFIX'
  58. * compareArrays( [ 0, 2 ], [ 0 ] ); // 'EXTENSION'
  59. * compareArrays( [ 0, 2 ], [ 1, 2 ] ); // 0
  60. * compareArrays( [ 0, 2 ], [ 0, 1 ] ); // 1
  61. *
  62. * @memberOf core.utils
  63. * @param {Array} a Array that is compared.
  64. * @param {Array} b Array to compare with.
  65. * @returns {utils.ArrayRelation} How array `a` is related to `b`.
  66. */
  67. compareArrays( a, b ) {
  68. const minLen = Math.min( a.length, b.length );
  69. for ( let i = 0; i < minLen; i++ ) {
  70. if ( a[ i ] != b[ i ] ) {
  71. // The arrays are different.
  72. return i;
  73. }
  74. }
  75. // Both arrays were same at all points.
  76. if ( a.length == b.length ) {
  77. // If their length is also same, they are the same.
  78. return 'SAME';
  79. } else if ( a.length < b.length ) {
  80. // Compared array is shorter so it is a prefix of the other array.
  81. return 'PREFIX';
  82. } else {
  83. // Compared array is longer so it is an extension of the other array.
  84. return 'EXTENSION';
  85. }
  86. },
  87. /**
  88. * Transforms object to map.
  89. *
  90. * const map = utils.objectToMap( { 'foo': 1, 'bar': 2 } );
  91. * map.get( 'foo' ); // 1
  92. *
  93. * @memberOf core.utils
  94. * @param {Object} obj Object to transform.
  95. * @returns {Map} Map created from object.
  96. */
  97. objectToMap( obj ) {
  98. const map = new Map();
  99. for ( let key in obj ) {
  100. map.set( key, obj[ key ] );
  101. }
  102. return map;
  103. },
  104. /**
  105. * Transforms object or iterable to map. Iterable needs to be in the format acceptable by the `Map` constructor.
  106. *
  107. * map = utils.toMap( { 'foo': 1, 'bar': 2 } );
  108. * map = utils.toMap( [ [ 'foo', 1 ], [ 'bar', 2 ] ] );
  109. * map = utils.toMap( anotherMap );
  110. *
  111. * @memberOf core.utils
  112. * @param {Object|Iterable} data Object or iterable to transform.
  113. * @returns {Map} Map created from data.
  114. */
  115. toMap( data ) {
  116. if ( isPlainObject( data ) ) {
  117. return utils.objectToMap( data );
  118. } else {
  119. return new Map( data );
  120. }
  121. },
  122. /**
  123. * Checks whether given {Map}s are equal, that is has same size and same key-value pairs.
  124. *
  125. * @memberOf core.utils
  126. * @returns {Boolean} `true` if given maps are equal, `false` otherwise.
  127. */
  128. mapsEqual( mapA, mapB ) {
  129. if ( mapA.size != mapB.size ) {
  130. return false;
  131. }
  132. for ( let attr of mapA.entries() ) {
  133. let valA = JSON.stringify( attr[ 1 ] );
  134. let valB = JSON.stringify( mapB.get( attr[ 0 ] ) );
  135. if ( valA !== valB ) {
  136. return false;
  137. }
  138. }
  139. return true;
  140. },
  141. /**
  142. * Returns `nth` (starts from `0` of course) item of an `iterable`.
  143. *
  144. * @memberOf core.utils
  145. * @param {Number} index
  146. * @param {Iterable.<*>} iterable
  147. * @returns {*}
  148. */
  149. nth( index, iterable ) {
  150. for ( let item of iterable ) {
  151. if ( index === 0 ) {
  152. return item;
  153. }
  154. index -= 1;
  155. }
  156. return null;
  157. },
  158. /**
  159. * Copies enumerable properties and symbols from the objects given as 2nd+ parameters to the
  160. * prototype of first object (a constructor).
  161. *
  162. * class Editor {
  163. * ...
  164. * }
  165. *
  166. * const SomeMixin = {
  167. * a() {
  168. * return 'a';
  169. * }
  170. * };
  171. *
  172. * utils.mix( Editor, SomeMixin, ... );
  173. *
  174. * new Editor().a(); // -> 'a'
  175. *
  176. * Note: Properties which already exist in the base class will not be overriden.
  177. *
  178. * @memberOf core.utils
  179. * @param {Function} [baseClass] Class which prototype will be extended.
  180. * @param {Object} [...mixins] Objects from which to get properties.
  181. */
  182. mix( baseClass, ...mixins ) {
  183. mixins.forEach( ( mixin ) => {
  184. Object.getOwnPropertyNames( mixin ).concat( Object.getOwnPropertySymbols( mixin ) )
  185. .forEach( ( key ) => {
  186. if ( key in baseClass.prototype ) {
  187. return;
  188. }
  189. const sourceDescriptor = Object.getOwnPropertyDescriptor( mixin, key );
  190. sourceDescriptor.enumerable = false;
  191. Object.defineProperty( baseClass.prototype, key, sourceDescriptor );
  192. } );
  193. } );
  194. }
  195. };
  196. /**
  197. * An index at which arrays differ. If arrays are same at all indexes, it represents how arrays are related.
  198. * In this case, possible values are: `'SAME'`, `'PREFIX'` or `'EXTENSION'`.
  199. *
  200. * @memberOf core.utils
  201. * @typedef {String|Number} ArrayRelation
  202. */
  203. export default utils;