-
Notifications
You must be signed in to change notification settings - Fork 4
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 #36 from trayio/release-CSP-2414-add-formatarrayto…
…delimitedlist-to-utils Release branch: [CSP-2414] Add formatArrayToDelimitedList to utils
- Loading branch information
Showing
5 changed files
with
149 additions
and
1 deletion.
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,35 @@ | ||
const { UserInputError } = require('./errors'); | ||
|
||
const setErrorMessage = input => | ||
`Expected an array but instead received input of type '${typeof input}'.`; | ||
|
||
/** | ||
* Given an array of strings, returns it as a string delimited by a delimiter. | ||
* | ||
* @param {Array} arrayToFormat An array of strings. | ||
* @param {String} [delimiter=','] The string that will separate the values | ||
* @return {String} Delimited string. | ||
*/ | ||
|
||
const formatArrayToDelimitedList = ({ arrayToFormat, delimiter = ',' }) => { | ||
if (arrayToFormat) { | ||
if (Array.isArray(arrayToFormat)) { | ||
return arrayToFormat.join(delimiter); | ||
} | ||
throw new UserInputError(setErrorMessage(arrayToFormat)); | ||
} | ||
|
||
return undefined; | ||
}; | ||
|
||
/** | ||
* Given an array of strings, returns it as a comma-delimited string. | ||
* | ||
* @param {Array} arrayToFormat An array of strings. | ||
* @return {String} Delimited string. | ||
*/ | ||
|
||
const formatArray = arrayToFormat => | ||
formatArrayToDelimitedList({ arrayToFormat }); | ||
|
||
module.exports = { formatArrayToDelimitedList, formatArray }; |
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
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,65 @@ | ||
const { UserInputError } = require('../../lib/errors'); | ||
const { | ||
formatArray, | ||
formatArrayToDelimitedList, | ||
} = require('../../lib/index').formatters; | ||
|
||
describe('Array should be correctly formatted as a delimited list', () => { | ||
describe('formatArray', () => { | ||
it('should return a comma delimited list, given an array of strings', () => { | ||
const arrayOfStrings = [1, 2, 'third', 'fourth']; | ||
expect(formatArray(arrayOfStrings)).toEqual('1,2,third,fourth'); | ||
}); | ||
|
||
it('should return "undefined" if argument is undefined', () => { | ||
expect(formatArray(undefined)).toBeUndefined(); | ||
}); | ||
|
||
it('should throw error if input exists but is not an array', () => { | ||
const notArray = 'not an array'; | ||
|
||
expect(() => formatArray(notArray)).toThrow( | ||
new UserInputError( | ||
"Expected an array but instead received input of type 'string'.", | ||
), | ||
); | ||
}); | ||
}); | ||
|
||
describe('formatArrayToDelimitedList', () => { | ||
it('should return a comma delimited list, given an array of strings', () => { | ||
const arrayOfStrings = [1, 2, 'third', 'fourth']; | ||
expect( | ||
formatArrayToDelimitedList({ arrayToFormat: arrayOfStrings }), | ||
).toEqual('1,2,third,fourth'); | ||
}); | ||
|
||
it('should return a list delimited by the specified delimiter "//", given an array of strings', () => { | ||
const arrayOfStrings = [1, 2, 'third', 'fourth']; | ||
expect( | ||
formatArrayToDelimitedList({ | ||
arrayToFormat: arrayOfStrings, | ||
delimiter: '//', | ||
}), | ||
).toEqual('1//2//third//fourth'); | ||
}); | ||
|
||
it('should return "undefined" if value of "arrayToFormat" is undefined', () => { | ||
expect( | ||
formatArrayToDelimitedList({ arrayToFormat: undefined }), | ||
).toBeUndefined(); | ||
}); | ||
|
||
it('should throw error if input exists but is not an array', () => { | ||
const notArray = {}; | ||
|
||
expect(() => | ||
formatArrayToDelimitedList({ arrayToFormat: notArray }), | ||
).toThrow( | ||
new UserInputError( | ||
"Expected an array but instead received input of type 'object'.", | ||
), | ||
); | ||
}); | ||
}); | ||
}); |