UNPKG

graphile-build

Version:

Build a GraphQL schema from plugins

78 lines (74 loc) 3.32 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.wrapDescription = exports.upperFirst = exports.upperCamelCase = exports.singularize = exports.pluralize = exports.formatInsideUnderscores = exports.constantCaseAll = exports.constantCase = exports.camelCase = exports.bindAll = void 0; var _upperFirst = _interopRequireDefault(require("lodash/upperFirst")); var _camelCase = _interopRequireDefault(require("lodash/camelCase")); var _pluralize = _interopRequireDefault(require("pluralize")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } const bindAll = (obj, keys) => { keys.forEach(key => { obj[key] = obj[key].bind(obj); }); return obj; }; exports.bindAll = bindAll; const constantCaseAll = str => str.replace(/[^a-zA-Z0-9_]+/g, "_").replace(/[A-Z]+/g, "_$&").replace(/__+/g, "_").replace(/^[^a-zA-Z0-9]+/, "").replace(/^[0-9]/, "_$&") // GraphQL enums must not start with a number .toUpperCase(); exports.constantCaseAll = constantCaseAll; const formatInsideUnderscores = fn => str => { const matches = str.match(/^(_*)([\s\S]*?)(_*)$/); if (!matches) { throw new Error("Impossible?"); // Satiate Flow } const [, start, middle, end] = matches; return `${start}${fn(middle)}${end}`; }; exports.formatInsideUnderscores = formatInsideUnderscores; const upperFirst = formatInsideUnderscores(_upperFirst.default); exports.upperFirst = upperFirst; const camelCase = formatInsideUnderscores(_camelCase.default); exports.camelCase = camelCase; const constantCase = formatInsideUnderscores(constantCaseAll); exports.constantCase = constantCase; const upperCamelCase = str => upperFirst(camelCase(str)); exports.upperCamelCase = upperCamelCase; const pluralize = str => (0, _pluralize.default)(str); exports.pluralize = pluralize; const singularize = str => _pluralize.default.singular(str); // Copied from GraphQL v14, MIT license (c) GraphQL Contributors. exports.singularize = singularize; function breakLine(line, maxLen) { const parts = line.split(new RegExp(`((?: |^).{15,${maxLen - 40}}(?= |$))`)); if (parts.length < 4) { return [line]; } const sublines = [parts[0] + parts[1] + parts[2]]; for (let i = 3; i < parts.length; i += 2) { sublines.push(parts[i].slice(1) + parts[i + 1]); } return sublines; } /** * Only use this on descriptions that are plain text, or that we create * manually in code; since descriptions are markdown, it's not safe to use on * descriptions that contain code blocks or long inline code strings. */ const wrapDescription = (description, position) => { const indentationLength = position === "root" ? 0 : position === "type" ? 0 : position === "field" ? 2 : position === "arg" ? 4 : 0; // This follows the implementation from GraphQL v14 to make our GraphQL v15 // schema more similar. Ref: // https://github.com/graphql/graphql-js/pull/2223/files const maxLen = 120 - indentationLength; return description.split("\n").map(line => { if (line.length < maxLen + 5) { return line; } // For > 120 character long lines, cut at space boundaries into sublines // of ~80 chars. return breakLine(line, maxLen).join("\n"); }).join("\n"); }; exports.wrapDescription = wrapDescription; //# sourceMappingURL=utils.js.map