Skip to content
This repository has been archived by the owner on Feb 9, 2022. It is now read-only.

Commit

Permalink
fix: logic related to resolve() method
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Vivash committed Dec 19, 2016
1 parent 49abb9e commit bceb9b2
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 90 deletions.
21 changes: 2 additions & 19 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -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",
}
26 changes: 9 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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": {
Expand Down
4 changes: 1 addition & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

8 changes: 4 additions & 4 deletions src/theme/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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) => ({
Expand Down Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand Down
45 changes: 18 additions & 27 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand All @@ -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;
}
Expand All @@ -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), []);
}

/**
Expand All @@ -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;
}, {});
}
20 changes: 0 additions & 20 deletions tests/Themer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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] });
Expand Down

0 comments on commit bceb9b2

Please sign in to comment.