flavor.js

  1. import _ from 'lodash';
  2. import CoreExt from './extensions/core';
  3. import LodashExt from './extensions/lodash';
  4. import ObjectExt from './extensions/object';
  5. import FunctionExt from './extensions/function';
  6. import BooleanExt from './extensions/boolean';
  7. import NumberExt from './extensions/number';
  8. import DateExt from './extensions/date';
  9. import StringExt from './extensions/string';
  10. import ArrayExt from './extensions/array';
  11. import release from './release.json';
  12. /**
  13. * constructs FlavorJS class & extends the js natives
  14. * @class FlavorJS
  15. * @classdesc FlavorJS the definitive JS natives chainable extensions methods
  16. * @public
  17. */
  18. export default class FlavorJS {
  19. constructor() {
  20. this.init();
  21. }
  22. /**
  23. * safe js native prototype extension using Object.defineProperty
  24. * @memberOf FlavorJS
  25. * @method extendPrototypeProperty
  26. * @instance
  27. * @param {prototype|object} proto - the prototype/object to extend
  28. * @param {string} prop - the name of the property to be defined or modified
  29. * @param {*} val - val to be used as value in the descriptor for the property, can be any kind of native (number, function, etc...) or what you want
  30. * @param {object} [options={}] - options to be used as parameters in the descriptor for the property<br>
  31. * possible options are (source documentation from <a href="https://developer.mozilla.org/it/docs/Web/JavaScript/Reference/Global_Objects/object/defineProperty" target="_blank">Javascript|MDN docs</a>)<br>
  32. * @param {boolean} [options.configurable=true] configurable - true if and only if the type of this property descriptor may be changed and if the property may be deleted from the corresponding Object.
  33. * @param {boolean} [options.enumerable=false] enumerable - true if and only if this property shows up during enumeration of the properties on the corresponding Object.
  34. * @param {boolean} [options.writable=true] writable - true if and only if the value associated with the property may be changed with an assignment operator.
  35. * @param {function} [options.get=undefined] get - A function which serves as a getter for the property, or undefined if there is no getter. The function return will be used as the value of property.<br>
  36. * for example...<br>
  37. * <pre>
  38. * function ClassName() {
  39. * var privateProp = null;
  40. *
  41. * Object.defineProperty(this, 'publicProp', {
  42. * get: function() {
  43. * return privateProp;
  44. * }
  45. * });
  46. * }
  47. * </pre>
  48. * @param {function} [options.set=undefined] set - A function which serves as a setter for the property, or undefined if there is no setter. The function will receive as only argument the new value being assigned to the property.<br>
  49. * for example...<br>
  50. * <pre>
  51. * function ClassName() {
  52. * var privateProp = null;
  53. *
  54. * Object.defineProperty(this, 'publicProp', {
  55. * set: function(value) {
  56. * privateProp = value;
  57. * }
  58. * });
  59. * }
  60. * </pre>
  61. */
  62. extendPrototypeProperty(proto, prop, val, options = {}) {
  63. Object.defineProperty(proto, prop, {
  64. value: val,
  65. writable: true,
  66. configurable: true,
  67. enumerable: false,
  68. });
  69. }
  70. /**
  71. * merges all keys in extend plain object to the prototype (
  72. * @memberOf FlavorJS
  73. * @method extendPrototype
  74. * @instance
  75. * @param {prototype|object} proto - the prototype/object to extend
  76. * @param {object} extend - the extend object to be merged in prototype
  77. */
  78. extendPrototype(proto, extend) {
  79. _.forOwn(extend, (value, key) => {
  80. this.extendPrototypeProperty(proto, key, value);
  81. });
  82. }
  83. /**
  84. * extendLodash
  85. * @memberOf FlavorJS
  86. * @method extendLodash
  87. * @instance
  88. */
  89. extendLodash() {
  90. _.mixin(LodashExt);
  91. }
  92. /**
  93. * extendObject
  94. * @memberOf FlavorJS
  95. * @method extendObject
  96. * @instance
  97. */
  98. extendObject() {
  99. this.extendPrototype(Object.prototype, ObjectExt.prototype);
  100. this.extendPrototype(Object, ObjectExt.native);
  101. }
  102. /**
  103. * extendFunction
  104. * @memberOf FlavorJS
  105. * @method extendFunction
  106. * @instance
  107. */
  108. extendFunction() {
  109. this.extendPrototype(Function.prototype, FunctionExt.prototype);
  110. this.extendPrototype(Function, FunctionExt.native);
  111. }
  112. /**
  113. * extendBoolean
  114. * @memberOf FlavorJS
  115. * @method extendBoolean
  116. * @instance
  117. */
  118. extendBoolean() {
  119. this.extendPrototype(Boolean.prototype, BooleanExt.prototype);
  120. this.extendPrototype(Boolean, BooleanExt.native);
  121. }
  122. /**
  123. * extendNumber
  124. * @memberOf FlavorJS
  125. * @method extendNumber
  126. * @instance
  127. */
  128. extendNumber() {
  129. this.extendPrototype(Number.prototype, NumberExt.prototype);
  130. this.extendPrototype(Number, NumberExt.native);
  131. }
  132. /**
  133. * extendDate
  134. * @memberOf FlavorJS
  135. * @method extendDate
  136. * @instance
  137. */
  138. extendDate() {
  139. this.extendPrototype(Date.prototype, DateExt.prototype);
  140. this.extendPrototype(Date, DateExt.native);
  141. }
  142. /**
  143. * extendString
  144. * @memberOf FlavorJS
  145. * @method extendString
  146. * @instance
  147. */
  148. extendString() {
  149. this.extendPrototype(String.prototype, StringExt.prototype);
  150. this.extendPrototype(String, StringExt.native);
  151. }
  152. /**
  153. * extendArray
  154. * @memberOf FlavorJS
  155. * @method extendArray
  156. * @instance
  157. */
  158. extendArray() {
  159. this.extendPrototype(Array.prototype, ArrayExt.prototype);
  160. this.extendPrototype(Array, ArrayExt.native);
  161. }
  162. /**
  163. * initialize all
  164. * @memberOf FlavorJS
  165. * @method init
  166. * @instance
  167. */
  168. init() {
  169. this.extendLodash();
  170. this.extendObject();
  171. this.extendFunction();
  172. this.extendBoolean();
  173. this.extendNumber();
  174. this.extendDate();
  175. this.extendString();
  176. this.extendArray();
  177. const flavorJSStatus = release.version.inherit({
  178. initialized: true,
  179. });
  180. Object.inherit(this, CoreExt, flavorJSStatus);
  181. console.log('FlavorJS initialized', flavorJSStatus);
  182. }
  183. }