UNPKG

@onesy/utils

Version:
18 lines (13 loc) 546 B
import isEnvironment from './isEnvironment'; const download = function (name, data) { let type = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'application/json'; if (isEnvironment('browser')) { const a = document.createElement('a'); a.download = name; a.href = data.startsWith("data:") ? data : "data:".concat(type, ";charset=utf-8,").concat(encodeURIComponent(data)); document.body.appendChild(a); // Trigger data download a.click(); // Clean up a.remove(); } }; export default download;