low.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*! lowdb v1.0.0 */
  2. var low =
  3. /******/ (function(modules) { // webpackBootstrap
  4. /******/ // The module cache
  5. /******/ var installedModules = {};
  6. /******/
  7. /******/ // The require function
  8. /******/ function __webpack_require__(moduleId) {
  9. /******/
  10. /******/ // Check if module is in cache
  11. /******/ if(installedModules[moduleId]) {
  12. /******/ return installedModules[moduleId].exports;
  13. /******/ }
  14. /******/ // Create a new module (and put it into the cache)
  15. /******/ var module = installedModules[moduleId] = {
  16. /******/ i: moduleId,
  17. /******/ l: false,
  18. /******/ exports: {}
  19. /******/ };
  20. /******/
  21. /******/ // Execute the module function
  22. /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
  23. /******/
  24. /******/ // Flag the module as loaded
  25. /******/ module.l = true;
  26. /******/
  27. /******/ // Return the exports of the module
  28. /******/ return module.exports;
  29. /******/ }
  30. /******/
  31. /******/
  32. /******/ // expose the modules object (__webpack_modules__)
  33. /******/ __webpack_require__.m = modules;
  34. /******/
  35. /******/ // expose the module cache
  36. /******/ __webpack_require__.c = installedModules;
  37. /******/
  38. /******/ // define getter function for harmony exports
  39. /******/ __webpack_require__.d = function(exports, name, getter) {
  40. /******/ if(!__webpack_require__.o(exports, name)) {
  41. /******/ Object.defineProperty(exports, name, {
  42. /******/ configurable: false,
  43. /******/ enumerable: true,
  44. /******/ get: getter
  45. /******/ });
  46. /******/ }
  47. /******/ };
  48. /******/
  49. /******/ // getDefaultExport function for compatibility with non-harmony modules
  50. /******/ __webpack_require__.n = function(module) {
  51. /******/ var getter = module && module.__esModule ?
  52. /******/ function getDefault() { return module['default']; } :
  53. /******/ function getModuleExports() { return module; };
  54. /******/ __webpack_require__.d(getter, 'a', getter);
  55. /******/ return getter;
  56. /******/ };
  57. /******/
  58. /******/ // Object.prototype.hasOwnProperty.call
  59. /******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
  60. /******/
  61. /******/ // __webpack_public_path__
  62. /******/ __webpack_require__.p = "";
  63. /******/
  64. /******/ // Load entry module and return exports
  65. /******/ return __webpack_require__(__webpack_require__.s = 0);
  66. /******/ })
  67. /************************************************************************/
  68. /******/ ([
  69. /* 0 */
  70. /***/ (function(module, exports, __webpack_require__) {
  71. "use strict";
  72. var lodash = __webpack_require__(1);
  73. var isPromise = __webpack_require__(2);
  74. module.exports = function (adapter) {
  75. if (typeof adapter !== 'object') {
  76. throw new Error('An adapter must be provided, see https://github.com/typicode/lowdb/#usage');
  77. }
  78. // Create a fresh copy of lodash
  79. var _ = lodash.runInContext();
  80. var db = _.chain({});
  81. // Add write function to lodash
  82. // Calls save before returning result
  83. _.prototype.write = _.wrap(_.prototype.value, function (func) {
  84. var funcRes = func.apply(this);
  85. return db.write(funcRes);
  86. });
  87. function plant(state) {
  88. db.__wrapped__ = state;
  89. return db;
  90. }
  91. // Lowdb API
  92. // Expose _ for mixins
  93. db._ = _;
  94. db.read = function () {
  95. var r = adapter.read();
  96. return isPromise(r) ? r.then(plant) : plant(r);
  97. };
  98. db.write = function (returnValue) {
  99. var w = adapter.write(db.getState());
  100. return isPromise(w) ? w.then(function () {
  101. return returnValue;
  102. }) : returnValue;
  103. };
  104. db.getState = function () {
  105. return db.__wrapped__;
  106. };
  107. db.setState = function (state) {
  108. return plant(state);
  109. };
  110. return db.read();
  111. };
  112. /***/ }),
  113. /* 1 */
  114. /***/ (function(module, exports) {
  115. module.exports = _;
  116. /***/ }),
  117. /* 2 */
  118. /***/ (function(module, exports) {
  119. module.exports = isPromise;
  120. function isPromise(obj) {
  121. return !!obj && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function';
  122. }
  123. /***/ })
  124. /******/ ]);