Skip to content

Instantly share code, notes, and snippets.

View WebReflection's full-sized avatar
🎯
Focusing

Andrea Giammarchi WebReflection

🎯
Focusing
View GitHub Profile
#!/usr/bin/env gjs
(function (Gtk){'use strict';
Gtk.init(null);
const
win = new Gtk.Window({
type : Gtk.WindowType.TOPLEVEL,
window_position: Gtk.WindowPosition.CENTER
})
;
@WebReflection
WebReflection / base64.js
Last active April 29, 2017 02:44
Quite possibly the smallest base 64 utility out there.
// base64.decode(base64.encode('💩'));
// Browsers
const base64 = {
encode: str => btoa(unescape(encodeURIComponent(str))),
decode: str => decodeURIComponent(escape(atob(str)))
};
/* ES3 compat version
var base64 = {
// ./components/form-secure.js
const components = [
'input-credit-card',
'input-human'
];
export default new Promise(async $export => {
// import all required sub components
await Promise.all(components.map(
// keeping the same pattern
export default await new Promise(async $export => {
// export the module
$export({my: 'module'});
});
// dropping the Promise all together
export default await (async () => {
// await async dependencies
// export the module
// grab the importer
import imports from './imports.js';
// export through the same pattern
export default new Promise(async $export => {
// grab many modules at once
const [a, b] = await imports(
import('./a.js'),
import('./b.js')
// ES2017 version
export default (...modules) => Promise.all(
// flatten arguments to enable both
// imports([m1, m2]) or imports(m1, m2)
modules.concat.apply([], modules)
// asynchronously await each module
// and return its default
.map(async m => (await m).default)
);
// CommonJS consumer and/or importer
module.exports = new Promise(async $export => {
const module = await require('./module');
$export({module, method(){}});
});
// ES2017 Asynchronous Export
// module.js
export default new Promise(async $export => {
const module = await Promise.resolve(
{my: 'module'}
);
$export(module);
});
// - - - - - - - - - - - - - - - - - - - - - - - - - - - -
export default new Promise(async $export => {
// await anything that needs to be imported
// await anything that asynchronous
// finally export the module resolving the Promise
// as object, function, class, ... anything
$export(
{module: 'object'} ||
function () {} ||
class Anything {}
);
<!doctype html>
<!-- all it takes, you could also use unpkg:
https://unpkg.com/import.js@latest -->
<script src="import.js"
data-main="js/main.js"
></script>