Browse Source

split sources into separate modules

mightyplow 7 years ago
parent
commit
b4e5579358

+ 0 - 46
lib/array.js

@@ -1,46 +0,0 @@
-'use strict';
-
-const checkArray = val => {
-    if (!Array.isArray(val)) {
-        throw Error('value must be an array');
-    }
-};
-
-module.exports = {
-    toArray: arraylike => [...arraylike],
-
-    ensureArray: (val = []) => Array.isArray(val) ? val : [val],
-
-    head (ar) {
-        checkArray(ar);
-
-        const [head, ...rest] = ar;
-        return head;
-    },
-
-    tail (ar) {
-        checkArray(ar);
-
-        const [ tail ] = ar.slice(-1);
-        return tail;
-    },
-
-    unique (ar) {
-        checkArray(ar);
-        return ar.filter((val, i, input) => i === input.indexOf(val));
-    },
-
-    flatten: function flatten (ar) {
-        checkArray(ar);
-
-        return ar.reduce((acc, item) => {
-            if (Array.isArray(item)) {
-                acc.push(...flatten(item));
-            } else {
-                acc.push(item);
-            }
-
-            return acc;
-        }, []);
-    }
-};

+ 7 - 0
lib/array/_checkArray.js

@@ -0,0 +1,7 @@
+const checkArray = val => {
+    if (!Array.isArray(val)) {
+        throw Error('value must be an array');
+    }
+};
+
+export default checkArray;

+ 3 - 0
lib/array/ensureArray.js

@@ -0,0 +1,3 @@
+const ensureArray = (val = []) => Array.isArray(val) ? val : [val];
+
+export default ensureArray;

+ 15 - 0
lib/array/flatten.js

@@ -0,0 +1,15 @@
+import checkArray  from './_checkArray';
+
+export default (ar) => {
+    checkArray(ar);
+
+    return ar.reduce((acc, item) => {
+        if (Array.isArray(item)) {
+            acc.push(...flatten(item));
+        } else {
+            acc.push(item);
+        }
+
+        return acc;
+    }, []);
+};

+ 8 - 0
lib/array/head.js

@@ -0,0 +1,8 @@
+import checkArray from "./_checkArray";
+
+const head = (ar) => {
+    checkArray(ar);
+
+    const [head, ...rest] = ar;
+    return head;
+};

+ 6 - 0
lib/array/index.js

@@ -0,0 +1,6 @@
+export { default as ensureArray} from './ensureArray';
+export { default as flatten } from './flatten';
+export { default as head } from './head';
+export { default as tail } from './tail';
+export { default as to } from './toArray';
+export { default as unique } from './unique';

+ 10 - 0
lib/array/tail.js

@@ -0,0 +1,10 @@
+import checkArray from "./_checkArray";
+
+const tail = (ar) => {
+    checkArray(ar);
+
+    const [ tail ] = ar.slice(-1);
+    return tail;
+};
+
+export default tail;

+ 3 - 0
lib/array/toArray.js

@@ -0,0 +1,3 @@
+const toArray = arraylike => [...arraylike];
+
+export default toArray;

+ 6 - 0
lib/array/unique.js

@@ -0,0 +1,6 @@
+import checkArray from "./_checkArray";
+
+export default (ar) => {
+    checkArray(ar);
+    return ar.filter((val, i, input) => i === input.indexOf(val));
+}

+ 0 - 29
lib/file.js

@@ -1,29 +0,0 @@
-'use strict';
-
-const fs = require('fs');
-
-module.exports = {
-    readFile: filename => {
-        return new Promise((resolve, reject) => {
-            fs.readFile(filename, (err, data) => {
-                if (err) {
-                    reject(err);
-                } else {
-                    resolve(data);
-                }
-            });
-        });
-    },
-
-    writeFile: (filename, data) => {
-        return new Promise((resolve, reject) => {
-            fs.writeFile(filename, data, err => {
-                if (err) {
-                    reject(err);
-                } else {
-                    resolve();
-                }
-            });
-        });
-    }
-};

+ 0 - 93
lib/function.js

@@ -1,93 +0,0 @@
-'use strict';
-
-const toArray = require('./array').toArray;
-
-const argsToArray = fn => {
-    // must not be a fat arrow since it breaks the arguments
-    return function () {
-        return fn(toArray(arguments));
-    };
-};
-
-const curry = fn => {
-    return function curried (...args) {
-        const that = this;
-
-        if (args.length < fn.length) {
-            return curried.bind.apply(curried, [that].concat(args));
-        } else {
-            return fn.apply(that, args);
-        }
-    };
-};
-
-const combine = argsToArray(fns => fns.reverse().reduce((fnc, fn) => val => fnc(fn(val))));
-
-// cannot use fat arrow function since we don't get the arguments, then
-const promisify = (fn, context = null) => function () {
-    // copy passed arguments to a new array
-    const args = toArray(arguments);
-
-    return new Promise((resolve, reject) => {
-        // add a callback to the arguments, which resolves the promise with the result
-        args.push((err, data) => {
-            if (err) {
-                reject(err);
-            } else {
-                resolve(data);
-            }
-        });
-
-        // call the original function with our callback flavoured arguments
-        fn.apply(context, args);
-    });
-};
-
-/**
- returns a function which executes promises one after another
- @param promiseGenerators an array of functions which return a promise
- @return function which executes the promises
- */
-const enqueue = (promiseGenerators) => {
-    return promiseGenerators.reduce((f, promiseGenerator) => {
-        return () => {
-            return f().then(promiseGenerator);
-        };
-    });
-};
-
-/**
- Returns a function which executes promises one after another. The resulting function
- returns a promise, which gets filled with an array of the results of the single promises.
- @param promiseGenerators an array of functions which return a promise
- @return function which executes the promises
- */
-const enqueueWithResults = (function () {
-    const fnQueue = (results, promiseGenerators) => promiseGenerators.reduce((f, promiseGenerator) => {
-        return () => {
-            return f().then(result => {
-                results.push(result);
-                return promiseGenerator();
-            });
-        };
-    });
-
-    return function (promiseGenerators) {
-        return () => {
-            const results = [];
-            return fnQueue(results, promiseGenerators)().then(result => {
-                results.push(result);
-                return results;
-            });
-        };
-    };
-}());
-
-module.exports = {
-    argsToArray,
-    combine,
-    curry,
-    promisify,
-    enqueue,
-    enqueueWithResults
-};

+ 10 - 0
lib/function/argsToArray.js

@@ -0,0 +1,10 @@
+import toArray from "../array/toArray";
+
+const argsToArray = fn => {
+    // must not be a fat arrow since it breaks the arguments
+    return function () {
+        return fn(toArray(arguments));
+    };
+};
+
+export default argsToArray;

+ 3 - 0
lib/function/combine.js

@@ -0,0 +1,3 @@
+const combine = argsToArray(fns => fns.reverse().reduce((fnc, fn) => val => fnc(fn(val))));
+
+export default combine;

+ 13 - 0
lib/function/curry.js

@@ -0,0 +1,13 @@
+const curry = fn => {
+    return function curried (...args) {
+        const that = this;
+
+        if (args.length < fn.length) {
+            return curried.bind.apply(curried, [that].concat(args));
+        } else {
+            return fn.apply(that, args);
+        }
+    };
+};
+
+export default curry;

+ 14 - 0
lib/function/enqueue.js

@@ -0,0 +1,14 @@
+/**
+ returns a function which executes promises one after another
+ @param promiseGenerators an array of functions which return a promise
+ @return function which executes the promises
+ */
+const enqueue = (promiseGenerators) => {
+    return promiseGenerators.reduce((f, promiseGenerator) => {
+        return () => {
+            return f().then(promiseGenerator);
+        };
+    });
+};
+
+export default enqueue;

+ 28 - 0
lib/function/enqueueWithResults.js

@@ -0,0 +1,28 @@
+/**
+ Returns a function which executes promises one after another. The resulting function
+ returns a promise, which gets filled with an array of the results of the single promises.
+ @param promiseGenerators an array of functions which return a promise
+ @return function which executes the promises
+ */
+const enqueueWithResults = (function () {
+    const fnQueue = (results, promiseGenerators) => promiseGenerators.reduce((f, promiseGenerator) => {
+        return () => {
+            return f().then(result => {
+                results.push(result);
+                return promiseGenerator();
+            });
+        };
+    });
+
+    return function (promiseGenerators) {
+        return () => {
+            const results = [];
+            return fnQueue(results, promiseGenerators)().then(result => {
+                results.push(result);
+                return results;
+            });
+        };
+    };
+}());
+
+export default enqueueWithResults;

+ 6 - 0
lib/function/index.js

@@ -0,0 +1,6 @@
+export { default as argsToArray } from './argsToArray';
+export { default as combine } from './combine';
+export { default as curry } from './curry';
+export { default as enqueue } from './enqueue';
+export { default as enqueueWithResults } from './enqueueWithResults';
+export { default as promisify } from './promisify';

+ 22 - 0
lib/function/promisify.js

@@ -0,0 +1,22 @@
+// cannot use fat arrow function since we don't get the arguments, then
+const promisify = (fn, context = null) => function () {
+    // copy passed arguments to a new array
+    const args = toArray(arguments);
+
+    return new Promise((resolve, reject) => {
+        // add a callback to the arguments, which resolves the promise with the result
+        args.push((err, data) => {
+            if (err) {
+                reject(err);
+            } else {
+                resolve(data);
+            }
+        });
+
+        // call the original function with our callback flavoured arguments
+        fn.apply(context, args);
+    });
+};
+
+
+export default promisify;

+ 0 - 11
lib/html.js

@@ -1,11 +0,0 @@
-'use strict';
-
-module.exports = {
-    createLinkTag: (filename, mediaQuery) => {
-        if (mediaQuery) {
-            return `<link rel="stylesheet" type="text/css" href="${filename}" media="${mediaQuery}" />`;
-        }
-
-        return `<link rel="stylesheet" type="text/css" href="${filename}" />`;
-    }
-}

+ 9 - 0
lib/html/createLinkTag.js

@@ -0,0 +1,9 @@
+const createLinkTag = (filename, mediaQuery) => {
+    if (mediaQuery) {
+        return `<link rel="stylesheet" type="text/css" href="${filename}" media="${mediaQuery}" />`;
+    }
+
+    return `<link rel="stylesheet" type="text/css" href="${filename}" />`;
+};
+
+export default createLinkTag;

+ 1 - 0
lib/html/index.js

@@ -0,0 +1 @@
+export { default as createLinkTag } from './createLinkTag';

+ 6 - 0
lib/index.js

@@ -0,0 +1,6 @@
+export * from './array';
+export * from './function';
+export * from './html';
+export * from './object';
+export * from './string';
+export * from './stylesheet';

+ 0 - 25
lib/object.js

@@ -1,25 +0,0 @@
-'use strict';
-
-module.exports = {
-    prop: prop => obj => obj[prop],
-
-    filter (obj, pred) {
-        return Object.keys(obj).reduce((filtered, key) => {
-            const value = obj[key];
-
-            if (pred(key, value)) {
-                filtered[key] = value;
-            }
-
-            return filtered;
-        }, {});
-    },
-
-    defaults (obj, values) {
-        Object.keys(values).forEach(key => {
-            if (!obj.hasOwnProperty(key)) {
-                obj[key] = values[key];
-            }
-        });
-    }
-};

+ 9 - 0
lib/object/defaults.js

@@ -0,0 +1,9 @@
+const defaults = (obj, values) => {
+    Object.keys(values).forEach(key => {
+        if (!obj.hasOwnProperty(key)) {
+            obj[key] = values[key];
+        }
+    });
+};
+
+export default defaults;

+ 13 - 0
lib/object/filter.js

@@ -0,0 +1,13 @@
+const filter = (obj, pred) => {
+    return Object.keys(obj).reduce((filtered, key) => {
+        const value = obj[key];
+
+        if (pred(key, value)) {
+            filtered[key] = value;
+        }
+
+        return filtered;
+    }, {});
+};
+
+export default filter;

+ 3 - 0
lib/object/index.js

@@ -0,0 +1,3 @@
+export { default as defaults } from './defaults';
+export { default as filter } from './filter';
+export { default as prop } from './prop';

+ 3 - 0
lib/object/prop.js

@@ -0,0 +1,3 @@
+const prop = prop => obj => obj[prop];
+
+export default prop;

+ 0 - 7
lib/string.js

@@ -1,7 +0,0 @@
-'use strict';
-
-module.exports = {
-    append: (str, append) => str + append,
-    toBool: val => val === 'true',
-    ciCompare: (val1 = '', val2 = '') => val1.toLowerCase() === val2.toLowerCase()
-};

+ 3 - 0
lib/string/append.js

@@ -0,0 +1,3 @@
+const append = (str, append) => str + append;
+
+export default append;

+ 3 - 0
lib/string/ciCompare.js

@@ -0,0 +1,3 @@
+const ciCompare = (val1 = '', val2 = '') => val1.toLowerCase() === val2.toLowerCase();
+
+export default ciCompare;

+ 3 - 0
lib/string/index.js

@@ -0,0 +1,3 @@
+export { default as append } from './append';
+export { default as ciCompare } from './ciCompare';
+export { default as toBool } from './toBool';

+ 3 - 0
lib/string/toBool.js

@@ -0,0 +1,3 @@
+const toBool = val => val === 'true';
+
+export default toBool;

+ 0 - 15
lib/stylesheet.js

@@ -1,15 +0,0 @@
-'use strict';
-
-module.exports = {
-    getRules: css => css.stylesheet.rules,
-    isRuleType: type => rule => rule.type === type,
-
-    createStylesheetAst: rules => {
-        return {
-            type: 'stylesheet',
-            stylesheet: {
-                rules: rules
-            }
-        };
-    }
-};

+ 10 - 0
lib/stylesheet/createStylesheetAst.js

@@ -0,0 +1,10 @@
+const createStylesheetAst = rules => {
+    return {
+        type: 'stylesheet',
+        stylesheet: {
+            rules: rules
+        }
+    };
+};
+
+export default createStylesheetAst;

+ 3 - 0
lib/stylesheet/getRules.js

@@ -0,0 +1,3 @@
+const getRules = css => css.stylesheet.rules;
+
+export default getRules;

+ 3 - 0
lib/stylesheet/index.js

@@ -0,0 +1,3 @@
+export { default as createStylesheetAst } from './createStylesheetAst';
+export { default as getRules } from './getRules';
+export { default as isRuleType } from './isRuleType';

+ 3 - 0
lib/stylesheet/isRuleType.js

@@ -0,0 +1,3 @@
+const isRuleType = type => rule => rule.type === type;
+
+export default isRuleType;