Clean code.

This commit is contained in:
Lars Jung 2016-06-19 23:05:31 +02:00
parent 324fceb5c3
commit fe9cdbc46b
3 changed files with 20 additions and 39 deletions

View file

@ -1,33 +1,27 @@
const {lo} = require('../globals');
const isStr = x => typeof x === 'string';
const isFn = x => typeof x === 'function';
const subscriptions = {};
function sub(topic, listener) {
if (lo.isString(topic) && lo.isFunction(listener)) {
const sub = (topic, listener) => {
if (isStr(topic) && isFn(listener)) {
if (!subscriptions[topic]) {
subscriptions[topic] = [];
}
subscriptions[topic].push(listener);
}
}
};
function unsub(topic, listener) {
if (lo.isString(topic) && lo.isFunction(listener) && subscriptions[topic]) {
subscriptions[topic] = lo.without(subscriptions[topic], listener);
}
}
function pub(topic, ...args) {
const pub = (topic, ...args) => {
// console.log(topic, args);
if (lo.isString(topic) && subscriptions[topic]) {
lo.each(subscriptions[topic], listener => {
if (isStr(topic) && subscriptions[topic]) {
subscriptions[topic].forEach(listener => {
listener.apply(topic, args);
});
}
}
};
module.exports = {
sub,
unsub,
pub
};

View file

@ -1,6 +1,6 @@
const {jq, lo} = require('./globals');
function request(data) {
const request = data => {
return new Promise(resolve => {
jq.ajax({
url: '?',
@ -11,9 +11,9 @@ function request(data) {
.done(json => resolve(json))
.fail(() => resolve());
});
}
};
function formRequest(data) {
const formRequest = data => {
const $form = jq('<form method="post" action="?" style="display:none;"/>');
lo.each(data, (val, key) => {
@ -24,7 +24,7 @@ function formRequest(data) {
});
$form.appendTo('body').submit().remove();
}
};
module.exports = {
request,

View file

@ -1,6 +1,6 @@
/* eslint-disable func-names,no-var */
(function (win) {
if (typeof win !== 'object' || win.window !== win || !win.document) {
if (!win || win.window !== win || !win.document) {
throw new Error('no-window');
}
@ -15,26 +15,13 @@
}
}
assert('console', win.console && typeof win.console.log === 'function');
assert('assign', win.Object && typeof win.Object.assign === 'function');
assert('promise', win.Promise && typeof win.Promise === 'function');
assert('history', win.history && typeof win.history.pushState === 'function');
function isFn(x) {
return typeof x === 'function';
}
assert('canvas', (function () {
var elem = win.document.createElement('canvas');
return elem.getContext && elem.getContext('2d');
}()));
assert('storage', (function () {
var key = '#test#';
try {
win.localStorage.setItem(key, key);
win.localStorage.removeItem(key);
return true;
} catch (e) {
return false;
}
}()));
assert('console', win.console && isFn(win.console.log));
assert('assign', win.Object && isFn(win.Object.assign));
assert('promise', isFn(win.Promise));
}(this));
/* eslint-enable */