From 48161b52664cc1e035777d4aae58706319dfcaad Mon Sep 17 00:00:00 2001 From: David Thai <7222115+dafuloth@users.noreply.github.com> Date: Thu, 14 May 2020 15:50:02 +0100 Subject: [PATCH] [CSP-2414] Add formatArrayToDelimitedList to utils (#35) * this release adds a new `formatters` module * semantic versioning 0.2.2 to 0.3.0 Major version unchanged Minor version incremented due to new functionality Patch version reset to 0 due to Minor version increment * added `formatArray` function that calls main function with defaults * will throw error in case input is something other than an array * included tests * README updated --- README.md | 44 +++++++++++++++++++++ lib/formatters.js | 35 +++++++++++++++++ lib/index.js | 4 ++ package.json | 2 +- tests/methods/formatters.test.js | 65 ++++++++++++++++++++++++++++++++ 5 files changed, 149 insertions(+), 1 deletion(-) create mode 100644 lib/formatters.js create mode 100644 tests/methods/formatters.test.js diff --git a/README.md b/README.md index 6bcd509..24a94c6 100644 --- a/README.md +++ b/README.md @@ -110,6 +110,12 @@ for arrays).
Helper for generating an operation input schema.
Helper to take an array of strings and return a comma-delimited string. Alias of formatArrayToDelimitedList({ arrayToFormat, delimiter: ',' })
Helper to take an array of strings and return a string that is a list, delimited by the specified delimiter (',
' by default).
Integer
\| String
| The maximum range specified by the API. |
| validation.inputName | String
| The name of the input the range is associated with. |
+
+
## generateInputSchema({ schema, keys, operation = 'schema' })
Helper for generating an operation input schema.
@@ -448,3 +456,39 @@ generateInputSchema({
* `new_key` is not in the full schema but it's full keys and values are supplied
*/
```
+
+
+
+## formatArrayToDelimitedList({ arrayToFormat, [delimiter] })
+
+Helper that takes an array and returns a string that is a delimited list of the given array.
+
+Alternatively, you can instead use `formatArray(arrayToFormat)`, which is an alias of `formatArrayToDelimitedList({ arrayToFormat })` and simply uses the default delimiter (`,`).
+
+Using `formatArrayToDelimitedList({ arrayToFormat, [delimiter] })` will allow you to specify an alternative delimiter.
+
+The envisioned use-case is in an operation model to format user array input into a delimited string to assign to a parameter. If it was an optional input and not supplied then the parameter should be `undefined`. This is reflected by the function returning `undefined` if it does not receive an array.
+
+**Kind**: global function
+
+| Param | Type | Default | Description |
+| ------------- | ------------------- | -------------- | -------------------------------------------------------------------------------- |
+| arrayToFormat | Array
| | Usually an array of Strings, or else equivalent string representations are used. |
+| [delimiter] | String
| ,
| A string that will be used to separate the values. |
+
+**Example**:
+
+```js
+const inputArray = [1, 2, 'third', 'fourth'];
+
+formatArrayToDelimitedList({ arrayToFormat: inputArray });
+
+// returns '1,2,third,fourth'
+```
+
+```js
+
+formatArrayToDelimitedList({ arrayToFormat: undefined });
+
+// returns undefined
+```
\ No newline at end of file
diff --git a/lib/formatters.js b/lib/formatters.js
new file mode 100644
index 0000000..46b9ef0
--- /dev/null
+++ b/lib/formatters.js
@@ -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 };
diff --git a/lib/index.js b/lib/index.js
index 5509bc4..2e8913a 100644
--- a/lib/index.js
+++ b/lib/index.js
@@ -4,6 +4,7 @@ const removeEmptyObjects = require('./removeEmptyObjects');
const lookup = require('./lookup');
const validatePaginationRange = require('./validatePaginationRange');
const generateInputSchema = require('./generateInputSchema');
+const formatters = require('./formatters');
// const xmlFormatter = require('./xmlFormatter');
const { mustachedDDL, DDL } = require('./ddl');
const {
@@ -61,6 +62,7 @@ module.exports = {
return DDL(object, textPath, valuePath);
},
},
+
deepMapKeys,
removeEmptyObjects,
lookup,
@@ -68,6 +70,8 @@ module.exports = {
DDL,
validatePaginationRange,
generateInputSchema,
+ formatters,
+
// Commenting for initial release pending functionality to add paths to treatAsArray
// xml: {
// xmlFormatter,
diff --git a/package.json b/package.json
index db82aac..8dc25da 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "@trayio/connector-utils",
- "version": "0.2.2",
+ "version": "0.3.0",
"description": "Common utility functions used in connectors.",
"main": "lib/index.js",
"scripts": {
diff --git a/tests/methods/formatters.test.js b/tests/methods/formatters.test.js
new file mode 100644
index 0000000..48cbc4a
--- /dev/null
+++ b/tests/methods/formatters.test.js
@@ -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'.",
+ ),
+ );
+ });
+ });
+});