@technobuddha/library
Version:
A large library of useful functions
29 lines (28 loc) • 1.11 kB
JavaScript
import { space, empty } from '../constants';
import zip from 'lodash/zip';
import isNumber from 'lodash/isNumber';
import isBoolean from 'lodash/isBoolean';
import isNull from 'lodash/isNull';
import isArray from 'lodash/isArray';
import isArrayLike from 'lodash/isArrayLike';
import isString from 'lodash/isString';
import escapeGraphQL from '../escapeGraphQL';
export function graphQL(template, ...args) {
if (isArrayLike(template) && template.raw) {
return zip(template.map(t => t.replace(/[\r\n]+\s*/gu, space)), args.map(arg => graphQL(arg)))
.flat()
.join(empty).trim();
}
if (isNumber(template))
return template.toString();
if (isString(template))
return `"${escapeGraphQL(template)}"`;
if (isNull(template))
return 'null';
if (isBoolean(template))
return template ? 'true' : 'false';
if (isArray(template))
return `[ ${template.map(a => graphQL(a)).join(', ')} ]`;
return `{ ${Object.entries(template).map(([key, value]) => `${key}: ${graphQL(value)}`).join(', ')} }`;
}
export default graphQL;