From bceb9b225490165d1ca2c428b28d74e2825f2ec1 Mon Sep 17 00:00:00 2001 From: Andrew Vivash Date: Mon, 19 Dec 2016 15:54:54 -0800 Subject: [PATCH] fix: logic related to resolve() method --- .eslintrc | 21 ++------------------- package.json | 26 +++++++++---------------- src/index.js | 4 +--- src/theme/index.js | 8 ++++---- src/utils/index.js | 45 ++++++++++++++++++-------------------------- tests/Themer.spec.js | 20 -------------------- 6 files changed, 34 insertions(+), 90 deletions(-) diff --git a/.eslintrc b/.eslintrc index 79d1644..38aa576 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,21 +1,4 @@ { - parser: "babel-eslint", - "extends": "airbnb", - "rules": { - "max-len": [1, 120, 2, {ignoreComments: true}], - "arrow-body-style": 0, - "prefer-const": 0, - "no-plusplus": 0, - "no-unused-expressions": 0, - "class-methods-use-this": 0, - "no-param-reassign": 0, - "no-prototype-builtins": 0, - "no-restricted-syntax": 0 - }, - "globals": { - "it": false, - "expect": false, - "describe": false, - beforeEach - }, + "parser": "babel-eslint", + "extends": "ca", } diff --git a/package.json b/package.json index eef5f99..8b2acd7 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,9 @@ "test:coverage": "babel-node ./node_modules/.bin/isparta cover --include './src/**/**/*.js' _mocha -- --reporter nyan './tests/**/*.spec.js'", "commit": "git-cz", "build": "babel --out-dir ./lib --ignore *.spec.js, ./src", - "release": "semantic-release pre && npm publish && semantic-release post" + "release": "semantic-release pre && npm publish && semantic-release post", + "commitmsg": "validate-commit-msg", + "precommit": "npm run lint && npm run test" }, "pre-commit": [ "lint", @@ -43,18 +45,15 @@ "commitizen": "2.8.6", "cz-conventional-changelog": "1.2.0", "eslint": "^3.12.0", - "eslint-config-airbnb": "^13.0.0", - "eslint-loader": "^1.6.1", - "eslint-plugin-cssx": "0.3.3", - "eslint-plugin-import": "^2.2.0", - "eslint-plugin-jsx-a11y": "^3.0.1", - "eslint-plugin-react": "6.8.0", + "eslint-plugin-jsx-a11y": "^2.2.3", + "eslint-config-ca": "git+ssh://git@github.com:CAAPIM/eslint-config-ca.git", "isparta": "4.0.0", "istanbul": "1.1.0-alpha.1", "mocha": "3.2.0", "pre-commit": "^1.2.1", "semantic-release": "6.3.2", - "validate-commit-msg": "^2.8.2" + "validate-commit-msg": "^2.8.2", + "husky": "^0.11.9" }, "keywords": [ "JS", @@ -65,15 +64,8 @@ "commitizen": { "path": "cz-conventional-changelog" }, - "config": { - "validate-commit-msg": { - "types": ["feat", "fix", "docs", "style", "refactor", "perf", "test", "chore", "revert"], - "warnOnFail": false, - "maxSubjectLength": 100, - "subjectPattern": ".+", - "subjectPatternErrorMsg": "subject does not match subject pattern!", - "helpMessage": "" - } + "validate-commit-msg": { + "types": "conventional-commit-types" } }, "repository": { diff --git a/src/index.js b/src/index.js index e45ea42..afc71bd 100644 --- a/src/index.js +++ b/src/index.js @@ -13,8 +13,6 @@ export { themer, create }; export default (component, rawTheme) => { themer.setTheme([rawTheme]); - return (props) => { - return themer.render(component, props); - }; + return (props) => themer.render(component, props); }; diff --git a/src/theme/index.js b/src/theme/index.js index f5f0760..a20e8b2 100644 --- a/src/theme/index.js +++ b/src/theme/index.js @@ -6,7 +6,7 @@ import uuid from 'uuid'; import { isObject, isFunction } from 'lodash'; -import { objectHasFunction, flatten, resolve } from './../utils'; +import { arrayHasFunction, flatten, resolve } from './../utils'; export default class Theme { @@ -114,7 +114,7 @@ export default class Theme { combine(themes = []) { const truthy = val => val; - let resolvedTheme = themes + const resolvedTheme = themes .map(Theme.parse) .filter(truthy) .reduce((previousTheme, currentTheme) => ({ @@ -145,7 +145,7 @@ export default class Theme { * @return {Object} Resolved theme variables, where local vars take priority */ getVariables(variables = {}) { - if (isFunction(this.theme.variables) || objectHasFunction(this.theme.variables)) { + if (isFunction(this.theme.variables) || arrayHasFunction(this.theme.variables)) { return resolve(this.theme.variables, variables); } @@ -159,7 +159,7 @@ export default class Theme { * @return {Object} Resolved styles object */ getStyles(variables = {}) { - if (isFunction(this.theme.styles) || objectHasFunction(this.theme.styles)) { + if (isFunction(this.theme.styles) || arrayHasFunction(this.theme.styles)) { return resolve(this.theme.styles, variables); } diff --git a/src/utils/index.js b/src/utils/index.js index 90915e5..f6e7a05 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -4,7 +4,7 @@ * of the MIT license. See the LICENSE file for details. */ -import { isFunction, isObject } from 'lodash'; +import { isFunction, isArray, find } from 'lodash'; import Themer from './../Themer'; /** @@ -18,16 +18,14 @@ export function createThemer(options = {}) { } /** - * Loops through object and checks to see if any values are of type function + * Loops through array and checks to see if any values are of type function * - * @param {Object} obj The array to scan - * @return {Boolean} If any values are of type function + * @param {array} arr The array to scan + * @return {Boolean} If any values are of type function * @public */ -export function objectHasFunction(obj = {}) { - const hasFunction = Object.keys(obj).find((val) => { - return isFunction(obj[val]); - }); +export function arrayHasFunction(arr = []) { + const hasFunction = find(arr, (val) => isFunction(arr[val])); return !!hasFunction; } @@ -40,9 +38,8 @@ export function objectHasFunction(obj = {}) { * @public */ export function flatten(arr) { - return arr.reduce((flat, toFlatten) => { - return flat.concat(Array.isArray(toFlatten) ? flatten(toFlatten) : toFlatten); - }, []); + return arr.reduce((flat, toFlatten) => + flat.concat(Array.isArray(toFlatten) ? flatten(toFlatten) : toFlatten), []); } /** @@ -53,24 +50,18 @@ export function flatten(arr) { * @return {Object} Contains only flat properties, no functions * @public */ -export function resolve(obj, ...args) { - if (isFunction(obj)) { - obj = obj(...args); - } +export function resolve(arrayToResolve, ...args) { + const arr = isArray(arrayToResolve) ? arrayToResolve : [arrayToResolve]; - if (isObject(obj)) { - for (let prop in obj) { - if (Object.prototype.hasOwnProperty.call(obj, prop)) { - if (isFunction(obj[prop])) { - obj[prop] = obj[prop](...args); - } + return arr.reduce((accumulator, val) => { + if (!val) { + return accumulator; + } - if (isObject(obj[prop])) { - obj[prop] = resolve(obj[prop], ...args); - } - } + if (isFunction(val)) { + return val(accumulator, ...args); } - } - return obj; + return val; + }, {}); } diff --git a/tests/Themer.spec.js b/tests/Themer.spec.js index 8a9f234..6636224 100644 --- a/tests/Themer.spec.js +++ b/tests/Themer.spec.js @@ -150,26 +150,6 @@ describe('Themer', () => { expect(resolvedStyles).to.deep.equal(testFuncTheme.styles()); }); - it('should accept theme.styles as a function', () => { - const testFuncTheme = { - styles: () => { - return { - header: () => { - return { - color: 'red', - margin: 0, - }; - }, - }; - }, - }; - - testInstance = create({ themes: [testFuncTheme] }); - const resolvedStyles = testInstance.getThemeStyles(); - - expect(resolvedStyles.header).to.deep.equal(testFuncTheme.styles().header()); - }); - describe('render', () => { it('should provide the rendered HTML snippet with styles resolved and mapped', () => { testInstance = create({ themes: [testTheme] });