helpers.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. 'use strict';
  2. exports.__esModule = true;
  3. var _isTypedArray2 = require('lodash/isTypedArray');
  4. var _isTypedArray3 = _interopRequireDefault(_isTypedArray2);
  5. var _isArray2 = require('lodash/isArray');
  6. var _isArray3 = _interopRequireDefault(_isArray2);
  7. var _isObject2 = require('lodash/isObject');
  8. var _isObject3 = _interopRequireDefault(_isObject2);
  9. var _isUndefined2 = require('lodash/isUndefined');
  10. var _isUndefined3 = _interopRequireDefault(_isUndefined2);
  11. var _isFunction2 = require('lodash/isFunction');
  12. var _isFunction3 = _interopRequireDefault(_isFunction2);
  13. var _keys2 = require('lodash/keys');
  14. var _keys3 = _interopRequireDefault(_keys2);
  15. var _pick2 = require('lodash/pick');
  16. var _pick3 = _interopRequireDefault(_pick2);
  17. var _map2 = require('lodash/map');
  18. var _map3 = _interopRequireDefault(_map2);
  19. exports.skim = skim;
  20. exports.normalizeArr = normalizeArr;
  21. exports.debugLog = debugLog;
  22. exports.error = error;
  23. exports.deprecate = deprecate;
  24. exports.warn = warn;
  25. exports.exit = exit;
  26. exports.containsUndefined = containsUndefined;
  27. var _chalk = require('chalk');
  28. var _chalk2 = _interopRequireDefault(_chalk);
  29. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  30. // Pick off the attributes from only the current layer of the object.
  31. function skim(data) {
  32. return (0, _map3.default)(data, function (obj) {
  33. return (0, _pick3.default)(obj, (0, _keys3.default)(obj));
  34. });
  35. }
  36. // Check if the first argument is an array, otherwise uses all arguments as an
  37. // array.
  38. /* eslint no-console:0 */
  39. function normalizeArr() {
  40. var args = new Array(arguments.length);
  41. for (var i = 0; i < args.length; i++) {
  42. args[i] = arguments[i];
  43. }
  44. if (Array.isArray(args[0])) {
  45. return args[0];
  46. }
  47. return args;
  48. }
  49. function debugLog(msg) {
  50. console.log(msg);
  51. }
  52. function error(msg) {
  53. console.log(_chalk2.default.red('Knex:Error ' + msg));
  54. }
  55. // Used to signify deprecated functionality.
  56. function deprecate(method, alternate) {
  57. warn(method + ' is deprecated, please use ' + alternate);
  58. }
  59. // Used to warn about incorrect use, without error'ing
  60. function warn(msg) {
  61. console.log(_chalk2.default.yellow('Knex:warning - ' + msg));
  62. }
  63. function exit(msg) {
  64. console.log(_chalk2.default.red(msg));
  65. process.exit(1);
  66. }
  67. function containsUndefined(mixed) {
  68. var argContainsUndefined = false;
  69. if ((0, _isTypedArray3.default)(mixed)) return false;
  70. if (mixed && (0, _isFunction3.default)(mixed.toSQL)) {
  71. //Any QueryBuilder or Raw will automatically be validated during compile.
  72. return argContainsUndefined;
  73. }
  74. if ((0, _isArray3.default)(mixed)) {
  75. for (var i = 0; i < mixed.length; i++) {
  76. if (argContainsUndefined) break;
  77. argContainsUndefined = this.containsUndefined(mixed[i]);
  78. }
  79. } else if ((0, _isObject3.default)(mixed)) {
  80. for (var key in mixed) {
  81. if (argContainsUndefined) break;
  82. argContainsUndefined = this.containsUndefined(mixed[key]);
  83. }
  84. } else {
  85. argContainsUndefined = (0, _isUndefined3.default)(mixed);
  86. }
  87. return argContainsUndefined;
  88. }