question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Hey,

I found your package tracing makara and adaro a couple months back and I found it really useful in a project I am now working on, so THANK YOU 👍 !

That said, I found my project to benefit from having a T/F returned for use in cases of function overloading via optional params.

TL;DR: I wrote a wrapper function which adds OR(|) and returns T/F if meets or doesn’t meet schema. Do you want it? I’ll make a PR.

My use case if a router function takes in multiple configurations.

  • function (options, [callback]) {}
  • function (template, dataObj, [options], [callback]) {}

After taking the information in, I parse into a single options object and call an internal function to do the actual work.

var q = require('q');
var extend = require('deep-extend');
var probe = require('./aproba-overload');

module.exports = {
  _compileOptions: {
    engine: null,
    template: null,
    data: null
  },
  _compile: function (options, callback) {
    var d = q.defer();

    // Do compile and resolve/reject promise

    return d.promise.nodeify(callback);
  },

  /**
   * Compile template
   * 
   * @param {String|Object} template   Either template name or options object
   * @param {Object}        [data]     data for template rendering
   * @param {Object}        [options]  optional object for options
   * @param {Function}      [callback] optional callback, returns promise of null
   * 
   * @return {q.Promise|null} promise if no callback, null if returned.
   */
  compile: function (template, data, options, callback) {
    var args = arguments;
    var opts = extend({}, module.exports._compileOptions);


    if (probe('O|OF', args)) { // Options with optional callback
      extend(opts, args[0]);
      callback = args[1];
    } else if (probe('SO|SOF', args)) { // template name and data, optional callback
      opts.template = args[0];
      opts.data = args[1];
      callback = args[2];
    } else if (probe('SOO|SOOF', args)) { // template name and data, additional template options, optional callback
      extend(opts, args[2]);
      opts.template = args[0];
      opts.data = args[1];
      callback = args[3];
    } else {
      throw new Error('Invalid Arguments'); // need more descriptive error message.
    }

    return module.exports._compile(opts, callback);
  }
};

Here is my wrapper:

/**
 * Filename: aproba-overload.js
 * Created by rhett on 2/22/16.
 */
'use strict';
var aproba = require('aproba');

var validate = module.exports = function (schema, args, throwErr) {

  // Enable OR syntax
  if (/.+\|.+/.test(schema)) {  // example schema = 'O|OF|O*F'
    var t = schema.split(/\|(.+)/); // t = ['O', 'OF|O*F', '']

    // return recursive for schema = 'O' OR recursive for schema = 'OF|O*F' <= which has another recurse
    return validate(t[0], args, throwErr) || validate(t[1], args, throwErr);
    // Todo: problem with throwErr here; If true passed it may not reach second call. Rethink this combo
  }

  try {
    aproba(schema, args);
  } catch (err) {
    if (!throwErr &&      // pass-through requested
      [
        'EMISSINGARG',
        'EUNKNOWNTYPE',
        'EINVALIDTYPE',
        'ETOOMANYARGS'
      ].indexOf(err.code) // Catch only known errors
    ) {
      return false;       // Doesn't meet schema
    } else {
      throw err;          // pass-through or other, unexpected error
    }
  }

  return true; // no problems, success
};

At the moment it is just a file in my libs, but I plan to later make into a tested module. Alternatively, I can make a pull request to add this into aproba if you are interested in having it.

If you think this code will be beneficial to you code base, tell me how you think it would be best to add. Otherwise, I’ll just make a new package.

Have a great day,

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:9 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
dhollenbeckcommented, Nov 8, 2016

Perhaps use lowercase letters to indicate optional fnc params.

  • function (options, [callback]) {} ==> aproba('Of')
  • function (template, dataObj, [options], [callback]) {} ==> aproba('SOof')

I am only considering using this module so please do not consider my comment as an endorsement of this feature request.

0reactions
iarnacommented, Feb 7, 2017

What I’ve described is merged and published. As this implements the original request, I’m going to close this. If after trying this out any of you all are still interested in seeing sugar for optionals, please feel free to open a new issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

No results found

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found