Functions as Dictionaries...Functionaries?

by plus5keen

HTML

<!DOCTYPE html>
<html>
    <head>
        <title></title>
    </head>
    <body>
        <h2>Output</h2>
        <div class="output"></div>
        <p>additional output goes to development console</p>
    </body>
</html>

JavaScript

(function(window, _, undef) {
    'use strict';
    var e = window.functionary = {};

    function getValue(d, key) {
        var result = _.find(d[key], function(item) {
            return item.key === key;
        });

        return result && result.value;
    }

    function setValue(d, key, value) {
        var a = d[key] || (d[key] = []);
        var result = _.find(a, function(item) {
            return item.key === key;
        });

        if (result) {
            result.value = value;
        } else {
            a.push({ key: key, value: value });
        }
    }
    
    e.create = function() {
        var d = {};

        return function thisFunc(key, value) {
            if (value !== undef) {
                setValue(d, key, value);
                return thisFunc;
            } else {
                return getValue(d, key);
            }
        };
    };
}(window, _));

(function(window, _, functionary, undef) {
    'use strict';
    
    var keyObjs = _.map(_.range(2), function () {
        return {};
    });

    var fs = _.map(_.range(10), function (i) {
        return functionary.create()
        ('greet', 'hi! ' + i)
        (keyObjs[0], 'hullo! ' + i)
        (keyObjs[1], 'Hello. ' + i);
    });
    
    _.each(fs, function (f) {
        console.log(f('missing'));
        console.log(f('greet'));
        console.log(f(keyObjs[0]));
        console.log(f(keyObjs[1]));
    });
    
}(window, _, window.functionary));