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

Commit

Permalink
refactor: rename get-all-values-of with deeper-object-values
Browse files Browse the repository at this point in the history
  • Loading branch information
mg901 committed Feb 9, 2019
1 parent ebc998a commit b28deb9
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 30 deletions.
22 changes: 22 additions & 0 deletions __tests__/helpers/deeper-object-values.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { deeperObjectValues } from '../../src/helpers/deeper-object-values';

const config = {
aa: 1,
bb: 2,
cc: {
d: {
x: 9,
},
},
dd: {
d: {
y: 9,
},
},
};

describe('deeperObjectValues', () => {
it('should', () => {
expect(deeperObjectValues('d')(config)).toEqual([{ x: 9 }, { y: 9 }]);
});
});
22 changes: 0 additions & 22 deletions __tests__/helpers/get-all-values-of.spec.js

This file was deleted.

19 changes: 19 additions & 0 deletions src/helpers/deeper-object-values.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// @flow

export const deeperObjectValues: (string, void | any[]) => (any) => any[] = (
target,
memo,
) => (obj) => {
const buffer = !Array.isArray(memo) ? [] : memo;

return Object.keys(Object(obj)).reduce((acc, key) => {
if (key === target) {
return [...acc, obj[key]];
}
if (obj[key] instanceof Object) {
return [...deeperObjectValues(target, acc)(obj[key])];
}

return acc;
}, buffer);
};
4 changes: 2 additions & 2 deletions src/validate-user-config/bases-validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import * as R from 'ramda';
import { Left, Right } from 'igogo';
import { VAL_WITH_PX_OR_EM } from '../constants';
import { BASE_ERROR_MESSAGE } from '../error-messages';
import { getAllValuesOf } from '../helpers/get-all-values-of';
import { deeperObjectValues } from '../helpers/deeper-object-values';
import { errorReporter } from '../helpers/error-reporter';
import { type UserConfig } from '../models/user-config';

export const getBases: (UserConfig) => * = getAllValuesOf('base');
export const getBases: (UserConfig) => * = deeperObjectValues('base');

export const eitherIsValidField = (field: mixed) =>
Array.prototype.concat.call([], field).every((f) => VAL_WITH_PX_OR_EM.test(f))
Expand Down
6 changes: 4 additions & 2 deletions src/validate-user-config/break-values-validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import * as R from 'ramda';
import { Right, Left } from 'igogo';
import { VAL_WITH_PX_OR_EM } from '../constants';
import { BREAKPOINT_ERROR_MESSAGE } from '../error-messages';
import { getAllValuesOf } from '../helpers/get-all-values-of';
import { deeperObjectValues } from '../helpers/deeper-object-values';
import { errorReporter } from '../helpers/error-reporter';
import { type UserConfig } from '../models/user-config';

export const getBreakValues: (UserConfig) => * = getAllValuesOf('breakpoint');
export const getBreakValues: (UserConfig) => * = deeperObjectValues(
'breakpoint',
);

export const hasPxOrEm: (any) => boolean = R.test(VAL_WITH_PX_OR_EM);

Expand Down
6 changes: 4 additions & 2 deletions src/validate-user-config/line-heights-validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
import * as R from 'ramda';
import { Right, Left } from 'igogo';
import { LINE_HEIGHT_ERRROR_MESSAGE } from '../error-messages';
import { getAllValuesOf } from '../helpers/get-all-values-of';
import { deeperObjectValues } from '../helpers/deeper-object-values';
import { errorReporter } from '../helpers/error-reporter';
import { isNumeric } from '../helpers/is-numeric';
import { type UserConfig } from '../models/user-config';

export const getLineHeights: (UserConfig) => * = getAllValuesOf('lineHeight');
export const getLineHeights: (UserConfig) => * = deeperObjectValues(
'lineHeight',
);

export const isValidLineHeight: (mixed) => boolean = R.both(
R.is(Number),
Expand Down
4 changes: 2 additions & 2 deletions src/validate-user-config/ratios-validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import {
POSITIVE_OR_NEGATIVE_INT_OR_FLOAT_NUM_AT_END_OF_STRING,
} from '../constants';
import { RATIO_ERROR_MESSAGE } from '../error-messages';
import { getAllValuesOf } from '../helpers/get-all-values-of';
import { deeperObjectValues } from '../helpers/deeper-object-values';
import { isNumeric } from '../helpers/is-numeric';
import { type UserConfig } from '../models/user-config';
import { errorReporter } from '../helpers/error-reporter';

export const getRatios: (UserConfig) => * = getAllValuesOf('ratio');
export const getRatios: (UserConfig) => * = deeperObjectValues('ratio');

const isNumerical = R.both(R.is(Number), isNumeric);

Expand Down

0 comments on commit b28deb9

Please sign in to comment.