utils.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. * An index at which arrays differ. If arrays are same at all indexes, it represents how arrays are related.
  9. * In this case, possible values are: 'SAME', 'PREFIX' or 'EXTENSION'.
  10. *
  11. * @typedef {String|Number} utils.ArrayRelation
  12. */
  13. /**
  14. * @namespace core.utils
  15. */
  16. const utils = {
  17. /**
  18. * Creates a spy function (ala Sinon.js) that can be used to inspect call to it.
  19. *
  20. * The following are the present features:
  21. *
  22. * * spy.called: property set to `true` if the function has been called at least once.
  23. *
  24. * @member core.utils.spy
  25. * @returns {Function} The spy function.
  26. */
  27. spy() {
  28. return function spy() {
  29. spy.called = true;
  30. };
  31. },
  32. /**
  33. * Returns a unique id. This id is a number (starting from 1) which will never get repeated on successive calls
  34. * to this method.
  35. *
  36. * @returns {Number} A number representing the id.
  37. */
  38. uid: ( () => {
  39. let next = 1;
  40. return () => {
  41. return next++;
  42. };
  43. } )(),
  44. /**
  45. * Checks if value implements iterator interface.
  46. *
  47. * @param {*} value The value to check.
  48. * @returns {Boolean} True if value implements iterator interface.
  49. */
  50. isIterable( value ) {
  51. return !!( value && value[ Symbol.iterator ] );
  52. },
  53. /**
  54. * Compares how given arrays relate to each other. One array can be: same as another array, prefix of another array
  55. * or completely different. If arrays are different, first index at which they differ is returned. Otherwise,
  56. * a flag specifying the relation is returned. Flags are negative numbers, so whenever a number >= 0 is returned
  57. * it means that arrays differ.
  58. *
  59. * compareArrays( [ 0, 2 ], [ 0, 2 ] ); // 'SAME'
  60. * compareArrays( [ 0, 2 ], [ 0, 2, 1 ] ); // 'PREFIX'
  61. * compareArrays( [ 0, 2 ], [ 0 ] ); // 'EXTENSION'
  62. * compareArrays( [ 0, 2 ], [ 1, 2 ] ); // 0
  63. * compareArrays( [ 0, 2 ], [ 0, 1 ] ); // 1
  64. *
  65. * @param {Array} a Array that is compared.
  66. * @param {Array} b Array to compare with.
  67. * @returns {utils.ArrayRelation} How array `a` is related to `b`.
  68. */
  69. compareArrays( a, b ) {
  70. const minLen = Math.min( a.length, b.length );
  71. for ( let i = 0; i < minLen; i++ ) {
  72. if ( a[ i ] != b[ i ] ) {
  73. // The arrays are different.
  74. return i;
  75. }
  76. }
  77. // Both arrays were same at all points.
  78. if ( a.length == b.length ) {
  79. // If their length is also same, they are the same.
  80. return 'SAME';
  81. } else if ( a.length < b.length ) {
  82. // Compared array is shorter so it is a prefix of the other array.
  83. return 'PREFIX';
  84. } else {
  85. // Compared array is longer so it is an extension of the other array.
  86. return 'EXTENSION';
  87. }
  88. },
  89. /**
  90. * Transforms object to map.
  91. *
  92. * const map = utils.objectToMap( { 'foo': 1, 'bar': 2 } );
  93. * map.get( 'foo' ); // 1
  94. *
  95. * @param {Object} obj Object to transform.
  96. * @returns {Map} Map created from object.
  97. */
  98. objectToMap( obj ) {
  99. const map = new Map();
  100. for ( let key in obj ) {
  101. map.set( key, obj[ key ] );
  102. }
  103. return map;
  104. },
  105. /**
  106. * Transforms object or iterable to map. Iterable needs to be in the format acceptable by the `Map` constructor.
  107. *
  108. * map = utils.toMap( { 'foo': 1, 'bar': 2 } );
  109. * map = utils.toMap( [ [ 'foo', 1 ], [ 'bar', 2 ] ] );
  110. * map = utils.toMap( anotherMap );
  111. *
  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. * @returns {Boolean} `true` if given maps are equal, `false` otherwise.
  126. */
  127. mapsEqual( mapA, mapB ) {
  128. if ( mapA.size != mapB.size ) {
  129. return false;
  130. }
  131. for ( let attr of mapA.entries() ) {
  132. let valA = JSON.stringify( attr[ 1 ] );
  133. let valB = JSON.stringify( mapB.get( attr[ 0 ] ) );
  134. if ( valA !== valB ) {
  135. return false;
  136. }
  137. }
  138. return true;
  139. },
  140. /**
  141. * Returns `nth` (starts from `0` of course) item of an `iterable`.
  142. *
  143. * @param {Number} index
  144. * @param {Iterable.<*>} iterable
  145. * @returns {*}
  146. */
  147. nth( index, iterable ) {
  148. for ( let item of iterable ) {
  149. if ( index === 0 ) {
  150. return item;
  151. }
  152. index -= 1;
  153. }
  154. return null;
  155. },
  156. /**
  157. * Copies enumerable properties and symbols from the objects given as 2nd+ parameters to the
  158. * prototype of first object (a constructor).
  159. *
  160. * class Editor {
  161. * ...
  162. * }
  163. *
  164. * const SomeMixin = {
  165. * a() {
  166. * return 'a';
  167. * }
  168. * };
  169. *
  170. * utils.mix( Editor, SomeMixin, ... );
  171. *
  172. * new Editor().a(); // -> 'a'
  173. *
  174. * Note: Properties which already exist in the base class will not be overriden.
  175. *
  176. * @param {Function} [baseClass] Class which prototype will be extended.
  177. * @param {Object} [...mixins] Objects from which to get properties.
  178. */
  179. mix( baseClass, ...mixins ) {
  180. mixins.forEach( ( mixin ) => {
  181. Object.getOwnPropertyNames( mixin ).concat( Object.getOwnPropertySymbols( mixin ) )
  182. .forEach( ( key ) => {
  183. if ( key in baseClass.prototype ) {
  184. return;
  185. }
  186. const sourceDescriptor = Object.getOwnPropertyDescriptor( mixin, key );
  187. sourceDescriptor.enumerable = false;
  188. Object.defineProperty( baseClass.prototype, key, sourceDescriptor );
  189. } );
  190. } );
  191. }
  192. };
  193. export default utils;