Domanda

I'm trying to code my very first NodeJS module, but I'm having trouble grasping some concepts.

Here's the code that I currently have (a genexer counter/generator):

"use strict";

var ret = require('ret');

module.exports = function (regex) {
    if (Object.prototype.toString.call(regex) === '[object RegExp]') {
        regex = regex.source;
    }

    else if (typeof regex !== 'string') {
        regex = String(regex);
    }

    try {
        var tokens = ret(regex);
    }

    catch (exception) {
        return false;
    }

    return {
        charset: '',
        reference: [],
        count: function (token) {
            var result = 0;

            if ((token.type === ret.types.ROOT) || (token.type === ret.types.GROUP)) {
                if (token.hasOwnProperty('stack') === true) {
                    result = 1;

                    token.stack.forEach(function (node) {
                        result *= count(node);
                    });
                }

                else if (token.hasOwnProperty('options') === true) {
                    var options = [];

                    token.options.forEach(function (stack, i) {
                        options[i] = 1;

                        stack.forEach(function (node) {
                            options[i] *= count(node);
                        });
                    });

                    options.forEach(function (option) {
                        result += option;
                    });
                }

                if ((token.type === ret.types.GROUP) && (token.remember === true)) {
                    reference.push(token);
                }
            }

            else if (token.type === ret.types.POSITION) {
            }

            else if (token.type === ret.types.SET) {
                token.set.forEach(function (node) {
                    if (token.not === true) {
                        if ((node.type === ret.types.CHAR) && (node.value === 10)) {
                        }
                    }

                    result += count(node);
                });
            }

            else if (token.type === ret.types.RANGE) {
                result = (token.to - token.from + 1);
            }

            else if (token.type === ret.types.REPETITION) {
                if (isFinite(token.max) !== true) {
                    return Infinity;
                }

                token.value = count(token.value);

                for (var i = token.min; i <= token.max; ++i) {
                    result += Math.pow(token.value, i);
                }
            }

            else if (token.type === ret.types.REFERENCE) {
                if (reference.hasOwnProperty(token.value - 1) === true) {
                    result = 1;
                }
            }

            else if (token.type === ret.types.CHAR) {
                result = 1;
            }

            return result;
        }(tokens),
        generate: function () {
            return false;
        },
    };
};

Questions:

  1. am I calling count correctly on my first iteration? count: function (token) {}(tokens)?
  2. how can I recursively call the count method? I get a "ReferenceError: count is not defined"
  3. is this the correct (or best-practice) approach of defining a small module with several methods?

Forgive me for not posting 3 different questions, but I'm not very familiar with all the terminology yet.

È stato utile?

Soluzione

  1. The convention for immediately invoked closures is count: (function(args) {return function() {}})(args) but your way will also work in all environments.
  2. You can't because count is a closure unfortunately - see 3.
  3. If you want to use methods on your module inside your module I would declare the module outside of the return statement. If you want a good example of this see underscore/lodash source code.

So you can define your module using a declaration like the skeleton below

module.exports = function (regex) {
    //...
    var count = function(tokens) {
        //...
        return function() {
           //...
           var ret *= count(node);


           return ret;
        }
    }
    var mymod = {
        count: count(tokens)
        //...
    };
    //...
    return mymod;

};
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top