chore: add code formatting and linting (#952)

This commit is contained in:
Caleb
2018-08-07 14:46:54 -06:00
committed by Shawn Erquhart
parent 32e0a9b2b5
commit f801b19221
265 changed files with 5988 additions and 4481 deletions

View File

@ -1,7 +1,7 @@
export const API_ERROR = 'API_ERROR';
export default class APIError extends Error {
constructor(message, status, api, meta={}) {
constructor(message, status, api, meta = {}) {
super(message);
this.message = message;
this.status = status;

View File

@ -1,4 +1,4 @@
import { fromJS, Map, Set } from "immutable";
import { fromJS, Map, Set } from 'immutable';
const jsToMap = obj => {
if (obj === undefined) {
@ -6,12 +6,12 @@ const jsToMap = obj => {
}
const immutableObj = fromJS(obj);
if (!Map.isMap(immutableObj)) {
throw new Error("Object must be equivalent to a Map.");
throw new Error('Object must be equivalent to a Map.');
}
return immutableObj;
};
const knownMetaKeys = Set(["index", "count", "pageSize", "pageCount", "usingOldPaginationAPI"]);
const knownMetaKeys = Set(['index', 'count', 'pageSize', 'pageCount', 'usingOldPaginationAPI']);
const filterUnknownMetaKeys = meta => meta.filter((v, k) => knownMetaKeys.has(k));
/*
@ -21,9 +21,10 @@ const filterUnknownMetaKeys = meta => meta.filter((v, k) => knownMetaKeys.has(k)
- (actions: <array/List>, data: <object/Map>, meta: <optional object/Map>) -> cursor
*/
const createCursorMap = (...args) => {
const { actions, data, meta } = args.length === 1
? jsToMap(args[0]).toObject()
: { actions: args[0], data: args[1], meta: args[2] };
const { actions, data, meta } =
args.length === 1
? jsToMap(args[0]).toObject()
: { actions: args[0], data: args[1], meta: args[2] };
return Map({
// actions are a Set, rather than a List, to ensure an efficient .has
actions: Set(actions),
@ -34,10 +35,13 @@ const createCursorMap = (...args) => {
});
};
const hasAction = (cursorMap, action) => cursorMap.hasIn(["actions", action]);
const hasAction = (cursorMap, action) => cursorMap.hasIn(['actions', action]);
const getActionHandlers = (cursorMap, handler) =>
cursorMap.get("actions", Set()).toMap().map(action => handler(action));
cursorMap
.get('actions', Set())
.toMap()
.map(action => handler(action));
// The cursor logic is entirely functional, so this class simply
// provides a chainable interface
@ -52,9 +56,9 @@ export default class Cursor {
}
this.store = createCursorMap(...args);
this.actions = this.store.get("actions");
this.data = this.store.get("data");
this.meta = this.store.get("meta");
this.actions = this.store.get('actions');
this.data = this.store.get('data');
this.meta = this.store.get('meta');
}
updateStore(...args) {
@ -68,42 +72,45 @@ export default class Cursor {
return hasAction(this.store, action);
}
addAction(action) {
return this.updateStore("actions", actions => actions.add(action));
return this.updateStore('actions', actions => actions.add(action));
}
removeAction(action) {
return this.updateStore("actions", actions => actions.delete(action));
return this.updateStore('actions', actions => actions.delete(action));
}
setActions(actions) {
return this.updateStore(store => store.set("actions", Set(actions)));
return this.updateStore(store => store.set('actions', Set(actions)));
}
mergeActions(actions) {
return this.updateStore("actions", oldActions => oldActions.union(actions));
return this.updateStore('actions', oldActions => oldActions.union(actions));
}
getActionHandlers(handler) {
return getActionHandlers(this.store, handler);
}
setData(data) {
return new Cursor(this.store.set("data", jsToMap(data)));
return new Cursor(this.store.set('data', jsToMap(data)));
}
mergeData(data) {
return new Cursor(this.store.mergeIn(["data"], jsToMap(data)));
return new Cursor(this.store.mergeIn(['data'], jsToMap(data)));
}
wrapData(data) {
return this.updateStore("data", oldData => jsToMap(data).set("wrapped_cursor_data", oldData));
return this.updateStore('data', oldData => jsToMap(data).set('wrapped_cursor_data', oldData));
}
unwrapData() {
return [this.store.get("data").delete("wrapped_cursor_data"), this.updateStore("data", data => data.get("wrapped_cursor_data"))];
return [
this.store.get('data').delete('wrapped_cursor_data'),
this.updateStore('data', data => data.get('wrapped_cursor_data')),
];
}
clearData() {
return this.updateStore("data", () => Map());
return this.updateStore('data', () => Map());
}
setMeta(meta) {
return this.updateStore(store => store.set("meta", jsToMap(meta)));
return this.updateStore(store => store.set('meta', jsToMap(meta)));
}
mergeMeta(meta) {
return this.updateStore(store => store.update("meta", oldMeta => oldMeta.merge(jsToMap(meta))))
return this.updateStore(store => store.update('meta', oldMeta => oldMeta.merge(jsToMap(meta))));
}
}
@ -112,4 +119,4 @@ export default class Cursor {
// backends at all. This should be removed in favor of wrapping old
// backends with a compatibility layer, as part of the backend API
// refactor.
export const CURSOR_COMPATIBILITY_SYMBOL = Symbol("cursor key for compatibility with old backends");
export const CURSOR_COMPATIBILITY_SYMBOL = Symbol('cursor key for compatibility with old backends');

View File

@ -2,100 +2,52 @@ import { fileExtensionWithSeparator, fileExtension } from '../path';
describe('fileExtensionWithSeparator', () => {
it('should return the extension of a file', () => {
expect(
fileExtensionWithSeparator('index.html')
).toEqual(
'.html'
);
expect(fileExtensionWithSeparator('index.html')).toEqual('.html');
});
it('should return the extension of a file path', () => {
expect(
fileExtensionWithSeparator('/src/main/index.html')
).toEqual(
'.html'
);
expect(fileExtensionWithSeparator('/src/main/index.html')).toEqual('.html');
});
it('should return the extension of a file path with trailing slash', () => {
expect(
fileExtensionWithSeparator('/src/main/index.html/')
).toEqual(
'.html'
);
expect(fileExtensionWithSeparator('/src/main/index.html/')).toEqual('.html');
});
it('should return the extension for an extension with two ..', () => {
expect(
fileExtensionWithSeparator('/src/main/index..html')
).toEqual(
'.html'
);
expect(fileExtensionWithSeparator('/src/main/index..html')).toEqual('.html');
});
it('should return an empty string for the parent path ..', () => {
expect(
fileExtensionWithSeparator('..')
).toEqual(
''
);
expect(fileExtensionWithSeparator('..')).toEqual('');
});
it('should return an empty string if the file has no extension', () => {
expect(
fileExtensionWithSeparator('/src/main/index')
).toEqual(
''
);
expect(fileExtensionWithSeparator('/src/main/index')).toEqual('');
});
});
describe('fileExtension', () => {
it('should return the extension of a file', () => {
expect(
fileExtension('index.html')
).toEqual(
'html'
);
expect(fileExtension('index.html')).toEqual('html');
});
it('should return the extension of a file path', () => {
expect(
fileExtension('/src/main/index.html')
).toEqual(
'html'
);
expect(fileExtension('/src/main/index.html')).toEqual('html');
});
it('should return the extension of a file path with trailing slash', () => {
expect(
fileExtension('/src/main/index.html/')
).toEqual(
'html'
);
expect(fileExtension('/src/main/index.html/')).toEqual('html');
});
it('should return the extension for an extension with two ..', () => {
expect(
fileExtension('/src/main/index..html')
).toEqual(
'html'
);
expect(fileExtension('/src/main/index..html')).toEqual('html');
});
it('should return an empty string for the parent path ..', () => {
expect(
fileExtension('..')
).toEqual(
''
);
expect(fileExtension('..')).toEqual('');
});
it('should return an empty string if the file has no extension', () => {
expect(
fileExtension('/src/main/index')
).toEqual(
''
);
expect(fileExtension('/src/main/index')).toEqual('');
});
});

View File

@ -1,6 +1,6 @@
import { get } from "lodash";
import { fromJS } from "immutable";
import { fileExtension } from "./path";
import { get } from 'lodash';
import { fromJS } from 'immutable';
import { fileExtension } from './path';
export const filterByPropExtension = (extension, propName) => arr =>
arr.filter(el => fileExtension(get(el, propName)) === extension);
@ -9,31 +9,31 @@ const catchFormatErrors = (format, formatter) => res => {
try {
return formatter(res);
} catch (err) {
throw new Error(`Response cannot be parsed into the expected format (${ format }): ${ err.message }`);
throw new Error(
`Response cannot be parsed into the expected format (${format}): ${err.message}`,
);
}
};
const responseFormatters = fromJS({
json: async res => {
const contentType = res.headers.get("Content-Type");
if (!contentType.startsWith("application/json") && !contentType.startsWith("text/json")) {
throw new Error(`${ contentType } is not a valid JSON Content-Type`);
const contentType = res.headers.get('Content-Type');
if (!contentType.startsWith('application/json') && !contentType.startsWith('text/json')) {
throw new Error(`${contentType} is not a valid JSON Content-Type`);
}
return res.json();
},
text: async res => res.text(),
blob: async res => res.blob(),
}).mapEntries(
([format, formatter]) => [format, catchFormatErrors(format, formatter)]
);
}).mapEntries(([format, formatter]) => [format, catchFormatErrors(format, formatter)]);
export const parseResponse = async (res, { expectingOk = true, format = "text" } = {}) => {
export const parseResponse = async (res, { expectingOk = true, format = 'text' } = {}) => {
if (expectingOk && !res.ok) {
throw new Error(`Expected an ok response, but received an error status: ${ res.status }.`);
throw new Error(`Expected an ok response, but received an error status: ${res.status}.`);
}
const formatter = responseFormatters.get(format, false);
if (!formatter) {
throw new Error(`${ format } is not a supported response format.`);
throw new Error(`${format} is not a supported response format.`);
}
const body = await formatter(res);
return body;

View File

@ -1,16 +1,19 @@
import localForage from "localforage";
import localForage from 'localforage';
function localForageTest() {
const testKey = 'localForageTest';
localForage.setItem(testKey, {expires: Date.now() + 300000}).then(() => {
localForage.removeItem(testKey);
}).catch((err) => {
if (err.code === 22) {
const message = `Unable to set localStorage key. Quota exceeded! Full disk?`;
return alert(`${message}\n\n${err}`);
}
console.log(err);
})
localForage
.setItem(testKey, { expires: Date.now() + 300000 })
.then(() => {
localForage.removeItem(testKey);
})
.catch(err => {
if (err.code === 22) {
const message = `Unable to set localStorage key. Quota exceeded! Full disk?`;
return alert(`${message}\n\n${err}`);
}
console.log(err);
});
}
localForageTest();

View File

@ -10,11 +10,11 @@ export function resolvePath(path, basePath) {
if (path.indexOf('/') === -1) {
// It's a single file name, no directories. Prepend public folder
return normalizePath(`/${ basePath }/${ path }`);
return normalizePath(`/${basePath}/${path}`);
}
// It's a relative path. Prepend a forward slash.
return normalizePath(`/${ path }`);
return normalizePath(`/${path}`);
}
/**
@ -28,7 +28,7 @@ export function resolvePath(path, basePath) {
* // returns
* 'quux'
*/
export function basename(p, ext = "") {
export function basename(p, ext = '') {
// Special case: Normalize will modify this to '.'
if (p === '') {
return p;

View File

@ -1,22 +1,20 @@
import zipObject from 'lodash/zipObject';
export const filterPromises = (arr, filter) =>
Promise.all(arr.map(entry => filter(entry)))
.then(bits => arr.filter(() => bits.shift()));
Promise.all(arr.map(entry => filter(entry))).then(bits => arr.filter(() => bits.shift()));
export const resolvePromiseProperties = (obj) => {
export const resolvePromiseProperties = obj => {
// Get the keys which represent promises
const promiseKeys = Object.keys(obj).filter(
key => typeof obj[key].then === "function");
const promiseKeys = Object.keys(obj).filter(key => typeof obj[key].then === 'function');
const promises = promiseKeys.map(key => obj[key]);
// Resolve all promises
return Promise.all(promises)
.then(resolvedPromises =>
return Promise.all(promises).then(resolvedPromises =>
// Return a copy of obj with promises overwritten by their
// resolved values
Object.assign({}, obj, zipObject(promiseKeys, resolvedPromises)));
Object.assign({}, obj, zipObject(promiseKeys, resolvedPromises)),
);
};
export const then = fn => p => Promise.resolve(p).then(fn);

View File

@ -3,26 +3,40 @@ import curry from 'lodash/curry';
import flow from 'lodash/flow';
import isString from 'lodash/isString';
const decodeParams = paramsString => List(paramsString.split("&"))
.map(s => List(s.split("=")).map(decodeURIComponent))
.update(Map);
const decodeParams = paramsString =>
List(paramsString.split('&'))
.map(s => List(s.split('=')).map(decodeURIComponent))
.update(Map);
const fromURL = wholeURL => {
const [url, allParamsString] = wholeURL.split("?");
const [url, allParamsString] = wholeURL.split('?');
return Map({ url, ...(allParamsString ? { params: decodeParams(allParamsString) } : {}) });
};
const encodeParams = params => params.entrySeq()
.map(([k, v]) => `${ encodeURIComponent(k) }=${ encodeURIComponent(v) }`)
.join("&");
const encodeParams = params =>
params
.entrySeq()
.map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(v)}`)
.join('&');
const toURL = req => `${ req.get("url") }${ req.get("params") ? `?${ encodeParams(req.get("params")) }` : "" }`;
const toURL = req =>
`${req.get('url')}${req.get('params') ? `?${encodeParams(req.get('params'))}` : ''}`;
const toFetchArguments = req => [toURL(req), req.delete("url").delete("params").toJS()];
const toFetchArguments = req => [
toURL(req),
req
.delete('url')
.delete('params')
.toJS(),
];
const maybeRequestArg = req => {
if (isString(req)) { return fromURL(req); }
if (req) { return fromJS(req); }
if (isString(req)) {
return fromURL(req);
}
if (req) {
return fromJS(req);
}
return Map();
};
const ensureRequestArg = func => req => func(maybeRequestArg(req));
@ -41,23 +55,27 @@ const getPropSetFunctions = path => [
getCurriedRequestProcessor((val, req) => (req.getIn(path) ? req : req.setIn(path, val))),
];
const getPropMergeFunctions = path => [
getCurriedRequestProcessor((obj, req) => req.updateIn(path, (p=Map()) => p.merge(obj))),
getCurriedRequestProcessor((obj, req) => req.updateIn(path, (p=Map()) => Map(obj).merge(p))),
getCurriedRequestProcessor((obj, req) => req.updateIn(path, (p = Map()) => p.merge(obj))),
getCurriedRequestProcessor((obj, req) => req.updateIn(path, (p = Map()) => Map(obj).merge(p))),
];
const [withMethod, withDefaultMethod] = getPropSetFunctions(["method"]);
const [withBody, withDefaultBody] = getPropSetFunctions(["body"]);
const [withParams, withDefaultParams] = getPropMergeFunctions(["params"]);
const [withHeaders, withDefaultHeaders] = getPropMergeFunctions(["headers"]);
const [withMethod, withDefaultMethod] = getPropSetFunctions(['method']);
const [withBody, withDefaultBody] = getPropSetFunctions(['body']);
const [withParams, withDefaultParams] = getPropMergeFunctions(['params']);
const [withHeaders, withDefaultHeaders] = getPropMergeFunctions(['headers']);
// withRoot sets a root URL, unless the URL is already absolute
const absolutePath = new RegExp('^(?:[a-z]+:)?//', 'i');
const withRoot = getCurriedRequestProcessor((root, req) => req.update("url", p => {
if (absolutePath.test(p)) { return p; }
return (root && p && p[0] !== "/" && root[root.length - 1] !== "/")
? `${ root }/${ p }`
: `${ root }${ p }`;
}));
const withRoot = getCurriedRequestProcessor((root, req) =>
req.update('url', p => {
if (absolutePath.test(p)) {
return p;
}
return root && p && p[0] !== '/' && root[root.length - 1] !== '/'
? `${root}/${p}`
: `${root}${p}`;
}),
);
// withTimestamp needs no argument and has to run as late as possible,
// so it calls `withParams` only when it's actually called with a