How to use generate-function - 5 common examples

To help you get started, we’ve selected a few generate-function examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github prometheusresearch-archive / react-forms / src / JSONSchema / compileValidator.js View on Github external
error(messages.IS_MORE_THAN_MAXIMUM);
      validate('}');
    }

    if (properties) {
      Object.keys(properties).forEach(function(p) {
        visit(genobj(name, p), genobj(dataSym, p), properties[p], reporter, filter);
      });
    }

    while (indent--) {
      validate('}');
    }
  }

  let validate = genfun('function validate(data) {')('validate.errors = null')('var errors = 0');

  visit('data', 'data', schema, reporter, opts && opts.filter);

  validate('return errors === 0')('}');

  validate = validate.toFunction(scope);
  validate.errors = null;

  validate.__defineGetter__('error', function() {
    if (!validate.errors) {
      return '';
    } else {
      return validate.errors
        .map(err => err.field + ' ' + err.message)
        .join('\n');
    }
github zalando-incubator / graphql-jit / src / variables.ts View on Github external
function generateInput(
  context: CompilationContext,
  varType: GraphQLInputType,
  varName: string,
  hasValueName: string,
  wrapInList: boolean
) {
  const currentOutput = getObjectPath(context.responsePath);
  const currentInput = getObjectPath(context.inputPath);
  const errorLocation = printErrorLocation(
    computeLocations([context.varDefNode])
  );

  const gen = genFn();
  gen(`if (${currentInput} == null) {`);

  if (isNonNullType(varType)) {
    let nonNullMessage;
    let omittedMessage;
    if (context.errorMessage) {
      const objectPath = printObjectPath(context.responsePath);
      nonNullMessage = `${
        context.errorMessage
      } + \`Expected non-nullable type ${varType} not to be null at ${objectPath}.\``;
      omittedMessage = `${
        context.errorMessage
      } + \`Field ${objectPath} of required type ${varType} was not provided.\``;
    } else {
      nonNullMessage = `'Variable "$${varName}" of non-null type "${varType}" must not be null.'`;
      omittedMessage = `'Variable "$${varName}" of required type "${varType}" was not provided.'`;
github micnews / html-to-article-json / lib / normalize / merge-text-nodes.js View on Github external
const createSameTypeTextNodes = textFormattings => {
  let fn = generateFunction();
  fn = fn('function (nodeA, nodeB) {');

  fn = fn('return ' + textFormattings
    .map(row => {
      return row.property;
    })
    .filter(property => {
      return property !== 'content';
    })
    .map(property => {
      return 'nodeA[\'' + property + '\'] === nodeB[\'' + property + '\']';
    })
    .join('&&'));

  fn = fn('}');
  return fn.toFunction();
github micnews / html-to-article-json / lib / parse / text.js View on Github external
export default opts => {
  const textFormattings = getTextFormattings(opts);

  let fn = generateFunction();

  const renderTextOpts = textFormattings.reduce((opts, row) => {
    opts[row.property] = row.get;
    if (row.classProperty) {
      opts[row.classProperty] = row.getClass;
    }
    return opts;
  }, {});

  fn = fn('function text (opts, elm) {')('return {');
  fn = fn('  type: \'text\',');

  Object.keys(renderTextOpts).forEach(key => {
    fn = fn(
      '  %s: opts[\'%s\'] || renderTextOpts[\'%s\'](elm),',
      key, key, key);
github zalando-incubator / graphql-jit / src / resolve-info.ts View on Github external
if (typeof enricher === "function") {
    enrichedInfo =
      enricher({
        fieldName,
        fieldNodes,
        returnType: fieldType,
        parentType,
        schema,
        fragments,
        operation
      }) || {};
    if (typeof enrichedInfo !== "object" || Array.isArray(enrichedInfo)) {
      enrichedInfo = {};
    }
  }
  const gen = genFn();
  gen(`return function getGraphQLResolveInfo(rootValue, variableValues, path) {
      return {
          fieldName,
          fieldNodes,
          returnType: fieldType,
          parentType,
          path,
          schema,
          fragments,
          rootValue,
          operation,
          variableValues,`);
  Object.keys(enrichedInfo).forEach(key => {
    gen(`${key}: enrichedInfo["${key}"],\n`);
  });
  gen(`};};`);

generate-function

Module that helps you write generated functions in Node

MIT
Latest version published 6 years ago

Package Health Score

67 / 100
Full package analysis

Popular generate-function functions