From 38e7a574a2ba70fd6c3bcbb4334984778dfa206a Mon Sep 17 00:00:00 2001 From: Jeff Handley Date: Tue, 19 Dec 2017 14:51:16 -0800 Subject: [PATCH] fix: renamed rangeLength to length --- .../src/{rangeLength.js => length.js} | 16 +++---- packages/strickland/src/strickland.js | 2 +- .../{rangeLength.spec.js => length.spec.js} | 46 +++++++++---------- packages/strickland/test/strickland.spec.js | 13 ++---- 4 files changed, 37 insertions(+), 40 deletions(-) rename packages/strickland/src/{rangeLength.js => length.js} (56%) rename packages/strickland/test/{rangeLength.spec.js => length.spec.js} (83%) diff --git a/packages/strickland/src/rangeLength.js b/packages/strickland/src/length.js similarity index 56% rename from packages/strickland/src/rangeLength.js rename to packages/strickland/src/length.js index d983fa4..ba081ac 100644 --- a/packages/strickland/src/rangeLength.js +++ b/packages/strickland/src/length.js @@ -2,22 +2,22 @@ import validate from './strickland'; import minLength from './minLength'; import maxLength from './maxLength'; -export default function range(minLengthValue, maxLengthValue, props) { +export default function length(min, max, props) { let validatorProps; - if (typeof minLengthValue === 'object') { + if (typeof min === 'object') { validatorProps = { - ...minLengthValue + ...min }; - } else if (typeof maxLengthValue === 'object') { + } else if (typeof max === 'object') { validatorProps = { - minLength: minLengthValue, - ...maxLengthValue + minLength: min, + ...max }; } else { validatorProps = { - minLength: minLengthValue, - maxLength: maxLengthValue, + minLength: min, + maxLength: max, ...props }; } diff --git a/packages/strickland/src/strickland.js b/packages/strickland/src/strickland.js index dc4c520..7fd0cdd 100644 --- a/packages/strickland/src/strickland.js +++ b/packages/strickland/src/strickland.js @@ -4,7 +4,7 @@ export {default as maxLength} from './maxLength'; export {default as min} from './min'; export {default as minLength} from './minLength'; export {default as range} from './range'; -export {default as rangeLength} from './rangeLength'; +export {default as length} from './length'; export {default as required} from './required'; export {default as composite} from './composite'; diff --git a/packages/strickland/test/rangeLength.spec.js b/packages/strickland/test/length.spec.js similarity index 83% rename from packages/strickland/test/rangeLength.spec.js rename to packages/strickland/test/length.spec.js index 72ec1e3..cc15526 100644 --- a/packages/strickland/test/rangeLength.spec.js +++ b/packages/strickland/test/length.spec.js @@ -1,36 +1,36 @@ import expect from 'expect'; import deepFreeze from 'deep-freeze'; -import rangeLength from '../src/rangeLength'; +import length from '../src/length'; -describe('rangeLength', () => { +describe('length', () => { describe('throws', () => { it('when props are not supplied', () => { - expect(() => rangeLength()).toThrow(); + expect(() => length()).toThrow(); }); it('when the min length is a string', () => { - expect(() => rangeLength('123', 456)).toThrow(); + expect(() => length('123', 456)).toThrow(); }); it('when the max length is a string', () => { - expect(() => rangeLength(123, '456')).toThrow(); + expect(() => length(123, '456')).toThrow(); }); it('when props is an object without a min or a max', () => { - expect(() => rangeLength({})).toThrow(); + expect(() => length({})).toThrow(); }); it('when props is an object without a min', () => { - expect(() => rangeLength({ maxLength: 456 })).toThrow(); + expect(() => length({ maxLength: 456 })).toThrow(); }); it('when props is an object without a max', () => { - expect(() => rangeLength({ minLength: 123 })).toThrow(); + expect(() => length({ minLength: 123 })).toThrow(); }); }); describe('with a single props argument', () => { - const validate = rangeLength({minLength: 3, maxLength: 5, message: 'Custom message'}); + const validate = length({minLength: 3, maxLength: 5, message: 'Custom message'}); const result = validate('1234'); it('uses the min prop', () => { @@ -47,7 +47,7 @@ describe('rangeLength', () => { }); describe('with the first argument as a number and the second as an object', () => { - const validate = rangeLength(3, {maxLength: 5, message: 'Custom message'}); + const validate = length(3, {maxLength: 5, message: 'Custom message'}); const result = validate('1234'); it('sets the min prop', () => { @@ -64,7 +64,7 @@ describe('rangeLength', () => { }); describe('with the first and second arguments as numbers and the third as an object', () => { - const validate = rangeLength(3, 5, {message: 'Custom message'}); + const validate = length(3, 5, {message: 'Custom message'}); const result = validate('1234'); it('sets the min prop', () => { @@ -81,14 +81,14 @@ describe('rangeLength', () => { }); it('returns the value on the result', () => { - const validate = rangeLength(3, 5); + const validate = length(3, 5); const result = validate('1234'); expect(result.value).toBe('1234'); }); describe('returns the parsedValue on the result', () => { - const validate = rangeLength(3, 5); + const validate = length(3, 5); it('when the value is a string', () => { const result = validate('1234'); @@ -127,7 +127,7 @@ describe('rangeLength', () => { }); describe('returns the length on the result', () => { - const validate = rangeLength(3, 5); + const validate = length(3, 5); it('when the value is a string', () => { const result = validate('1234'); @@ -209,15 +209,15 @@ describe('rangeLength', () => { }); }); - validates('with numeric arguments', rangeLength(3, 5)); - validates('with a props argument', rangeLength({minLength: 3, maxLength: 5})); - validates('with a numeric min and props', rangeLength(3, {maxLength: 5})); - validates('with numeric min and max plus props', rangeLength(3, 5, {other: 'other'})); + validates('with numeric arguments', length(3, 5)); + validates('with a props argument', length({minLength: 3, maxLength: 5})); + validates('with a numeric min and props', length(3, {maxLength: 5})); + validates('with numeric min and max plus props', length(3, 5, {other: 'other'})); describe('with props passed into validation', () => { it('allows the minLength value to be specified at time of validation', () => { const validatorProps = {minLength: 4, maxLength: 6}; - const validate = rangeLength(validatorProps); + const validate = length(validatorProps); const result = validate('123', {minLength: 2}); expect(result).toMatchObject({ @@ -228,7 +228,7 @@ describe('rangeLength', () => { it('allows the maxLength value to be specified at time of validation', () => { const validatorProps = {minLength: 4, maxLength: 6}; - const validate = rangeLength(validatorProps); + const validate = length(validatorProps); const result = validate('1234567', {maxLength: 8}); expect(result).toMatchObject({ @@ -243,21 +243,21 @@ describe('rangeLength', () => { const props = {minLength: 3, maxLength: 5}; deepFreeze(props); - expect(() => rangeLength(props)('1234')).not.toThrow(); + expect(() => length(props)('1234')).not.toThrow(); }); it('when a min length and props are used', () => { const props = {maxLength: 5, message: 'Custom message'}; deepFreeze(props); - expect(() => rangeLength(3, props)('1234')).not.toThrow(); + expect(() => length(3, props)('1234')).not.toThrow(); }); it('when min and max values and props are used', () => { const props = {message: 'Custom message'}; deepFreeze(props); - expect(() => rangeLength(3, 5, props)('1234')).not.toThrow(); + expect(() => length(3, 5, props)('1234')).not.toThrow(); }); }); }); diff --git a/packages/strickland/test/strickland.spec.js b/packages/strickland/test/strickland.spec.js index eb42112..7be0a1f 100644 --- a/packages/strickland/test/strickland.spec.js +++ b/packages/strickland/test/strickland.spec.js @@ -1,8 +1,5 @@ import expect from 'expect'; -import validate, {isValid} from '../src/strickland'; -import required from '../src/required'; -import minLength from '../src/minLength'; -import rangeLength from '../src/rangeLength'; +import validate, {isValid, required, minLength, length} from '../src/strickland'; describe('validate', () => { describe('throws', () => { @@ -305,7 +302,7 @@ describe('validate', () => { homeAddress: { street: required(), city: required(), - state: [required(), rangeLength(2, 2)] + state: [required(), length(2, 2)] }, workAddress: { street: { @@ -313,7 +310,7 @@ describe('validate', () => { name: required() }, city: required(), - state: [required(), rangeLength(2, 2)] + state: [required(), length(2, 2)] } }; @@ -506,7 +503,7 @@ describe('validate', () => { homeAddress: { street: required(), city: required(), - state: [required(), rangeLength(2, 2)] + state: [required(), length(2, 2)] }, workAddress: { street: { @@ -514,7 +511,7 @@ describe('validate', () => { name: required() }, city: required(), - state: [required(), rangeLength(2, 2)] + state: [required(), length(2, 2)] } };