This repository has been archived by the owner on Jun 12, 2023. It is now read-only.
forked from billinghamj/vrm-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from motorway/anton/ch28432/tidy-up-vrm
Tidy up VRM [ch28432]
- Loading branch information
Showing
7 changed files
with
4,986 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
const { coerce, info } = require('./index'); | ||
|
||
describe('coerce', () => { | ||
it('should return normalized VRM', () => { | ||
const [coerced] = coerce('HJ 1'); | ||
expect(coerced.vrm).toStrictEqual('HJ1'); | ||
}); | ||
|
||
it('should return empty array if invalid sting', () => { | ||
const coerced = coerce('lalalalala'); | ||
expect(coerced).toStrictEqual([]); | ||
}); | ||
|
||
it('should throw on invalid input', () => { | ||
expect(() => coerce(1)).toThrow('input invalid'); | ||
expect(() => coerce(null)).toThrow('input invalid'); | ||
expect(() => coerce([])).toThrow('input invalid'); | ||
expect(() => coerce({})).toThrow('input invalid'); | ||
expect(() => coerce(undefined)).toThrow('input invalid'); | ||
}); | ||
}); | ||
|
||
describe('info', () => { | ||
it('should return pretty VRM', () => { | ||
const vrmInfo = info('HJ1'); | ||
expect(vrmInfo.prettyVrm).toStrictEqual('HJ 1'); | ||
}); | ||
|
||
it('should throw on invalid input', () => { | ||
expect(() => info(1)).toThrow('normalized vrm invalid'); | ||
expect(() => info(null)).toThrow('normalized vrm invalid'); | ||
expect(() => info([])).toThrow('normalized vrm invalid'); | ||
expect(() => info({})).toThrow('normalized vrm invalid'); | ||
expect(() => info(undefined)).toThrow('normalized vrm invalid'); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
const isString = (val) => { | ||
return typeof val === 'string'; | ||
}; | ||
|
||
const minLength = (val, length) => { | ||
return val.length >= length; | ||
}; | ||
|
||
const maxLength = (val, length) => { | ||
return val.length <= length; | ||
}; | ||
|
||
const validateInput = (input) => { | ||
return isString(input) && minLength(input, 1); | ||
}; | ||
|
||
const formatPattern = /^[a-z0-9_]+/; | ||
|
||
const validateAllowedFormats = (formats) => { | ||
if (!Array.isArray(formats)) return false; | ||
if (!minLength(formats, 1)) return false; | ||
return formats.reduce((acc, val) => { | ||
if (!acc) return false; | ||
return isString(val) && minLength(val, 1) && formatPattern.test(val); | ||
}, true); | ||
}; | ||
|
||
const validateNormalizedVRM = (vrm) => { | ||
return ( | ||
isString(vrm) && | ||
minLength(vrm, 1) && | ||
maxLength(vrm, 7) && | ||
/^[A-Z0-9]+$/.test(vrm) | ||
); | ||
}; | ||
|
||
const validateFormat = (format) => { | ||
return isString(format) && minLength(format, 1) && formatPattern.test(format); | ||
}; | ||
|
||
module.exports = { | ||
validateInput, | ||
validateAllowedFormats, | ||
validateNormalizedVRM, | ||
validateFormat, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
const { | ||
validateInput, | ||
validateAllowedFormats, | ||
validateNormalizedVRM, | ||
validateFormat, | ||
} = require('./validators'); | ||
|
||
describe('validateInput', () => { | ||
it('should return false if not string', () => { | ||
expect(validateInput(2)).toBe(false); | ||
}); | ||
|
||
it('should return false if length is 0', () => { | ||
expect(validateInput('')).toBe(false); | ||
}); | ||
|
||
it('should return true if string with value', () => { | ||
expect(validateInput('test')).toBe(true); | ||
}); | ||
}); | ||
|
||
describe('validateAllowedFormats', () => { | ||
it('should return false if not an array', () => { | ||
expect(validateAllowedFormats('test')).toBe(false); | ||
}); | ||
|
||
it('should return false if empty array', () => { | ||
expect(validateAllowedFormats([])).toBe(false); | ||
}); | ||
|
||
it('should return false if at least one of the children is not a string', () => { | ||
expect(validateAllowedFormats([1, 'test', null])).toBe(false); | ||
}); | ||
|
||
it('should return false if at least one of the children has wrong format', () => { | ||
expect(validateAllowedFormats(['Test', 'test'])).toBe(false); | ||
}); | ||
|
||
it('should return true if all formats are valid', () => { | ||
expect(validateAllowedFormats(['testTest', 'test1', 'test2'])).toBe(true); | ||
}); | ||
}); | ||
|
||
describe('validateNormalizedVRM', () => { | ||
it('should return false if not a string', () => { | ||
expect(validateNormalizedVRM(1)).toBe(false); | ||
}); | ||
|
||
it('should return false if empty', () => { | ||
expect(validateNormalizedVRM('')).toBe(false); | ||
}); | ||
|
||
it('should return false if longer than 7 characters', () => { | ||
expect(validateNormalizedVRM('this is a long string')).toBe(false); | ||
}); | ||
|
||
it('should return false if wrong format', () => { | ||
expect(validateNormalizedVRM('test @')).toBe(false); | ||
}); | ||
|
||
it('should return true if correct format', () => { | ||
expect(validateNormalizedVRM('HJ1')).toBe(true); | ||
}); | ||
}); | ||
|
||
describe('validateFormat', () => { | ||
it('should return false if not a string', () => { | ||
expect(validateFormat(1)).toBe(false); | ||
}); | ||
|
||
it('should return false if empty', () => { | ||
expect(validateFormat('')).toBe(false); | ||
}); | ||
|
||
it('should return false if wrong format', () => { | ||
expect(validateFormat('Test')).toBe(false); | ||
}); | ||
|
||
it('should return true if correct format', () => { | ||
expect(validateFormat('military')).toBe(true); | ||
expect(validateFormat('gb_2001')).toBe(true); | ||
}); | ||
}); |
Oops, something went wrong.