utils.js 5.3 KB

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