UNPKG

4.66 kBJavaScriptView Raw
1"use strict";
2
3// Dependencies
4
5var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
6
7function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
8
9var sift = require("sift"),
10 isThere = require("is-there"),
11 abs = require("abs"),
12 rJson = require("r-json"),
13 wJson = require("w-json");
14
15// Constants
16var DEFAULT_PATH = abs("~/.ideas.json");
17
18/**
19 * Idea
20 *
21 * @name Idea
22 * @function
23 * @param {String} path The path to the JSON file where your ideas will be stored (default: `~/.ideas.json`).
24 * @param {Function} callback The callback function.
25 * @return {Idea} The `Idea` instance.
26 */
27
28var Idea = function () {
29 function Idea(path, callback) {
30 var _this = this;
31
32 _classCallCheck(this, Idea);
33
34 if (typeof path === "function") {
35 callback = path;
36 path = null;
37 }
38
39 // Defaults
40 this.path = path = path || DEFAULT_PATH;
41 callback = callback || function (err) {
42 if (err) throw err;
43 };
44
45 // Init
46 if (!isThere(this.path)) {
47 this.ideas = [];
48 this.save(callback);
49 } else {
50 this.list(function (err, ideas) {
51 if (err) {
52 return callback(err);
53 }
54 _this.ideas = ideas;
55 callback(null, ideas, _this);
56 });
57 }
58 }
59 /**
60 * list
61 * Lists all ideas.
62 *
63 * @name list
64 * @function
65 * @param {Function} callback The callback function.
66 * @return {Idea} The `Idea` instance.
67 */
68
69
70 _createClass(Idea, [{
71 key: "list",
72 value: function list(callback) {
73 rJson(this.path, function (err, content) {
74 if (err) {
75 return callback(err);
76 }
77 callback(err, content);
78 });
79 return this;
80 }
81 /**
82 * filter
83 * Filters ideas.
84 *
85 * @name filter
86 * @function
87 * @param {Object} filters An MongoDB like query object.
88 * @param {Function} callback The callback function.
89 * @return {Idea} The `Idea` instance.
90 */
91
92 }, {
93 key: "filter",
94 value: function filter(filters, callback) {
95 callback(null, sift(filters, this.ideas));
96 return this;
97 }
98 /**
99 * create
100 *
101 * @name create
102 * @function
103 * @param {String} idea The idea you have.
104 * @param {Function} callback The callback function.
105 * @return {Idea} The `Idea` instance.
106 */
107
108 }, {
109 key: "create",
110 value: function create(idea, callback) {
111 if (!idea) {
112 return callback(new Error("Idea cannot be empty."));
113 }
114 this.ideas.push({
115 id: this.ideas.length,
116 idea: idea,
117 date: new Date(),
118 state: "OPEN"
119 });
120 return this;
121 }
122 /**
123 * solve
124 * Solves an idea.
125 *
126 * @name solve
127 * @function
128 * @param {String} id The idea id.
129 * @param {Function} callback The callback function.
130 * @return {Idea} The `Idea` instance.
131 */
132
133 }, {
134 key: "solve",
135 value: function solve(id, callback) {
136 if (!this.ideas[id - 1]) {
137 return callback(new Error("Cannot find any idea with this id."));
138 }
139 this.ideas[id - 1].state = "SOLVED";
140 return this;
141 }
142 /**
143 * save
144 * Saves the ideas in the file.
145 *
146 * @name save
147 * @function
148 * @param {Function} callback The callback function.
149 * @return {Idea} The `Idea` instance.
150 */
151
152 }, {
153 key: "save",
154 value: function save(callback) {
155 wJson(this.path, this.ideas, callback);
156 return this;
157 }
158 }]);
159
160 return Idea;
161}();
162
163module.exports = Idea;
\No newline at end of file