8
0

utils.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. /**
  7. * A utilities library.
  8. *
  9. * @class utils
  10. * @singleton
  11. */
  12. CKEDITOR.define( function() {
  13. return {
  14. /**
  15. * Extends one JavaScript object with the properties defined in one or more objects. Existing properties are
  16. * overridden.
  17. *
  18. * @param {Object} target The object to be extended.
  19. * @param {Object} source One or more objects which properties will be copied (by reference) to `target`.
  20. * @returns {Object} The `target` object.
  21. */
  22. extend: function( target, source ) {
  23. if ( !this.isObject( source ) ) {
  24. return target;
  25. }
  26. if ( arguments.length > 2 ) {
  27. var args = Array.prototype.splice.call( arguments, 1 );
  28. while ( args.length ) {
  29. this.extend( target, args.shift() );
  30. }
  31. } else {
  32. var keys = Object.keys( source );
  33. while ( keys.length ) {
  34. var key = keys.shift();
  35. target[ key ] = source[ key ];
  36. }
  37. }
  38. return target;
  39. },
  40. /**
  41. * Checks if the provided object is a JavaScript function.
  42. *
  43. * @param obj The object to be checked.
  44. * @returns {Boolean} `true` if the provided object is a JavaScript function. Otherwise `false`.
  45. */
  46. isFunction: function( obj ) {
  47. return typeof obj == 'function';
  48. },
  49. /**
  50. * Checks if the provided object is a "pure" JavaScript object. In other words, if it is not any other
  51. * JavaScript native type, like Number or String.
  52. *
  53. * @param obj The object to be checked.
  54. * @returns {Boolean} `true` if the provided object is a "pure" JavaScript object. Otherwise `false`.
  55. */
  56. isObject: function( obj ) {
  57. return typeof obj === 'object' && !!obj;
  58. },
  59. /**
  60. * Creates a spy function (ala Sinon.js) that can be used to inspect call to it.
  61. *
  62. * The following are the present features:
  63. *
  64. * * spy.called: property set to `true` if the function has been called at least once.
  65. *
  66. * @returns {Function} The spy function.
  67. */
  68. spy: function() {
  69. var spy = function() {
  70. spy.called = true;
  71. };
  72. return spy;
  73. },
  74. /**
  75. * Returns a unique id. This id is a number (starting from 1) which will never get repeated on successive calls
  76. * to this method.
  77. *
  78. * @returns {Number} A number representing the id.
  79. */
  80. uid: ( function() {
  81. var next = 1;
  82. return function() {
  83. return next++;
  84. };
  85. } )()
  86. };
  87. } );