quran-promise
Version:
ES2016 async/await (promise) enabled node module for the Holy Quran.
317 lines (250 loc) • 9.17 kB
JavaScript
import _Object$keys from 'babel-runtime/core-js/object/keys';
import _regeneratorRuntime from 'babel-runtime/regenerator';
import _asyncToGenerator from 'babel-runtime/helpers/asyncToGenerator';
import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
import _createClass from 'babel-runtime/helpers/createClass';
import db from 'sqlite';
import util from 'util';
// Defines file paths
var databaseFilePath = __dirname + '/db.sqlite';
var Quran = function () {
function Quran() {
_classCallCheck(this, Quran);
}
_createClass(Quran, [{
key: 'get',
// Helper method to get arabic verses from a chapter
value: function () {
var _ref = _asyncToGenerator(_regeneratorRuntime.mark(function _callee(chapterId) {
var verseId = arguments.length <= 1 || arguments[1] === undefined ? undefined : arguments[1];
var response, verses;
return _regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_context.next = 2;
return this.select({ chapter: chapterId, verse: verseId });
case 2:
response = _context.sent;
// return arabic strings only (map from array)
verses = void 0;
if (!!response && response.length > 0) {
verses = response.map(function (x) {
return x.ar;
});
}
return _context.abrupt('return', verses);
case 6:
case 'end':
return _context.stop();
}
}
}, _callee, this);
}));
function get(_x, _x2) {
return _ref.apply(this, arguments);
}
return get;
}()
// Helper method to get information about chapters
// Note: If no chapterId is specified then this method returns information
// about all chapters.
}, {
key: 'chapters',
value: function () {
var _ref2 = _asyncToGenerator(_regeneratorRuntime.mark(function _callee2(chapterId) {
var query;
return _regeneratorRuntime.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
query = 'SELECT * FROM chapters ';
// sanitize inputs
if (!chapterId) {
_context2.next = 7;
break;
}
if (!(chapterId > 114)) {
_context2.next = 4;
break;
}
throw new Error('chapterId specified is out of bounds: ' + chapterId + '.');
case 4:
if (!isNaN(chapterId)) {
_context2.next = 6;
break;
}
throw new Error('Invalid chapterId: ' + chapterId);
case 6:
query += 'WHERE id=' + chapterId;
case 7:
_context2.next = 9;
return db.open(databaseFilePath);
case 9:
_context2.next = 11;
return db.all(query);
case 11:
return _context2.abrupt('return', _context2.sent);
case 12:
case 'end':
return _context2.stop();
}
}
}, _callee2, this);
}));
function chapters(_x4) {
return _ref2.apply(this, arguments);
}
return chapters;
}()
// Helper method to get information about a juz
// Note: If no juzId is specified then this method returns information
// about all juz.
}, {
key: 'juz',
value: function () {
var _ref3 = _asyncToGenerator(_regeneratorRuntime.mark(function _callee3(juzId) {
var query;
return _regeneratorRuntime.wrap(function _callee3$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
query = 'SELECT * FROM juz ';
// sanitize inputs
if (!juzId) {
_context3.next = 7;
break;
}
if (!(juzId > 30)) {
_context3.next = 4;
break;
}
throw new Error('juzId specified is out of bounds: ' + juzId + '.');
case 4:
if (!isNaN(juzId)) {
_context3.next = 6;
break;
}
throw new Error('Invalid juzId: ' + juzId);
case 6:
query += 'WHERE id=' + juzId;
case 7:
_context3.next = 9;
return db.open(databaseFilePath);
case 9:
_context3.next = 11;
return db.all(query);
case 11:
return _context3.abrupt('return', _context3.sent);
case 12:
case 'end':
return _context3.stop();
}
}
}, _callee3, this);
}));
function juz(_x5) {
return _ref3.apply(this, arguments);
}
return juz;
}()
// Helper method to do a free-form text search of the Quran
}, {
key: 'search',
value: function () {
var _ref4 = _asyncToGenerator(_regeneratorRuntime.mark(function _callee4(language, text) {
var query;
return _regeneratorRuntime.wrap(function _callee4$(_context4) {
while (1) {
switch (_context4.prev = _context4.next) {
case 0:
query = 'SELECT chapter, verse, ' + language + ' FROM ' + language + ('WHERE ' + language + ' LIKE "%' + text + '%";');
_context4.next = 3;
return db.open(databaseFilePath);
case 3:
_context4.next = 5;
return db.all(query);
case 5:
return _context4.abrupt('return', _context4.sent);
case 6:
case 'end':
return _context4.stop();
}
}
}, _callee4, this);
}));
function search(_x6, _x7) {
return _ref4.apply(this, arguments);
}
return search;
}()
// Full low level select method. Allows you to specify:
// 1. Filters: Any array to filter on, e.g. { verse: 2, chapter: 5 }
// 2. Options for offset and limit, e.g. { limit: 10, offset: 5 }
// 3. Translations: An array of translations to return, e.g. ['en', 'hi']
}, {
key: 'select',
value: function () {
var _ref5 = _asyncToGenerator(_regeneratorRuntime.mark(function _callee5(filters, options) {
var translations = arguments.length <= 2 || arguments[2] === undefined ? ['ar'] : arguments[2];
var query, params;
return _regeneratorRuntime.wrap(function _callee5$(_context5) {
while (1) {
switch (_context5.prev = _context5.next) {
case 0:
query = 'SELECT * FROM ar a ';
params = [];
// join translations from other tables, if any are requested
// beyond arabic
if (!(translations.length === 1 && translations[0] === 'ar')) {
translations.forEach(function (l) {
query += 'JOIN ' + l + ' USING(chapter, verse) ';
});
}
// add filters (where clauses)
if (!!filters) {
_Object$keys(filters).forEach(function (k) {
var f = filters[k];
if (!!f) {
if (util.isArray(f)) {
params.push('a.' + k + ' IN (' + f.join(',') + ')');
} else {
params.push('a.' + k + '=' + f);
}
}
});
query += 'WHERE ' + params.join(' AND ');
}
// apply sorting, offset and limit options
query += ' ORDER BY chapter, verse ';
if (!!options) {
['limit', 'offset'].forEach(function (x) {
if (options[x]) {
query += x + ' ' + options[x] + ' ';
}
});
}
_context5.next = 8;
return db.open(databaseFilePath);
case 8:
_context5.next = 10;
return db.all(query);
case 10:
return _context5.abrupt('return', _context5.sent);
case 11:
case 'end':
return _context5.stop();
}
}
}, _callee5, this);
}));
function select(_x8, _x9, _x10) {
return _ref5.apply(this, arguments);
}
return select;
}()
}]);
return Quran;
}();
export default Quran;
//# sourceMappingURL=index.es6.js.map