UNPKG

@adncorp/account-book-parser-adapter

Version:

parsing sheet adapter for account book

111 lines (87 loc) 2.6 kB
"use strict"; /* eslint-disable spellcheck/spell-checker */ var axios = require('axios'); var fs = require('fs'); var splitUri = function splitUri(uri) { var splitted = uri.match(/(?:([^:\/?#]+):)?(?:\/\/([^\/?#]*))?([^?#]*)(?:\?([^#]*))?(?:#(.*))?/); return splitted; }; function isUri(value) { if (!value) { return null; } // check for illegal characters if (/[^a-z0-9\:\/\?\#\[\]\@\!\$\&\'\(\)\*\+\,\;\=\.\-\_\~\%]/i.test(value)) { return null; } // check for hex escapes that aren't complete if (/%[^0-9a-f]/i.test(value)) return null; if (/%[0-9a-f](:?[^0-9a-f]|$)/i.test(value)) return null; var splitted = []; var scheme = ''; var authority = ''; var path = ''; var query = ''; var fragment = ''; var out = ''; // from RFC 3986 splitted = splitUri(value); scheme = splitted[1]; authority = splitted[2]; path = splitted[3]; query = splitted[4]; fragment = splitted[5]; // scheme and path are required, though the path can be empty if (!(scheme && scheme.length && path.length >= 0)) return null; // if authority is present, the path must be empty or begin with a / if (authority && authority.length) { if (!(path.length === 0 || /^\//.test(path))) return null; } else { // if authority is not present, the path must not start with // if (/^\/\//.test(path)) return null; } // scheme must begin with a letter, then consist of letters, digits, +, ., or - if (!/^[a-z][a-z0-9\+\-\.]*$/.test(scheme.toLowerCase())) return null; // re-assemble the URL per section 5.3 in RFC 3986 out += scheme + ':'; if (authority && authority.length) { out += '//' + authority; } out += path; if (query && query.length) { out += '?' + query; } if (fragment && fragment.length) { out += '#' + fragment; } return out; } function readFileUri(url) { return new Promise(function (resolve, reject) { axios.get(url, { responseType: 'arraybuffer' }).then(function (res) { resolve(res.data); })["catch"](function (err) { reject(err); }); }); } /** * read file weather from uri or fir path * @param {string} file filepath or url * @returns {Promise} */ function readFile(file) { if (isUri(file)) { return readFileUri(file); } return new Promise(function (resolve, reject) { fs.readFile(file, function (error, data) { if (error) { reject(error); } else { resolve(data); } }); }); } module.exports = { isUri: isUri, readFileUri: readFileUri, readFile: readFile }; //# sourceMappingURL=fileUtils.js.map