From 1d0561f8875e877d089625094d974784856289fd Mon Sep 17 00:00:00 2001 From: Jacob Thomason Date: Fri, 13 Oct 2023 21:55:29 -0400 Subject: [PATCH] Improved label formatting --- lib/index.js | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/lib/index.js b/lib/index.js index 4e112df..3037689 100644 --- a/lib/index.js +++ b/lib/index.js @@ -34,9 +34,8 @@ let cachedSchema; /** - * A hacked JSON.stringify that will preserve functions and regexes, etc. - * This isn't perfect for sure and could use improvment, but is sufficient for single layer sample - * data at the moment. It's really nasty, I'm aware. + * A hacked JSON.stringify that will allow us to print raw javascript expressions without quoting + * them as strings, essentially allowing us to add dynamic execution of data. * * @param {Object|Array} input * @@ -77,6 +76,23 @@ const indentString = (string, spaces = 0) => { } +/** + * Cleans up the label with better formatting. + * + * @param {String} label + * + * @returns {String} + */ +const inflectLabel = (label) => { + label = inflection.transform(label, ['underscore', 'titleize']); + + // Remove any trailing " Id" or " Key" from the label + label = label.replace(/ (Id|Key)$/, ''); + + return label; +} + + /** * Gets the GraphQL request configuration and performs necessary validation. * @@ -428,7 +444,7 @@ const getInputFields = ( inputFields.push(new InputField({ key: `${fieldDepth.length > 1 ? fieldDepth.slice(-1) + '__' : ''}${typeDetails.fieldName}`, ...(forGQL && {field: typeDetails.fieldName}), - label: inflection.transform(typeDetails.fieldName, ['underscore', 'titleize']), + label: inflectLabel(typeDetails.fieldName), type: typeDetails.scalarType, // typeDetails won't be processed for outermost type on children, since the getTypeDetails // attempts to get the innermost type, while gathering important information, like isRequired. @@ -452,7 +468,7 @@ const getInputFields = ( inputFields.push(new InputField({ key: `${fieldDepth.length > 1 ? fieldDepth.join('.') + '.' : ''}${typeDetails.fieldName}`, ...(forGQL && {field: typeDetails.fieldName}), - label: inflection.transform(typeDetails.fieldName, ['underscore', 'titleize']), + label: inflectLabel(typeDetails.fieldName), // typeDetails won't be processed for outermost type on children, since the getTypeDetails // attempts to get the innermost type, while gathering important information, like isRequired. // To get the isRequired value, we just check the outer field (typeDetails in this case). @@ -535,7 +551,7 @@ const getOutputFields = (type, forGQL = false) => { return [new OutputField({ key: typeDetails.typeName === 'ID' ? 'id' : 'value', // GraphQL implicity returns don't have a field - default to 'value' - label: inflection.transform(type.name, ['underscore', 'titleize']), + label: inflectLabel(type.name), type: typeDetails.scalarType, ...(typeDetails.enumValues.length && {choices: typeDetails.enumValues}), // required: typeDetails.isRequired, // Will require sample data @@ -560,7 +576,7 @@ const getOutputFields = (type, forGQL = false) => { outputFields.push(new OutputField({ key: typeDetails.fieldName, - label: inflection.transform(field.name, ['underscore', 'titleize']), + label: inflectLabel(field.name), type: typeDetails.scalarType, ...(typeDetails.enumValues.length && {choices: typeDetails.enumValues}), // required: typeDetails.isRequired, // Will require sample data