@t7/utils
Version:
Utility methods for T7 components.
64 lines (41 loc) • 2.01 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
/*
This method allows you to save data as a `console.json`
file, when perusing a large JSON object via console.log
would be too time consuming and/or laborious. Enjoy! :)
// Called like so…
save(json, 'my_file_name.json')
*/
var save = function save() {
var data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var fileName = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'console.json';
/*
Ensure all the necessary featuers are
available to actually create a file.
*/
var hasFeatures = typeof window.Blob === 'function' && typeof window.URL === 'function' && typeof document.createElement === 'function' && typeof document.createEvent === 'function'; // Early exit.
if (!hasFeatures) {
return;
} // Format nicely.
if (_typeof(data) === 'object') {
data = JSON.stringify(data, null, 2);
} // Set type.
var type = 'text/json'; // Create blob.
var blob = new window.Blob([data], {
type: type
}); // Create event.
var e = document.createEvent('MouseEvents'); // Tailor event.
e.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null); // Create tag.
var a = document.createElement('a'); // Use file name.
a.download = fileName; // Set the path.
a.href = window.URL.createObjectURL(blob); // Set the URL.
a.dataset.downloadurl = [type, a.download, a.href].join(':'); // Fire event.
a.dispatchEvent(e);
}; // Expose function.
var _default = save;
exports.default = _default;
;