Source: functions/common.js

  1. /**
  2. * Determines whether the given variable is defined.
  3. *
  4. * ```javascript
  5. * Rekord.isDefined(); // false
  6. * Rekord.isDefined(0); // true
  7. * Rekord.isDefined(true); // true
  8. * Rekord.isDefined(void 0); // false
  9. * Rekord.isDefined(undefined); // false
  10. * ```
  11. *
  12. * @memberof Rekord
  13. * @param {Any} x
  14. * The variable to test.
  15. * @return {Boolean} -
  16. * True if the variable is defined, otherwise false.
  17. */
  18. function isDefined(x)
  19. {
  20. return x !== undefined;
  21. }
  22. /**
  23. * Determines whether the given variable is a function.
  24. *
  25. * ```javascript
  26. * Rekord.isFunction(); // false
  27. * Rekord.isFunction(parseInt); // true
  28. * Rekord.isFunction(2); // false
  29. * ```
  30. *
  31. * @memberof Rekord
  32. * @param {Any} x
  33. * The variable to test.
  34. * @return {Boolean} -
  35. * True if the variable is a function, otherwise false.
  36. */
  37. function isFunction(x)
  38. {
  39. return !!(x && x.constructor && x.call && x.apply);
  40. }
  41. /**
  42. * Determines whether the given variable is a Rekord object. A Rekord object is a
  43. * constructor for a model and also has a Database variable. A Rekord object is
  44. * strictly created by the Rekord function.
  45. *
  46. * ```javascript
  47. * var Task = Rekord({
  48. * name: 'task',
  49. * fields: ['name', 'done', 'finished_at', 'created_at', 'assigned_to']
  50. * });
  51. * Rekord.isRekord( Task ); // true
  52. * ```
  53. *
  54. * @memberof Rekord
  55. * @param {Any} x
  56. * The variable to test.
  57. * @return {Boolean} -
  58. * True if the variable is a Rekord object, otherwise false.
  59. */
  60. function isRekord(x)
  61. {
  62. return !!(x && x.Database && isFunction( x ) && x.prototype instanceof Model);
  63. }
  64. /**
  65. * Determines whether the given variable is a string.
  66. *
  67. * ```javascript
  68. * Rekord.isString(); // false
  69. * Rekord.isString('x'): // true
  70. * Rekord.isString(1); // false
  71. * ```
  72. *
  73. * @memberof Rekord
  74. * @param {Any} x
  75. * The variable to test.
  76. * @return {Boolean} -
  77. * True if the variable is a string, otherwise false.
  78. */
  79. function isString(x)
  80. {
  81. return typeof x === 'string';
  82. }
  83. /**
  84. * Determines whether the given variable is a valid number. NaN and Infinity are
  85. * not valid numbers.
  86. *
  87. * ```javascript
  88. * Rekord.isNumber(); // false
  89. * Rekord.isNumber('x'): // false
  90. * Rekord.isNumber(1); // true
  91. * Rekord.isNumber(NaN); // false
  92. * Rekord.isNumber(Infinity); // true
  93. * ```
  94. *
  95. * @memberof Rekord
  96. * @param {Any} x
  97. * The variable to test.
  98. * @return {Boolean} -
  99. * True if the variable is a valid number, otherwise false.
  100. */
  101. function isNumber(x)
  102. {
  103. return typeof x === 'number' && !isNaN(x);
  104. }
  105. /**
  106. * Determines whether the given variable is a boolean value.
  107. *
  108. * ```javascript
  109. * Rekord.isBoolean(); // false
  110. * Rekord.isBoolean('x'): // false
  111. * Rekord.isBoolean(1); // false
  112. * Rekord.isBoolean(true); // true
  113. * ```
  114. *
  115. * @memberof Rekord
  116. * @param {Any} x
  117. * The variable to test.
  118. * @return {Boolean} -
  119. * True if the variable is a boolean value, otherwise false.
  120. */
  121. function isBoolean(x)
  122. {
  123. return typeof x === 'boolean';
  124. }
  125. /**
  126. * Determines whether the given variable is an instance of Date.
  127. *
  128. * ```javascript
  129. * Rekord.isDate(); // false
  130. * Rekord.isDate('x'): // false
  131. * Rekord.isDate(1); // false
  132. * Rekord.isDate(true); // false
  133. * Rekord.isDate(new Date()); // true
  134. * ```
  135. *
  136. * @memberof Rekord
  137. * @param {Any} x
  138. * The variable to test.
  139. * @return {Boolean} -
  140. * True if the variable is an instance of Date, otherwise false.
  141. */
  142. function isDate(x)
  143. {
  144. return x instanceof Date;
  145. }
  146. /**
  147. * Determines whether the given variable is an instance of RegExp.
  148. *
  149. * ```javascript
  150. * Rekord.isRegExp(); // false
  151. * Rekord.isRegExp('x'): // false
  152. * Rekord.isRegExp(1); // false
  153. * Rekord.isRegExp(true); // false
  154. * Rekord.isRegExp(/[xyz]/); // true
  155. * ```
  156. *
  157. * @memberof Rekord
  158. * @param {Any} x
  159. * The variable to test.
  160. * @return {Boolean} -
  161. * True if the variable is an instance of RegExp, otherwise false.
  162. */
  163. function isRegExp(x)
  164. {
  165. return x instanceof RegExp;
  166. }
  167. /**
  168. * Determines whether the given variable is an instance of Array.
  169. *
  170. * ```javascript
  171. * Rekord.isArray(); // false
  172. * Rekord.isArray('x'): // false
  173. * Rekord.isArray(1); // false
  174. * Rekord.isArray([]); // true
  175. * Rekord.isArray(Rekord.collect(1, 2, 3)); // true
  176. * ```
  177. *
  178. * @memberof Rekord
  179. * @param {Any} x
  180. * The variable to test.
  181. * @return {Boolean} -
  182. * True if the variable is an instance of Array, otherwise false.
  183. */
  184. function isArray(x)
  185. {
  186. return x instanceof Array;
  187. }
  188. /**
  189. * Determines whether the given variable is a non-null object. As a note,
  190. * Arrays are considered objects.
  191. *
  192. * ```javascript
  193. * Rekord.isObject(); // false
  194. * Rekord.isObject('x'): // false
  195. * Rekord.isObject(1); // false
  196. * Rekord.isObject([]); // true
  197. * Rekord.isObject({}); // true
  198. * Rekord.isObject(null); // false
  199. * ```
  200. *
  201. * @memberof Rekord
  202. * @param {Any} x
  203. * The variable to test.
  204. * @return {Boolean} -
  205. * True if the variable is a non-null object, otherwise false.
  206. */
  207. function isObject(x)
  208. {
  209. return x !== null && typeof x === 'object';
  210. }
  211. /**
  212. * Determines whether the given variable is not null and is not undefined.
  213. *
  214. * ```javascript
  215. * Rekord.isValue(); // false
  216. * Rekord.isValue('x'): // true
  217. * Rekord.isValue(1); // true
  218. * Rekord.isValue([]); // true
  219. * Rekord.isValue({}); // true
  220. * Rekord.isValue(null); // false
  221. * Rekord.isValue(void 0); // false
  222. * Rekord.isValue(undefined); // false
  223. * ```
  224. *
  225. * @memberof Rekord
  226. * @param {Any} x
  227. * The variable to test.
  228. * @return {Boolean} -
  229. * True if the variable is non-null and not undefined.
  230. */
  231. function isValue(x)
  232. {
  233. return !!(x !== undefined && x !== null);
  234. }
  235. /**
  236. * A function that doesn't perform any operations.
  237. *
  238. * @memberof Rekord
  239. */
  240. function noop()
  241. {
  242. }
  243. /**
  244. * Returns the given function with the given context (`this`). This also has the
  245. * benefits of returning a "copy" of the function which makes it ideal for use
  246. * in listening on/once events and off events.
  247. *
  248. * ```javascript
  249. * var context = {};
  250. * var func = Rekord.bind( context, function(x) {
  251. * this.y = x * 2;
  252. * });
  253. * func( 4 );
  254. * context.y; // 8
  255. * ```
  256. *
  257. * @memberof Rekord
  258. * @param {Object} context
  259. * The value of `this` for the given function.
  260. * @param {Function}
  261. * The function to invoke with the given context.
  262. * @return {Function} -
  263. * A new function which is a copy of the given function with a new context.
  264. */
  265. function bind(context, func)
  266. {
  267. return function bindedFunction()
  268. {
  269. func.apply( context, arguments );
  270. };
  271. }
  272. /**
  273. * Generates a UUID using the random number method.
  274. *
  275. * @memberof Rekord
  276. * @return {String} -
  277. * The generated UUID.
  278. */
  279. function uuid()
  280. {
  281. return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
  282. }
  283. function S4()
  284. {
  285. return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
  286. }
  287. function sizeof(x)
  288. {
  289. if ( isArray(x) || isString(x) )
  290. {
  291. return x.length;
  292. }
  293. else if ( isObject(x) )
  294. {
  295. var properties = 0;
  296. for (var prop in x) // jshint ignore:line
  297. {
  298. properties++;
  299. }
  300. return properties;
  301. }
  302. else if ( isNumber( x ) )
  303. {
  304. return x;
  305. }
  306. return 0;
  307. }
  308. function isEmpty(x)
  309. {
  310. if (x === null || x === void 0 || x === 0)
  311. {
  312. return true;
  313. }
  314. if (isArray(x) || isString(x))
  315. {
  316. return x.length === 0;
  317. }
  318. if (isDate(x))
  319. {
  320. return x.getTime() === 0 || isNaN( x.getTime() );
  321. }
  322. if (isObject(x))
  323. {
  324. for (var prop in x) // jshint ignore:line
  325. {
  326. return false;
  327. }
  328. return true;
  329. }
  330. return false;
  331. }
  332. function evaluate(x)
  333. {
  334. if ( !isValue( x ) )
  335. {
  336. return x;
  337. }
  338. if ( isRekord( x ) )
  339. {
  340. return new x();
  341. }
  342. if ( isFunction( x ) )
  343. {
  344. return x();
  345. }
  346. return copy( x );
  347. }